Package: torch2coreml
This tool helps convert Torch7 models intoApple
CoreMLformat
which can then be run on Apple devices.fast-neural-style example app screenshotInstallationpipinstall-Utorch2coremlIn order to use this tool you need to have these installed: * Xcode 9
* python 2.7If you want to run tests, you need MacOS High Sierra 10.13 installed.Dependenciescoremltools (0.6.2+)PyTorchHow to useUsing 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:fromtorch2coremlimportconvertThis function is simple enough to be self-describing:defconvert(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)Parametersmodel: Torch7 model (loaded with PyTorch) | strA 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 ashttps://apple.github.io/coremltools/generated/coremltools.models.neural_network.html#coremltools.models.neural_network.NeuralNetworkBuilder.set_pre_processing_parametersdeprocessing_args: dictSame 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: strName 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 classname: str - generated layer namelayer: object - PyTorch (python) object for corresponding layerinput_names: list of stringsoutput_names: list of stringsReturns: list of strings for layer output namesCallback function to handle unknown for torch2coreml layersReturnsmodel: A coreml model.Currently supportedModelsOnly Torch7 “nn” module is supported now.LayersList of Torch7 layers that can be converted into their CoreML
equivalent:SequentialConcatTableSpatialConvolutionELUReLUSpatialBatchNormalizationIdentityCAddTableSpatialFullConvolutionSpatialSoftMaxSpatialMaxPoolingSpatialAveragePoolingViewLinearTanhMulConstantSpatialZeroPaddingSpatialReflectionPaddingNarrowSpatialUpSamplingNearestSplitTableLicenseCopyright (c) 2017 Prisma Labs, Inc. All rights reserved.Use of this source code is governed by theMIT
Licensethat can be found in
the LICENSE.txt file.