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

port tests for type conversion transforms #8003

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
55 changes: 0 additions & 55 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,61 +122,6 @@ def test_check_transformed_types(self, inpt_type, mocker):
t(inpt)


class TestToImage:
@pytest.mark.parametrize(
"inpt_type",
[torch.Tensor, PIL.Image.Image, tv_tensors.Image, np.ndarray, tv_tensors.BoundingBoxes, str, int],
)
def test__transform(self, inpt_type, mocker):
fn = mocker.patch(
"torchvision.transforms.v2.functional.to_image",
return_value=torch.rand(1, 3, 8, 8),
)

inpt = mocker.MagicMock(spec=inpt_type)
transform = transforms.ToImage()
transform(inpt)
if inpt_type in (tv_tensors.BoundingBoxes, tv_tensors.Image, str, int):
assert fn.call_count == 0
else:
fn.assert_called_once_with(inpt)


class TestToPILImage:
@pytest.mark.parametrize(
"inpt_type",
[torch.Tensor, PIL.Image.Image, tv_tensors.Image, np.ndarray, tv_tensors.BoundingBoxes, str, int],
)
def test__transform(self, inpt_type, mocker):
fn = mocker.patch("torchvision.transforms.v2.functional.to_pil_image")

inpt = mocker.MagicMock(spec=inpt_type)
transform = transforms.ToPILImage()
transform(inpt)
if inpt_type in (PIL.Image.Image, tv_tensors.BoundingBoxes, str, int):
assert fn.call_count == 0
else:
fn.assert_called_once_with(inpt, mode=transform.mode)


class TestToTensor:
@pytest.mark.parametrize(
"inpt_type",
[torch.Tensor, PIL.Image.Image, tv_tensors.Image, np.ndarray, tv_tensors.BoundingBoxes, str, int],
)
def test__transform(self, inpt_type, mocker):
fn = mocker.patch("torchvision.transforms.functional.to_tensor")

inpt = mocker.MagicMock(spec=inpt_type)
with pytest.warns(UserWarning, match="deprecated and will be removed"):
transform = transforms.ToTensor()
transform(inpt)
if inpt_type in (tv_tensors.Image, torch.Tensor, tv_tensors.BoundingBoxes, str, int):
assert fn.call_count == 0
else:
fn.assert_called_once_with(inpt)


class TestContainers:
@pytest.mark.parametrize("transform_cls", [transforms.Compose, transforms.RandomChoice, transforms.RandomOrder])
def test_assertions(self, transform_cls):
Expand Down
23 changes: 0 additions & 23 deletions test/test_transforms_v2_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,6 @@ def __init__(
LINEAR_TRANSFORMATION_MATRIX = torch.rand([LINEAR_TRANSFORMATION_MEAN.numel()] * 2)

CONSISTENCY_CONFIGS = [
ConsistencyConfig(
v2_transforms.ToPILImage,
legacy_transforms.ToPILImage,
[NotScriptableArgsKwargs()],
make_images_kwargs=dict(
color_spaces=[
"GRAY",
"GRAY_ALPHA",
"RGB",
"RGBA",
],
extra_dims=[()],
),
supports_pil=False,
),
ConsistencyConfig(
v2_transforms.Lambda,
legacy_transforms.Lambda,
Expand All @@ -97,14 +82,6 @@ def __init__(
# images given that the transform does nothing but call it anyway.
supports_pil=False,
),
ConsistencyConfig(
v2_transforms.PILToTensor,
legacy_transforms.PILToTensor,
),
ConsistencyConfig(
v2_transforms.ToTensor,
legacy_transforms.ToTensor,
),
ConsistencyConfig(
v2_transforms.Compose,
legacy_transforms.Compose,
Expand Down
64 changes: 64 additions & 0 deletions test/test_transforms_v2_refactored.py
Original file line number Diff line number Diff line change
Expand Up @@ -5046,3 +5046,67 @@ def test_transform_error_cuda(self):
ValueError, match="Input tensor should be on the same device as transformation matrix and mean vector"
):
transform(input)


def make_image_numpy(*args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

make_image_pil is only ever used in this file (test/test_transforms_v2_consistency.py), yet it lives in common_utils.py. Maybe we should align where we put these make_input stuff?

image = make_image_tensor(*args, **kwargs)
return image.permute((1, 2, 0)).numpy()


class TestToImage:
@pytest.mark.parametrize("make_input", [make_image_tensor, make_image_pil, make_image, make_image_numpy])
@pytest.mark.parametrize("fn", [F.to_image, transform_cls_to_functional(transforms.ToImage)])
def test_functional_and_transform(self, make_input, fn):
input = make_input()
output = fn(input)

assert isinstance(output, tv_tensors.Image)

input_size = list(input.shape[:2]) if isinstance(input, np.ndarray) else F.get_size(input)
assert F.get_size(output) == input_size

if isinstance(input, torch.Tensor):
assert output.data_ptr() == input.data_ptr()

def test_functional_error(self):
with pytest.raises(TypeError, match="Input can either be a numpy array or a PIL image"):
pmeier marked this conversation as resolved.
Show resolved Hide resolved
F.to_image(object())


class TestToPILImage:
@pytest.mark.parametrize("make_input", [make_image_tensor, make_image, make_image_numpy])
@pytest.mark.parametrize("color_space", ["RGB", "GRAY"])
@pytest.mark.parametrize("fn", [F.to_pil_image, transform_cls_to_functional(transforms.ToPILImage)])
def test_functional_and_transform(self, make_input, color_space, fn):
input = make_input(color_space=color_space)
output = fn(input)

assert isinstance(output, PIL.Image.Image)

input_size = list(input.shape[:2]) if isinstance(input, np.ndarray) else F.get_size(input)
assert F.get_size(output) == input_size

def test_functional_error(self):
with pytest.raises(TypeError, match="pic should be Tensor or ndarray"):
F.to_pil_image(object())

for ndim in [1, 4]:
with pytest.raises(ValueError, match="pic should be 2/3 dimensional"):
F.to_pil_image(torch.empty(*[1] * ndim))

with pytest.raises(ValueError, match="pic should not have > 4 channels"):
num_channels = 5
F.to_pil_image(torch.empty(num_channels, 1, 1))


class TestToTensor:
@pytest.mark.parametrize("make_input", [make_image_tensor, make_image_pil, make_image, make_image_numpy])
def test_smoke(self, make_input):
with pytest.warns(UserWarning, match="deprecated and will be removed"):
transform = transforms.ToTensor()

input = make_input()
output = transform(input)

input_size = list(input.shape[:2]) if isinstance(input, np.ndarray) else F.get_size(input)
assert F.get_size(output) == input_size