models icon indicating copy to clipboard operation
models copied to clipboard

The error about convert ssd mobilenet v2 coco models into tflite model.

Open yangchaoFree opened this issue 6 years ago • 6 comments

I need to convert ssd_mobilenet_v2_coco model into tflite model. I searched the web, and I didn't get a viable answer. I used tf version is 1.13 and 1.15 .It has same error

Method 1: via command line

tflite_convert --output_file=ssdMobile2.tflite --graph_def_file=ssdlite_mobilenet_v2_coco_2018_05_09/frozen_inference_graph.pb --input_arrays=image_tensor --output_arrays=detection_boxes,detection_scores,num_detections,detection_classes

the error message: 2020-01-15 23:57:30.550956: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version Traceback (most recent call last): File "/root/anaconda3/envs/tf_1.15_cpu/bin/tflite_convert", line 8, in sys.exit(main()) File "/root/anaconda3/envs/tf_1.15_cpu/lib/python3.6/site-packages/tensorflow_core/lite/python/tflite_convert.py", line 515, in main app.run(main=run_main, argv=sys.argv[:1]) File "/root/anaconda3/envs/tf_1.15_cpu/lib/python3.6/site-packages/tensorflow_core/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/root/anaconda3/envs/tf_1.15_cpu/lib/python3.6/site-packages/absl/app.py", line 299, in run _run_main(main, args) File "/root/anaconda3/envs/tf_1.15_cpu/lib/python3.6/site-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "/root/anaconda3/envs/tf_1.15_cpu/lib/python3.6/site-packages/tensorflow_core/lite/python/tflite_convert.py", line 511, in run_main _convert_tf1_model(tflite_flags) File "/root/anaconda3/envs/tf_1.15_cpu/lib/python3.6/site-packages/tensorflow_core/lite/python/tflite_convert.py", line 199, in _convert_tf1_model output_data = converter.convert() File "/root/anaconda3/envs/tf_1.15_cpu/lib/python3.6/site-packages/tensorflow_core/lite/python/lite.py", line 896, in convert _get_tensor_name(tensor), shape_list)) ValueError: None is only supported in the 1st dimension. Tensor 'image_tensor' has invalid shape '[None, None, None, 3]'.

Method 2: via Python API

import tensorflow as tf graph_def_file = "/data/models/ssdlite_mobilenet_v2_coco_2018_05_09/frozen_inference_graph.pb" input_arrays = ["image_tensor"] output_arrays = ["detection_boxes","detection_scores","num_detections","detection_classes"]

converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_arrays, output_arrays) converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_arrays, output_arrays, input_shapes={'image_tensor': [1, 300, 300, 3]}) tflite_model = converter.convert()

This is the error: 2020-01-15 23:41:03.884504: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After Removing unused ops pass 1: 4311 operators, 7077 arrays (0 quantized) 2020-01-15 23:41:04.700810: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 4311 operators, 7077 arrays (0 quantized) 2020-01-15 23:41:05.290222: F ./tensorflow/lite/toco/toco_tooling.h:38] Check failed: s.ok() Found StridedSlice as non-selected output from Switch, but only Merge supported. Control flow ops like Switch and Merge are not generally supported. We are working on fixing this, please see the Github issue at https://github.com/tensorflow/tensorflow/issues/28485. Fatal Python error: Aborted

yangchaoFree avatar Jan 16 '20 06:01 yangchaoFree

Regarding the command line approach - TFLite doesn't currently support dynamic shapes. In order to fix this error, you need to use the --input_shapes command line argument (examples) (reference). However, once you do that, you will get the same error as the Python API.

Regarding the Python API approach - TFLite doesn't support control flow ops Switch and Merge. We have some tooling to rewrite the operations using control flow in some object detection models. A medium post detailing how to do that is available here. However, this approach does not work with all object detection models.

If you can generate the model in 2.0 then you can try using the new converter which is enabled with converter.experimental_new_converter = True. The model needs to be generated in 2.0 in order for the control flow conversion to work. This approach works consistently across models with control flow.

gargn avatar Jan 16 '20 21:01 gargn

@gargn

I've hit the same problem. I have mobilenet v2 ssd object detection models trained using the tf object detection api and I can't overcome the control flow issue. The suggestion to "generate the model in 2.0" just wasted two hours of my life because as I now find out, according to https://github.com/tensorflow/models/issues/8084, the object detection api is not yet supported in 2.0.

I have to ask how useful the tflite converter is if it doesn't support the provided mobilenet v2 ssd tf models from the object_detection zoo? I'd retrain using a different model type if there was a chance that it would work, but I'm not seeing anything that gives me confidence that it would. Is there any known working combination of model/tensorflow version/tflite that works for object detection?

martinlyons avatar Feb 15 '20 00:02 martinlyons

Build from source with bazel?

luciid3 avatar Feb 25 '20 12:02 luciid3

I got the same issue. I spent days on converting a pretrained mobilenetv2 ssd model to TFLite. I know the command line (export_tflite_ssd_graph.py) solution ) works. But I would like to write python code to do the same thing and optimize (compress, quantize) the model. I have been failing in doing it. Any pointers?

jackyvr avatar Apr 29 '20 20:04 jackyvr

the same error

gttiankai avatar May 19 '20 04:05 gttiankai

I got the same error. How we can resolve it?

dsnsabari avatar Sep 11 '20 09:09 dsnsabari