使用TFLiteConverter(Python API)将冻结的图形转换为用于珊瑚的tflite

时间:2019-11-27 21:04:55

标签: python tensorflow google-coral

我正在尝试将这种经过预先训练的MobileNet V2 modelTensorflow Model Zoo转换为tflite格式,以便在Google Coral板上工作;我无法解决此错误:Check failed: dim >= 1 (0 vs. 1)。知道我在做什么错吗?这是代码(Tensorflow版本:1.13.1; Python版本Python 3.7.3,平台:Mac):

import tensorflow as tf

graph_def_file      = "frozen_inference_graph.pb"
input_arrays        = ["image_tensor"]
output_arrays       = ["detection_boxes","detection_scores","detection_classes","num_detections"]
input_layer_shape   = {"image_tensor" : [1,224,224,3]}

converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
        graph_def_file, 
        input_arrays, 
        output_arrays, 
        input_shapes=input_layer_shape)

converter.post_training_quantize = True # I've tried with and without this line.

tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

这是输出:

Traceback (most recent call last):
  File "converter_ts1.py", line 18, in <module>
    tflite_model = converter.convert()
  File "/anaconda3/lib/python3.7/site-packages/tensorflow/lite/python/lite.py", line 455, in convert
    **converter_kwargs)
  File "/anaconda3/lib/python3.7/site-packages/tensorflow/lite/python/convert.py", line 442, in toco_convert_impl
    input_data.SerializeToString())
  File "/anaconda3/lib/python3.7/site-packages/tensorflow/lite/python/convert.py", line 205, in toco_convert_protos
    "TOCO failed. See console for info.\n%s\n%s\n" % (stdout, stderr))
tensorflow.lite.python.convert.ConverterError: TOCO failed. See console for info.
2019-11-27 12:45:56.966056: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:56.986099: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:57.035935: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:57.035992: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:57.036104: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:57.036144: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: LoopCond
2019-11-27 12:45:57.036228: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArrayV3
2019-11-27 12:45:57.036264: I tensorflow/lite/toco/import_tensorflow.cc:193] Unsupported data type in placeholder op: 20
2019-11-27 12:45:57.036281: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:57.036297: I tensorflow/lite/toco/import_tensorflow.cc:193] Unsupported data type in placeholder op: 20
2019-11-27 12:45:57.036312: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:57.036339: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Exit
2019-11-27 12:45:57.036357: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArraySizeV3
2019-11-27 12:45:57.036380: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArrayGatherV3
2019-11-27 12:45:57.036464: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArrayWriteV3
2019-11-27 12:45:57.036491: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArrayV3
2019-11-27 12:45:57.036508: I tensorflow/lite/toco/import_tensorflow.cc:193] Unsupported data type in placeholder op: 20
2019-11-27 12:45:57.036523: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:45:57.036538: I tensorflow/lite/toco/import_tensorflow.cc:193] Unsupported data type in placeholder op: 20
...
2019-11-27 12:34:01.625025: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArrayScatterV3
2019-11-27 12:34:01.625040: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: Enter
2019-11-27 12:34:01.625057: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArrayReadV3
2019-11-27 12:34:01.625085: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: TensorArrayWriteV3
2019-11-27 12:34:06.976866: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 14621 operators, 28988 arrays (0 quantized)
2019-11-27 12:34:12.928567: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After Removing unused ops pass 1: 14614 operators, 28972 arrays (0 quantized)
2019-11-27 12:34:21.394682: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 14614 operators, 28972 arrays (0 quantized)
2019-11-27 12:34:29.163567: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After general graph transformations pass 1: 14268 operators, 26728 arrays (0 quantized)
2019-11-27 12:34:40.443505: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before dequantization graph transformations: 14268 operators, 26728 arrays (0 quantized)
2019-11-27 12:34:41.877219: F tensorflow/lite/toco/tooling_util.cc:627] Check failed: dim >= 1 (0 vs. 1)

任何帮助表示赞赏!

我已经在命令行中看到this related post

1 个答案:

答案 0 :(得分:0)

tf.lite.TFLiteConverter API支持要转换的有限数量的操作。也许MobileNetV2包含不受支持的此类操作。我会坚持要求您遵循以下步骤:

  1. 如果您需要MobileNetV2模型,请在Python文件中创建tf.keras.applications.MobileNetV2的实例。

  2. 您将有一个tf.keras.models.Model实例。如果需要,可以在您的自定义数据集上训练该模型。

  3. 现在,使用tf.lite.TFLiteConverter.from_keras_model()方法将此模型转换为.tflite。

此模型将转换为TFLite,而不会出现任何兼容性问题。

  

您从TensorFlow模型动物园下载了.pb文件。该文件包含TF op,而TFLite可能不支持。因此,我们使用Keras预训练模型,该模型默认包含受支持的操作。