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

问题描述 投票:0回答:1

我有以下脚本使用,我能够成功地将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格式。我已经定制训练了一个用于张量流的语义分割的模型,并以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模型

python tensorflow tensorflow-lite
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.