ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
51.34k stars 16.46k forks source link

Unable to convert .onnx to tflite #1453

Closed Roshnee closed 3 years ago

Roshnee commented 4 years ago

❔Question

I have tensorflow 2.3.1 installed.

I was able to convert the pytorch model into an onnx file using, python models/export.py --weights yolov5s.pt --img 640 --batch 1

i also was able to convert .onnx into a tensorflow model using the following code, `import onnx from onnx_tf.backend import prepare import tensorflow as tf

onnx_model = onnx.load('yolov5s.onnx') tf_rep = prepare(onnx_model) tf_rep.export_graph("yolov5.pb") `

This yolov5.pb directory consists of the saved_model.pb file and 2 other folders: variables (2 files) and assets (empty folder)

I couldnt further convert it to a tflite model. I used the following code, `import tensorflow as tf

saved_model_dir = 'yolov5.pb'

Convert the model.

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) tflite_model = converter.convert()

Save the TF Lite model.

with tf.io.gfile.GFile('model.tflite', 'wb') as f: f.write(tflite_model)`

This causes a strange error: Screenshot from 2020-11-19 13-12-25

Screenshot from 2020-11-19 13-12-30

I also used:

_tflite_convert --saved_model_dir=yolov5 --outputfile=yolo.tflite

which gives me the same error.

Additional context

UPDATE:

looks like the error is because the conversion from onnx to tensorflow model is the saved model and not the frozen tf file. By finding the signature of the saved model using saved_model_cli show --dir yolov5_trial2 --all

gives me weird signature def for input and output arrays

signature_def['__saved_model_init_op']: The given SavedModel SignatureDef contains the following input(s): The given SavedModel SignatureDef contains the following output(s): outputs['__saved_model_init_op'] tensor_info: dtype: DT_INVALID shape: unknown_rank name: NoOp Method name is:

signature_def['serving_default']: The given SavedModel SignatureDef contains the following input(s): inputs['images'] tensor_info: dtype: DT_FLOAT shape: (1, 3, 640, 640) name: serving_default_images:0 The given SavedModel SignatureDef contains the following output(s): outputs['output_0'] tensor_info: dtype: DT_FLOAT shape: (1, 25200, 85) name: StatefulPartitionedCall:0 outputs['output_1'] tensor_info: dtype: DT_FLOAT shape: (1, 3, 80, 80, 85) name: StatefulPartitionedCall:1 outputs['output_2'] tensor_info: dtype: DT_FLOAT shape: (1, 3, 40, 40, 85) name: StatefulPartitionedCall:2 outputs['output_3'] tensor_info: dtype: DT_FLOAT shape: (1, 3, 20, 20, 85) name: StatefulPartitionedCall:3 Method name is: tensorflow/serving/predict

Defined Functions: Function Name: 'call' Named Argument #1 images

Function Name: 'gen_tensor_dict'

Please help.

github-actions[bot] commented 4 years ago

Hello @Roshnee, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.