Skip to content

[PyOV] Fix from_torchvision with a torch.Tensor input_example on NumPy 2 - #38938

Open
Somay-kousis wants to merge 2 commits into
openvinotoolkit:masterfrom
Somay-kousis:fix/torchvision-tensor-input
Open

Somay-kousis wants to merge 2 commits into
openvinotoolkit:masterfrom
Somay-kousis:fix/torchvision-tensor-input

Conversation

@Somay-kousis

Copy link
Copy Markdown

Details:

  • What a user sees: PreprocessConverter.from_torchvision(model, transform, input_example=torch.rand(1, 3, H, W)) raises ValueError: Unable to avoid copy while creating an array as requested. on NumPy 2. The docstring lists torch.Tensor as a supported input_example. np.ndarray and PIL.Image examples work, and nothing changes for them.
  • Cause: _get_shape_layout_from_data reads the shape with np.array(input_example, copy=False). NumPy 2 changed copy=False from "copy only if needed" to "never copy", and torch.Tensor.__array__ takes no copy argument, so NumPy raises. The bindings allow numpy<2.6.0.
  • Fix: np.asarray(input_example), which is the NumPy 1 meaning of copy=False (NumPy's error message suggests the same replacement). Only .shape is used.
  • New test test_torch_tensor_input_example in tests/layer_tests/py_frontend_tests/test_torchvision_preprocessor.py: a torch.Tensor example with Normalize, compared against the torch model. The existing tests only pass PIL.Image examples.

Tickets:

AI Assistance:

  • AI assistance used: yes
  • AI tools helped find the bug and write the fix and the test; I reviewed every line. Validation on macOS arm64 with the 2026.5.0 nightly wheel, numpy 2.5.4, torch 2.11, torchvision 0.26, using this branch's torchvision_preprocessing.py:

Signed-off-by: Somay-kousis <somaykaush@gmail.com>
Signed-off-by: Somay-kousis <somaykaush@gmail.com>
@Somay-kousis
Somay-kousis requested review from a team as code owners October 17, 2026 11:06
@github-actions github-actions Bot added category: Python API OpenVINO Python bindings no-match-files labels Oct 17, 2026
@sys-openvino-ci sys-openvino-ci added the ExternalPR External contributor label Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: Python API OpenVINO Python bindings ExternalPR External contributor no-match-files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: [PyOV] from_torchvision fails with a torch.Tensor input_example on NumPy 2

2 participants