tf-coreml TensorFlow 转换到 CoreML 的转换器 项目简介
tfcoremlTensorFlow (TF) to CoreML ConverterDependenciestensorflow >= 1.5.0coremltools >= 0.8numpy >= 1.6.2protobuf >= 3.1.0six >= 1.10.0InstallationInstall From SourceTo get the latest version of the converter, clone this repo and install from source. That is,git clone https://github.com/tf-coreml/tf-coreml.git
cd tf-coremlTo install as a package with pip, either run (at the root directory):pip install -e .or run:python setup.py bdist_wheelThis will generate a pip installable wheel inside the dist directory.Install From PyPITo install the Pypi package:pip install -U tfcoremlUsageSee iPython notebooks in the directory examples/ for examples of how to use the converter.The following arguments are required by the CoreML converter:path to the frozen .pb graph file to be convertedpath where the .mlmodel should be writtena list of output tensor names present in the TF grapha dictionary of input names and their shapes (as list of integers). This is only required if input tensors' shapes are not fully defined in the frozen .pb file (e.g. they contain None or ?)Note that the frozen .pb file can be obtained from the checkpoint and graph def files by using the tensorflow.python.tools.freeze_graph utility. For details of freezing TF graphs, please refer to the TensorFlow documentation and the notebooks in directory examples/ in this repo. There are scripts in the utils/ directory for visualizing and writing out a text summary of a given frozen TF graph. This could be useful in determining the input/output names and shapes. Another useful tool for visualizing frozen TF graphs is Netron.There are additional arguments that the converter can take. For details, refer to the full function definition here.For example:When input shapes are fully determined in the frozen .pb file:import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'my_model.pb',
mlmodel_path = 'my_model.mlmodel',
output_feature_names = ['softmax:0'])When input shapes are not fully specified in the frozen .pb file:import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'my_model.pb',
mlmodel_path = 'my_model.mlmodel',
output_feature_names = ['softmax:0'],
input_name_shape_dict = {'input:0' : [1, 227, 227, 3]})Following topics are discussed in the jupyter notebooks under the examples/ folder:inception_v1_preprocessing_steps.ipynb: How to generate a classifier model with image input types and the importance of properly setting the preprocessing parameters.inception_v3.ipynb: How to strip the "DecodeJpeg" op from the TF graph to prepare it for CoreML conversion.linear_mnist_example.ipynb: How to get a frozen graph from the checkpoint and graph description files generated by training in TF.ssd_example.ipynb: How to extract a portion of the TF graph that can be converted, from the overall graph that may have unsupported ops.style_transfer_example.ipynb: How to edit a CoreML model to get an image output type (by default the outputs are MultiArrays).custom_layer_examples.ipynb: A few examples to demonstrate the process of adding custom CoreML layers for unsupported TF ops.Supported OpsList of TensorFlow ops that are supported currently (see tfcoreml/_ops_to_layers.py):AbsAddArgMaxAvgPoolBatchNormWithGlobalNormalizationBatchToSpaceND*BiasAddConcatV2, ConcatConstConv2DConv2DBackpropInputCropAndResize*DepthToSpaceDepthwiseConv2dNativeEluExpExtractImagePatchesFusedBatchNormIdentityLogLRNMatMulMax*MaximumMaxPoolMean*Min*MinimumMirrorPadMulNegOneHotPadPlaceholderPow*Prod*RealDivReciprocalReluRelu6Reshape*ResizeNearestNeighborResizeBilinearRsqrtSigmoidSlice*SoftmaxSpaceToBatchND*SpaceToDepthSplit*SqrtSquareSquaredDifferenceStridedSlice*SubSum*TanhTranspose*Note that certain parameterizations of these ops may not be fully supported. For ops marked with an asterisk, only very specific usage patterns are supported. In addition, there are several other ops (not listed above) that are skipped by the converter as they generally have no effect during inference. Kindly refer to the files tfcoreml/_ops_to_layers.py and tfcoreml/_layers.py for full details. For unsupported ops or configurations, the custom layer feature of CoreML can be used. For details, refer to the examples/custom_layer_examples.ipynb notebook.Scripts for converting several of the following pretrained models can be found at tests/test_pretrained_models.py. Other models with similar structures and supported ops can be converted. Below is a list of publicly available TensorFlow frozen models that can be converted with this converter:Inception v1 (Slim)Inception v2 (Slim)Inception v3 (Slim)Inception v4 (Slim)Inception v3 (non-Slim)*Inception/ResNet v2 (Slim)MobileNet variations (Slim):Image size: 128 (1, 2, 3, 4)Image size: 160 (1, 2, 3, 4)Image size: 192 (1, 2, 3, 4)Image size: 224 (1, 2, 3, 4)Image stylization network+Mobilenet SSD**Converting these models requires extra steps to extract subgraphs from the TF frozen graphs. See examples/ for details. <br> +There are known issues running image stylization network on GPU. (See Issue #26)Limitationstfcoreml converter has the following constraints:TF graph must be cycle free (cycles are generally created due to control flow ops like if, while, map, etc.)Must have NHWC ordering (Batch size, Height, Width, Channels) for image feature map tensorsMust have tensors with rank less than or equal to 4 (len(tensor.shape) <= 4)The converter produces CoreML model with float values. A quantized TF graph (such as the style transfer network linked above) gets converted to a float CoreML modelRunning Unit TestsIn order to run unit tests, you need pytest.pip install pytestTo add a new unit test, add it to the tests/ folder. Make sure you name the file with a 'test' as the prefix. To run all unit tests, navigate to the tests/ folder and runpytestDirectories"tfcoreml": the tfcoreml package"examples": examples to use this converter"tests": unit tests"utils": general scripts for graph inspectionLicenseApache License 2.0
cd tf-coremlTo install as a package with pip, either run (at the root directory):pip install -e .or run:python setup.py bdist_wheelThis will generate a pip installable wheel inside the dist directory.Install From PyPITo install the Pypi package:pip install -U tfcoremlUsageSee iPython notebooks in the directory examples/ for examples of how to use the converter.The following arguments are required by the CoreML converter:path to the frozen .pb graph file to be convertedpath where the .mlmodel should be writtena list of output tensor names present in the TF grapha dictionary of input names and their shapes (as list of integers). This is only required if input tensors' shapes are not fully defined in the frozen .pb file (e.g. they contain None or ?)Note that the frozen .pb file can be obtained from the checkpoint and graph def files by using the tensorflow.python.tools.freeze_graph utility. For details of freezing TF graphs, please refer to the TensorFlow documentation and the notebooks in directory examples/ in this repo. There are scripts in the utils/ directory for visualizing and writing out a text summary of a given frozen TF graph. This could be useful in determining the input/output names and shapes. Another useful tool for visualizing frozen TF graphs is Netron.There are additional arguments that the converter can take. For details, refer to the full function definition here.For example:When input shapes are fully determined in the frozen .pb file:import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'my_model.pb',
mlmodel_path = 'my_model.mlmodel',
output_feature_names = ['softmax:0'])When input shapes are not fully specified in the frozen .pb file:import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'my_model.pb',
mlmodel_path = 'my_model.mlmodel',
output_feature_names = ['softmax:0'],
input_name_shape_dict = {'input:0' : [1, 227, 227, 3]})Following topics are discussed in the jupyter notebooks under the examples/ folder:inception_v1_preprocessing_steps.ipynb: How to generate a classifier model with image input types and the importance of properly setting the preprocessing parameters.inception_v3.ipynb: How to strip the "DecodeJpeg" op from the TF graph to prepare it for CoreML conversion.linear_mnist_example.ipynb: How to get a frozen graph from the checkpoint and graph description files generated by training in TF.ssd_example.ipynb: How to extract a portion of the TF graph that can be converted, from the overall graph that may have unsupported ops.style_transfer_example.ipynb: How to edit a CoreML model to get an image output type (by default the outputs are MultiArrays).custom_layer_examples.ipynb: A few examples to demonstrate the process of adding custom CoreML layers for unsupported TF ops.Supported OpsList of TensorFlow ops that are supported currently (see tfcoreml/_ops_to_layers.py):AbsAddArgMaxAvgPoolBatchNormWithGlobalNormalizationBatchToSpaceND*BiasAddConcatV2, ConcatConstConv2DConv2DBackpropInputCropAndResize*DepthToSpaceDepthwiseConv2dNativeEluExpExtractImagePatchesFusedBatchNormIdentityLogLRNMatMulMax*MaximumMaxPoolMean*Min*MinimumMirrorPadMulNegOneHotPadPlaceholderPow*Prod*RealDivReciprocalReluRelu6Reshape*ResizeNearestNeighborResizeBilinearRsqrtSigmoidSlice*SoftmaxSpaceToBatchND*SpaceToDepthSplit*SqrtSquareSquaredDifferenceStridedSlice*SubSum*TanhTranspose*Note that certain parameterizations of these ops may not be fully supported. For ops marked with an asterisk, only very specific usage patterns are supported. In addition, there are several other ops (not listed above) that are skipped by the converter as they generally have no effect during inference. Kindly refer to the files tfcoreml/_ops_to_layers.py and tfcoreml/_layers.py for full details. For unsupported ops or configurations, the custom layer feature of CoreML can be used. For details, refer to the examples/custom_layer_examples.ipynb notebook.Scripts for converting several of the following pretrained models can be found at tests/test_pretrained_models.py. Other models with similar structures and supported ops can be converted. Below is a list of publicly available TensorFlow frozen models that can be converted with this converter:Inception v1 (Slim)Inception v2 (Slim)Inception v3 (Slim)Inception v4 (Slim)Inception v3 (non-Slim)*Inception/ResNet v2 (Slim)MobileNet variations (Slim):Image size: 128 (1, 2, 3, 4)Image size: 160 (1, 2, 3, 4)Image size: 192 (1, 2, 3, 4)Image size: 224 (1, 2, 3, 4)Image stylization network+Mobilenet SSD**Converting these models requires extra steps to extract subgraphs from the TF frozen graphs. See examples/ for details. <br> +There are known issues running image stylization network on GPU. (See Issue #26)Limitationstfcoreml converter has the following constraints:TF graph must be cycle free (cycles are generally created due to control flow ops like if, while, map, etc.)Must have NHWC ordering (Batch size, Height, Width, Channels) for image feature map tensorsMust have tensors with rank less than or equal to 4 (len(tensor.shape) <= 4)The converter produces CoreML model with float values. A quantized TF graph (such as the style transfer network linked above) gets converted to a float CoreML modelRunning Unit TestsIn order to run unit tests, you need pytest.pip install pytestTo add a new unit test, add it to the tests/ folder. Make sure you name the file with a 'test' as the prefix. To run all unit tests, navigate to the tests/ folder and runpytestDirectories"tfcoreml": the tfcoreml package"examples": examples to use this converter"tests": unit tests"utils": general scripts for graph inspectionLicenseApache License 2.0