tflite-support icon indicating copy to clipboard operation
tflite-support copied to clipboard

YOLOv8 `tensorflow>=2.14.0` ImportError: generic_type: cannot initialize type "StatusCode": an object with that name is already defined

Open glenn-jocher opened this issue 2 years ago • 6 comments

YOLOv8 TFLite export is failing with the latest release of tensorflow==2.14.0 but works with tensorflow==2.13.1, the prior release.

The error originates from the tflite_support package.

Traceback (most recent call last):
  File "/usr/local/bin/yolo", line 8, in <module>
    sys.exit(entrypoint())
  File "/usr/local/lib/python3.10/dist-packages/ultralytics/cfg/__init__.py", line 445, in entrypoint
    getattr(model, mode)(**overrides)  # default args from model
  File "/usr/local/lib/python3.10/dist-packages/ultralytics/engine/model.py", line 306, in export
    return Exporter(overrides=args, _callbacks=self.callbacks)(model=self.model)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/ultralytics/engine/exporter.py", line 261, in __call__
    f[5], keras_model = self.export_saved_model()
  File "/usr/local/lib/python3.10/dist-packages/ultralytics/engine/exporter.py", line 122, in outer_func
    raise e
  File "/usr/local/lib/python3.10/dist-packages/ultralytics/engine/exporter.py", line 117, in outer_func
    f, model = inner_func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/ultralytics/engine/exporter.py", line 707, in export_saved_model
    f.unlink() if 'quant_with_int16_act.tflite' in str(f) else self._add_tflite_metadata(file)
  File "/usr/local/lib/python3.10/dist-packages/ultralytics/engine/exporter.py", line 813, in _add_tflite_metadata
    from tflite_support import flatbuffers  # noqa
  File "/usr/local/lib/python3.10/dist-packages/tflite_support/__init__.py", line 53, in <module>
    from tflite_support import task
  File "/usr/local/lib/python3.10/dist-packages/tflite_support/task/__init__.py", line 32, in <module>
    from . import vision
  File "/usr/local/lib/python3.10/dist-packages/tflite_support/task/vision/__init__.py", line 20, in <module>
    from tensorflow_lite_support.python.task.vision import image_classifier
  File "/usr/local/lib/python3.10/dist-packages/tensorflow_lite_support/python/task/vision/image_classifier.py", line 23, in <module>
    from tensorflow_lite_support.python.task.vision.core import tensor_image
  File "/usr/local/lib/python3.10/dist-packages/tensorflow_lite_support/python/task/vision/core/tensor_image.py", line 19, in <module>
    from tensorflow_lite_support.python.task.vision.core.pybinds import image_utils
ImportError: generic_type: cannot initialize type "StatusCode": an object with that name is already defined

Reproduce

Run in Google Colab: https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/tutorial.ipynb

!pip install 'tensorflow>=2.14.0' ultralytics
import ultralytics
ultralytics.checks()

!yolo export format=tflite
Screenshot 2023-09-29 at 14 21 09

Additional

Tracked in https://github.com/ultralytics/ultralytics/issues/5161

glenn-jocher avatar Sep 29 '23 12:09 glenn-jocher

May you try screening if StatusCode is used in any of your code and if you can use a different namespace for it?

lu-wang-g avatar Oct 09 '23 00:10 lu-wang-g

@lu-wang-g the ultralytics package doesn't use any type called 'StatusCode', but checking other dependencies like onnx and tensorflow I see that it is a pretty common name.

But this seems unrelated to ultralytics as everything works correctly with tensorflow==2.13.1, this error only appears in tflite_support when used with tensorflow==2.14.0.

glenn-jocher avatar Oct 10 '23 00:10 glenn-jocher

@lu-wang-g I think this is due to the usage of the pybind11_abseil/status_casters.h. I think this is somehow due to the the usage of pybind11::google::ImportStatusModule() in this project as well as in TensorFlow and they are conflicting with each other.

The pybind side of issue that may be related is https://github.com/pybind/pybind11/issues/439.

I am not entirely sure how to address this as I am less experience with C++..

ethanluoyc avatar Oct 19 '23 22:10 ethanluoyc

@lu-wang-g @ethanluoyc I just realized this error is very easily reproducible directly in Google Colab just by installing and attempting to import the package. If this package is not even usable in Google Colab this must raise some pretty serious red flags here no?

Are there any plans to resolve this and update the package?

Reproduce

!pip install tflite_support

import tflite_support

Result

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
[<ipython-input-2-10ee15e7e9a8>](https://localhost:8080/#) in <cell line: 1>()
----> 1 import tflite_support

4 frames
[/usr/local/lib/python3.10/dist-packages/tensorflow_lite_support/python/task/vision/core/tensor_image.py](https://localhost:8080/#) in <module>
     17 
     18 from tensorflow_lite_support.python.task.vision.core import color_space_type
---> 19 from tensorflow_lite_support.python.task.vision.core.pybinds import image_utils
     20 
     21 

ImportError: generic_type: cannot initialize type "StatusCode": an object with that name is already defined

Screenshot 2024-02-19 at 16 52 04

glenn-jocher avatar Feb 19 '24 15:02 glenn-jocher

A temporary workaround could be to skip the import statement that causing the issue, and import the metadata_writer that you're using directly. We're looking into fixing the root cause.

import flatbuffers
import platform

from tensorflow_lite_support.metadata import metadata_schema_py_generated
from tensorflow_lite_support.metadata import schema_py_generated
from tensorflow_lite_support.metadata.python import metadata
from tensorflow_lite_support.metadata.python import metadata_writers

khanhlvg avatar May 13 '24 18:05 khanhlvg

@khanhlvg we've implemented your solution, it works, and we have now successfully updated to the latest version of TF 2.16 for Ultralytics exports in https://github.com/ultralytics/ultralytics/pull/13176

Thank you for the help!

glenn-jocher avatar May 29 '24 10:05 glenn-jocher