UNeedCryDear / yolov8-opencv-onnxruntime-cpp

yolov8 hub,cpp with onnxruntime and opencv
Apache License 2.0
319 stars 56 forks source link

difference between c++ and python results #45

Closed mucahitrtn closed 6 months ago

mucahitrtn commented 6 months ago

I'm attempting to achieve same results using C++, but I'm getting different results between C++ and Python. Could this discrepancy be related to the thresholds? How can I solve this? In Python, there are confidence and IOU thresholds, while in C++, there are class thresholds and NMS thresholds. What values should I set these thresholds to in order to obtain consistent results?

UNeedCryDear commented 6 months ago

IOU ==NMS_threahold confidence ==cls_threshold

There are many reasons lead to different results between CPP and Python. Firstly, the cv::resize parameter may be different. The parameters used for zooming in and out in Python are different, while in C++, I use the same parameters.

Secondly,methods of IOU are different. https://github.com/ultralytics/ultralytics/blob/014f0b4b8d97b356246673f652ecbfc4e332cec2/ultralytics/utils/metrics.py#L75

You can change the IoU from py-code to cpp.

mucahitrtn commented 6 months ago

thank you

UNeedCryDear commented 6 months ago

Hey guy, I have come up with another method, you can give it a try.

Export the NMS part from Python to ONNX, so that the part with NMS in your model is the same as Python, and you don't need to NMS in CPP

mucahitrtn commented 6 months ago

Hey guy, I have come up with another method, you can give it a try.

Export the NMS part from Python to ONNX, so that the part with NMS in your model is the same as Python, and you don't need to NMS in CPP

I've revised my code, and now I'm getting the same results in both C++ and Python. But this is still interesting.

I don't know about the process of exporting NMS from Python to an ONNX model. Should I add nms as a layer, and if so, will OpenCV support it? Are there any example?