Skip to content
OPQAI.
Sourced intermediate / 💻 Coding Free tools

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.py script. 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

  1. Download the YOLOv5 code: clone the official repository, which contains the export script:

    git clone https://github.com/ultralytics/yolov5

    You should see a new yolov5 folder appear.

  2. Open the folder:

    cd yolov5
  3. Install the requirements: this pulls in PyTorch and the other Python packages (you need Python 3.8+ first):

    pip install -r requirements.txt

    You should see packages download; this can take a few minutes.

  4. Export to ONNX: run the export script on a model. (yolov5s.pt is a small pre-trained model; the --weights flag points to the model file, and --include chooses the output format. The script downloads yolov5s.pt automatically if you do not have it.)

    python export.py --weights yolov5s.pt --include onnx

    You should get a yolov5s.onnx file in the yolov5 weights folder.

  5. Export to Core ML (for Apple devices):

    python export.py --weights yolov5s.pt --include coreml

    You should get a yolov5s.mlmodel file.

  6. Export to TFLite (for Android and small devices):

    python export.py --weights yolov5s.pt --include tflite

    You should get a yolov5s.tflite file.

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.txt expects, 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

Keep going

More Coding workflows