tf-coreml

TensorFlow 转换到 CoreML 的转换器
授权协议 Apache
开发语言 Python
所属分类 神经网络/人工智能、 机器学习/深度学习
软件类型 开源软件
地区 不详
投 递 者 南门英飙
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

tfcoreml

TensorFlow (TF) to CoreML Converter

Dependencies

  • tensorflow >= 1.5.0

  • coremltools >= 0.8

  • numpy >= 1.6.2

  • protobuf >= 3.1.0

  • six >= 1.10.0

Installation

Install From Source

To 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-coreml

To install as a package with pip, either run (at the root directory):

pip install -e .

or run:

python setup.py bdist_wheel

This will generate a pip installable wheel inside the dist directory.

Install From PyPI

To install the Pypi package:

pip install -U tfcoreml

Usage

See 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 converted

  • path where the .mlmodel should be written

  • a list of output tensor names present in the TF graph

  • a 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 Ops

List of TensorFlow ops that are supported currently (see tfcoreml/_ops_to_layers.py):

  • Abs

  • Add

  • ArgMax

  • AvgPool

  • BatchNormWithGlobalNormalization

  • BatchToSpaceND*

  • BiasAdd

  • ConcatV2, Concat

  • Const

  • Conv2D

  • Conv2DBackpropInput

  • CropAndResize*

  • DepthToSpace

  • DepthwiseConv2dNative

  • Elu

  • Exp

  • ExtractImagePatches

  • FusedBatchNorm

  • Identity

  • Log

  • LRN

  • MatMul

  • Max*

  • Maximum

  • MaxPool

  • Mean*

  • Min*

  • Minimum

  • MirrorPad

  • Mul

  • Neg

  • OneHot

  • Pad

  • Placeholder

  • Pow*

  • Prod*

  • RealDiv

  • Reciprocal

  • Relu

  • Relu6

  • Reshape*

  • ResizeNearestNeighbor

  • ResizeBilinear

  • Rsqrt

  • Sigmoid

  • Slice*

  • Softmax

  • SpaceToBatchND*

  • SpaceToDepth

  • Split*

  • Sqrt

  • Square

  • SquaredDifference

  • StridedSlice*

  • Sub

  • Sum*

  • Tanh

  • Transpose*

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:

*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)

Limitations

tfcoreml 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 tensors

  • Must 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 model

Running Unit Tests

In order to run unit tests, you need pytest.

pip install pytest

To 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 run

pytest

Directories

  • "tfcoreml": the tfcoreml package

  • "examples": examples to use this converter

  • "tests": unit tests

  • "utils": general scripts for graph inspection

License

Apache License 2.0

  • issues unsupported ops of type gatherv2 Embedding layer: ‘embedding_1’: height dimension of the input blob must be 1. NSLocalizedDescription = “Failure dynamically resizing for sequence length.” NSLoc

  •     好久好久没有写博客了,今天记录下前段时间做一个项目遇到的问题和解决办法。之前做一个ios app,需要将训练好的 mobilenetv1 ssd 模型部署到 app上,所以研究了下模型转换(说实话mobilenet v1 ssd coco 这个模型略微坑爹,转tensorflow lite 时也遇到诸多问题,这个有机会之后讲)。     要点时一定要根据自己的 model.config文件

  • Core ML工具安装: pip install -U coremltools Caffe模型转换到Core ML模型 下载一个caffe模型: # Age and gender models wget http://www.openu.ac.il/home/hassner/projects/cnn_agegender/cnn_age_gender_models_and_data.0.0.2.zi

  • Demo仅供参考 使用spark1.6 import java.io.{BufferedReader, InputStreamReader} import java.util.Arrays import org.ansj.splitWord.analysis.ToAnalysis import org.apache.hadoop.fs.FSDataInputStream import org.

 相关资料
  • 我正在使用Spring Boot开发一个应用程序,在这里我实现了身份验证和授权。这是我的角色实体 Rold id在BaseModel中。我想从数据库中获取所有角色,为此我在RoleRepository中编写了一个方法,如下所示 但这又让我犯了这个错误 org . spring framework . core . convert . converternotfoundexception:未找到能够

  • 我正试图从Netsuite中获取数据,并使用Mulesoft Dataweave元素和when将其保存到salesforce对象中,否则,请参见下面的代码片段,但给出了错误: 找不到将“SimpleDataType{type=java.util.LinkedHashMap,mimetype='/',encoding='null'}”转换为“CollectionDataType{type=java.

  • 我有这门课: 这个问题: 但我有这个错误

  • 问题内容: 什么是转换不同的方法来,我知道有一个选项,但想知道是什么其他可用于相同? 注意: 我没有进一步的要求,因此我不能在这个问题上添加更多内容,但是在此时,如果我能知道有哪些不同的方法可用于转换,那将非常棒。 更新: 建议的不同方法是: javax.xml.bind.Marshaller和javax.xml.bind.Unmarshaller XStream的 XMLBean jaxb ca

  • 问题内容: 我想将java.awt.color对象转换为字符串。 它打印java.awt.Color [r = 255,g = 0,b = 0] 现在,我想解析该字符串并解码颜色名称,例如蓝色等。 我在想有没有其他方法可以将Color直接转换为String。 提前致谢。 编辑:我想将此颜色值存储在数据库中。我应该将其存储为String还是RGB值? 问题答案: 否。至少是因为: 几个rgb组合具有

  • 我有pdf格式的数据,我想把它转换成文本。我想删除图像,页眉和页脚,而数据将只以多行表格的形式保留,你能建议转换它的最佳方式吗?我尝试了Tabula和apache tika,但结果并不理想。