全部利用tf官方python代码(bazel我真滴是mac下编译各种出问题搞不动)
有一个比较坑的地方是第一步和第二步在tf 1.4.0, numpy 1.15.4可以成功,第三步在tf 1.13.1, numpy 1.16.1可以成功
过程分为三步:
- 导出前向传播图(不包含权重)
python export_inference_graph.py \
--model_name=mobilenet_v2_035 \
--output_file=0318_log_v2_035_mobilenet/0318_export_v2_035_mobilenet.pb \
--dataset_name=cards \
--dataset_dir=../datasets/cards_0317
- 将前向传播图(不包含权重)和训练得到的cpkt文件(包含权重)固化在一起
python freeze_graph.py \
--input_binary=true \
--input_graph=0318_log_v2_035_mobilenet/0318_export_v2_035_mobilenet.pb \
--input_checkpoint=0318_log_v2_035_mobilenet/model.ckpt-18000 \
--output_graph=0318_log_v2_035_mobilenet/0318_frozen_v2_035_mobilenet.pb \
--output_node_names=MobilenetV2/Predictions/Reshape_1
- 利用TFLiteConverter将固化后的pb文件转成tflite格式文件
import tensorflow as tf
graph_def_file = "0318_log_v2_035_mobilenet/0318_frozen_v2_035_mobilenet.pb"
input_arrays = ["input"]
output_arrays = ["MobilenetV2/Predictions/Softmax"]
converter = tf.lite.TFLiteConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("0318_log_v2_035_mobilenet/0318_v2_035_float_mobilenet.tflite", "wb").write(tflite_model)