[Bug]: [PyOV] from_torchvision resizes to a square for Resize([n]) instead of keeping the aspect ratio
OpenVINO Version
master (1c169aa2c4); also reproduces with openvino 2026.4.1 from PyPI
Operating System
Other (Please specify in description)
Device used for inference
CPU
Framework
PyTorch
Model used
Not model specific (torchvision preprocessing converter)
Issue description
PreprocessConverter.from_torchvision doesn't handle transforms.Resize with a one-element size like torchvision does.
torchvision treats Resize([n]) the same as Resize(n): the smaller edge is resized to n and the aspect ratio is kept (the docs suggest [size, ] for torchscript, where a plain int isn't allowed). In the converter (torchvision_preprocessing.py#L302-L310) the smaller-edge logic only runs for isinstance(transform.size, int), and _setup_size turns [n] into (n, n), so the image is resized to a square instead:
target_h, target_w = _setup_size(transform.size, "Incorrect size type for Resize operation")
if isinstance(transform.size, int):
# rescale the smaller image edge
current_h, current_w = meta["image_dimensions"]
if current_h > current_w:
target_h = int(transform.size * (current_h / current_w))
elif current_w > current_h:
target_w = int(transform.size * (current_w / current_h))
There is no error, the converted model just outputs a different shape (and a stretched image) compared to torchvision.
A smaller thing in the same lines: the long edge is computed as int(size * (long / short)), while torchvision uses int(size * long / short) (_compute_resized_output_size). Doing the division first can land just below an integer and get truncated, so the long edge is sometimes one pixel short, e.g. a 112x115 image with Resize(224) gives 224x229 instead of 224x230.
The Resize cases in tests/layer_tests/py_frontend_tests/test_torchvision_preprocessor.py only use an int or an (h, w) pair, so neither case is covered.
Environment: Debian 13, Python 3.13.5, openvino 2026.4.1 (with torchvision_preprocessing.py from master 1c169aa2c4 dropped in, same result), torch 2.14.1+cpu, torchvision 0.29.1+cpu, numpy 2.5.4, Pillow 12.3.0.
I used AI tools to help with researching this; the repro and output below are from my own run.
Step-by-step reproduction
import numpy as np
import openvino as ov
import openvino.opset13 as ops
import torchvision.transforms as T
from openvino.preprocess.torchvision import PreprocessConverter
from PIL import Image
def compare(size, h, w):
img = Image.fromarray(np.random.randint(0, 256, (h, w, 3), dtype=np.uint8))
tf = T.Compose([T.Resize(size), T.ToTensor()])
p = ops.parameter([1, 3, -1, -1], ov.Type.f32, name="x")
model = PreprocessConverter.from_torchvision(ov.Model([ops.relu(p)], [p]), tf, input_example=img)
out = ov.compile_model(model, "CPU")(np.expand_dims(np.array(img), 0))[0]
print(f"{h}x{w} image, Resize({size!r}): torchvision {tuple(tf(img).unsqueeze(0).shape)}, openvino {out.shape}")
compare(120, 240, 320)
compare([120], 240, 320)
compare(224, 112, 115)
Relevant log output
240x320 image, Resize(120): torchvision (1, 3, 120, 160), openvino (1, 3, 120, 160)
240x320 image, Resize([120]): torchvision (1, 3, 120, 160), openvino (1, 3, 120, 120)
112x115 image, Resize(224): torchvision (1, 3, 224, 230), openvino (1, 3, 224, 229)
Issue submission checklist
- [x] I'm reporting an issue. It's not a question.
- [x] I checked the problem with the documentation, FAQ, open issues, Stack Overflow, etc., and have not found a solution.
- [x] There is reproducer code and related data files such as images, videos, models, etc.