Query/Issue with Custom YOLOv5 Model and ONNX Export #13473
Description
Search before asking
- I have searched the YOLOv5 issues and found no similar bug report.
YOLOv5 Component
Detection, Export
Bug
I am working with a custom-trained YOLOv5 model that was trained on a dataset with 4 classes. After exporting the model to ONNX format, I am facing discrepancies in the output tensor shape and class configurations, which are creating confusion and potential issues in downstream tasks. Below, I outline the details of my observations, potential root causes, and attempts to resolve the issue.
Environment
yolov5s.pt, ubuntu 22.04, in own system.
Minimal Reproducible Example
normal detection code from"https://github.com/arindal1/yolov5-onnx-object-recognition/blob/main/yolov5.py"
Additional
Observations:
Custom Model Details:
The .pt model was trained on a dataset with 4 classes (bird, drone, helicopter, jetplane).
When inspecting the .pt model, the number of classes is confirmed as 4 both in the names field and in the nc parameter from the data.yaml.
The .pt model performs as expected, detecting all 4 classes correctly during inference.
ONNX Export Details:
After exporting the model to ONNX, the output tensor shape is reported as [1, 8, 8400].
The 8 indicates the number of output channels in the detection head, which suggests it is configured for only 3 classes (5 + 3 = 8 instead of 5 + 4 = 9).
This is inconsistent with the .pt model, which was trained on 4 classes.
When checking the ONNX model metadata, the class names (bird, drone, helicopter, jetplane) are correctly stored, indicating 4 classes in the metadata.
Comparison with Default COCO Model:
For reference, the output tensor shape of a YOLOv5 model trained on the COCO dataset (80 classes) is [1, 25200, 85].
Here, 85 = 5 + 80 (5 for bounding box attributes + 80 for classes).
This format aligns with the expected configuration for YOLO models.
Key Issues:
Mismatch in Output Tensor Shape:
The ONNX model’s output tensor shape suggests it is configured for only 3 classes ([1, 8, 8400]), despite the .pt model being trained on 4 classes.
This raises concerns about whether the ONNX model will correctly detect all 4 classes.
Potential Causes of the Issue:
The detection head in the .pt model might have been misconfigured during training or export.
For 4 classes, the detection head’s out_channels should be 5 + 4 = 9, but it appears to be set to 8.
The ONNX export process might not be correctly handling the model’s class configuration.
Implications for Object Detection:
If the ONNX model is truly configured for only 3 classes, it may fail to detect one of the classes or produce incorrect predictions.
Steps Taken to Debug:
Inspected Detection Head of .pt Model:
Verified the out_channels of the detection head (last layer).
The .pt model’s detection head is confirmed to have out_channels = 8, indicating a configuration for 3 classes.
This discrepancy persists despite the model being trained on 4 classes.
Verified ONNX Model Metadata:
Extracted metadata from the ONNX model, which correctly lists 4 class names (bird, drone, helicopter, jetplane).
Tried Re-exporting the Model:
Re-exported the .pt model to ONNX using the official YOLOv5 export script.
The issue with the output tensor shape ([1, 8, 8400]) remains.
Request for Assistance:
Clarification on Detection Head Configuration:
Could this issue arise from a misconfiguration of the detection head during training? If so, how can I fix it without retraining the model?
Is there a way to manually adjust the detection head’s out_channels in the .pt model and re-export it to ONNX?
ONNX Export Process:
Are there known issues with the YOLOv5 ONNX export script that could cause this mismatch?
How can I ensure the ONNX model’s detection head is correctly configured for 4 classes?
General Guidance:
What steps can I take to verify that the ONNX model will correctly detect all 4 classes?
Are there tools or scripts you recommend for validating the ONNX model’s outputs?
Additional Context:
ultralytics - 2.4.1
PyTorch Version: 2.4.1
ONNX Runtime Version:1.16.3
Thank you for your assistance in resolving this issue!
Are you willing to submit a PR?
- Yes I'd like to help by submitting a PR!