Skip to content

Commit

Permalink
Port some tests in test_transforms to pytest (#3957)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilg06 authored Jun 4, 2021
1 parent 136ca60 commit dc5ede7
Showing 1 changed file with 113 additions and 111 deletions.
224 changes: 113 additions & 111 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,6 @@ def test_random_crop(self):
with self.assertRaisesRegex(ValueError, r"Required crop size .+ is larger then input image size .+"):
t(img)

def test_lambda(self):
trans = transforms.Lambda(lambda x: x.add(10))
x = torch.randn(10)
y = trans(x)
assert_equal(y, torch.add(x, 10))

trans = transforms.Lambda(lambda x: x.add_(10))
x = torch.randn(10)
y = trans(x)
assert_equal(y, x)

# Checking if Lambda can be printed as string
trans.__repr__()

@unittest.skipIf(stats is None, 'scipy.stats not available')
def test_random_apply(self):
random_state = random.getstate()
Expand Down Expand Up @@ -1179,61 +1165,6 @@ def test_linear_transformation(self):
# Checking if LinearTransformation can be printed as string
whitening.__repr__()

def test_rotate(self):
x = np.zeros((100, 100, 3), dtype=np.uint8)
x[40, 40] = [255, 255, 255]

with self.assertRaisesRegex(TypeError, r"img should be PIL Image"):
F.rotate(x, 10)

img = F.to_pil_image(x)

result = F.rotate(img, 45)
self.assertEqual(result.size, (100, 100))
r, c, ch = np.where(result)
self.assertTrue(all(x in r for x in [49, 50]))
self.assertTrue(all(x in c for x in [36]))
self.assertTrue(all(x in ch for x in [0, 1, 2]))

result = F.rotate(img, 45, expand=True)
self.assertEqual(result.size, (142, 142))
r, c, ch = np.where(result)
self.assertTrue(all(x in r for x in [70, 71]))
self.assertTrue(all(x in c for x in [57]))
self.assertTrue(all(x in ch for x in [0, 1, 2]))

result = F.rotate(img, 45, center=(40, 40))
self.assertEqual(result.size, (100, 100))
r, c, ch = np.where(result)
self.assertTrue(all(x in r for x in [40]))
self.assertTrue(all(x in c for x in [40]))
self.assertTrue(all(x in ch for x in [0, 1, 2]))

result_a = F.rotate(img, 90)
result_b = F.rotate(img, -270)

assert_equal(np.array(result_a), np.array(result_b))

def test_rotate_fill(self):
img = F.to_pil_image(np.ones((100, 100, 3), dtype=np.uint8) * 255, "RGB")

modes = ("L", "RGB", "F")
nums_bands = [len(mode) for mode in modes]
fill = 127

for mode, num_bands in zip(modes, nums_bands):
img_conv = img.convert(mode)
img_rot = F.rotate(img_conv, 45.0, fill=fill)
pixel = img_rot.getpixel((0, 0))

if not isinstance(pixel, tuple):
pixel = (pixel,)
self.assertTupleEqual(pixel, tuple([fill] * num_bands))

for wrong_num_bands in set(nums_bands) - {num_bands}:
with self.assertRaises(ValueError):
F.rotate(img_conv, 45.0, fill=tuple([fill] * wrong_num_bands))

def test_affine(self):
input_img = np.zeros((40, 40, 3), dtype=np.uint8)
cnt = [20, 20]
Expand Down Expand Up @@ -1579,48 +1510,6 @@ def test_random_grayscale(self):
# Checking if RandomGrayscale can be printed as string
trans3.__repr__()

def test_gaussian_blur_asserts(self):
np_img = np.ones((100, 100, 3), dtype=np.uint8) * 255
img = F.to_pil_image(np_img, "RGB")

with self.assertRaisesRegex(ValueError, r"If kernel_size is a sequence its length should be 2"):
F.gaussian_blur(img, [3])

with self.assertRaisesRegex(ValueError, r"If kernel_size is a sequence its length should be 2"):
F.gaussian_blur(img, [3, 3, 3])
with self.assertRaisesRegex(ValueError, r"Kernel size should be a tuple/list of two integers"):
transforms.GaussianBlur([3, 3, 3])

with self.assertRaisesRegex(ValueError, r"kernel_size should have odd and positive integers"):
F.gaussian_blur(img, [4, 4])
with self.assertRaisesRegex(ValueError, r"Kernel size value should be an odd and positive number"):
transforms.GaussianBlur([4, 4])

with self.assertRaisesRegex(ValueError, r"kernel_size should have odd and positive integers"):
F.gaussian_blur(img, [-3, -3])
with self.assertRaisesRegex(ValueError, r"Kernel size value should be an odd and positive number"):
transforms.GaussianBlur([-3, -3])

with self.assertRaisesRegex(ValueError, r"If sigma is a sequence, its length should be 2"):
F.gaussian_blur(img, 3, [1, 1, 1])
with self.assertRaisesRegex(ValueError, r"sigma should be a single number or a list/tuple with length 2"):
transforms.GaussianBlur(3, [1, 1, 1])

with self.assertRaisesRegex(ValueError, r"sigma should have positive values"):
F.gaussian_blur(img, 3, -1.0)
with self.assertRaisesRegex(ValueError, r"If sigma is a single number, it must be positive"):
transforms.GaussianBlur(3, -1.0)

with self.assertRaisesRegex(TypeError, r"kernel_size should be int or a sequence of integers"):
F.gaussian_blur(img, "kernel_size_string")
with self.assertRaisesRegex(ValueError, r"Kernel size should be a tuple/list of two integers"):
transforms.GaussianBlur("kernel_size_string")

with self.assertRaisesRegex(TypeError, r"sigma should be either float or sequence of floats"):
F.gaussian_blur(img, 3, "sigma_string")
with self.assertRaisesRegex(ValueError, r"sigma should be a single number or a list/tuple with length 2"):
transforms.GaussianBlur(3, "sigma_string")

def test_autoaugment(self):
for policy in transforms.AutoAugmentPolicy:
for fill in [None, 85, (128, 128, 128)]:
Expand Down Expand Up @@ -1990,5 +1879,118 @@ def test_adjusts_L_mode():
assert F.adjust_gamma(x_l, 0.5).mode == 'L'


def test_rotate():
x = np.zeros((100, 100, 3), dtype=np.uint8)
x[40, 40] = [255, 255, 255]

with pytest.raises(TypeError, match=r"img should be PIL Image"):
F.rotate(x, 10)

img = F.to_pil_image(x)

result = F.rotate(img, 45)
assert result.size == (100, 100)
r, c, ch = np.where(result)
assert all(x in r for x in [49, 50])
assert all(x in c for x in [36])
assert all(x in ch for x in [0, 1, 2])

result = F.rotate(img, 45, expand=True)
assert result.size == (142, 142)
r, c, ch = np.where(result)
assert all(x in r for x in [70, 71])
assert all(x in c for x in [57])
assert all(x in ch for x in [0, 1, 2])

result = F.rotate(img, 45, center=(40, 40))
assert result.size == (100, 100)
r, c, ch = np.where(result)
assert all(x in r for x in [40])
assert all(x in c for x in [40])
assert all(x in ch for x in [0, 1, 2])

result_a = F.rotate(img, 90)
result_b = F.rotate(img, -270)

assert_equal(np.array(result_a), np.array(result_b))


@pytest.mark.parametrize('mode', ["L", "RGB", "F"])
def test_rotate_fill(mode):
img = F.to_pil_image(np.ones((100, 100, 3), dtype=np.uint8) * 255, "RGB")

num_bands = len(mode)
wrong_num_bands = num_bands + 1
fill = 127

img_conv = img.convert(mode)
img_rot = F.rotate(img_conv, 45.0, fill=fill)
pixel = img_rot.getpixel((0, 0))

if not isinstance(pixel, tuple):
pixel = (pixel,)
assert pixel == tuple([fill] * num_bands)

with pytest.raises(ValueError):
F.rotate(img_conv, 45.0, fill=tuple([fill] * wrong_num_bands))


def test_gaussian_blur_asserts():
np_img = np.ones((100, 100, 3), dtype=np.uint8) * 255
img = F.to_pil_image(np_img, "RGB")

with pytest.raises(ValueError, match=r"If kernel_size is a sequence its length should be 2"):
F.gaussian_blur(img, [3])
with pytest.raises(ValueError, match=r"If kernel_size is a sequence its length should be 2"):
F.gaussian_blur(img, [3, 3, 3])
with pytest.raises(ValueError, match=r"Kernel size should be a tuple/list of two integers"):
transforms.GaussianBlur([3, 3, 3])

with pytest.raises(ValueError, match=r"kernel_size should have odd and positive integers"):
F.gaussian_blur(img, [4, 4])
with pytest.raises(ValueError, match=r"Kernel size value should be an odd and positive number"):
transforms.GaussianBlur([4, 4])

with pytest.raises(ValueError, match=r"kernel_size should have odd and positive integers"):
F.gaussian_blur(img, [-3, -3])
with pytest.raises(ValueError, match=r"Kernel size value should be an odd and positive number"):
transforms.GaussianBlur([-3, -3])

with pytest.raises(ValueError, match=r"If sigma is a sequence, its length should be 2"):
F.gaussian_blur(img, 3, [1, 1, 1])
with pytest.raises(ValueError, match=r"sigma should be a single number or a list/tuple with length 2"):
transforms.GaussianBlur(3, [1, 1, 1])

with pytest.raises(ValueError, match=r"sigma should have positive values"):
F.gaussian_blur(img, 3, -1.0)
with pytest.raises(ValueError, match=r"If sigma is a single number, it must be positive"):
transforms.GaussianBlur(3, -1.0)

with pytest.raises(TypeError, match=r"kernel_size should be int or a sequence of integers"):
F.gaussian_blur(img, "kernel_size_string")
with pytest.raises(ValueError, match=r"Kernel size should be a tuple/list of two integers"):
transforms.GaussianBlur("kernel_size_string")

with pytest.raises(TypeError, match=r"sigma should be either float or sequence of floats"):
F.gaussian_blur(img, 3, "sigma_string")
with pytest.raises(ValueError, match=r"sigma should be a single number or a list/tuple with length 2"):
transforms.GaussianBlur(3, "sigma_string")


def test_lambda():
trans = transforms.Lambda(lambda x: x.add(10))
x = torch.randn(10)
y = trans(x)
assert_equal(y, torch.add(x, 10))

trans = transforms.Lambda(lambda x: x.add_(10))
x = torch.randn(10)
y = trans(x)
assert_equal(y, x)

# Checking if Lambda can be printed as string
trans.__repr__()


if __name__ == '__main__':
unittest.main()

0 comments on commit dc5ede7

Please sign in to comment.