Convert Yolov5 to CoreML. Also add a decode layer.

How to use Yolov5 on iOS

Yolov5 is an object detection machine learning model.
Convert to CoreML format for use on iPhone and iPad.

The converted model cannot be used as it is

The converted model is a large number of boxes for each class, so it cannot be used as it is.
We need to add a layer for decoding and a layer for Non Max Suppression to narrow down the coordinates of the reliable box.
By doing these things, wewill be able to handle it with the iOS Vision framework, and we will be able to use a simple file preview function like the image at the beginning.

Here is a script that can be used on iOS

Click here for the converted model (COCO data set)

Yolov5 models trained with your own dataset can also be converted to CoreML format with this conversion script.

Conversion code explanation

Use the export code in the Yolov5 repository to convert the Pytorch model to a CoreML model.

Defines the decode layer.

Defines Non Max Suppression.

Add a decode layer and Non Max Suppression to your CoreML model.

This will save the CoreML model that you can use with Vision.

How to use on iOS

Decoding and NMS refer to the following repositories and books.


I’m a freelance engineer.
Work consultation
Please feel free to contact us with a brief development description.

I am making an app that uses Core ML and ARKit.
We send machine learning / AR related information.





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store