Search code examples
pythontensorflowgoogle-coral

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; I can't get around this error: 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):

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.


Solution

  • The tf.lite.TFLiteConverter API supports a limited number of ops to be transformed. Maybe the MobileNetV2 contains such ops which are unsupported. 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.

    2. You will have a tf.keras.models.Model instance. 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.

    This model will get transformed into TFLite without any compatibility issues.

    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.