Skip to main content

This tool helps convert Torch7 models into Apple CoreML format which can then be run on Apple devices.

fast-neural-style example app

fast-neural-style example app screenshot

Installation

pip install -U torch2coreml

In order to use this tool you need to have these installed: * Xcode 9 * python 2.7

If you want to run tests, you need MacOS High Sierra 10.13 installed.

Dependencies

  • coremltools (0.6.2+)

  • PyTorch

How to use

Using this library you can implement converter for your own model types. An example of such a converter is located at “example/fast-neural-style/convert-fast-neural-style.py”. To implement converters you should use single function “convert” from torch2coreml:

from torch2coreml import convert

This function is simple enough to be self-describing:

def convert(model,
            input_shapes,
            input_names=['input'],
            output_names=['output'],
            mode=None,
            image_input_names=[],
            preprocessing_args={},
            image_output_names=[],
            deprocessing_args={},
            class_labels=None,
            predicted_feature_name='classLabel',
            unknown_layer_converter_fn=None)

Parameters

model: Torch7 model (loaded with PyTorch) | str
A trained Torch7 model loaded in python using PyTorch or path to file with model (*.t7).

input_shapes: list of tuples Shapes of the input tensors.

mode: str (‘classifier’, ‘regressor’ or None)
Mode of the converted coreml model:
‘classifier’, a NeuralNetworkClassifier spec will be constructed.
‘regressor’, a NeuralNetworkRegressor spec will be constructed.
preprocessing_args: dict
‘is_bgr’, ‘red_bias’, ‘green_bias’, ‘blue_bias’, ‘gray_bias’, ‘image_scale’ keys with the same meaning as https://apple.github.io/coremltools/generated/coremltools.models.neural_network.html#coremltools.models.neural_network.NeuralNetworkBuilder.set_pre_processing_parameters
deprocessing_args: dict
Same as ‘preprocessing_args’ but for deprocessing.
class_labels: A string or list of strings.
As a string it represents the name of the file which contains the classification labels (one per line). As a list of strings it represents a list of categories that map the index of the output of a neural network to labels in a classifier.
predicted_feature_name: str
Name of the output feature for the class labels exposed in the Core ML model (applies to classifiers only). Defaults to ‘classLabel’
unknown_layer_converter_fn: function with signature:
(builder, name, layer, input_names, output_names)
builder: object - instance of NeuralNetworkBuilder class
name: str - generated layer name
layer: object - PyTorch (python) object for corresponding layer
input_names: list of strings
output_names: list of strings
Returns: list of strings for layer output names
Callback function to handle unknown for torch2coreml layers

Returns

model: A coreml model.

Currently supported

Models

Only Torch7 “nn” module is supported now.

Layers

List of Torch7 layers that can be converted into their CoreML equivalent:

  1. Sequential

  2. ConcatTable

  3. SpatialConvolution

  4. ELU

  5. ReLU

  6. SpatialBatchNormalization

  7. Identity

  8. CAddTable

  9. SpatialFullConvolution

  10. SpatialSoftMax

  11. SpatialMaxPooling

  12. SpatialAveragePooling

  13. View

  14. Linear

  15. Tanh

  16. MulConstant

  17. SpatialZeroPadding

  18. SpatialReflectionPadding

  19. Narrow

  20. SpatialUpSamplingNearest

  21. SplitTable

License

Copyright (c) 2017 Prisma Labs, Inc. All rights reserved.

Use of this source code is governed by the MIT License that can be found in the LICENSE.txt file.

Release files for torch2coreml 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for torch2coreml 0.2.0
File Interpreter ABI Platform
torch2coreml-0.2.0-py2.7-none-any.whl Details

Release files / torch2coreml-0.2.0-py2.7-none-any.whl

Download URL torch2coreml-0.2.0-py2.7-none-any.whl
Size 12.2 kB
SHA-256 checksum
How to use checksums
86f748be0b889f0952cfa1e850f20c74bdcc034bb7c5c477bc5f40d9d8f790e5
BLAKE2b-256 checksum
How to use checksums
4bf14d16dd03c483ce63e05f15b1b3bc6024abd6d6fe6636b53c060397cdf197
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

0.1.0

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page