yolov8n onnx for object detection -> how to postprocess the output? #20712
-
When using the Ultralytics library to perform model inference directly on an image, the model correctly predicts the bounding box positions and class identities:
Next, I exported the model to onnx:
When exporting the model to onnx, I have to do the post-processing:
This code is able to display bounding boxes around the correct objects. However, this only works when I reduce the conf_thres to very low values, such as 0.007. I haven't added non-maximum suppression yet, because I believe the issue occurs before that step. (but code with non-maxiumum suppression can be found in the colab code below) Can anyone help me understand what I'm doing wrong? here is the colab link: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
👋 Hello @dominicnoy, thank you for sharing your detailed workflow and for using Ultralytics 🚀! For new users or those looking to deepen their understanding, we recommend checking out the Docs, which include Python and CLI examples that may help clarify common questions. Since this appears to be a 🐛 Bug Report regarding ONNX model output and post-processing, please provide a minimum reproducible example (MRE) if you haven't already. This helps us debug more efficiently. If your question is about custom training or inference, please share additional details such as dataset examples, full inference logs, or any modifications made to the pipeline. Also, review our Tips for Best Training Results to ensure optimal outcomes. You're welcome to join the Ultralytics community in the way that suits you best. For real-time support, visit our Discord 🎧. For focused discussions, use Discourse. Or connect with peers on our Subreddit. UpgradeFirst, please upgrade to the latest pip install -U ultralytics EnvironmentsYOLO can be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf this badge is green, all Ultralytics CI tests are passing. CI tests verify correct operation of all YOLO Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit. This is an automated response 🤖. An Ultralytics engineer will review your discussion and offer further assistance soon! |
Beta Was this translation helpful? Give feedback.
Hi @dominicnoy,
Looking at your ONNX inference code, I see a few issues with how you're processing the model output:
The output format for YOLOv8 ONNX models is different from what you're expecting. The raw output needs to be transposed from (1, 84, N) to (N, 84), which you're doing, but the data interpretation is incorrect.
In YOLOv8's ONNX output, the boxes are already in xywh format and class confidences don't need the softmax function. The model has already applied the sigmoid activation.
You're missing the non-maximum suppression step which is crucial.
Here's how to correctly process the outputs: