Skip to content
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

[NOMERGE] drop in transforms v2 into the v1 tests #7159

Draft
wants to merge 42 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0305a4d
[NOMERGE] drop in transforms v2 into the v1 tests
pmeier Feb 1, 2023
7e032ad
Fix all tests in test_transforms.py
NicolasHug Feb 6, 2023
4d670ef
Merge branch 'main' into test-transforms-v2-dropin
pmeier Feb 7, 2023
99bc85f
Make test_functional_tensor.py pass
NicolasHug Feb 7, 2023
d0affa2
Make test_transforms_tensor.py pass
NicolasHug Feb 7, 2023
3864496
update test_functional_tensor
NicolasHug Feb 15, 2023
23655f4
update test_functional_tensor - bis
NicolasHug Feb 15, 2023
d185946
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Feb 15, 2023
5f2978d
Update test_transforms_tensor.py
NicolasHug Feb 15, 2023
44aac11
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Feb 15, 2023
3f3ad5b
removed some todos in test_transforms.py
NicolasHug Feb 15, 2023
136f7e0
Some more
NicolasHug Feb 15, 2023
c1c626a
remove TODOs from test_functional_tensor except elastic
pmeier Feb 15, 2023
df081f9
add error for max_size with size sequence in resize
pmeier Feb 15, 2023
8bd6e3c
Merge branch 'main' into test-transforms-v2-dropin
pmeier Feb 15, 2023
3ec22ef
allow integer parameters in ColorJitter
pmeier Feb 15, 2023
1194a01
Merge branch 'main' into test-transforms-v2-dropin
pmeier Feb 15, 2023
132c1cd
remove some TODOs from test_transforms
pmeier Feb 15, 2023
892b50b
Merge branch 'main' into test-transforms-v2-dropin
NicolasHug Feb 15, 2023
4688a38
test_random_apply got fixed
NicolasHug Feb 15, 2023
739ac02
revert some outdated changes
NicolasHug Feb 15, 2023
95baeab
Fixed elastic tests
vfdev-5 Feb 15, 2023
4915482
Fixed issues in elastic transform
vfdev-5 Feb 15, 2023
057bca5
Remove TODOs related to elastic
NicolasHug Feb 15, 2023
e747ba4
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Feb 15, 2023
fbc0497
Merge branch 'main' into test-transforms-v2-dropin
pmeier Feb 16, 2023
f6592f4
Merge branch 'main' into test-transforms-v2-dropin
pmeier Feb 16, 2023
4e0ae59
change from prototype to v2
pmeier Feb 16, 2023
cd19f40
Merge branch 'test-transforms-v2-dropin' of https://github.com/pmeier…
pmeier Feb 16, 2023
2b218ec
handle RandomRotation except for expand=True
pmeier Feb 16, 2023
729bd72
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Feb 16, 2023
6c803e0
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Feb 16, 2023
1b58362
Put back expand=True
NicolasHug Feb 16, 2023
a4e8838
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Aug 9, 2023
699529a
Merge branch 'main' into test-transforms-v2-dropin
vfdev-5 Aug 15, 2023
a3b7cc5
Merge branch 'main' into test-transforms-v2-dropin
vfdev-5 Aug 15, 2023
3749cad
Merge branch 'main' into test-transforms-v2-dropin
NicolasHug Aug 29, 2023
adfdb4d
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Aug 30, 2023
b54257c
Some fixes
NicolasHug Aug 30, 2023
2dfff2b
Merge branch 'main' of github.com:pytorch/vision into test-transforms…
NicolasHug Aug 30, 2023
6768182
empty
NicolasHug Aug 30, 2023
dd5a564
Merge branch 'main' into test-transforms-v2-dropin
pmeier Aug 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions test/test_functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import PIL.Image
import pytest
import torch
import torchvision.transforms as T
import torchvision.transforms.functional as F
import torchvision.prototype.transforms as T
import torchvision.prototype.transforms.functional as F
import torchvision.transforms.functional_pil as F_pil
import torchvision.transforms.functional_tensor as F_t
from common_utils import (
Expand Down Expand Up @@ -1323,13 +1323,13 @@ def test_ten_crop(device):


def test_elastic_transform_asserts():
img_tensor = torch.rand(1, 3, 32, 24)
with pytest.raises(TypeError, match="Argument displacement should be a Tensor"):
_ = F.elastic_transform("abc", displacement=None)
_ = F.elastic_transform(img_tensor, displacement=None)

with pytest.raises(TypeError, match="img should be PIL Image or Tensor"):
with pytest.raises(TypeError, match="Input can either be"):
_ = F.elastic_transform("abc", displacement=torch.rand(1))

img_tensor = torch.rand(1, 3, 32, 24)
with pytest.raises(ValueError, match="Argument displacement shape should"):
_ = F.elastic_transform(img_tensor, displacement=torch.rand(1, 2))

Expand Down
64 changes: 40 additions & 24 deletions test/test_transforms.py
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
Expand Down Expand Up @@ -240,6 +240,8 @@ def test_to_tensor_errors(self):
trans = transforms.ToTensor()
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())

Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now it should be

    with pytest.raises(TypeError, match=r"(Input can either|supports inputs of type)"):
        F.rotate(x, 10)

F.rotate(x, 10)

img = F.to_pil_image(x)
Expand Down Expand Up @@ -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"):
Expand All @@ -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")


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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, fill=None and fill=0 is not the same. However, some v1 transforms (the three highlighted here) simply coerce None into 0:

if fill is None:
fill = 0

AFAIK this behavior is undocumented. In v2 we pass through None values:

# Fill = 0 is not equivalent to None, https://github.com/pytorch/vision/issues/6517
# So, we can't reassign fill to 0
# if fill is None:
# fill = 0
if fill is None:
return fill

Note that in both cases, fill=None is not the default value

def __init__(self, degrees, interpolation=InterpolationMode.NEAREST, expand=False, center=None, fill=0):

fill: Union[datapoints.FillType, Dict[Type, datapoints.FillType]] = 0,

So v1 is silently ignoring user input, while v2 doesn't.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, this assertion should be

assert t.fill is None


t = transforms.RandomRotation(10)
angle = t.get_params(t.degrees)
Expand All @@ -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():
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
Loading