I have been trying to follow, https://www.tensorflow.org/lite/examples/object_detection/overview#model_customization all day to convert any of the tensorflow Zoo models to a TensorFlow Lite model for running on Android with no luck.
I downloaded several of the models from here, https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md (FYI, Chrome does not let you down these links as not https, I had to right-click Inspect the link and click on the link in the inspector)
I have the script,
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_frozen_graph(
graph_def_file='frozen_graph.pb',
input_shapes = {'normalized_input_image_tensor':[1,300,300,3]},
input_arrays = ['normalized_input_image_tensor'],
output_arrays = ['TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1', 'TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3']
)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
but gives the error, ValueError: Invalid tensors 'normalized_input_image_tensor' were found
so the lines,
input_shapes = {'normalized_input_image_tensor':[1,300,300,3]}, input_arrays = ['normalized_input_image_tensor'], output_arrays = ['TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1', 'TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3']
must be wrong, need a different shape, but how do I get this for each of the zoo models, or is there some preconvert code I need to run first?
Running the "code snipet" below I get,
--------------------------------------------------
Frozen model layers:
name: "add/y"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 1.0
}
}
}
Input layer: add/y
Output layer: Postprocessor/BatchMultiClassNonMaxSuppression/map/while/NextIteration_1
--------------------------------------------------
But I don't see how this would map to the input_shape or help with the conversion??
Is it even possible to convert models like faster_rcnn_inception_v2_coco to tflite? I read somewhere that only SSD models are supported?
So I tried to convert the faster_rcnn_inception_v2_coco to tflite using the below suggested code, the conversation code did not work in TF1, but did work in TF2, but when I try to use the tflite file in the TFlite Example app I get this error,
2021-12-14 13:23:01.979 24542-24542/org.tensorflow.lite.examples.detection E/tflite: Missing 'operators' section in subgraph.
2021-12-14 13:23:01.984 24542-24542/org.tensorflow.lite.examples.detection E/TaskJniUtils: Error getting native address of native library: task_vision_jni
java.lang.RuntimeException: Error occurred when initializing ObjectDetector: Could not build model from the provided pre-loaded flatbuffer: Missing 'operators' section in subgraph.
at org.tensorflow.lite.task.vision.detector.ObjectDetector.initJniWithByteBuffer(Native Method)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.access$100(ObjectDetector.java:88)
at org.tensorflow.lite.task.vision.detector.ObjectDetector$3.createHandle(ObjectDetector.java:223)
at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromLibrary(TaskJniUtils.java:91)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.createFromBufferAndOptions(ObjectDetector.java:219)
at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.<init>(TFLiteObjectDetectionAPIModel.java:88)
at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.create(TFLiteObjectDetectionAPIModel.java:82)
at org.tensorflow.lite.examples.detection.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:99)
at org.tensorflow.lite.examples.detection.CameraActivity$7.onPreviewSizeChosen(CameraActivity.java:446)
