Repository: ultralytics/ultralytics
Issue category: Bug Report

Issue:
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!


Hints:
👋 Hello @Magitoneu, thank you for your interest in Ultralytics 🚀! We appreciate you taking the time to report this issue. Here’s a quick guide to help us investigate this further:

It seems like you’re experiencing an error related to image resizing when running the Sony IMX500 example. If this is indeed a 🐛 Bug Report, we kindly request you to confirm the behavior by sharing a [minimum reproducible example](https://docs.ultralytics.com/help/minimum_reproducible_example/). This will help ensure we can reproduce and debug the issue effectively.

In the meantime, please ensure that you’ve updated to the latest version of the `ultralytics` package and all related requirements. You can do so by running the following command in your terminal:

```bash
pip install -U ultralytics
```

We also recommend reviewing the [Sony IMX500 integration documentation](https://docs.ultralytics.com/integrations/sony-imx500/#usage-examples) to double-check the expected behavior of the example code. It’s possible that some additional steps may be required for handling image preprocessing.

## Resources

For troubleshooting tips and to learn more ways of using the library, visit the [Ultralytics Docs](https://docs.ultralytics.com). Additional examples for Python and CLI-based workflows are available to guide you:
- [Python](https://docs.ultralytics.com/usage/python/)
- [CLI](https://docs.ultralytics.com/usage/cli/)

If you’re trying to improve your training results or explore new features, don’t miss our [Tips for Best Training Results](https://docs.ultralytics.com/guides/model-training-tips/).

## Join the Community

For real-time discussions with other Ultralytics users, join our [Discord](https://discord.com/invite/ultralytics) server 🎧. We also host conversations on [Discourse](https://community.ultralytics.com) and our [Subreddit](https://reddit.com/r/Ultralytics) for deeper discussions.

## Verified Environments

If possible, try running your code in one of our tested environments for a consistent experience:
- **Notebooks** with free GPU: <a href="https://console.paperspace.com/github/ultralytics/ultralytics"><img src="https://assets.paperspace.io/img/gradient-badge.svg" alt="Run on Gradient"/></a> <a href="https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/tutorial.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a> <a href="https://www.kaggle.com/models/ultralytics/yolo11"><img src="https://kaggle.com/static/images/open-in-kaggle.svg" alt="Open In Kaggle"></a>
- **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial/)
- **Amazon** Deep Learning AMI. See [AWS Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial/)
- **Docker Image**. Check the [Docker Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial/) <a href="https://hub.docker.com/r/ultralytics/ultralytics"><img src="https://img.shields.io/docker/pulls/ultralytics/ultralytics?logo=docker" alt="Docker Pulls"></a>

## Status

<a href="https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml?query=event%3Aschedule"><img src="https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml/badge.svg" alt="Ultralytics CI"></a>

If this badge is green, all [Ultralytics CI](https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml?query=event%3Aschedule) tests are currently passing, verifying that core functionalities across different environments are operating correctly.

This is an automated response 💡. An Ultralytics engineer will review your report and reach out with further assistance soon. Thank you for helping us improve! 😊
@Magitoneu thank you for reporting this issue. It seems that the input dimensions mismatch is due to the image size not being adjusted automatically during inference with the IMX500 export. While resizing the input image to 640x640 manually resolves the issue, automatic resizing is currently not implemented for IMX500-exported models.

As a workaround, ensure your input images are pre-processed to 640x640 before inference. For improvements in this behavior, feel free to provide additional insights, or you can open a feature request. Let us know if you need further assistance!

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]


Test patch: