ultralytics / yolo-ios-app

Ultralytics YOLO iOS App source code for running YOLOv8 in your own iOS apps 🌟
https://ultralytics.com/yolo
GNU Affero General Public License v3.0
135 stars 24 forks source link

Exporting yolo models does not include Class Labels #43

Open gustavofuhr opened 2 months ago

gustavofuhr commented 2 months ago

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 commented 2 months ago

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