2017-06-15 77 views

回答

3

您可以通過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可以容易地通過創建「順序」

0

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

那麼可能是你將需要重建的計算中TF和火車模型

1

是,如果您的機器學習模型採用以下格式之一: Caffe,Keras,XGBoost,Scikit-learn,MXNet,LibSVM。 在Awesome Core ML上有關於它們的教程和例子。

尚未支持Tensorflow的直接轉換,但您可以使用帶有TF的Caffe體系結構使其正常工作。

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

3

​​

可以某些共同Tensorflow模型轉換爲CoreML與​​包。截至撰寫本文時(1月16日),這仍然是相當新的。它看起來是由一對蘋果工程師創建的。

概述

根據他們的例子,你先用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) 
+1

雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接以供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/18535009) – MarqueIV

相關問題