-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[NOMERGE] drop in transforms v2 into the v1 tests #7159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 25 commits
0305a4d
7e032ad
4d670ef
99bc85f
d0affa2
3864496
23655f4
d185946
5f2978d
44aac11
3f3ad5b
136f7e0
c1c626a
df081f9
8bd6e3c
3ec22ef
1194a01
132c1cd
892b50b
4688a38
739ac02
95baeab
4915482
057bca5
e747ba4
fbc0497
f6592f4
4e0ae59
cd19f40
2b218ec
729bd72
6c803e0
1b58362
a4e8838
699529a
a3b7cc5
3749cad
adfdb4d
b54257c
2dfff2b
6768182
dd5a564
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,15 +1,15 @@ | ||||||||||||||||||||||
import math | ||||||||||||||||||||||
import os | ||||||||||||||||||||||
import random | ||||||||||||||||||||||
import re | ||||||||||||||||||||||
import warnings | ||||||||||||||||||||||
from collections import defaultdict | ||||||||||||||||||||||
from functools import partial | ||||||||||||||||||||||
|
||||||||||||||||||||||
import numpy as np | ||||||||||||||||||||||
import pytest | ||||||||||||||||||||||
import torch | ||||||||||||||||||||||
import torchvision.transforms as transforms | ||||||||||||||||||||||
import torchvision.transforms.functional as F | ||||||||||||||||||||||
import torchvision.prototype.transforms as transforms | ||||||||||||||||||||||
import torchvision.prototype.transforms.functional as F | ||||||||||||||||||||||
import torchvision.transforms.functional_tensor as F_t | ||||||||||||||||||||||
from PIL import Image | ||||||||||||||||||||||
from torch._utils_internal import get_file_path_2 | ||||||||||||||||||||||
|
@@ -240,6 +240,8 @@ def test_to_tensor_errors(self): | |||||||||||||||||||||
trans = transforms.ToTensor() | ||||||||||||||||||||||
np_rng = np.random.RandomState(0) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# TODO: DID NOT RAISE | ||||||||||||||||||||||
return | ||||||||||||||||||||||
with pytest.raises(TypeError): | ||||||||||||||||||||||
trans(np_rng.rand(1, height, width).tolist()) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -298,6 +300,9 @@ def test_pil_to_tensor_errors(self): | |||||||||||||||||||||
trans = transforms.PILToTensor() | ||||||||||||||||||||||
np_rng = np.random.RandomState(0) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# TODO: DID NOT RAISE | ||||||||||||||||||||||
return | ||||||||||||||||||||||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(TypeError): | ||||||||||||||||||||||
trans(np_rng.rand(1, height, width).tolist()) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -1230,7 +1235,7 @@ def test_rotate(): | |||||||||||||||||||||
x = np.zeros((100, 100, 3), dtype=np.uint8) | ||||||||||||||||||||||
x[40, 40] = [255, 255, 255] | ||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(TypeError, match=r"img should be PIL Image"): | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"Input can either"): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now it should be
|
||||||||||||||||||||||
F.rotate(x, 10) | ||||||||||||||||||||||
|
||||||||||||||||||||||
img = F.to_pil_image(x) | ||||||||||||||||||||||
|
@@ -1286,6 +1291,10 @@ def test_gaussian_blur_asserts(): | |||||||||||||||||||||
np_img = np.ones((100, 100, 3), dtype=np.uint8) * 255 | ||||||||||||||||||||||
img = F.to_pil_image(np_img, "RGB") | ||||||||||||||||||||||
|
||||||||||||||||||||||
# TODO: Not critical, but is it really better to distinguish between | ||||||||||||||||||||||
# TypeError and ValueError? Would it be easier to treat any user-provided | ||||||||||||||||||||||
# input failure as ValueError? | ||||||||||||||||||||||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(ValueError, match=r"If kernel_size is a sequence its length should be 2"): | ||||||||||||||||||||||
F.gaussian_blur(img, [3]) | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"If kernel_size is a sequence its length should be 2"): | ||||||||||||||||||||||
|
@@ -1305,22 +1314,22 @@ def test_gaussian_blur_asserts(): | |||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(ValueError, match=r"If sigma is a sequence, its length should be 2"): | ||||||||||||||||||||||
F.gaussian_blur(img, 3, [1, 1, 1]) | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"sigma should be a single number or a list/tuple with length 2"): | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"sigma should be a single "): | ||||||||||||||||||||||
transforms.GaussianBlur(3, [1, 1, 1]) | ||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(ValueError, match=r"sigma should have positive values"): | ||||||||||||||||||||||
F.gaussian_blur(img, 3, -1.0) | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"If sigma is a single number, it must be positive"): | ||||||||||||||||||||||
transforms.GaussianBlur(3, -1.0) | ||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(TypeError, match=r"kernel_size should be int or a sequence of integers"): | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"If kernel_size is a sequence"): | ||||||||||||||||||||||
F.gaussian_blur(img, "kernel_size_string") | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"Kernel size should be a tuple/list of two integers"): | ||||||||||||||||||||||
transforms.GaussianBlur("kernel_size_string") | ||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(TypeError, match=r"sigma should be either float or sequence of floats"): | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"sigma should be "): | ||||||||||||||||||||||
F.gaussian_blur(img, 3, "sigma_string") | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"sigma should be a single number or a list/tuple with length 2"): | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"sigma should be "): | ||||||||||||||||||||||
transforms.GaussianBlur(3, "sigma_string") | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -1810,7 +1819,7 @@ def test_color_jitter(): | |||||||||||||||||||||
|
||||||||||||||||||||||
@pytest.mark.parametrize("hue", [1, (-1, 1)]) | ||||||||||||||||||||||
def test_color_jitter_hue_out_of_bounds(hue): | ||||||||||||||||||||||
with pytest.raises(ValueError, match=re.escape("hue values should be between (-0.5, 0.5)")): | ||||||||||||||||||||||
with pytest.raises((ValueError, TypeError), match="hue"): | ||||||||||||||||||||||
transforms.ColorJitter(hue=hue) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -1869,7 +1878,8 @@ def test_random_rotation(): | |||||||||||||||||||||
transforms.RandomRotation([-0.7, 0, 0.7]) | ||||||||||||||||||||||
|
||||||||||||||||||||||
t = transforms.RandomRotation(0, fill=None) | ||||||||||||||||||||||
assert t.fill == 0 | ||||||||||||||||||||||
# TODO: BC-break - do we care? | ||||||||||||||||||||||
assert t.fill == defaultdict() | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vfdev-5 and I discussed offline and our answer is no. As explained in #6517, vision/torchvision/transforms/transforms.py Lines 1338 to 1339 in 55d3ba6
AFAIK this behavior is undocumented. In v2 we pass through vision/torchvision/transforms/v2/_utils.py Lines 56 to 61 in 55d3ba6
Note that in both cases, vision/torchvision/transforms/transforms.py Line 1321 in 55d3ba6
vision/torchvision/transforms/v2/_geometry.py Line 333 in 55d3ba6
So v1 is silently ignoring user input, while v2 doesn't. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now, this assertion should be
|
||||||||||||||||||||||
|
||||||||||||||||||||||
t = transforms.RandomRotation(10) | ||||||||||||||||||||||
angle = t.get_params(t.degrees) | ||||||||||||||||||||||
|
@@ -1889,7 +1899,7 @@ def test_random_rotation(): | |||||||||||||||||||||
def test_random_rotation_error(): | ||||||||||||||||||||||
# assert fill being either a Sequence or a Number | ||||||||||||||||||||||
with pytest.raises(TypeError): | ||||||||||||||||||||||
transforms.RandomRotation(0, fill={}) | ||||||||||||||||||||||
transforms.RandomRotation(0, fill="BLAH") | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def test_randomperspective(): | ||||||||||||||||||||||
|
@@ -1918,10 +1928,11 @@ def test_randomperspective_fill(mode, seed): | |||||||||||||||||||||
|
||||||||||||||||||||||
# assert fill being either a Sequence or a Number | ||||||||||||||||||||||
with pytest.raises(TypeError): | ||||||||||||||||||||||
transforms.RandomPerspective(fill={}) | ||||||||||||||||||||||
transforms.RandomPerspective(fill="LOL") | ||||||||||||||||||||||
|
||||||||||||||||||||||
t = transforms.RandomPerspective(fill=None) | ||||||||||||||||||||||
assert t.fill == 0 | ||||||||||||||||||||||
# TODO this is BC break - do we care?? | ||||||||||||||||||||||
assert t.fill == defaultdict() | ||||||||||||||||||||||
|
||||||||||||||||||||||
height = 100 | ||||||||||||||||||||||
width = 100 | ||||||||||||||||||||||
|
@@ -2018,6 +2029,7 @@ def input_img(self): | |||||||||||||||||||||
return input_img | ||||||||||||||||||||||
|
||||||||||||||||||||||
def test_affine_translate_seq(self, input_img): | ||||||||||||||||||||||
input_img = torch.randint(0, 256, size=(224, 224), dtype=torch.uint8) | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"Argument translate should be a sequence"): | ||||||||||||||||||||||
F.affine(input_img, 10, translate=0, scale=1, shear=1) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -2063,7 +2075,9 @@ def _test_transformation(self, angle, translate, scale, shear, pil_image, input_ | |||||||||||||||||||||
true_matrix = np.matmul(T, np.matmul(C, np.matmul(RSS, Cinv))) | ||||||||||||||||||||||
|
||||||||||||||||||||||
result_matrix = self._to_3x3_inv( | ||||||||||||||||||||||
F._get_inverse_affine_matrix(center=cnt, angle=angle, translate=translate, scale=scale, shear=shear) | ||||||||||||||||||||||
F._geometry._get_inverse_affine_matrix( | ||||||||||||||||||||||
center=cnt, angle=angle, translate=translate, scale=scale, shear=shear | ||||||||||||||||||||||
) | ||||||||||||||||||||||
) | ||||||||||||||||||||||
assert np.sum(np.abs(true_matrix - result_matrix)) < 1e-10 | ||||||||||||||||||||||
# 2) Perform inverse mapping: | ||||||||||||||||||||||
|
@@ -2190,10 +2204,11 @@ def test_random_affine(): | |||||||||||||||||||||
|
||||||||||||||||||||||
# assert fill being either a Sequence or a Number | ||||||||||||||||||||||
with pytest.raises(TypeError): | ||||||||||||||||||||||
transforms.RandomAffine(0, fill={}) | ||||||||||||||||||||||
transforms.RandomAffine(0, fill="BLAH") | ||||||||||||||||||||||
|
||||||||||||||||||||||
t = transforms.RandomAffine(0, fill=None) | ||||||||||||||||||||||
assert t.fill == 0 | ||||||||||||||||||||||
# TODO: do we care? | ||||||||||||||||||||||
assert t.fill == defaultdict() | ||||||||||||||||||||||
|
||||||||||||||||||||||
x = np.zeros((100, 100, 3), dtype=np.uint8) | ||||||||||||||||||||||
img = F.to_pil_image(x) | ||||||||||||||||||||||
|
@@ -2212,7 +2227,7 @@ def test_random_affine(): | |||||||||||||||||||||
t.__repr__() | ||||||||||||||||||||||
|
||||||||||||||||||||||
t = transforms.RandomAffine(10, interpolation=transforms.InterpolationMode.BILINEAR) | ||||||||||||||||||||||
assert "bilinear" in t.__repr__() | ||||||||||||||||||||||
assert "bilinear" in t.__repr__().lower() | ||||||||||||||||||||||
|
||||||||||||||||||||||
t = transforms.RandomAffine(10, interpolation=Image.BILINEAR) | ||||||||||||||||||||||
assert t.interpolation == transforms.InterpolationMode.BILINEAR | ||||||||||||||||||||||
|
@@ -2221,23 +2236,24 @@ def test_random_affine(): | |||||||||||||||||||||
def test_elastic_transformation(): | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"alpha should be float or a sequence of floats"): | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=True, sigma=2.0) | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"alpha should be a sequence of floats"): | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"alpha should be a sequence of floats"): | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=[1.0, True], sigma=2.0) | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"alpha is a sequence its length should be 2"): | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"If alpha is a sequence"): | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=[1.0, 0.0, 1.0], sigma=2.0) | ||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(TypeError, match=r"sigma should be float or a sequence of floats"): | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=2.0, sigma=True) | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"sigma should be a sequence of floats"): | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"sigma should be a sequence of floats"): | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=2.0, sigma=[1.0, True]) | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"sigma is a sequence its length should be 2"): | ||||||||||||||||||||||
with pytest.raises(ValueError, match=r"If sigma is a sequence"): | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=2.0, sigma=[1.0, 0.0, 1.0]) | ||||||||||||||||||||||
|
||||||||||||||||||||||
t = transforms.transforms.ElasticTransform(alpha=2.0, sigma=2.0, interpolation=Image.BILINEAR) | ||||||||||||||||||||||
t = transforms.ElasticTransform(alpha=2.0, sigma=2.0, interpolation=Image.BILINEAR) | ||||||||||||||||||||||
assert t.interpolation == transforms.InterpolationMode.BILINEAR | ||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(TypeError, match=r"fill should be int or float"): | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=1.0, sigma=1.0, fill={}) | ||||||||||||||||||||||
with pytest.raises(TypeError, match=r"Got inappropriate fill arg"): | ||||||||||||||||||||||
# Had to change {} to a str because {} is actually valid now | ||||||||||||||||||||||
transforms.ElasticTransform(alpha=1.0, sigma=1.0, fill="LOL") | ||||||||||||||||||||||
|
||||||||||||||||||||||
x = torch.randint(0, 256, (3, 32, 32), dtype=torch.uint8) | ||||||||||||||||||||||
img = F.to_pil_image(x) | ||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.