从预训练模型创建冻结图

时间:2018-04-12 11:12:52

标签: python-2.7 tensorflow bazel

嗨,我是tensorflow的新手。我的目的是根据我的理解将.pb文件从pretrain模型转换为.tflite。我已下载mobilenet_v1_1.0_224 Model。以下是模型的结构

mobilenet_v1_1.0_224.ckpt.data-00000-of-00001  - 66312kb
mobilenet_v1_1.0_224.ckpt.index  - 20kb
mobilenet_v1_1.0_224.ckpt.meta  - 3308kb
mobilenet_v1_1.0_224.tflite     - 16505kb
mobilenet_v1_1.0_224_eval.pbtxt - 520kb
mobilenet_v1_1.0_224_frozen.pb   - 16685kb

我知道模型已经有 .tflite文件,但据我了解,我正在尝试转换它。

我的第一步:创建冻结的图形文件

import tensorflow as tf

imported_meta = tf.train.import_meta_graph(base_dir + model_folder_name + meta_file,clear_devices=True)
graph_ = tf.get_default_graph()

with tf.Session() as sess:
    #saver = tf.train.import_meta_graph(base_dir + model_folder_name + meta_file, clear_devices=True)
    imported_meta.restore(sess, base_dir + model_folder_name + checkpoint)

    graph_def = sess.graph.as_graph_def()

    output_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, ['MobilenetV1/Predictions/Reshape_1'])

    with tf.gfile.GFile(base_dir + model_folder_name + './my_frozen.pb', "wb") as f:
        f.write(output_graph_def.SerializeToString())

我已成功创建 my_frozen.pb - 16590 kb 。但原始文件大小为16,685kb,这在上面的文件夹结构中清晰可见。所以这是我的第一个问题,为什么文件大小不同,我是否遵循了一些错误的路径。

我的第二步:使用bazel命令创建tflite文件

bazel run --config=opt tensorflow/contrib/lite/toco:toco -- --input_file=/path_to_folder/my_frozen.pb --output_file=/path_to_folder/model.tflite --inference_type=FLOAT --input_shape=1,224,224,3 --input_array=input --output_array=MobilenetV1/Predictions/Reshape_1

这个命令给我 model.tflite - 0 kb

bazel Command的引用

INFO: Analysed target //tensorflow/contrib/lite/toco:toco (0 packages loaded).
INFO: Found 1 target...
Target //tensorflow/contrib/lite/toco:toco up-to-date:
  bazel-bin/tensorflow/contrib/lite/toco/toco
INFO: Elapsed time: 0.369s, Critical Path: 0.01s
INFO: Build completed successfully, 1 total action

INFO: Running command line: bazel-bin/tensorflow/contrib/lite/toco/toco '--input_file=/home/ubuntu/DEEP_LEARNING/Prashant/TensorflowBasic/mobilenet_v1_1.0_224/frozengraph.pb' '--output_file=/home/ubuntu/DEEP_LEARNING/Prashant/TensorflowBasic/mobilenet_v1_1.0_224/float_model.tflite' '--inference_type=FLOAT' '--input_shape=1,224,224,3' '--input_array=input' '--output_array=MobilenetV1/Predictions/Reshape_1'
2018-04-12 16:36:16.190375: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1265] Converting unsupported operation: FIFOQueueV2
2018-04-12 16:36:16.190707: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1265] Converting unsupported operation: QueueDequeueManyV2
2018-04-12 16:36:16.202293: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 290 operators, 462 arrays (0 quantized)
2018-04-12 16:36:16.211322: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 290 operators, 462 arrays (0 quantized)
2018-04-12 16:36:16.211756: F tensorflow/contrib/lite/toco/graph_transformations/resolve_batch_normalization.cc:86] Check failed: mean_shape.dims() == multiplier_shape.dims()

Python版本 - 2.7.6 Tensorflow版本 - 1.5.0

提前谢谢:)

2 个答案:

答案 0 :(得分:0)

错误检查失败:mean_shape.dims()== multiplier_shape.dims()

是批处理规范解析的问题,已在以下位置解决:

https://github.com/tensorflow/tensorflow/commit/460a8b6a5df176412c0d261d91eccdc32e9d39f1#diff-49ed2a40acc30ff6d11b7b326fbe56bc

答案 1 :(得分:0)

就我而言,使用tensorflow v1.7时发生了错误 解决方法是使用tensorflow v1.15(每晚)

toco --graph_def_file=/path_to_folder/my_frozen.pb \
--input_format=TENSORFLOW_GRAPHDEF \
--output_file=/path_to_folder/my_output_model.tflite \
--input_shape=1,224,224,3 \
--input_arrays=input \
--output_format=TFLITE \
--output_arrays=MobilenetV1/Predictions/Reshape_1 \
--inference-type=FLOAT