【问题标题】:Convert frozen graph to tflite for Coral using TFLiteConverter (Python API)使用 TFLiteConverter (Python API) 将冻结图转换为珊瑚的 tflite
【发布时间】:2019-11-28 16:32:09
【问题描述】:

我正在尝试将这个经过预训练的 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 使用命令行。

【问题讨论】:

    标签: python tensorflow google-coral


    【解决方案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,不会出现任何兼容性问题。

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

    【讨论】:

    • 谢谢舒巴姆。我仍然希望有一种方法可以转换这个预训练模型。我选择了一个 MobileNet 模型,因为我认为它可以转换。但是,该错误实际上表明可能不支持某些操作。我在哪里可以找到默认限制为支持的操作的 Keras 预训练模型?我正在寻找与 TPU/Coral 兼容的 Google Open Images 训练模型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    相关资源
    最近更新 更多