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

Exporting yolo models does not include Class Labels #43

Open
gustavofuhr opened this issue Jul 10, 2024 · 3 comments
Open

Exporting yolo models does not include Class Labels #43

gustavofuhr opened this issue Jul 10, 2024 · 3 comments

Comments

@gustavofuhr
Copy link

Hello, thanks for this project, it's very useful.

I tried to export the models using the following code:

from ultralytics import YOLO

model_name = "yolov8n"

# Load a model
model = YOLO("yolov8n.pt")  # load an official model

# Export the model
model.export(format="coreml",  int8=True, nms=True, imgsz=[640, 384])

that did work, but when using it on the app, it fails in the following:

guard let classLabels = mlModel.modelDescription.classLabels as? [String] else {
    fatalError("Class labels are missing from the model description")
}

I verified the model and it turns out that the exported model does not include anymore the classLabels as the models in the current release. There is some option in model.export to include classLabels or maybe it's possible to read names from the model's additional metadata?

@pderrenger
Copy link
Member

@gustavofuhr hello,

Thank you for your kind words and for using our project! 😊

Regarding your issue with exporting YOLO models and missing class labels, it appears that the class labels are not being included in the exported model's metadata. Unfortunately, the current export functionality does not automatically include class labels in the exported model's metadata.

However, you can manually add the class labels to the model's metadata before exporting. Here’s an example of how you can achieve this:

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt")  # load an official model

# Define class labels
class_labels = ["class1", "class2", "class3", ...]  # replace with your actual class labels

# Add class labels to model's metadata
model.names = class_labels

# Export the model with the updated metadata
model.export(format="coreml", int8=True, nms=True, imgsz=[640, 384])

This way, the class labels will be included in the exported model's metadata, and you should be able to access them in your app as expected.

If you continue to experience issues, please ensure you are using the latest version of the Ultralytics package. If the problem persists, providing a minimum reproducible example would be very helpful for us to diagnose the issue further. You can find more information on creating a reproducible example here: Minimum Reproducible Example.

Feel free to reach out if you have any more questions or need further assistance!

@VladKovalski
Copy link

@pderrenger Hi,
Have the same problem with yolov8xobb export to coreml.
I try you solution but have error in Visual Studio Code:
AttributeError: can't set attribute 'names'

How to fix that ? plese help

@pderrenger
Copy link
Member

Hi @VladKovalski,

To fix the error, ensure you're setting model.names directly after loading the model. If the issue persists, please verify you're using the latest version of the Ultralytics package. Let me know if you need further assistance!

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

No branches or pull requests

3 participants