Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble exporting to coreml. ONNX & Torchscript export working fine. #5575

Closed
1 of 2 tasks
UnkoC0wbOy opened this issue Nov 8, 2021 · 6 comments · Fixed by #6195
Closed
1 of 2 tasks

Trouble exporting to coreml. ONNX & Torchscript export working fine. #5575

UnkoC0wbOy opened this issue Nov 8, 2021 · 6 comments · Fixed by #6195
Labels

Comments

@UnkoC0wbOy
Copy link

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

No response

Bug

Image 11-8-21 at 2 48 PM

Image 11-8-21 at 2 50 PM

Image 11-8-21 at 2 38 PM

Image 11-8-21 at 2 52 PM

Environment

Running in Google Colab and on device (2014 Mac mini) having same issue.
YOLOv5 🚀 v6.0-76-g79bca2b torch 1.9.0+cu111 CPU

Minimal Reproducible Example

! git clone https://github.com/ultralytics/yolov5
! cd yolov5
! pip install -r /content/yolov5/requirements.txt
! pip install -U coremltools==5.0 onnx scikit-learn==0.20.0
! pip install torch
! python /content/yolov5/export.py --weights yolov5s.pt --img 640 --batch 1

Additional

Cannot export model to coreml format. Having no issues exporting model to onnx or TorchScript.pt format

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@UnkoC0wbOy UnkoC0wbOy added the bug Something isn't working label Nov 8, 2021
@glenn-jocher glenn-jocher removed the bug Something isn't working label Nov 8, 2021
@glenn-jocher
Copy link
Member

@UnkoC0wbOy if you want to export to coreml you can include it in the list of formats to export to:

python export.py --include coreml

See argparser for details:

yolov5/export.py

Lines 352 to 354 in 79bca2b

parser.add_argument('--include', nargs='+',
default=['torchscript', 'onnx'],
help='available formats are (torchscript, onnx, coreml, saved_model, pb, tflite, tfjs)')

@mareczech1982
Copy link

@UnkoC0wbOy, Im not sure how CoreML works with an intel mac. Wit my silicon M1 pro I have to use Conda for installing all the dependencies.

Good start is here:
https://towardsdatascience.com/yes-you-can-run-pytorch-natively-on-m1-macbooks-and-heres-how-35d2eaa07a83
and here:
https://coremltools.readme.io/docs

@glenn-jocher I can export the way you propose, but I when I run the detection:
python detect.py --weights yolov5s.mlmodel

I get following error:
File "foo_bar/yolov5/models/common.py", line 363, in forward
box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels

It seems that the model output does not have the key coordinates. The result has these following keys:
dict_keys(['var_875', 'var_860', 'var_845'])

torch version: 1.8.0.post3
torchvision version: 0.9.0a0
numpy version: 1.21.1
coremltools version: 5.1.0
Python 3.9.7

@github-actions
Copy link
Contributor

github-actions bot commented Dec 22, 2021

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@github-actions github-actions bot added the Stale label Dec 22, 2021
@glenn-jocher glenn-jocher linked a pull request Jan 5, 2022 that will close this issue
@glenn-jocher
Copy link
Member

@UnkoC0wbOy @mareczech1982 good news 😃! Your original issue may now be fixed ✅ in PR #6195. This PR adds support for YOLOv5 CoreML inference.

!python export.py --weights yolov5s.pt --include coreml  # CoreML export
!python detect.py --weights yolov5s.mlmodel  # CoreML inference (MacOS-only)
!python val.py --weights yolov5s.mlmodel  # CoreML validation (MacOS-only)

model = torch.hub.load('ultralytics/yolov5', 'custom', 'yolov5s.mlmodel')  # CoreML PyTorch Hub model

Screen Shot 2022-01-04 at 5 41 07 PM

To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

@zhaoqier
Copy link

zhaoqier commented Oct 10, 2022

@UnkoC0wbOy, Im not sure how CoreML works with an intel mac. Wit my silicon M1 pro I have to use Conda for installing all the dependencies.

Good start is here: https://towardsdatascience.com/yes-you-can-run-pytorch-natively-on-m1-macbooks-and-heres-how-35d2eaa07a83 and here: https://coremltools.readme.io/docs

@glenn-jocher I can export the way you propose, but I when I run the detection: python detect.py --weights yolov5s.mlmodel

I get following error: File "foo_bar/yolov5/models/common.py", line 363, in forward box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels

It seems that the model output does not have the key coordinates. The result has these following keys: dict_keys(['var_875', 'var_860', 'var_845'])

torch version: 1.8.0.post3 torchvision version: 0.9.0a0 numpy version: 1.21.1 coremltools version: 5.1.0 Python 3.9.7

@mareczech1982 hi, have you solved this problem, I met the this error now when I use the .mlmodel to detect, it says the coordinated error. @glenn-jocher could you please help me with it? I found it is ok for yolov5s.pt while failed for yolov3.pt, does this the root cause of my problem?

Traceback (most recent call last):
File "/Users/qierzhao/yolov3/detect.py", line 244, in
main(opt)
File "/Users/qierzhao/yolov3/detect.py", line 239, in main
run(**vars(opt))
File "/Users/qierzhao/opt/anaconda3/envs/yolov3/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
return func(*args, **kwargs)
File "/Users/qierzhao/yolov3/detect.py", line 115, in run
pred = model(im, augment=augment, visualize=visualize)
File "/Users/qierzhao/opt/anaconda3/envs/yolov3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "/Users/qierzhao/yolov3/models/common.py", line 364, in forward
box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels
KeyError: 'coordinates'

Thanks

@glenn-jocher
Copy link
Member

@zhaoqier the issue you are experiencing with the CoreML model likely stems from an inconsistency in the model output keys. Specifically, the error message KeyError: 'coordinates' indicates that the 'coordinates' key is missing from the model's output.

To resolve this issue, you can modify the model's output to ensure it matches the expected format.

In the meantime, make sure to use the latest YOLOv5 version from the official GitHub repository to ensure you have the most up-to-date fixes and improvements.

If you encounter any further difficulties, feel free to reach out for more assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants