无法将自定义训练的冻结模型转换为tflite格式

时间:2019-03-25 07:47:19

标签: python tensorflow tensorflow-lite

我使用以下脚本,可以将deeplabv3_mnv2_pascal_train.pb模型(click here to download)成功转换为tflite格式

tflite_convert \
  --output_file=test.lite \
  --graph_def_file=deeplabv3_mnv2_pascal_tain.pb \
  --input_arrays=ImageTensor \
  --output_arrays=SemanticPredictions \
  --input_shapes=1,513,513,3 \
  --inference_input_type=QUANTIZED_UINT8 \
  --inference_type=FLOAT \
  --mean_values=128 \
  --std_dev_values=128

我获得了 使用以下python脚本来获取deeplabv3_mnv2_pascal_train.pb的input_arrays和output_arrays。我从以下网址获取了此python脚本:Obtain input_array and output_array items to convert model to tflite format

import tensorflow as tf
gf = tf.GraphDef()   
m_file = open('deeplabv3_mnv2_pascal_tain.pb','rb')
gf.ParseFromString(m_file.read())

#We get the names of the nodes
for n in gf.node:
    print( n.name )

#To get the tensor
tensor = n.op

我计划对定制训练的模型应用上述相同步骤,并将其转换为tflite格式。我已经定制训练了一个用于在Tensorflow上进行语义分割的模型,并将其导出为inference graph的形式。我使用上面的python脚本来获取input_arrays和output_arrays,然后运行以下命令:

tflite_convert \
  --output_file=test.lite \
  --graph_def_file=my_graph.pb \
  --input_arrays=Const \
  --output_arrays=detection_masks \
  --input_shapes=1,513,513,3 \
  --inference_input_type=QUANTIZED_UINT8 \
  --inference_type=FLOAT \
  --mean_values=128 \
  --std_dev_values=128

我遇到以下错误

2019-03-25 12:54:10.156375: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 558, in set_shape
    unknown_shape)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 1 and 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ajinkya/.local/bin/tflite_convert", line 11, in <module>
    sys.exit(main())
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 412, in main
    app.run(main=run_main, argv=sys.argv[:1])
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 408, in run_main
    _convert_model(tflite_flags)
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 100, in _convert_model
    converter = _get_toco_converter(flags)
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 87, in _get_toco_converter
    return converter_fn(**converter_kwargs)
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/lite.py", line 286, in from_frozen_graph
    _set_tensor_shapes(input_tensors, input_shapes)
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/convert_saved_model.py", line 205, in set_tensor_shapes
    tensor.set_shape(shape)
  File "/home/ajinkya/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 561, in set_shape
    raise ValueError(str(e))
ValueError: Shapes must be equal rank, but are 1 and 4

如何解决此错误?并获得tflite模型用于定制训练的语义分割冻结推理图

1 个答案:

答案 0 :(得分:0)

Tflite没有正确安装,结果代码产生了奇怪的输出。我在另一个操作系统上重新安装了TensorFlow,此问题已解决。