Search code examples
androidtensorflow-lite

Insert normalizeOption in tflite model with command line


I am trying to insert my custom model into android tensorflow lite object detection. I created a MobileNetv2 model using tflite_convert and inserted it into the Android demo project, but an error asking me to specify the NormalizationOptions metadata occurred.

    Process: org.tensorflow.lite.examples.detection, PID: 6420
    java.lang.AssertionError: Error occurred when initializing ObjectDetector: Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to preprocess input images.

The above phenomenon occurs even though mean_value and std_dev_values ​​are specified as shown in the code below. Is there a way to enter NormalizationOption on the command line?

!tflite_convert \
  --input_shape=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 \
  --allow_custom_ops \
  --graph_def_file=/content/models/research/fine_tuned_model/tflite/tflite_graph.pb \
  --output_file="/content/models/research/fine_tuned_model/final_model.tflite" \
  --inference_type=FLOAT \
  --mean_values=128 \
  --std_dev_values=128 \

Solution

  • The official object detection example two variants: 1) using task library, 2) using interpreter API directly. The default build variant uses the task library, and it expects the object detection model to have metadata added.

    Looks like the NormalizationOption is from the metadata section, so please try adding metadata following the guide linked above, or use a different build variant of the example app to not use the task library.