0

I'm following this tutorial on how to export my saved model to tflite for building an android app using my custom detection model Link

But after executing this code

ObjectDetectorWriter = object_detector.MetadataWriter

_MODEL_PATH = "/content/detect.tflite"
_LABEL_FILE = "/content/labelmap.txt"
_SAVE_TO_PATH = "/content/tflite_with_metadata/detect.tflite"

writer = ObjectDetectorWriter.create_for_inference(
    writer_utils.load_file(_MODEL_PATH), [127.5], [127.5], [_LABEL_FILE])
writer_utils.save_file(writer.populate(), _SAVE_TO_PATH)

# Verify the populated metadata and associated files.
displayer = metadata.MetadataDisplayer.with_model_file(_SAVE_TO_PATH)
print("Metadata populated:")
print(displayer.get_metadata_json())
print("Associated file(s) populated:")
print(displayer.get_packed_associated_file_list())

model_meta = _metadata_fb.ModelMetadataT()
model_meta.name = "SSD_Detector"
model_meta.description = (
    "Identify which of a known set of objects might be present and provide "
    "information about their positions within the given image or a video "
    "stream.")

# Creates input info.
input_meta = _metadata_fb.TensorMetadataT()
input_meta.name = "image"
input_meta.content = _metadata_fb.ContentT()
input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT()
input_meta.content.contentProperties.colorSpace = (
...

it gave an error:

KeyError                                  Traceback (most recent call last)
<ipython-input-31-2e8ec3b1f9af> in <cell line: 7>()
      5 _SAVE_TO_PATH = "/content/tflite_with_metadata/detect.tflite"
      6 
----> 7 writer = ObjectDetectorWriter.create_for_inference(
      8     writer_utils.load_file(_MODEL_PATH), [127.5], [127.5], [_LABEL_FILE])
      9 writer_utils.save_file(writer.populate(), _SAVE_TO_PATH)

2 frames
/usr/local/lib/python3.10/dist-packages/tensorflow_lite_support/metadata/python/metadata_writers/object_detector.py in <listcomp>(.0)
    210 
    211     # Output metadata according to output_tensor_indices.
--> 212     output_metadata = [indices_to_tensors[i] for i in output_tensor_indices]
    213 
    214     # Create subgraph info.

KeyError: 1720

here's my jupyter notebook Link

I've tried other methods but all give the same error.

3
  • Your issue is that your indices_to_tensors list is not as long as your output_tensor_indices. While this is just a Python error, it does mean something is wrong with your model training flow. Commented Jan 23, 2024 at 17:04
  • @transposeglobal how do I fix it? Commented Jan 23, 2024 at 17:26
  • It depends on your input files. I see that this is a large flow and you are very deep into it with this step. I don't know everything about this flow, but I would recommend you trace back your steps and make sure you have followed the steps closely. If you do find a more specific problem with it, feel free to ask here. Commented Jan 23, 2024 at 17:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.