-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Comments
@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! |
@pderrenger Hi, How to fix that ? plese help |
Hi @VladKovalski, To fix the error, ensure you're setting |
for code below from ultralytics import YOLO
class_labels = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train",
"truck", "boat", "traffic light", "fire hydrant", "stop sign",
"parking meter", "bench", "bird", "cat", "dog", "horse", "sheep",
"cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
"kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife",
"spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "TV", "laptop",
"mouse", "remote", "keyboard", "cell phone", "microwave", "oven",
"toaster", "sink", "refrigerator", "book", "clock", "vase",
"scissors", "teddy bear", "hair drier", "toothbrush"]
# Export all YOLOv8 models to CoreML INT8
for size in ("n", "s", "m", "l", "x"): # all YOLOv8 model sizes
model = YOLO(f"Models/Original/yolo11{size}.pt")
model.names = class_labels
model.export(format="coreml", verbose=True)
print("All models have been successfully exported with updated class labels.") it reports
python --version ─╯
Python 3.10.13 pip list ─╯
Package Version
------------------ -----------
attrs 24.2.0
cattrs 24.1.2
certifi 2024.12.14
charset-normalizer 3.4.0
contourpy 1.3.1
coremltools 8.1
cycler 0.12.1
exceptiongroup 1.2.2
filelock 3.16.1
fonttools 4.55.3
fsspec 2024.10.0
idna 3.10
Jinja2 3.1.4
joblib 1.4.2
kiwisolver 1.4.7
markdown-it-py 3.0.0
MarkupSafe 3.0.2
matplotlib 3.10.0
mdurl 0.1.2
mpmath 1.3.0
networkx 3.4.2
numpy 1.26.4
onnx 1.17.0
onnxsim 0.4.36
opencv-python 4.10.0.84
packaging 24.2
pandas 2.2.3
pillow 11.0.0
pip 23.0.1
protobuf 5.29.1
psutil 6.1.0
py-cpuinfo 9.0.0
pyaml 24.12.1
Pygments 2.18.0
pyparsing 3.2.0
python-dateutil 2.9.0.post0
pytz 2024.2
PyYAML 6.0.2
requests 2.32.3
rich 13.9.4
scikit-learn 1.6.0
scipy 1.14.1
seaborn 0.13.2
setuptools 65.5.0
six 1.17.0
sympy 1.13.1
threadpoolctl 3.5.0
torch 2.5.1
torchvision 0.20.1
tqdm 4.67.1
typing_extensions 4.12.2
tzdata 2024.2
ultralytics 8.3.49
ultralytics-thop 2.0.13
urllib3 2.2.3 |
It seems you're encountering the error because from ultralytics import YOLO
class_labels = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train",
"truck", "boat", "traffic light", "fire hydrant", "stop sign",
"parking meter", "bench", "bird", "cat", "dog", "horse", "sheep",
"cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
"kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife",
"spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "TV", "laptop",
"mouse", "remote", "keyboard", "cell phone", "microwave", "oven",
"toaster", "sink", "refrigerator", "book", "clock", "vase",
"scissors", "teddy bear", "hair drier", "toothbrush"]
# Export all YOLOv8 models to CoreML INT8
for size in ("n", "s", "m", "l", "x"): # all YOLOv8 model sizes
model = YOLO(f"Models/Original/yolo11{size}.pt")
model.overrides['names'] = class_labels # Update class labels
model.export(format="coreml", verbose=True)
print("All models have been successfully exported with updated class labels.") This approach ensures the labels are properly updated before exporting. If the issue persists, ensure you're using the latest version of the |
got an error again python main.py ─╯
Traceback (most recent call last):
File "/Users/edison/Downloads/RTObjRec/main.py", line 21, in <module>
model.export(format="coreml", verbose=True)
File "/Users/edison/.pyenv/versions/yolov11-iphone/lib/python3.10/site-packages/ultralytics/engine/model.py", line 738, in export
return Exporter(overrides=args, _callbacks=self.callbacks)(model=self.model)
File "/Users/edison/.pyenv/versions/yolov11-iphone/lib/python3.10/site-packages/ultralytics/engine/exporter.py", line 169, in __init__
self.args = get_cfg(cfg, overrides)
File "/Users/edison/.pyenv/versions/yolov11-iphone/lib/python3.10/site-packages/ultralytics/cfg/__init__.py", line 297, in get_cfg
check_dict_alignment(cfg, overrides)
File "/Users/edison/.pyenv/versions/yolov11-iphone/lib/python3.10/site-packages/ultralytics/cfg/__init__.py", line 485, in check_dict_alignment
raise SyntaxError(string + CLI_HELP_MSG) from e
SyntaxError: 'names' is not a valid YOLO argument. Similar arguments are i.e. ['name', 'nms=False'].
Arguments received: ['yolo']. Ultralytics 'yolo' commands use the following syntax:
yolo TASK MODE ARGS
Where TASK (optional) is one of {'classify', 'detect', 'segment', 'obb', 'pose'}
MODE (required) is one of {'train', 'track', 'predict', 'val', 'export', 'benchmark'}
ARGS (optional) are any number of custom 'arg=value' pairs like 'imgsz=320' that override defaults.
See all ARGS at https://docs.ultralytics.com/usage/cfg or with 'yolo cfg'
1. Train a detection model for 10 epochs with an initial learning_rate of 0.01
yolo train data=coco8.yaml model=yolo11n.pt epochs=10 lr0=0.01
2. Predict a YouTube video using a pretrained segmentation model at image size 320:
yolo predict model=yolo11n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320
3. Val a pretrained detection model at batch-size 1 and image size 640:
yolo val model=yolo11n.pt data=coco8.yaml batch=1 imgsz=640
4. Export a YOLO11n classification model to ONNX format at image size 224 by 128 (no TASK required)
yolo export model=yolo11n-cls.pt format=onnx imgsz=224,128
5. Streamlit real-time webcam inference GUI
yolo streamlit-predict
6. Ultralytics solutions usage
yolo solutions count or in ['heatmap', 'queue', 'speed', 'workout', 'analytics', 'trackzone'] source="path/to/video/file.mp4"
7. Run special commands:
yolo help
yolo checks
yolo version
yolo settings
yolo copy-cfg
yolo cfg
yolo solutions help
Docs: https://docs.ultralytics.com
Solutions: https://docs.ultralytics.com/solutions/
Community: https://community.ultralytics.com
GitHub: https://github.com/ultralytics/ultralytics
``` |
The error occurs because Here's the corrected approach: from ultralytics import YOLO
class_labels = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "TV", "laptop", "mouse", "remote", "keyboard",
"cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
"scissors", "teddy bear", "hair drier", "toothbrush"]
# Export all YOLOv8 models to CoreML INT8
for size in ("n", "s", "m", "l", "x"): # all YOLOv8 model sizes
model = YOLO(f"Models/Original/yolo11{size}.pt")
model.model.names = {i: name for i, name in enumerate(class_labels)} # Update the class labels
model.export(format="coreml", verbose=True)
print("All models have been successfully exported with updated class labels.") This updates the |
Cool! it's working, thank you so much. |
The above script could export Yolov8 models succ with class labels, but when export yolo11 models, still not have class labels |
It's fixed! thank both of you @john-rocky, @pderrenger! the final lines are below: from ultralytics import YOLO
class_labels = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "TV", "laptop", "mouse", "remote", "keyboard",
"cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
"scissors", "teddy bear", "hair drier", "toothbrush"]
# Export all YOLOv11 models to CoreML INT8
for size in ("n", "s", "m", "l", "x"): # all YOLOv8 model sizes
model = YOLO(f"Models/Original/yolo11{size}.pt")
model.model.names = {i: name for i, name in enumerate(class_labels)} # Update the class labels
model.export(format="mlpackage", verbose=True, nms=True)
print("All models have been successfully exported with updated class labels.") |
Glad to hear it worked for you! Adding |
It works after I update Ultralytics to 8.3.50 . My environment is Python-3.8.20 torch-2.4.1 CPU (Apple M1 Pro). |
Great to hear it's working now! If you run into any other issues, feel free to reach out. |
Hello, thanks for this project, it's very useful.
I tried to export the models using the following code:
that did work, but when using it on the app, it fails in the following:
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 readnames
from the model's additional metadata?The text was updated successfully, but these errors were encountered: