简体   繁体   English

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

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

I am trying to convert this pre-trained MobileNet V2 model from the Tensorflow Model Zoo to tflite format to work on a Google Coral board; 我正在尝试将这种经过预先训练的MobileNet V2模型Tensorflow Model Zoo转换为tflite格式,以便在Google Coral板上工作; I can't get around this error: Check failed: dim >= 1 (0 vs. 1) . 我无法解决此错误: Check failed: dim >= 1 (0 vs. 1) Any idea what I'm doing wrong? 知道我在做什么错吗? Here is the the code (Tensorflow version: 1.13.1 ; Python version Python 3.7.3 , Platform: Mac ): 以下是代码(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)

And here is the output: 这是输出:

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)

Any help appreciated! 任何帮助表示赞赏!

I've seen this related post using the command line. 我已经使用命令行查看了此相关文章

The tf.lite.TFLiteConverter API supports a limited number of ops to be transformed. tf.lite.TFLiteConverter API支持要转换的有限数量的操作。 Maybe the MobileNetV2 contains such ops which are unsupported. 也许MobileNetV2包含不受支持的此类操作。 I would insist you to follow these steps: 我会坚持要求您遵循以下步骤:

  1. If you need the MobileNetV2 model, create an instance of tf.keras.applications.MobileNetV2 in a Python file. 如果需要MobileNetV2模型, tf.keras.applications.MobileNetV2在Python文件中创建MobileNetV2的实例。

  2. You will have a tf.keras.models.Model instance. 您将有一个tf.keras.models.Model实例。 If required train this model on your custom dataset. 如果需要,可以在您的自定义数据集上训练此模型。

  3. Now, convert this model to .tflite using the tf.lite.TFLiteConverter.from_keras_model() method. 现在,使用tf.lite.TFLiteConverter.from_keras_model()方法将此模型转换为.tflite。

This model will get transformed into TFLite without any compatibility issues. 该模型将转换为TFLite,而不会出现任何兼容性问题。

You downloaded the .pb file from the TensorFlow Model Zoo. 您从TensorFlow模型动物园下载了.pb文件。 This file contains TF ops which may not be supported by TFLite. 该文件包含TF op,而TFLite可能不支持。 Hence, we use the Keras pretrained model which contains supported ops by default. 因此,我们使用Keras预训练模型,该模型默认包含受支持的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM