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

converted core ml model doen't include classes in metadata #3238

Closed
dhgokul opened this issue May 19, 2021 · 11 comments · Fixed by #3262
Closed

converted core ml model doen't include classes in metadata #3238

dhgokul opened this issue May 19, 2021 · 11 comments · Fixed by #3262
Labels
question Further information is requested

Comments

@dhgokul
Copy link

dhgokul commented May 19, 2021

converted yolo5s.pt model to core ml model using cmd

!python models/export.py --weights weights/yolov5s.pt --img 640 --batch 1 --train

in previous tried cmd
!python models/export.py --weights weights/yolov5s.pt --img 640 --batch 1 but coreml model fails to convert

once the core ml model tried in ios below result got
MicrosoftTeams-image (6)

here no class found.

Tried in mac x code

@dhgokul dhgokul added the question Further information is requested label May 19, 2021
@github-actions
Copy link
Contributor

github-actions bot commented May 19, 2021

👋 Hello @dhgokul, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@dhgokul
Copy link
Author

dhgokul commented May 20, 2021

not able to convert yolo5s model to core ml model . as suggessted command
!python models/export.py --weights weights/yolov5s.pt --img 640 --batch 1

tried both linux cpu machine and in google colab. In both cases not showing the exact error on conversion
Screenshot from 2021-05-20 18-29-39

so just removed exception in coreml conversion part in model.py file , got the error as follows in google colab
Screenshot from 2021-05-20 18-32-03

just installed all in the requirement.txt

how to over come the issue.

@glenn-jocher
Copy link
Member

@dhgokul good news 😃! Your original issue may now been fixed ✅ in PR #3262. This PR improves error handling for CoreML exports, displaying an error message that notifies the user that CoreML exports should be done when the model is in .train() mode with python models/export.py --train. To receive this update you can:

  • git pull from within your yolov5/ directory
  • git clone https://github.com/ultralytics/yolov5 again
  • Force-reload PyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • View our updated notebooks: Open In Colab Open In Kaggle

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 🚀!

@dhgokul
Copy link
Author

dhgokul commented May 20, 2021

thanks for the quick response

we just pulled the repo and tried conversion

!python models/export.py --weights weights/yolov5s.pt --img 640 --batch 1 --train
the generated core model is tested on x code, but we have not any class labels, generally in other core ml mode list in metadata
Screenshot from 2021-05-20 21-08-55

here is the converted ml model of yolov5s.pt
yolov5s_3.zip
we tried this ml model only in ios x code.

@yohandarosa
Copy link

anyone had find a solution to include class labels in the CoreML model ?

@jiwon-hae
Copy link

Same issue here... Was this solved?

@Sic-1
Copy link

Sic-1 commented Apr 5, 2022

Still seems to be an issue. Any solutions?

@MaxNazarov93
Copy link

MaxNazarov93 commented May 2, 2022

I figured out this problem and what you need is create ClassifierConfig and pass it into convert function

from coremltools.converters import ClassifierConfig

classifier_config = ClassifierConfig(class_labels=['class1', 'class2', 'class3', 'class4'])
ct_model = ct.convert(ts, inputs=[ct.ImageType('image', shape=im.shape, scale=1 / 255, bias=[0, 0, 0])], classifier_config=classifier_config)

@dhgokul
Copy link
Author

dhgokul commented May 2, 2022

Thanks @MaxNazarov93 will check it. Anyone try this and post in this group

Thanks in advance!

@zjsjayce
Copy link

I also meet this problem

@BasanthVerma
Copy link

I figured out this problem and what you need is create ClassifierConfig and pass it into convert function

from coremltools.converters import ClassifierConfig

classifier_config = ClassifierConfig(class_labels=['class1', 'class2', 'class3', 'class4'])
ct_model = ct.convert(ts, inputs=[ct.ImageType('image', shape=im.shape, scale=1 / 255, bias=[0, 0, 0])], classifier_config=classifier_config)

@MaxNazarov93 this process adds the classes to the model and we can see the classes in the metadata. But when we run the model, we get:
Error Domain=com.apple.vis Code=3 "The VNCoreMLTransform request failed" UserInfo={NSLocalizedDescription=The VNCoreMLTransform request failed, NSUnderlyingError=0x280a81590 {Error Domain=com.apple.CoreML Code=0 "The size of the output layer 'var_887' in the neural network does not match the number of classes in the classifier." UserInfo={NSLocalizedDescription=The size of the output layer 'var_887' in the neural network does not match the number of classes in the classifier.}}}

We tried multiple Yolov5 that we had custom trained, we made sure that class_labels has the exact number of labels that we used for training. But we still keep getting this error.

So we ended up following the script from https://rockyshikoku.medium.com/convert-yolov5-to-coreml-also-add-a-decode-layer-113408b7a848 and it worked!

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

Successfully merging a pull request may close this issue.

8 participants