MTK工具文檔:https://neuropilot.mediatek.com/auth/resources/login?redirect_uri=/resources/common/latest/en/docs/converter-and-quantization
MTK工具是聯發科所研發的。提供了易使用的界面,包括將網格模式轉化為一種更好的量化網格模型的解決方案。在此就不多贅述為何使用該工具了,開發中有需求進行的實踐。
本文主要是進行TF-Frozen-pb到TFLite的轉換。實現如下:
(1)下載工具包
下載鏈接見參考文檔,只用到了Neuropilot_converter_quantizer_package_2.0_2019_07_17工具。
(2)安裝
文檔中提到了四種方法,這里采用了最簡單的pip方法。
①解壓安裝包
②安裝工具包
tar -xf neuropilot_converter_quantizer_package_2.0_2019_07_17.tar.gz
pip install 所安裝的路徑/volumes/neuropilot-release/neuropilot-package/dist/neuropilot-1.0.0rc0-cp35-cp35m-linux_x86_64.whl --user
③測試是否安裝完成$(python3 -c 'import neuropilot.path; print(neuropilot.path.export_paths())') 也可以進入Python,import
neuropilot 不報錯就是安裝好了
tips:安裝包是用於Python3.5的,版本不符合需要建立相應虛擬環境
虛擬環境的使用(使用了anaconda,安裝方法可以搜索一下):
①建立:conda create -n 虛擬環境名 python=X.X 安裝后虛擬環境文件可以在Anaconda安裝目錄envs文件下找到
還可以同時安裝需要的包,如:conda create -n env_name numpy matplotlib python=2.7
②激活:
Linux: source activate 虛擬環境名
Windows: activate 虛擬環境名
tips:可以用python --version檢查版本
③安裝相應需要的包
conda install -n 虛擬環境名[package]
④退出虛擬環境
不用時即可退出:
Linux: source deactivate / Windows: deactivate
(3).pb文件的生成:
from tensorflow.python.framework import graph_util
### 保存pb文件 ###
input_graph_def = self.graph.as_graph_def()
constant_graph = graph_util.convert_variables_to_constants(self.session, input_graph_def, ['output'])
with tf.gfile.FastGFile(checkpoint + 'model.pb', mode='wb') as f:
f.write(constant_graph.SerializeToString())
其中的‘output’是在TensorFlow模型中設定的圖輸出的name
(4)使用工具:
輸入指令
toco \
--graph_def_file=.pb文件 \
--input_format=TENSORFLOW_GRAPHDEF \
--output_file=converted_model.tflite \
--output_format=TFLITE \
--inference_type=FLOAT \
--inference_input_type=FLOAT \
--input_arrays=圖中的輸入 name\
--output_arrays=圖中的輸出 name\
--input_shapes=輸入圖的shape \
--partial_quant=true
/
紅色部分填寫自己所保存模型的相關信息,最后一句是用於量化模型,可不進行
錯誤及解決
(1)ValueError: NodeDef mentions attr 'explicit_paddings' not in Op<name=Conv2D; signature=input:T, filter:T -> output:T; attr=T:type,allowed=[DT_HALF, DT_BFLOAT16, DT_FLOAT, DT_DOUBLE]; attr=strides:list(int); attr=use_cudnn_on_gpu:bool,default=true; attr=padding:string,allowed=["SAME", "VALID"]; attr=data_format:string,default="NHWC",allowed=["NHWC", "NCHW"]; attr=dilations:list(int),default=[1, 1, 1, 1]>; NodeDef: {{node conv1d/conv1d}}. (Check whether your GraphDef-interpreting binary is up to date with your GraphDef-generating binary.)
答:生成pb使用的TensorFlow和運行toco指令的TensorFlow版本不符合。用同一版本運行即可
(2)-bash:syntax error near unexpected token `newline'
答:指令中存在不能識別的字符,例如<>
(3)提示找不到input/output
答:檢查自己填寫的參數