Unverified Commit 32757a26 authored by Philip Meier's avatar Philip Meier Committed by GitHub
Browse files

fix warnings in prototype transforms test suite (#6785)

* fix, ignore, or assert warnings for consistency tests

* fix, ignore, or assert warnings for kernel infos

* fix to_image_tensor for numpy inputs

* make image from numpy contiguous

* fix test
parent f8b5a7af
import functools import functools
import itertools import itertools
import math import math
import re
import numpy as np import numpy as np
import pytest import pytest
...@@ -172,6 +173,12 @@ KERNEL_INFOS.extend( ...@@ -172,6 +173,12 @@ KERNEL_INFOS.extend(
KernelInfo( KernelInfo(
F.horizontal_flip_bounding_box, F.horizontal_flip_bounding_box,
sample_inputs_fn=sample_inputs_horizontal_flip_bounding_box, sample_inputs_fn=sample_inputs_horizontal_flip_bounding_box,
test_marks=[
TestMark(
("TestKernels", "test_scripted_vs_eager"),
pytest.mark.filterwarnings(f"ignore:{re.escape('operator() profile_node %72')}:UserWarning"),
)
],
), ),
KernelInfo( KernelInfo(
F.horizontal_flip_mask, F.horizontal_flip_mask,
...@@ -443,10 +450,10 @@ def reference_affine_bounding_box(bounding_box, *, format, spatial_size, angle, ...@@ -443,10 +450,10 @@ def reference_affine_bounding_box(bounding_box, *, format, spatial_size, angle,
transformed_points = np.matmul(points, affine_matrix.T) transformed_points = np.matmul(points, affine_matrix.T)
out_bbox = torch.tensor( out_bbox = torch.tensor(
[ [
np.min(transformed_points[:, 0]), np.min(transformed_points[:, 0]).item(),
np.min(transformed_points[:, 1]), np.min(transformed_points[:, 1]).item(),
np.max(transformed_points[:, 0]), np.max(transformed_points[:, 0]).item(),
np.max(transformed_points[:, 1]), np.max(transformed_points[:, 1]).item(),
], ],
dtype=bbox.dtype, dtype=bbox.dtype,
) )
......
import enum import enum
import inspect import inspect
import random import random
import re
from collections import defaultdict from collections import defaultdict
from importlib.machinery import SourceFileLoader from importlib.machinery import SourceFileLoader
from pathlib import Path from pathlib import Path
...@@ -598,6 +599,7 @@ def check_call_consistency( ...@@ -598,6 +599,7 @@ def check_call_consistency(
for idx, args_kwargs in enumerate(config.args_kwargs) for idx, args_kwargs in enumerate(config.args_kwargs)
], ],
) )
@pytest.mark.filterwarnings("ignore")
def test_call_consistency(config, args_kwargs): def test_call_consistency(config, args_kwargs):
args, kwargs = args_kwargs args, kwargs = args_kwargs
...@@ -671,21 +673,21 @@ class TestContainerTransforms: ...@@ -671,21 +673,21 @@ class TestContainerTransforms:
check_call_consistency(prototype_transform, legacy_transform) check_call_consistency(prototype_transform, legacy_transform)
# We can't test other values for `p` since the random parameter generation is different # We can't test other values for `p` since the random parameter generation is different
@pytest.mark.parametrize("p", [(0, 1), (1, 0)]) @pytest.mark.parametrize("probabilities", [(0, 1), (1, 0)])
def test_random_choice(self, p): def test_random_choice(self, probabilities):
prototype_transform = prototype_transforms.RandomChoice( prototype_transform = prototype_transforms.RandomChoice(
[ [
prototype_transforms.Resize(256), prototype_transforms.Resize(256),
legacy_transforms.CenterCrop(224), legacy_transforms.CenterCrop(224),
], ],
p=p, probabilities=probabilities,
) )
legacy_transform = legacy_transforms.RandomChoice( legacy_transform = legacy_transforms.RandomChoice(
[ [
legacy_transforms.Resize(256), legacy_transforms.Resize(256),
legacy_transforms.CenterCrop(224), legacy_transforms.CenterCrop(224),
], ],
p=p, p=probabilities,
) )
check_call_consistency(prototype_transform, legacy_transform) check_call_consistency(prototype_transform, legacy_transform)
...@@ -702,7 +704,8 @@ class TestToTensorTransforms: ...@@ -702,7 +704,8 @@ class TestToTensorTransforms:
assert_equal(prototype_transform(image_pil), legacy_transform(image_pil)) assert_equal(prototype_transform(image_pil), legacy_transform(image_pil))
def test_to_tensor(self): def test_to_tensor(self):
prototype_transform = prototype_transforms.ToTensor() with pytest.warns(UserWarning, match=re.escape("The transform `ToTensor()` is deprecated")):
prototype_transform = prototype_transforms.ToTensor()
legacy_transform = legacy_transforms.ToTensor() legacy_transform = legacy_transforms.ToTensor()
for image in make_images(extra_dims=[()]): for image in make_images(extra_dims=[()]):
......
...@@ -1012,17 +1012,10 @@ def test_normalize_output_type(): ...@@ -1012,17 +1012,10 @@ def test_normalize_output_type():
def test_to_image_tensor(inpt): def test_to_image_tensor(inpt):
output = F.to_image_tensor(inpt) output = F.to_image_tensor(inpt)
assert isinstance(output, torch.Tensor) assert isinstance(output, torch.Tensor)
assert output.shape == (3, 32, 32)
assert np.asarray(inpt).sum() == output.sum().item() assert np.asarray(inpt).sum() == output.sum().item()
if isinstance(inpt, PIL.Image.Image):
# we can't check this option
# as PIL -> numpy is always copying
return
inpt[0, 0, 0] = 11
assert output[0, 0, 0] == 11
@pytest.mark.parametrize( @pytest.mark.parametrize(
"inpt", "inpt",
......
...@@ -27,7 +27,7 @@ def decode_video_with_av(encoded_video: torch.Tensor) -> Tuple[torch.Tensor, tor ...@@ -27,7 +27,7 @@ def decode_video_with_av(encoded_video: torch.Tensor) -> Tuple[torch.Tensor, tor
@torch.jit.unused @torch.jit.unused
def to_image_tensor(image: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> features.Image: def to_image_tensor(image: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> features.Image:
if isinstance(image, np.ndarray): if isinstance(image, np.ndarray):
output = torch.from_numpy(image) output = torch.from_numpy(image).permute((2, 0, 1)).contiguous()
elif isinstance(image, PIL.Image.Image): elif isinstance(image, PIL.Image.Image):
output = pil_to_tensor(image) output = pil_to_tensor(image)
else: # isinstance(inpt, torch.Tensor): else: # isinstance(inpt, torch.Tensor):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment