If you train or finetune a model with yolov5 and then export to CoreML you'll have a bad time. Dragging and dropping it in to Xcode will leave you with a whole bag of complex stuff that needs doing in order to be able to use that model in your iOS or macOS code.
Instead of exporting a CoreML model using the script included in yolov5 run this export script instead.
Use the same parameters you'd use for the original export script, for example:
python export-nms.py --weights weights.pt --include "coreml"
While the official yolov5 repository includes an export for CoreML, it's missing some features that many would consider essential for using the resulting model. e.g.
ultralytics/yolov5#535 ultralytics/yolov5#1575
There's a glaring lack of information about this on the Ultralytics docs site:
https://docs.ultralytics.com/yolov5/tutorials/model_export/#exported-model-usage-examples
This site (and this page as well as this one from the same author), explains the issue.
One stage detectors, like YOLO, are not only performing classification they're also performing detection. There may be several objects in an image and these must be detected and classified.
YOLO uses a fixed grid of detectors, each cell of the grid can end up with overlapping predictions. This is where non-maximum suppression (NMS) comes in, it keeps the predictions with the highest confidence scores and removes other boxes that overlap with lower confidence.
The default export script will just spit out a MultiArray
and you're left to your own devices to sort out whats what. Instead this script sets up the NMS and specifies types including an image, thresholds, confidence, etc.
The export from yolov5 will result in a <model>.mlmodel
file, the newer format is <model>.mlpackage
.
Exporting with the correct types etc specified means we can use Xcode's preview of the model to drag and drop test images and Xcode displays results from the model.
This script is almost entirely from Julius Hietala's yolov5 fork here
https://github.com/dbsystel/yolov5-coreml-tools https://www.neuralception.com/exportcoreml/