models icon indicating copy to clipboard operation
models copied to clipboard

The order of object detection output tensors in TF2 TFLITE model is different from TF1 and from description in export_tflite_graph_tf2.py

Open akotlarsky opened this issue 1 year ago • 2 comments

Prerequisites

Please answer the following questions for yourself before submitting an issue.

[+] I am using the latest TensorFlow Model Garden release and TensorFlow 2. [+] I am reporting the issue to the correct repository. (Model Garden official or research directory) [+] I checked to make sure that this issue has not already been filed.

1. The entire URL of the file you are using

https://github.com/tensorflow/models/blob/master/research/object_detection/export_tflite_graph_tf2.py

2. Describe the bug

According to the comments in export_tflite_graph_tf2.py, it should generate 4 output tensors in the following order:

detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box locations detection_classes: a float32 tensor of shape [1, num_boxes] with class indices detection_scores: a float32 tensor of shape [1, num_boxes] with class scores num_boxes: a float32 tensor of size 1 containing the number of detected boxes

This was, in fact, the order of outputs produced by export_tflite_ssd_graph.py in TF1.

However, export_tflite_graph_tf2.py, followed by execution of tflite_convert.py in TF2, produces output tensors in this order: detection_scores detection_boxes num_boxes detection_classes

Which creates incompatibility between TF1 and TF2 for run-time inferencing at the edge.

3. Steps to reproduce

*) Train an object detection model (I am using ssd_mobilenet_v2_128x128_coco17_tpu-8)

*) Export TF2 TFLITE saved_model: python object_detection/export_tflite_graph_tf2.py --pipeline_config_path=${CONFIG_FILE} --trained_checkpoint_dir=${MODEL_DIR} --output_directory=${OUTPUT_DIR}

*) Convert to TFLITE format: python tflite_convert.py --output_file=${TFLITE_FILE} --saved_model_dir=${TFLITE_SAVED_MODEL_DIR}

*) Run inference in Python and examine interpreter's output details: output_details = interpreter.get_output_details()

4. Expected behavior

Expected order of output tensors should be as described in export_tflite_graph_tf2.py, which should be identical to the order of output tensors produced in TF1 TFLITE.

5. Additional context

N/A

6. System information

  • TensorFlow 2.10.0
  • Python version 3.8

akotlarsky avatar Apr 01 '23 00:04 akotlarsky

I posted this issue 3 months ago, and there has been no response. Just wondering: Does anybody care?

akotlarsky avatar Jun 26 '23 15:06 akotlarsky

Thank you @akotlarsky. We have a 3rd party provided model for some work we do, and it all of a sudden stopped working with our tool. It took me days to finally figure out the parameters had changed order thanks to your issue. We are following up with our provider to see if they have any idea why the change.

codewise-nicolas avatar Jan 26 '24 01:01 codewise-nicolas