簡體   English   中英

如何使用Tensorflow將訓練模型轉換為Core ML

[英]How to convert trained model to Core ML with Tensorflow

Apple今年在iOS11上推出了Core ML 有一個Core ML工具可以將訓練有素的模型轉換為Core ML格式(.mlmodel)。

是否可以使用Tensorflow轉換Core ML模型? 怎么樣?

您可以通過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'輕松創建

TF-coreml

您可以使用tf-coreml包將某些常見的Tensorflow模型轉換為CoreML。 截至本文撰寫時(1月16日)它仍然相當新。 它看起來是由幾位Apple工程師創建的。

概觀

根據他們的示例,首先使用tensorflow.python.tools.freeze_graph凍結TF模型,然后使用tfcoreml.convert方法生成CoreML對象。

引用他們的一個例子

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

根據文檔(至少現在)你需要自己編寫: https//developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml#2903105

是的,如果您的機器學習模型采用以下格式之一,您可以: Caffe,Keras,XGBoost,Scikit-learn,MXNet,LibSVM Awesome Core ML上有每個教程和示例。

目前尚不支持從Tensorflow直接轉換,但您可以將Caffe架構與TF結合使用。

不,這是不可能的。 主要是因為在保存模型時沒有所有NN框架都應該遵循的格式。

因此,您可能需要在TF中重建計算並訓練模型

Keras是一個高級神經網絡API,用Python編寫,能夠在TensorFlow ,CNTK或Theano之上運行。

目前, coremltools 0.7可以Keras (1.2.2, 2.0.4+) with Tensorflow (1.0.x, 1.1.x)轉換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')

你可以看看我的項目在這里 😀

請轉到此鏈接並查看Convert_pb_coreml.ipynb

如果你想將訓練過的模型即ckpt文件轉換為pb(Tensor Flow),你可以簡單地使用這個命令在YOLO(Darkflow)中

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

它保存.pb文件和.meta文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM