Skip to content

Commit ce36a5e

Browse files
Joao Gomesfacebook-github-bot
authored andcommitted
[fbsync] fix warnings in prototype transforms test suite (#6785)
Summary: * 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 Reviewed By: YosuaMichael Differential Revision: D40588163 fbshipit-source-id: 39793e69a7ad5a17f69ee443a69f62b501bcc870
1 parent 3bf4c2f commit ce36a5e

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

test/prototype_transforms_kernel_infos.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
import itertools
33
import math
4+
import re
45

56
import numpy as np
67
import pytest
@@ -172,6 +173,12 @@ def sample_inputs_horizontal_flip_video():
172173
KernelInfo(
173174
F.horizontal_flip_bounding_box,
174175
sample_inputs_fn=sample_inputs_horizontal_flip_bounding_box,
176+
test_marks=[
177+
TestMark(
178+
("TestKernels", "test_scripted_vs_eager"),
179+
pytest.mark.filterwarnings(f"ignore:{re.escape('operator() profile_node %72')}:UserWarning"),
180+
)
181+
],
175182
),
176183
KernelInfo(
177184
F.horizontal_flip_mask,
@@ -443,10 +450,10 @@ def transform(bbox):
443450
transformed_points = np.matmul(points, affine_matrix.T)
444451
out_bbox = torch.tensor(
445452
[
446-
np.min(transformed_points[:, 0]),
447-
np.min(transformed_points[:, 1]),
448-
np.max(transformed_points[:, 0]),
449-
np.max(transformed_points[:, 1]),
453+
np.min(transformed_points[:, 0]).item(),
454+
np.min(transformed_points[:, 1]).item(),
455+
np.max(transformed_points[:, 0]).item(),
456+
np.max(transformed_points[:, 1]).item(),
450457
],
451458
dtype=bbox.dtype,
452459
)

test/test_prototype_transforms_consistency.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import enum
22
import inspect
33
import random
4+
import re
45
from collections import defaultdict
56
from importlib.machinery import SourceFileLoader
67
from pathlib import Path
@@ -598,6 +599,7 @@ def check_call_consistency(
598599
for idx, args_kwargs in enumerate(config.args_kwargs)
599600
],
600601
)
602+
@pytest.mark.filterwarnings("ignore")
601603
def test_call_consistency(config, args_kwargs):
602604
args, kwargs = args_kwargs
603605

@@ -671,21 +673,21 @@ def test_random_apply(self, p):
671673
check_call_consistency(prototype_transform, legacy_transform)
672674

673675
# We can't test other values for `p` since the random parameter generation is different
674-
@pytest.mark.parametrize("p", [(0, 1), (1, 0)])
675-
def test_random_choice(self, p):
676+
@pytest.mark.parametrize("probabilities", [(0, 1), (1, 0)])
677+
def test_random_choice(self, probabilities):
676678
prototype_transform = prototype_transforms.RandomChoice(
677679
[
678680
prototype_transforms.Resize(256),
679681
legacy_transforms.CenterCrop(224),
680682
],
681-
p=p,
683+
probabilities=probabilities,
682684
)
683685
legacy_transform = legacy_transforms.RandomChoice(
684686
[
685687
legacy_transforms.Resize(256),
686688
legacy_transforms.CenterCrop(224),
687689
],
688-
p=p,
690+
p=probabilities,
689691
)
690692

691693
check_call_consistency(prototype_transform, legacy_transform)
@@ -702,7 +704,8 @@ def test_pil_to_tensor(self):
702704
assert_equal(prototype_transform(image_pil), legacy_transform(image_pil))
703705

704706
def test_to_tensor(self):
705-
prototype_transform = prototype_transforms.ToTensor()
707+
with pytest.warns(UserWarning, match=re.escape("The transform `ToTensor()` is deprecated")):
708+
prototype_transform = prototype_transforms.ToTensor()
706709
legacy_transform = legacy_transforms.ToTensor()
707710

708711
for image in make_images(extra_dims=[()]):

test/test_prototype_transforms_functional.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,17 +1012,10 @@ def test_normalize_output_type():
10121012
def test_to_image_tensor(inpt):
10131013
output = F.to_image_tensor(inpt)
10141014
assert isinstance(output, torch.Tensor)
1015+
assert output.shape == (3, 32, 32)
10151016

10161017
assert np.asarray(inpt).sum() == output.sum().item()
10171018

1018-
if isinstance(inpt, PIL.Image.Image):
1019-
# we can't check this option
1020-
# as PIL -> numpy is always copying
1021-
return
1022-
1023-
inpt[0, 0, 0] = 11
1024-
assert output[0, 0, 0] == 11
1025-
10261019

10271020
@pytest.mark.parametrize(
10281021
"inpt",

torchvision/prototype/transforms/functional/_type_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def decode_video_with_av(encoded_video: torch.Tensor) -> Tuple[torch.Tensor, tor
2727
@torch.jit.unused
2828
def to_image_tensor(image: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> features.Image:
2929
if isinstance(image, np.ndarray):
30-
output = torch.from_numpy(image)
30+
output = torch.from_numpy(image).permute((2, 0, 1)).contiguous()
3131
elif isinstance(image, PIL.Image.Image):
3232
output = pil_to_tensor(image)
3333
else: # isinstance(inpt, torch.Tensor):

0 commit comments

Comments
 (0)