Skip to content

Cleanup and deduplicate tests for transforms #3106

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
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
21 changes: 8 additions & 13 deletions test/test_functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,19 +863,14 @@ def test_gaussian_blur(self):
)

def test_invert(self):
script_invert = torch.jit.script(F.invert)

img_tensor, pil_img = self._create_data(16, 18, device=self.device)
inverted_img = F.invert(img_tensor)
inverted_pil_img = F.invert(pil_img)
self.compareTensorToPIL(inverted_img, inverted_pil_img)

# scriptable function test
inverted_img_script = script_invert(img_tensor)
self.assertTrue(inverted_img.equal(inverted_img_script))

batch_tensors = self._create_data_batch(16, 18, num_samples=4, device=self.device)
self._test_fn_on_batch(batch_tensors, F.invert)
self._test_adjust_fn(
F.invert,
F_pil.invert,
F_t.invert,
[{}],
tol=1.0,
agg_method="max"
)


@unittest.skipIf(not torch.cuda.is_available(), reason="Skip if no CUDA device")
Expand Down
45 changes: 22 additions & 23 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,37 +1749,36 @@ def test_gaussian_blur_asserts(self):
with self.assertRaisesRegex(ValueError, r"sigma should be a single number or a list/tuple with length 2"):
transforms.GaussianBlur(3, "sigma_string")

@unittest.skipIf(stats is None, 'scipy.stats not available')
def test_random_invert(self):
def _test_randomness(self, fn, trans, configs):
random_state = random.getstate()
random.seed(42)
img = transforms.ToPILImage()(torch.rand(3, 10, 10))
inv_img = F.invert(img)

num_samples = 250
num_inverts = 0
for _ in range(num_samples):
out = transforms.RandomInvert()(img)
if out == inv_img:
num_inverts += 1
for p in [0.5, 0.7]:
for config in configs:
inv_img = fn(img, **config)

p_value = stats.binom_test(num_inverts, num_samples, p=0.5)
random.setstate(random_state)
self.assertGreater(p_value, 0.0001)
num_samples = 250
counts = 0
for _ in range(num_samples):
out = trans(p=p, **config)(img)
if out == inv_img:
counts += 1

num_samples = 250
num_inverts = 0
for _ in range(num_samples):
out = transforms.RandomInvert(p=0.7)(img)
if out == inv_img:
num_inverts += 1
p_value = stats.binom_test(counts, num_samples, p=p)
random.setstate(random_state)
self.assertGreater(p_value, 0.0001)

p_value = stats.binom_test(num_inverts, num_samples, p=0.7)
random.setstate(random_state)
self.assertGreater(p_value, 0.0001)
# Checking if it can be printed as string
trans().__repr__()

# Checking if RandomInvert can be printed as string
transforms.RandomInvert().__repr__()
@unittest.skipIf(stats is None, 'scipy.stats not available')
def test_random_invert(self):
self._test_randomness(
F.invert,
transforms.RandomInvert,
[{}]
)


if __name__ == '__main__':
Expand Down
13 changes: 0 additions & 13 deletions torchvision/transforms/functional_pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,19 +610,6 @@ def to_grayscale(img, num_output_channels):

@torch.jit.unused
def invert(img):
"""PRIVATE METHOD. Invert the colors of an image.

.. warning::

Module ``transforms.functional_pil`` is private and should not be used in user application.
Please, consider instead using methods from `transforms.functional` module.

Args:
img (PIL Image): Image to have its colors inverted.

Returns:
PIL Image: Color inverted image Tensor.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
return ImageOps.invert(img)
13 changes: 0 additions & 13 deletions torchvision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,19 +1182,6 @@ def gaussian_blur(img: Tensor, kernel_size: List[int], sigma: List[float]) -> Te


def invert(img: Tensor) -> Tensor:
"""PRIVATE METHOD. Invert the colors of a grayscale or RGB image.

.. warning::``

Module ``transforms.functional_tensor`` is private and should not be used in user application.
Please, consider instead using methods from `transforms.functional` module.

Args:
img (Tensor): Image to have its colors inverted in the form [C, H, W].

Returns:
Tensor: Color inverted image Tensor.
"""
if not _is_tensor_a_torch_image(img):
raise TypeError('tensor is not a torch image.')

Expand Down