pytorch / pytorch/vision

Transforms v2.RandomRotation fail when expand=True with ValueError(f"Found multiple HxW dimensions in the sample: {sequence_to_str(sorted(sizes))}")

Open
#8,654 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🐛 Describe the bug

Example:

import torchvision
from torchvision.transforms import v2

 transforms = v2.Compose([
        v2.ToImage(),
        v2.RandomRotation(degrees=(-45, 45), expand=True),
    ])

    # Set up COCO dataset

    train_dataset = torchvision.datasets.CocoDetection(coco_img_root, coco_ann_root, transforms=transforms)
    train_dataset = torchvision.datasets.wrap_dataset_for_transforms_v2(train_dataset, target_keys=['boxes', 'labels'])
    img, labels = train_dataset[0]

Suggested Solution

The off by one error is thrown in v2.RandomRotation when expand=True. A similar error is referenced for image rotation in issue #7714, however a similar solution was not implemented for bounding boxes.

The issue can be solved by changing line 837 of torchvision/transforms/v2/functional/_geometry.py
in _affine_bounding_boxes_with_expand().

from:

def _affine_bounding_boxes_with_expand()
...
    if expand:
    ....
    # Estimate meta-data for image with inverted=True
    affine_vector = _get_inverse_affine_matrix(center, angle, translate, scale, shear)
...

to:

def _affine_bounding_boxes_with_expand()
...
    if expand:
    ....
    # Estimate meta-data for image with inverted=True
    affine_vector = _get_inverse_affine_matrix([0, 0], angle, translate, scale, shear)
...
Versions

Collecting environment information...
PyTorch version: 2.4.0+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Linux Mint 21.3 (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.11.10 (main, Sep 7 2024, 18:35:41) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.8.0-40-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 43 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Vendor ID: AuthenticAMD
Model name: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
CPU family: 23
Model: 24
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
Stepping: 1
Frequency boost: enabled
CPU max MHz: 2100.0000
CPU min MHz: 1400.0000
BogoMIPS: 4192.45
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb hw_pstate ssbd ibpb vmmcall fsgsbase bmi1 avx2 smep bmi2 rdseed adx smap clflushopt sha_ni xsaveopt xsavec xgetbv1 clzero irperf xsaveerptr arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif overflow_recov succor smca sev sev_es
Virtualization: AMD-V
L1d cache: 128 KiB (4 instances)
L1i cache: 256 KiB (4 instances)
L2 cache: 2 MiB (4 instances)
L3 cache: 4 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-7
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Mitigation; untrained return thunk; SMT vulnerable
Vulnerability Spec rstack overflow: Mitigation; Safe RET
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Retpolines; IBPB conditional; STIBP disabled; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] numpy==2.1.0
[pip3] torch==2.4.0+cu124
[pip3] torchmetrics==1.4.1
[pip3] torchvision==0.19.0+cu124
[pip3] triton==3.0.0
[conda] Could not collect

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in torchvision/transforms/v2/functional/_geometry.py at _affine_bounding_boxes_with_expand() and reproduce the provided RandomRotation example with expand=True and COCO bounding boxes. Verify that rotated samples no longer raise the multiple HxW dimensions ValueError and that bounding boxes remain valid after expansion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
computer-vision
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.