Tensor Flow to Core
ML model
Abby Lai


2019/08/19
How to convert TensorFlow
to Core ML?
1. Use Tensor Flow to export
fi
le to xxx.pb


2. Get model Input/Output


3. Use tf-coreml to convert and get .mlmodel
fi
le


4. Import to Xcode project
– TensorFlow (TF) to CoreML Converter 

https://github.com/tf-coreml/tf-coreml
tf-coreml
Install tf-coreml
• Clone


• $ git clone https://github.com/tf-coreml/tf-coreml.git


• With pip


• $ pip install tensor
fl
ow —user


• $ pip install coremltools —user


• $ pip install -e .


• Without pip


• $ sudo python setup.py bdist_wheel


• Make sure you installed coremltools, then


$ pip install -U tfcoreml==0.3.0 —user


Version 0.3.0 is suitable, 0.4.0 will get some error.
Tf-coreml Dependencies


tensor
fl
ow >= 1.5.0


coremltools >= 0.8


numpy >= 1.6.2


protobuf >= 3.1.0


six >= 1.10.0
If you don’t know the input/out of model, you can get the
information from .pb
Get Model Input/Output
Get Input/Output from .pb
• $cd tf-coreml


• $cd utils


• $python inspect_pb.py inception.pb(.pb name)
inceptionInfo.text(info
fi
le name)


• You will get inceptionInfo.text in the same folder


• Sample


• https://blog.csdn.net/weixin_41713230/article/details/
84146087


• https://www.jianshu.com/p/80d930394282
inceptionInfo.text
• Find


• input:0


• Softmax2:0
Convert
Run python convert.py
import tfcoreml as tf_converter


import tensor
fl
ow as tf




from tensor
fl
ow.python.framework import ops


ops.reset_default_graph()




coreml_model =
tf_converter.convert(tf_model_path="tensor
fl
ow_inception_graph.pb",
mlmodel_path='inception.mlmodel',
output_feature_names=['softmax2:0'], image_input_names=
['image'], input_name_shape_dict={'input:0': [1, 416, 416, 3]},
is_bgr=True, image_scale=1/255.0)
Limitation
• Run


• coreml_model = tf_converter.convert(tf_model_path="deeplab.pb",
mlmodel_path='deeplab.mlmodel',
output_feature_names=['SemanticPredictions:0'],
input_name_shape_dict={'ImageTensor:0':[1, 512, 512, 3]})


• Result


• NotImplementedError: Unsupported Ops of type: Shape,Pack


• Some layer is not support


• Custom layer - Convert Your Network to Core ML


https://developer.apple.com/documentation/coreml/core_ml_api/
creating_a_custom_layer
Reference
• facial landmark localization by deep learning port to ios with
coreml


• 机器学习Tensor
fl
ow笔记4:iOS通过Core ML使⽤Tensor
fl
ow训
练模型


• 把 Darknet 模型轉換成 CoreML 模型


• Download Pre-trained Models (.pb)


• Deeplab


• Inception

Tensor Flow to CoreML model

  • 1.
    Tensor Flow toCore ML model Abby Lai 2019/08/19
  • 2.
    How to convertTensorFlow to Core ML? 1. Use Tensor Flow to export fi le to xxx.pb 2. Get model Input/Output 3. Use tf-coreml to convert and get .mlmodel fi le 4. Import to Xcode project
  • 3.
    – TensorFlow (TF)to CoreML Converter 
 https://github.com/tf-coreml/tf-coreml tf-coreml
  • 4.
    Install tf-coreml • Clone •$ git clone https://github.com/tf-coreml/tf-coreml.git • With pip • $ pip install tensor fl ow —user • $ pip install coremltools —user • $ pip install -e . • Without pip • $ sudo python setup.py bdist_wheel • Make sure you installed coremltools, then 
 $ pip install -U tfcoreml==0.3.0 —user 
 Version 0.3.0 is suitable, 0.4.0 will get some error. Tf-coreml Dependencies tensor fl ow >= 1.5.0 coremltools >= 0.8 numpy >= 1.6.2 protobuf >= 3.1.0 six >= 1.10.0
  • 5.
    If you don’tknow the input/out of model, you can get the information from .pb Get Model Input/Output
  • 6.
    Get Input/Output from.pb • $cd tf-coreml • $cd utils • $python inspect_pb.py inception.pb(.pb name) inceptionInfo.text(info fi le name) • You will get inceptionInfo.text in the same folder • Sample • https://blog.csdn.net/weixin_41713230/article/details/ 84146087 • https://www.jianshu.com/p/80d930394282
  • 7.
  • 8.
  • 9.
    Run python convert.py importtfcoreml as tf_converter import tensor fl ow as tf 
 from tensor fl ow.python.framework import ops ops.reset_default_graph() 
 
 coreml_model = tf_converter.convert(tf_model_path="tensor fl ow_inception_graph.pb", mlmodel_path='inception.mlmodel', output_feature_names=['softmax2:0'], image_input_names= ['image'], input_name_shape_dict={'input:0': [1, 416, 416, 3]}, is_bgr=True, image_scale=1/255.0)
  • 10.
    Limitation • Run • coreml_model= tf_converter.convert(tf_model_path="deeplab.pb", mlmodel_path='deeplab.mlmodel', output_feature_names=['SemanticPredictions:0'], input_name_shape_dict={'ImageTensor:0':[1, 512, 512, 3]}) • Result • NotImplementedError: Unsupported Ops of type: Shape,Pack • Some layer is not support • Custom layer - Convert Your Network to Core ML 
 https://developer.apple.com/documentation/coreml/core_ml_api/ creating_a_custom_layer
  • 11.
    Reference • facial landmarklocalization by deep learning port to ios with coreml • 机器学习Tensor fl ow笔记4:iOS通过Core ML使⽤Tensor fl ow训 练模型 • 把 Darknet 模型轉換成 CoreML 模型 • Download Pre-trained Models (.pb) • Deeplab • Inception