Conversio yolo pytorch model to tflite for deploying it on mobile

sys.exit(entrypoint())
File “/usr/local/lib/python3.10/dist-packages/ultralytics/cfg/init.py”, line 448, in entrypoint
getattr(model, mode)(**overrides) # default args from model
File “/usr/local/lib/python3.10/dist-packages/ultralytics/engine/model.py”, line 310, 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 717, 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 823, 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
from tflite_support import task
File “/usr/local/lib/python3.10/dist-packages/tflite_support/task/init.py”, line 32, in
from . import vision
File “/usr/local/lib/python3.10/dist-packages/tflite_support/task/vision/init.py”, line 20, in
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
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
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

can you guys help me providing a right track for the success conversion of yolo
pytorch model to tflite

Hi @INSAAF_SULAIMAANSA ,
Upon observing, the log, try to update the tflite_support' library using pip install - -upgrade tflite_support`. Alternatively you can try:

if you are able to access a linux system you may be able to resolve your issue by using AI-Edge-Torch, you can find more information here: googleblog.

I have actually created a sample script for converting your model here , you can quantize your model after this step:


import torch
import torch.nn as nn
import torch.onnx  
from ai_edge_torch import convert  

model = torch.load('yolo_model.pt')
model.eval() 
example_input = torch.randn(1, 3, 416, 416)  # Example input tensor

# Convert the PyTorch model to ONNX format
torch.onnx.export(model, (example_input,), "yolo.onnx", opset_version=11)  

# Convert the ONNX model to TFLite
edge_model = convert(torch.onnx.load("yolo.onnx"), (example_input,))

edge_model.export('yolo_converted.tflite')

Please try it and let us know if this resolves your issue. If you still need further help, feel free to share reproducible code.

Thank You