RandomPerspective (v1 and v2) crashes with _LinAlgError on images with a one-pixel side; perspective can return garbage coefficients for collinear points
🐛 Describe the bug
andomPerspective(p=1.0) raises an internal torch.linalg.lstsq error on any image whose height or width is 1, in both transforms and transforms.v2:
import torch
import torchvision.transforms as T1
import torchvision.transforms.v2 as T2
for T in (T1, T2):
for h, w in [(8, 1), (1, 8), (1, 1), (6, 7)]:
torch.manual_seed(0)
try:
T.RandomPerspective(p=1.0)(torch.rand(3, h, w))
print(T.__name__, h, w, "ok")
except Exception as e:
print(T.__name__, h, w, type(e).__name__)
Output: _LinAlgError for 8×1, 1×8 and 1×1 in both v1 and v2; ok for 6×7.
get_params builds the start corners from width - 1 and height - 1, so a one-pixel side collapses the four corners onto a line or a point, and the least-squares system in _get_perspective_coeffs becomes singular. This can happen in real pipelines when an earlier crop produces a thin image.
A related, quieter case: for collinear start points, _get_perspective_coeffs sometimes returns a result instead of raising. For the corners of a 1×8 image ([[0,0],[7,0],[7,0],[0,0]] → [[1,0],[8,1],[7,1],[0,0]]) it returns approximately [0, 0, 0, 0, 0, 0, 0, -1], which maps every pixel to the origin.
Expected: either RandomPerspective leaves an image with a one-pixel side unchanged, since no perspective warp is possible there, or it raises a clear ValueError. perspective could likewise raise a clear error for degenerate start points instead of a solver error or a silent wrong result. I’m happy to open a PR for whichever behavior you prefer.
Related: #8248 (stability of the same helper) and #9082 (open PR touching _get_perspective_coeffs).
AI disclosure: I used Claude to write an edge-case script that runs transforms.v2 functionals on one-pixel images, and it flagged this. I reproduced both cases myself in v1 and v2 and checked the cause.
Versions
PyTorch version: 2.15.0+cpu OS: Microsoft Windows 11 Pro (10.0.26200 64-bit) Python version: 3.12.10 (tags/v3.12.10:0cc8128, Apr 8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)] (64-bit runtime) Python platform: Windows-11-10.0.26200-SP0 GPU models and configuration: GPU 0: NVIDIA RTX A3000 Laptop GPU [pip3] numpy==2.5.4 [pip3] torch==2.15.0+cpu [pip3] torchvision==0.29.2+cpu [conda] Could not collect
Hi @aounullah, thanks for the detailed report. Are you still planning to open the PR for this? If you're busy, I'd like to take it. I'd have RandomPerspective leave an image with a one-pixel side unchanged (or raise a clear ValueError if the maintainers prefer that), with tests for both v1 and v2. I'll wait a couple of days for your reply before starting. I'm using an AI assistant to help write the patch, and I'll say so in the PR.
Thanks for offering, @Hirata-Origami ! Yes, I’m planning to I’ll open a PR in the next couple of days. I’ll go with leaving an image with a one-pixel side unchanged, with tests for v1 and v2, unless a maintainer prefers a ValueError.
Quick update: the fix is ready, with tests for v1 and v2: https://github.com/pytorch/vision/compare/main...aounullah:vision:fix/perspective-one-pixel
It returns the input unchanged when a side is one pixel (v1 in forward, v2 via make_params/transform). I can’t open the PR yet because of the one-open-PR limit for new contributors; I’ll open it as soon as #9750 is resolved.