如何使用Tensorflow将训练模型转换为Core ML

时间:2017-06-15 02:35:17

标签: ios tensorflow

Apple今年在iOS11上推出Core ML。有一个Core ML Tool将训练模型转换为Core ML格式(.mlmodel)。

是否可以使用Tensorflow转换Core ML模型?怎么样?

7 个答案:

答案 0 :(得分:4)

您可以通过keras训练模型。

coreml_model = coremltools.converters.keras.convert('./Any.h5',
                                                input_names='image',
                                                image_input_names='image',
                                                output_names='output',
                                                class_labels=['1', '2'],
                                                image_scale=1/255)
coreml_model.save('abc.mlmodel')

.h5可以通过'Sequential'轻松创建

答案 1 :(得分:4)

tf-coreml

您可以使用tf-coreml包将某些常见的Tensorflow模型转换为CoreML。截至本文撰写时(1月16日)它仍然相当新。它看起来是由几位Apple工程师创建的。

概述

根据他们的示例,您首先使用tensorflow.python.tools.freeze_graph冻结TF模型,然后使用tfcoreml.convert方法生成CoreML对象。

实施例

引用one of their examples

"""
Step 1: "Freeze" your tensorflow model - convert your TF model into a 
stand-alone graph definition file
Inputs: 
(1) TensorFlow code
(2) trained weights in a checkpoint file
(3) The output tensors' name you want to use in inference
(4) [Optional] Input tensors' name to TF model
Outputs: 
(1) A frozen TensorFlow GraphDef, with trained weights frozen into it
"""

# Provide these to run freeze_graph:
# Graph definition file, stored as protobuf TEXT
graph_def_file = './model.pbtxt'
# Trained model's checkpoint name
checkpoint_file = './checkpoints/model.ckpt'
# Frozen model's output name
frozen_model_file = './frozen_model.pb'
# Output nodes. If there're multiple output ops, use comma separated string, e.g. "out1,out2".
output_node_names = 'Softmax' 


# Call freeze graph
freeze_graph(input_graph=graph_def_file,
             input_saver="",
             input_binary=False,
             input_checkpoint=checkpoint_file,
             output_node_names=output_node_names,
             restore_op_name="save/restore_all",
             filename_tensor_name="save/Const:0",
             output_graph=frozen_model_file,
             clear_devices=True,
             initializer_nodes="")

"""
Step 2: Call converter
"""

# Provide these inputs in addition to inputs in Step 1
# A dictionary of input tensors' name and shape (with batch)
input_tensor_shapes = {"Placeholder:0":[1,784]} # batch size is 1
# Output CoreML model path
coreml_model_file = './model.mlmodel'
output_tensor_names = ['Softmax:0']


# Call the converter
coreml_model = tfcoreml.convert(
        tf_model_path=frozen_model_file, 
        mlmodel_path=coreml_model_file, 
        input_name_shape_dict=input_tensor_shapes,
        output_feature_names=output_tensor_names)

答案 2 :(得分:1)

答案 3 :(得分:1)

是的,如果您的机器学习模型采用以下格式之一,则可以: Caffe,Keras,XGBoost,Scikit-learn,MXNet,LibSVM 。 在Awesome Core ML上有每个教程和示例。

目前尚不支持Tensorflow的直接转换,但您可以将Caffe架构与TF结合使用。

答案 4 :(得分:0)

不,不可能。主要是因为在保存模型时没有所有NN框架都应该遵循的格式。

所以你可能需要在TF中重建计算并训练你的模型

答案 5 :(得分:0)

Keras是一个高级神经网络API,用Python编写,能够在TensorFlow,CNTK或Theano之上运行。

目前,coremltools 0.7可以转换模型 Keras (1.2.2, 2.0.4+) with Tensorflow (1.0.x, 1.1.x)

# Make a Keras model
>>> model = Sequential()
>>> model.add(Dense(num_channels, input_dim = input_dim))

# Convert it with default input and output names
>>> import coremltools
>>> coreml_model = coremltools.converters.keras.convert(model)

# Saving the Core ML model to a file.
>>> coreml_model.save('my_model.mlmodel')

您可以查看我的项目here

答案 6 :(得分:-1)

请转到此link 并查看Convert_pb_coreml.ipynb

如果要将训练后的模型即ckpt文件转换为pb(Tensor Flow),只需在YOLO(Darkflow)中使用此命令即可

./flow --model cfg/(yourCfgFileName).cfg --load -1 --savepb 

保存一个.pb文件和一个.meta文件。