Error while writing metadata for tflite

Platform: Google Colab
Tensorflow version: 2.8.0

I tried to write metadata into my custom tflite object detection model using this code from this documentation:

from tflite_support.metadata_writers import object_detector
from tflite_support.metadata_writers import writer_utils
from tflite_support import metadata

ObjectDetectorWriter = object_detector.MetadataWriter
label_txt = "labelmap.txt"

writer = ObjectDetectorWriter.create_for_inference(
    [label_txt] # <-- Error Here
writer_utils.save_file(writer.populate(), tflite_model)

But this error occur:

TypeError                                 Traceback (most recent call last)
<ipython-input-26-ec01babab0ee> in <module>()
      6 label_txt = "labelmap.txt"
----> 8 writer = ObjectDetectorWriter.create_for_inference(writer_utils.load_file(tflite_model), [127.5], [127.5], [label_txt])
      9 writer_utils.save_file(writer.populate(), tflite_model)

6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow_lite_support/metadata/ in Pack(self, builder)
   1306                 for i in reversed(range(len(self.mean))):
   1307                     builder.PrependFloat32(self.mean[i])
-> 1308                 mean = builder.EndVector(len(self.mean))
   1309         if self.std is not None:
   1310             if np is not None and type(self.std) is np.ndarray:

TypeError: EndVector() takes 1 positional argument but 2 were given

tflite_support is still using Flatbuffer 1.12, not 2.0. Looks like your flatbuffer version is 2.0. EndVector() is a known backward incompatible API change in Flatbuffer 2.0.

1 Like

Thanks! You’re right.
It seems tensorflow==2.8.0 requires flatbuffers==2.0.
Meanwhile tflite-support==0.3.1 requires flatbuffers==1.12.

1 Like