Convert YOLOv5 Models: PyTorch to ONNX, CoreML, TFLite
Job to be done: Convert YOLOv5 object detection models between different formats.
🇳🇬 Ways to use this in Nigeria
Ideas to get you started, adapt to your situation.
- Student
For your final year project, convert your YOLOv5 model trained to detect specific plant diseases into TFLite format to integrate it into an Android app for farmers.
- Entrepreneur
If building a mobile app to identify counterfeit products, convert your custom YOLOv5 model to TFLite or CoreML for seamless integration into Android or iOS apps.
- 9-5 employee
As an ML engineer, convert a YOLOv5 model trained to detect faulty products on an assembly line into ONNX for deployment on an edge device for real-time quality checks.
What you’ll get
A trained YOLOv5 model (a popular AI that spots objects in images, “object detection”) converted from its original PyTorch form into other formats so it can run where you need it: ONNX for general portability, CoreML for iPhone apps, and TFLite for Android and small devices. A model only runs on a platform that understands its format, so converting is what lets you actually ship it. This is intermediate: you run a few Python commands in a terminal, but the tool does the hard part.
Tools you need
- Ultralytics YOLOv5 (freemium): the object-detection project. You will run its
export.pyscript. Free to use; commercial use may need a licence. - PyTorch (free): the AI framework YOLOv5 is built on; its native model files end in
.pt. - ONNX (free): a universal model format that many tools and devices can read.
- Core ML (free): Apple’s format for running models in iPhone and Mac apps.
- TensorFlow Lite / TFLite (free): a format for running models on Android phones and small (“edge”) devices.
Steps
-
Download the YOLOv5 code: clone the official repository, which contains the export script:
git clone https://github.com/ultralytics/yolov5You should see a new
yolov5folder appear. -
Open the folder:
cd yolov5 -
Install the requirements: this pulls in PyTorch and the other Python packages (you need Python 3.8+ first):
pip install -r requirements.txtYou should see packages download; this can take a few minutes.
-
Export to ONNX: run the export script on a model. (
yolov5s.ptis a small pre-trained model; the--weightsflag points to the model file, and--includechooses the output format. The script downloadsyolov5s.ptautomatically if you do not have it.)python export.py --weights yolov5s.pt --include onnxYou should get a
yolov5s.onnxfile in theyolov5weights folder. -
Export to Core ML (for Apple devices):
python export.py --weights yolov5s.pt --include coremlYou should get a
yolov5s.mlmodelfile. -
Export to TFLite (for Android and small devices):
python export.py --weights yolov5s.pt --include tfliteYou should get a
yolov5s.tflitefile.
Original source
Based on the Ultralytics YOLOv5 documentation on GitHub, the official home of the model’s code, export tools, and guides.
Notes & variations
-
Free-tier viability: PyTorch, ONNX, Core ML, and TFLite are all free and open-source, and YOLOv5 is free for most uses. The main cost is your time and the data to download packages and models.
-
Common pitfall: version mismatches. Make sure your Python and PyTorch versions match what
requirements.txtexpects, or the export can fail. -
Tip for better results: export several formats at once by listing them together, which saves running the command repeatedly:
python export.py --weights yolov5s.pt --include onnx coreml tflite