2

Could anybody please help me to convert YOLOv5 PyTorch model to ONNX or TensorFlow format to be able to use it with OpenCV C++ inference?

I used this tutorial to train the model with colab: https://blog.roboflow.com/how-to-train-yolov5-on-a-custom-dataset/

Maybe there is a way of conversion in colab or I should use any other tools?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
al072
  • 79
  • 2
  • 14

3 Answers3

1

Below is the recent reply from ultralytics developers(YoloV5 creators) regarding OpenCV & YoloV5 compatibility. In fact I'm also looking for correct implementation tutorial.

Original Thread

Good news ! Your original issue may now be fixed ✅ in PR #4833 by @SamFC10. This PR implements architecture updates to allow for ONNX-exported YOLOv5 models to be used with OpenCV DNN.

To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5
  • PyTorch Hub – Force-reload with model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle. Colab
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image.

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 !

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
snehit vaddi
  • 3,854
  • 2
  • 9
  • 13
1

pt model to onnx that is able to be read by OpenCV 4.5.4 is as follows:

python3 export.py --weights best.pt --include onnx --simplify

PauloM
  • 11
  • 2
0

The best way is to use Google Colab where you don't have to worry about downloading huge chunks of data and fixing dependencies. You can use this notebook for converting models.

You can also check out this LearnOpenCV blog post for more details.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 11:51