ultralytics / hub

Ultralytics HUB tutorials and support
https://hub.ultralytics.com
GNU Affero General Public License v3.0
138 stars 14 forks source link

Exporting YoloV11 Pytorch Model to CoreML #885

Open rittim opened 1 month ago

rittim commented 1 month ago

Search before asking

Question

I'm relatively new to using Ultralytics and YOLOV11n and I was having some major issues attempting to convert my trained model from .pt to either a .mlpackage or .mlmodel format for use in a test iOS app I've created, that has a Live Viewer.

Firstly, I've got some images from Roboflow Universe, there's about 4 classes and the model is for object detection. I've not touched any of the advanced settings and the training images are 640x640. I've trained to about 100 Epochs on Ultralytics.

I can see that in ultralytics, uploading a test image works, and the model works fine.

Once the training is complete I download the PyTorch file and then used this command to convert to an .mlpackage in my MacOS Terminal.

yolo export model=hailmarymodel.pt format=coreml imgsz=640 nms=True

I've also tried

yolo export model=hailmarymodel.pt format=coreml batch=1 half=True nms=True

When opening the mlpackage up on XCode and going to the Preview, i upload an image, and XCode Crashes.

I presume it does not work at all.

Furthermore, downloading the exported CoreML version directly from Ultralytics gives me a .mlmodel file, which when opened simply says 'There was a problem decoding this Core ML document', validator error: unable to deserialize object.

This also happens to me no matter what YOLO version I use. I'm struggling to find any documentation on what to do.

I'm at a complete loss on what to do now. I'd be grateful for any kind of help if possible.

Here is what i see in my exported mlpackage.

Screenshot 2024-10-16 at 21 33 46

Annoyingly it seems that the iOS app for Ultralytics is currently broken and won't display my projects, so nor can I test my model on my device using the app.

I'd be grateful for any kind of help that could shed light on a potential solution I've spent almost 12 hours at this and I'm sure I'm doing something silly wrong. Thanks in advance!

Here is the exported core ml file that doesn't seem to work using just yolo export model=defectv11n.pt format=coreml nms=True: defectv11n.zip

Here is the pytorch file downloaded from Ultralytics Cloud: defectv11npt.zip

Additional

No response

pderrenger commented 1 month ago

@rittim hi there! 😊

Thanks for reaching out and providing detailed information about the issue you're facing. Let's try to tackle this step-by-step.

  1. Verify Latest Versions: First, ensure that you're using the latest version of the Ultralytics package. You can update it using:

    pip install ultralytics --upgrade
  2. Exporting to CoreML: The commands you used seem correct, but let's try simplifying it first:

    yolo export model=hailmarymodel.pt format=coreml imgsz=640

    If this still causes issues, try removing optional parameters like nms and half to see if the basic export works.

  3. Xcode Crashes: Xcode crashing might be due to compatibility issues. Ensure your Xcode is up-to-date. Also, try testing with a smaller model or fewer classes to see if the issue persists.

  4. CoreML Validator Error: This error might indicate a problem during the export process. Double-check the PyTorch model for any anomalies and try re-exporting.

  5. Ultralytics iOS App: If you're experiencing issues with the Ultralytics iOS app, please check for any updates or reinstall the app. You can find more information and troubleshooting tips in the Ultralytics iOS App Documentation.

If these steps don't resolve the issue, please let us know, and we can explore further options. Remember, the YOLO community and the Ultralytics team are here to support you!

Good luck, and feel free to reach out with any more questions! 🚀