4

I have trained a YOLOv8 object detection model using a custom dataset, and I want to convert it to a Core ML model so that I can use it on iOS.

After exporting the model, I have a converted model to core ml, but I need the coordinates or boxes of the detected objects as output in order to draw rectangular boxes around the detected objects.

As a beginner in this area, I am unsure how to achieve this. Can anyone help me with this problem?

Training model:

!yolo task=detect mode=train model=yolov8s.pt data= data.yaml epochs=25 imgsz=640 plots=True

Validation:

!yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=data.yaml

Export this model to coreML:

!yolo mode=export model=runs/detect/train/weights/best.pt format=coreml

How can I get the co ordinate output?

Barbara Gendron
  • 385
  • 1
  • 2
  • 16
Dhiman Das
  • 45
  • 6
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 16 '23 at 07:28

2 Answers2

3

To get the cordinates as output use nms=True

from ultralytics import YOLO

model=YOLO('best.pt')

model.export(format='coreml',nms=True)

or

yolo export model=path/to/best.pt format=onnx nms=True

This will give a option to preview your model in Xcode , and the output will return coordinates Screenshot of .mlmodel from xcode

Nuhman Pk
  • 112
  • 7
  • from ultralytics import YOLO model=YOLO('runs/detect/train/weights/best.pt') model.export(format='coreml',nms=True) CoreML: export failure ❌ 9.9s: 1 names found for nc=8395 CoreML model is not converted. – Dhiman Das Mar 23 '23 at 13:42
  • If I run this code without nms=True or nms=False then it's successfully converted best.pt to best.mlmodel. CoreML: export success ✅ 17.0s, saved as runs/detect/train/weights/best.mlmodel (42.6 MB) Export complete (27.8s) Results saved to /content/drive/MyDrive/Classes/YOLOv8/runs/detect/train/weights – Dhiman Das Mar 23 '23 at 19:41
  • Could you help me to figure out of this problem? – Dhiman Das Mar 23 '23 at 19:42
  • double-check that the number of classes specified in the YOLO model configuration matches the number of classes in your dataset. If it is correct try converting the model to onnx then to coreml – Nuhman Pk Mar 24 '23 at 05:24
  • if it works , Dont forget to up vote the answer and mark it as correct – Nuhman Pk Mar 24 '23 at 05:27
  • 0 I have double-checked, and it works if I set nms=false, then The converted model 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. https://colab.research.google.com/drive/1ARLylF2Iix1bAmjEyH2N5Lea3jxGU4Ld?authuser=2 – Dhiman Das Mar 24 '23 at 11:39
0

I have double-checked, and it works if I set nms=false, then The converted model 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. enter image description here

Python code is working properly. I have only one class in my data.yml file because the model detects only barcodes. Here is my google collab code, https://colab.research.google.com/drive/1ARLylF2Iix1bAmjEyH2N5Lea3jxGU4Ld?authuser=2

Dhiman Das
  • 45
  • 6