The error about convert ssd mobilenet v2 coco models into tflite model.
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
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
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
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?
Build from source with bazel?
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?
the same error
I got the same error. How we can resolve it?