【tfcoreml】tensorflow向CoreML模型的轉換工具封裝


安裝tf向apple coreml模型轉換包tfcoreml
基於蘋果自己的轉換工具coremltools進行封裝

tfcoreml

為了將訓練的模型轉換到apple中使用,需要將模型轉換為ios支持的mlmodel形式。目前蘋果官方的推薦使用Core ML tools來進行轉換。
它支持

Scikit Learn
LIBSVM
Caffe
Keras
XGBoost.
Tensorflow
MXNet

等工具包的模型轉換。
但為了更方便的使用tensorflow的模型,在github中找到了tfcoreml的封裝,直接對tensorflow的pb模型進行轉換。

首選需要安裝tfcoreml的依賴:

tensorflow >= 1.5.0
coremltools >= 0.8
numpy >= 1.6.2
protobuf >= 3.1.0
six >= 1.10.0

在安裝CoreMLtools時會遇到以下問題:

  • 1.coremltools的pip 安裝只在python2.7下work,pip
  • 2.tensorflow的win版本只支持python3.x
    直接pip install coremltools會報錯:ERROR: Could not find a version that satisfies the requirement coremltools

為了在這台win上安裝coremltools,只能選擇從源碼編譯安裝了,在release中找到合適的版本
cd coremltools-2.1
這里可以看到setup.cfg:

[bdist_wheel]
plat-name=any
python-tag=2.7   #這里將2.7改為目前平台的python版本3.5

隨后運行wheel安裝:
python setup.py install
即可編譯安裝coremltools。

隨后即可pip安裝tfcoreml
pip install -U tfcoreml

模型轉換demo

這一封裝選擇了pb(protobuf )模型,如果輸入輸出確定的情況下可以直接轉換:

import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'path/to/your_tf_model.pb',
                     mlmodel_path = 'path/to/your_apple_model.mlmodel',
                     output_feature_names = ['softmax:0'])

如果需要指定輸入大小的話,可以利用input_name_shape_dict關鍵字:

import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'path/to/your_tf_model.pb',
                     mlmodel_path = 'path/to/your_apple_model.mlmodel',
                     output_feature_names = ['softmax:0'],
                     input_name_shape_dict = {'your_input:0' : [1, 227, 227, 3]})

ref:
https://stackoverflow.com/questions/44510701/no-matching-distribution-found-for-coremltools
https://stackoverflow.com/questions/44612991/error-installing-coremltools
https://github.com/apple/coremltools/issues/228
ssd_demo:https://github.com/vonholst/SSDMobileNet_CoreML/tree/master/SSDMobileNet/SSDMobileNet
coreml:https://developer.apple.com/documentation/coreml
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM