2017年底,Tensorflow 推出Lite版本,可實(shí)現(xiàn)移動(dòng)端的快速運(yùn)行,其中,一個(gè)很關(guān)鍵的問(wèn)題,如何把現(xiàn)有分類模型(.pb) 轉(zhuǎn)換為(.lite)模型呢?
其實(shí),步驟如下
1- 進(jìn)入 Tensorflow 源碼文件夾(以便bazel可以無(wú)需配置找打相應(yīng)路徑)
2- 轉(zhuǎn)換可執(zhí)行文件
bazel run --config=opt / //tensorflow/contrib/lite/toco:toco -- / --input_file=/tmp/mobilenet_v1_1.0_224_frozen.pb / # 待轉(zhuǎn)換模型路徑 --output_file=/tmp/tflite_model2.tflite / # 目標(biāo)模型路徑 --input_format=TENSORFLOW_GRAPHDEF / --output_format=TFLITE / --input_shape=1,224,224,3 / # 輸入圖像寬高 --input_array=input / # 輸入節(jié)點(diǎn)名稱<參考1> --output_array=MobilenetV1/Predictions/Reshape_1 / # 輸出節(jié)點(diǎn)名稱<參考1> --inference_type=FLOAT / # 圖像數(shù)據(jù)類型 --input_data_type=FLOAT
參考1: 如果輸入輸出節(jié)點(diǎn)不知道:進(jìn)入python,import tf ,并通過(guò)如下命令( tf.GraphDef() )查找:
>>> import tensorflow as tf>>> gf = tf.GraphDef()>>> gf.ParseFromString(open('/your/path/to/graphname.pb','rb').read())>>> for n in gf.node:>>> print ( n.name +' ===> '+n.op )
參轉(zhuǎn)換后,在android手機(jī)上運(yùn)行,在不考慮精度的前提下,不同實(shí)現(xiàn)方式得到的結(jié)果如下:
模型 | type | model size | speed |
---|---|---|---|
mobilenet-v1-224 | .pb | 17 mb | 106 ms |
mobilenet-v1-224 | .lite | 16.4 mb | 72 ms |
以上這篇Tensorflow 模型轉(zhuǎn)換 .pb convert to .lite實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選