|
| 1 | +import itertools |
1 | 2 | import os
|
2 | 3 | import torch
|
3 | 4 | import torchvision.transforms as transforms
|
|
29 | 30 |
|
30 | 31 | class Tester(unittest.TestCase):
|
31 | 32 |
|
32 |
| - def test_crop(self): |
| 33 | + def test_center_crop(self): |
33 | 34 | height = random.randint(10, 32) * 2
|
34 | 35 | width = random.randint(10, 32) * 2
|
35 | 36 | oheight = random.randint(5, (height - 2) / 2) * 2
|
@@ -70,6 +71,64 @@ def test_crop(self):
|
70 | 71 | self.assertGreater(sum2, sum1,
|
71 | 72 | "height: {} width: {} oheight: {} owdith: {}".format(height, width, oheight, owidth))
|
72 | 73 |
|
| 74 | + def test_center_crop_2(self): |
| 75 | + """ Tests when center crop size is larger than image size, along any dimension""" |
| 76 | + even_image_size = (random.randint(10, 32) * 2, random.randint(10, 32) * 2) |
| 77 | + odd_image_size = (even_image_size[0] + 1, even_image_size[1] + 1) |
| 78 | + |
| 79 | + # Since height is independent of width, we can ignore images with odd height and even width and vice-versa. |
| 80 | + input_image_sizes = [even_image_size, odd_image_size] |
| 81 | + |
| 82 | + # Get different crop sizes |
| 83 | + delta = random.choice((1, 3, 5)) |
| 84 | + crop_size_delta = [-2 * delta, -delta, 0, delta, 2 * delta] |
| 85 | + crop_size_params = itertools.product(input_image_sizes, crop_size_delta, crop_size_delta) |
| 86 | + |
| 87 | + for (input_image_size, delta_height, delta_width) in crop_size_params: |
| 88 | + img = torch.ones(3, *input_image_size) |
| 89 | + crop_size = (input_image_size[0] + delta_height, input_image_size[1] + delta_width) |
| 90 | + |
| 91 | + # Test both transforms, one with PIL input and one with tensor |
| 92 | + output_pil = transforms.Compose([ |
| 93 | + transforms.ToPILImage(), |
| 94 | + transforms.CenterCrop(crop_size), |
| 95 | + transforms.ToTensor()], |
| 96 | + )(img) |
| 97 | + self.assertEqual(output_pil.size()[1:3], crop_size, |
| 98 | + "image_size: {} crop_size: {}".format(input_image_size, crop_size)) |
| 99 | + |
| 100 | + output_tensor = transforms.CenterCrop(crop_size)(img) |
| 101 | + self.assertEqual(output_tensor.size()[1:3], crop_size, |
| 102 | + "image_size: {} crop_size: {}".format(input_image_size, crop_size)) |
| 103 | + |
| 104 | + # Ensure output for PIL and Tensor are equal |
| 105 | + self.assertEqual((output_tensor - output_pil).sum(), 0, |
| 106 | + "image_size: {} crop_size: {}".format(input_image_size, crop_size)) |
| 107 | + |
| 108 | + # Check if content in center of both image and cropped output is same. |
| 109 | + center_size = (min(crop_size[0], input_image_size[0]), min(crop_size[1], input_image_size[1])) |
| 110 | + crop_center_tl, input_center_tl = [0, 0], [0, 0] |
| 111 | + for index in range(2): |
| 112 | + if crop_size[index] > input_image_size[index]: |
| 113 | + crop_center_tl[index] = (crop_size[index] - input_image_size[index]) // 2 |
| 114 | + else: |
| 115 | + input_center_tl[index] = (input_image_size[index] - crop_size[index]) // 2 |
| 116 | + |
| 117 | + output_center = output_pil[ |
| 118 | + :, |
| 119 | + crop_center_tl[0]:crop_center_tl[0] + center_size[0], |
| 120 | + crop_center_tl[1]:crop_center_tl[1] + center_size[1] |
| 121 | + ] |
| 122 | + |
| 123 | + img_center = img[ |
| 124 | + :, |
| 125 | + input_center_tl[0]:input_center_tl[0] + center_size[0], |
| 126 | + input_center_tl[1]:input_center_tl[1] + center_size[1] |
| 127 | + ] |
| 128 | + |
| 129 | + self.assertEqual((output_center - img_center).sum(), 0, |
| 130 | + "image_size: {} crop_size: {}".format(input_image_size, crop_size)) |
| 131 | + |
73 | 132 | def test_five_crop(self):
|
74 | 133 | to_pil_image = transforms.ToPILImage()
|
75 | 134 | h = random.randint(5, 25)
|
|
0 commit comments