Skip to content

fix warnings in prototype transforms test suite #6785

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

Merged
merged 6 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 11 additions & 4 deletions test/prototype_transforms_kernel_infos.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
import itertools
import math
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -172,6 +173,12 @@ def sample_inputs_horizontal_flip_video():
KernelInfo(
F.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(
F.horizontal_flip_mask,
Expand Down Expand Up @@ -443,10 +450,10 @@ def transform(bbox):
transformed_points = np.matmul(points, affine_matrix.T)
out_bbox = torch.tensor(
[
np.min(transformed_points[:, 0]),
np.min(transformed_points[:, 1]),
np.max(transformed_points[:, 0]),
np.max(transformed_points[:, 1]),
np.min(transformed_points[:, 0]).item(),
np.min(transformed_points[:, 1]).item(),
np.max(transformed_points[:, 0]).item(),
np.max(transformed_points[:, 1]).item(),
],
dtype=bbox.dtype,
)
Expand Down
13 changes: 8 additions & 5 deletions test/test_prototype_transforms_consistency.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import inspect
import random
import re
from collections import defaultdict
from importlib.machinery import SourceFileLoader
from pathlib import Path
Expand Down Expand Up @@ -598,6 +599,7 @@ def check_call_consistency(
for idx, args_kwargs in enumerate(config.args_kwargs)
],
)
@pytest.mark.filterwarnings("ignore")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since these are throw-away tests anyway, I didn't bother to write a proper warnings handling on this test. All warnings here were deprecation warnings from us, so there is nothing to do besides ignoring anyway.

def test_call_consistency(config, args_kwargs):
args, kwargs = args_kwargs

Expand Down Expand Up @@ -671,21 +673,21 @@ def test_random_apply(self, p):
check_call_consistency(prototype_transform, legacy_transform)

# We can't test other values for `p` since the random parameter generation is different
@pytest.mark.parametrize("p", [(0, 1), (1, 0)])
def test_random_choice(self, p):
@pytest.mark.parametrize("probabilities", [(0, 1), (1, 0)])
def test_random_choice(self, probabilities):
prototype_transform = prototype_transforms.RandomChoice(
[
prototype_transforms.Resize(256),
legacy_transforms.CenterCrop(224),
],
p=p,
probabilities=probabilities,
)
legacy_transform = legacy_transforms.RandomChoice(
[
legacy_transforms.Resize(256),
legacy_transforms.CenterCrop(224),
],
p=p,
p=probabilities,
)

check_call_consistency(prototype_transform, legacy_transform)
Expand All @@ -702,7 +704,8 @@ def test_pil_to_tensor(self):
assert_equal(prototype_transform(image_pil), legacy_transform(image_pil))

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()

for image in make_images(extra_dims=[()]):
Expand Down
9 changes: 1 addition & 8 deletions test/test_prototype_transforms_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,17 +1012,10 @@ def test_normalize_output_type():
def test_to_image_tensor(inpt):
output = F.to_image_tensor(inpt)
assert isinstance(output, torch.Tensor)
assert output.shape == (3, 32, 32)

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can no longer test this, since the .contiguous() call will always copy.

assert output[0, 0, 0] == 11


@pytest.mark.parametrize(
"inpt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def decode_video_with_av(encoded_video: torch.Tensor) -> Tuple[torch.Tensor, tor
@torch.jit.unused
def to_image_tensor(image: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> features.Image:
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):
output = pil_to_tensor(image)
else: # isinstance(inpt, torch.Tensor):
Expand Down