Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
glenn-jocher opened this issue Sep 29, 2023 · 6 comments

Comments

@glenn-jocher
Copy link

glenn-jocher commented Sep 29, 2023

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 ultralytics/ultralytics#5161

@lu-wang-g
Copy link
Member

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

@glenn-jocher
Copy link
Author

@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.

@ethanluoyc
Copy link

@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 pybind/pybind11#439.

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

@glenn-jocher
Copy link
Author

@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

@khanhlvg
Copy link
Member

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

@glenn-jocher
Copy link
Author

@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 ultralytics/ultralytics#13176

Thank you for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@khanhlvg @ethanluoyc @glenn-jocher @lu-wang-g and others