Repository: ultralytics/ultralytics
Issue category: Bug Report

Imx500 usage example error
### Search before asking

- [X] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and found no similar bug report.


### Ultralytics YOLO Component

Export

### Bug

I encountered an error when running the example code from the [Sony IMX500 usage examples](https://docs.ultralytics.com/integrations/sony-imx500/#usage-examples). The image is resized to 480x640 instead of the expected 640x640, despite both the ONNX model input and the packerOut description specifying a 640x640 input.

The model is exported successfully, but unable to run the inference. 


```
Export complete (298.0s)
Results saved to /home/magi/mido/ultralytics
Predict:         yolo predict task=detect model=yolov8n_imx_model imgsz=640 int8 
Validate:        yolo val task=detect model=yolov8n_imx_model imgsz=640 data=coco.yaml int8 
Visualize:       https://netron.app
WARNING ⚠️ Unable to automatically guess model task, assuming 'task=detect'. Explicitly define task for your model, i.e. 'task=detect', 'segment', 'classify','pose' or 'obb'.
Loading yolov8n_imx_model for ONNX Runtime inference...
Preferring ONNX Runtime AzureExecutionProvider
Loading yolov8n_imx_model/yolov8n_imx.onnx for ONNX IMX inference...

Found https://ultralytics.com/images/bus.jpg locally at bus.jpg
Traceback (most recent call last):
  File "/home/magi/mido/ultralytics/yolo_v8_playground.py", line 13, in <module>
    results = imx_model("https://ultralytics.com/images/bus.jpg")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/ultralytics/engine/model.py", line 176, in __call__
    return self.predict(source, stream, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/ultralytics/engine/model.py", line 554, in predict
    return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/ultralytics/engine/predictor.py", line 173, in __call__
    return list(self.stream_inference(source, model, *args, **kwargs))  # merge list of Result into one
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 36, in generator_context
    response = gen.send(None)
               ^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/ultralytics/engine/predictor.py", line 259, in stream_inference
    preds = self.inference(im, *args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/ultralytics/engine/predictor.py", line 143, in inference
    return self.model(im, augment=self.args.augment, visualize=visualize, embed=self.args.embed, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/ultralytics/nn/autobackend.py", line 542, in forward
    y = self.session.run(self.output_names, {self.session.get_inputs()[0].name: im})
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/magi/mido/ultralytics/.venv/lib/python3.11/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 220, in run
    return self._sess.run(output_names, input_feed, run_options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: input for the following indices
 index: 3 Got: 480 Expected: 640
 Please fix either the inputs/outputs or the model.
```

I tested resizing the image to 640x640 before inference, and it worked properly. However, I assume that the inference call should handle the resizing automatically without requiring manual adjustment beforehand.

### Environment

Ultralytics 8.3.37 🚀 Python-3.11.10 torch-2.5.1+cu124 CUDA:0 (NVIDIA GeForce GTX 1650 Ti, 3906MiB)
Setup complete ✅ (12 CPUs, 31.0 GB RAM, 444.7/913.8 GB disk)

OS                  Linux-6.8.0-49-generic-x86_64-with-glibc2.35
Environment         Linux
Python              3.11.10
Install             pip
RAM                 30.97 GB
Disk                444.7/913.8 GB
CPU                 Intel Core(TM) i7-10750H 2.60GHz
CPU count           12
GPU                 NVIDIA GeForce GTX 1650 Ti, 3906MiB
GPU count           1
CUDA                12.4

numpy               ✅ 1.26.4>=1.23.0
numpy               ✅ 1.26.4<2.0.0; sys_platform == "darwin"
matplotlib          ✅ 3.9.2>=3.3.0
opencv-python       ✅ 4.10.0.84>=4.6.0
pillow              ✅ 11.0.0>=7.1.2
pyyaml              ✅ 6.0.2>=5.3.1
requests            ✅ 2.32.3>=2.23.0
scipy               ✅ 1.14.1>=1.4.1
torch               ✅ 2.5.1>=1.8.0
torch               ✅ 2.5.1!=2.4.0,>=1.8.0; sys_platform == "win32"
torchvision         ✅ 0.20.1>=0.9.0
tqdm                ✅ 4.67.1>=4.64.0
psutil              ✅ 6.1.0
py-cpuinfo          ✅ 9.0.0
pandas              ✅ 2.2.3>=1.1.4
seaborn             ✅ 0.13.2>=0.11.0
ultralytics-thop    ✅ 2.0.12>=2.0.0
{'OS': 'Linux-6.8.0-49-generic-x86_64-with-glibc2.35', 'Environment': 'Linux', 'Python': '3.11.10', 'Install': 'pip', 'RAM': '30.97 GB', 'Disk': '444.7/913.8 GB', 'CPU': 'Intel Core(TM) i7-10750H 2.60GHz', 'CPU count': 12, 'GPU': 'NVIDIA GeForce GTX 1650 Ti, 3906MiB', 'GPU count': 1, 'CUDA': '12.4', 'Package Info': {'numpy': '✅ 1.26.4<2.0.0; sys_platform == "darwin"', 'matplotlib': '✅ 3.9.2>=3.3.0', 'opencv-python': '✅ 4.10.0.84>=4.6.0', 'pillow': '✅ 11.0.0>=7.1.2', 'pyyaml': '✅ 6.0.2>=5.3.1', 'requests': '✅ 2.32.3>=2.23.0', 'scipy': '✅ 1.14.1>=1.4.1', 'torch': '✅ 2.5.1!=2.4.0,>=1.8.0; sys_platform == "win32"', 'torchvision': '✅ 0.20.1>=0.9.0', 'tqdm': '✅ 4.67.1>=4.64.0', 'psutil': '✅ 6.1.0', 'py-cpuinfo': '✅ 9.0.0', 'pandas': '✅ 2.2.3>=1.1.4', 'seaborn': '✅ 0.13.2>=0.11.0', 'ultralytics-thop': '✅ 2.0.12>=2.0.0'}}

### Minimal Reproducible Example

```python
from ultralytics import YOLO

# Load a YOLOv8n PyTorch model
model = YOLO("yolov8n.pt")

# # # Export the model
model.export(format="imx")  # exports with PTQ quantization by default

# Load the exported model
imx_model = YOLO("yolov8n_imx_model")

# Run inference
results = imx_model("https://ultralytics.com/images/bus.jpg")

```

### Additional

_No response_

### Are you willing to submit a PR?

- [ ] Yes I'd like to help by submitting a PR!


Functions to edit:
ultralytics/engine/predictor.py:BasePredictor.pre_transform

Patch:
diff --git a/ultralytics/engine/predictor.py b/ultralytics/engine/predictor.py
index c28e1895d07..c5250166e9e 100644
--- a/ultralytics/engine/predictor.py
+++ b/ultralytics/engine/predictor.py
@@ -155,7 +155,7 @@ def pre_transform(self, im):
         same_shapes = len({x.shape for x in im}) == 1
         letterbox = LetterBox(
             self.imgsz,
-            auto=same_shapes and (self.model.pt or getattr(self.model, "dynamic", False)),
+            auto=same_shapes and (self.model.pt or (getattr(self.model, "dynamic", False) and not self.model.imx)),
             stride=self.model.stride,
         )
         return [letterbox(image=x) for x in im]