You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent preprocessing between PyTorch (Stretch) and ONNX (Letterbox) scripts causes accuracy drop and class flickering #358
Describe the bug
There is a significant preprocessing inconsistency between the official PyTorch inference script and the ONNX inference script, which leads to a severe accuracy drop (e.g., class flickering between similar classes like cars and motorbikes on distant/small objects) when deploying the ONNX model.
In the PyTorch script, the preprocessing uses T.Resize((640, 640)), which forcefully stretches/squashes the image without preserving the aspect ratio. However, in the provided ONNX script, the preprocessing uses the resize_with_aspect_ratio function (letterboxing with padding).
Because the ONNX script treats the input image differently than the PyTorch evaluation pipeline, the model struggles to recognize objects correctly, especially smaller ones. When I manually modified the ONNX script to use T.Resize (stretching) to match the PyTorch script, the accuracy and stability were perfectly restored.
To Reproduce
Steps to reproduce the behavior:
Export a D-FINE model to ONNX format.
Run inference on a video containing distant objects (e.g., traffic footage) using the official PyTorch inference script. Observe that the detections and class predictions are stable.
Run inference on the exact same video using the provided ONNX inference script (resize_with_aspect_ratio).
See error: Observe that the bounding boxes might be less accurate and the classes flicker continuously (e.g., alternating between car and motorbike).
Modify the ONNX script to use T.Resize instead of letterboxing. Observe that the ONNX output now perfectly matches the PyTorch output.
Expected behavior
The preprocessing pipeline (resizing method, padding, etc.) should be exactly consistent across both PyTorch and ONNX inference scripts. They should both use the exact same approach that the model was trained/evaluated on to prevent performance degradation during deployment.
Screenshots
Desktop (please complete the following information):
OS: Windows 11
Environment: Python 3.11
Version: ONNXRuntime 1.29.0, PyTorch 2.13.0+cpu
Smartphone (please complete the following information):
Device: N/A
OS: N/A
Browser: N/A
Version: N/A
Additional context
While letterboxing (resize_with_aspect_ratio) is generally considered a best practice in computer vision to preserve object features, if the official evaluation/training pipeline relies on stretching (T.Resize), the ONNX deployment script must follow suit to avoid input distribution mismatch. I suggest either updating the ONNX script to match PyTorch's stretching method, or updating both to use proper letterboxing if that is the intended behavior of the architecture.
Activity
RicePasteM commentedon Sep 8, 2026
Thanks for your feedback and suggestions! We really appreciate it.
We've started looking into this issue and are currently analyzing the possible causes. We'll keep you updated if we have any findings or progress.