使用 TFLiteConverter 将冻结图转换为 Coral 的 tflite (Python API)

Convert frozen graph to tflite for Coral using TFLiteConverter (Python API)

我正在尝试将这个预训练的 MobileNet V2 model from the Tensorflow Model Zoo 转换为 tflite 格式以在 Google Coral board 上工作;我无法解决此错误: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)

感谢任何帮助!

我见过 使用命令行。

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。

You downloaded the .pb file from the TensorFlow Model Zoo. This file contains TF ops which may not be supported by TFLite. Hence, we use the Keras pretrained model which contains supported ops by default.