Skip to content

Commit 5178a2e

Browse files
pmeierNicolasHug
andauthored
[PoC] refactor transforms v2 tests (#7562)
Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Nicolas Hug <[email protected]>
1 parent 17d50fc commit 5178a2e

5 files changed

+743
-210
lines changed

test/common_utils.py

+22
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import os
88
import pathlib
99
import random
10+
import re
1011
import shutil
1112
import sys
1213
import tempfile
14+
import warnings
1315
from collections import defaultdict
1416
from subprocess import CalledProcessError, check_output, STDOUT
1517
from typing import Callable, Sequence, Tuple, Union
@@ -880,3 +882,23 @@ def assert_run_python_script(source_code):
880882
raise RuntimeError(f"script errored with output:\n{e.output.decode()}")
881883
if out != b"":
882884
raise AssertionError(out.decode())
885+
886+
887+
@contextlib.contextmanager
888+
def assert_no_warnings():
889+
# The name `catch_warnings` is a misnomer as the context manager does **not** catch any warnings, but rather scopes
890+
# the warning filters. All changes that are made to the filters while in this context, will be reset upon exit.
891+
with warnings.catch_warnings():
892+
warnings.simplefilter("error")
893+
yield
894+
895+
896+
@contextlib.contextmanager
897+
def ignore_jit_no_profile_information_warning():
898+
# Calling a scripted object often triggers a warning like
899+
# `UserWarning: operator() profile_node %$INT1 : int[] = prim::profile_ivalue($INT2) does not have profile information`
900+
# with varying `INT1` and `INT2`. Since these are uninteresting for us and only clutter the test summary, we ignore
901+
# them.
902+
with warnings.catch_warnings():
903+
warnings.filterwarnings("ignore", message=re.escape("operator() profile_node %"), category=UserWarning)
904+
yield

test/test_transforms_v2.py

-24
Original file line numberDiff line numberDiff line change
@@ -1711,8 +1711,6 @@ def test_antialias_warning():
17111711
tensor_video = torch.randint(0, 256, size=(2, 3, 10, 10), dtype=torch.uint8)
17121712

17131713
match = "The default value of the antialias parameter"
1714-
with pytest.warns(UserWarning, match=match):
1715-
transforms.Resize((20, 20))(tensor_img)
17161714
with pytest.warns(UserWarning, match=match):
17171715
transforms.RandomResizedCrop((20, 20))(tensor_img)
17181716
with pytest.warns(UserWarning, match=match):
@@ -1722,18 +1720,6 @@ def test_antialias_warning():
17221720
with pytest.warns(UserWarning, match=match):
17231721
transforms.RandomResize(10, 20)(tensor_img)
17241722

1725-
with pytest.warns(UserWarning, match=match):
1726-
transforms.functional.resize(tensor_img, (20, 20))
1727-
with pytest.warns(UserWarning, match=match):
1728-
transforms.functional.resize_image_tensor(tensor_img, (20, 20))
1729-
1730-
with pytest.warns(UserWarning, match=match):
1731-
transforms.functional.resize(tensor_video, (20, 20))
1732-
with pytest.warns(UserWarning, match=match):
1733-
transforms.functional.resize_video(tensor_video, (20, 20))
1734-
1735-
with pytest.warns(UserWarning, match=match):
1736-
datapoints.Image(tensor_img).resize((20, 20))
17371723
with pytest.warns(UserWarning, match=match):
17381724
datapoints.Image(tensor_img).resized_crop(0, 0, 10, 10, (20, 20))
17391725

@@ -1744,27 +1730,17 @@ def test_antialias_warning():
17441730

17451731
with warnings.catch_warnings():
17461732
warnings.simplefilter("error")
1747-
transforms.Resize((20, 20))(pil_img)
17481733
transforms.RandomResizedCrop((20, 20))(pil_img)
17491734
transforms.ScaleJitter((20, 20))(pil_img)
17501735
transforms.RandomShortestSize((20, 20))(pil_img)
17511736
transforms.RandomResize(10, 20)(pil_img)
1752-
transforms.functional.resize(pil_img, (20, 20))
17531737

1754-
transforms.Resize((20, 20), antialias=True)(tensor_img)
17551738
transforms.RandomResizedCrop((20, 20), antialias=True)(tensor_img)
17561739
transforms.ScaleJitter((20, 20), antialias=True)(tensor_img)
17571740
transforms.RandomShortestSize((20, 20), antialias=True)(tensor_img)
17581741
transforms.RandomResize(10, 20, antialias=True)(tensor_img)
17591742

1760-
transforms.functional.resize(tensor_img, (20, 20), antialias=True)
1761-
transforms.functional.resize_image_tensor(tensor_img, (20, 20), antialias=True)
1762-
transforms.functional.resize(tensor_video, (20, 20), antialias=True)
1763-
transforms.functional.resize_video(tensor_video, (20, 20), antialias=True)
1764-
1765-
datapoints.Image(tensor_img).resize((20, 20), antialias=True)
17661743
datapoints.Image(tensor_img).resized_crop(0, 0, 10, 10, (20, 20), antialias=True)
1767-
datapoints.Video(tensor_video).resize((20, 20), antialias=True)
17681744
datapoints.Video(tensor_video).resized_crop(0, 0, 10, 10, (20, 20), antialias=True)
17691745

17701746

0 commit comments

Comments
 (0)