Skip to main content

Convert Torch7 models into Apple CoreML format.

Project description

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

Installation

pip install -U torch2coreml

In order to use this tool you need to have these installed: * MacOS - High Sierra 10.13 * Xcode 9 * python 2.7

Dependencies

  • coremltools (0.5.0+)

  • 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_shape,
            input_name='input',
            output_name='output',
            mode=None,
            is_image_input=False,
            preprocessing_args={},
            is_image_output=False,
            deprocessing_args={},
            class_labels=None,
            predicted_feature_name='classLabel')

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_shape: tuple
Shape of the input tensor.
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’

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch2coreml-0.0.2-py2.7-none-any.whl (11.4 kB view details)

Uploaded Python 2

File details

Details for the file torch2coreml-0.0.2-py2.7-none-any.whl.

File metadata

File hashes

Hashes for torch2coreml-0.0.2-py2.7-none-any.whl
Algorithm Hash digest
SHA256 7858a7dcee06fdd077e9bf7a2fc673213988785815b69576c669263ecb6d8090
MD5 fa733634760faa4ecc35e3adbabd0e8b
BLAKE2b-256 60f971fbc1dd608707edf1d66e67d34a7ec39ba22d73db0957a9c5cd2ee4f7a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page