|
| 1 | +from __future__ import division |
| 2 | +import torch |
| 3 | +import torchvision.transforms as transforms |
| 4 | +import unittest |
| 5 | +import random |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +try: |
| 9 | + from scipy import stats |
| 10 | +except ImportError: |
| 11 | + stats = None |
| 12 | + |
| 13 | + |
| 14 | +class Tester(unittest.TestCase): |
| 15 | + |
| 16 | + def test_random_crop_video(self): |
| 17 | + numFrames = random.randint(4, 128) |
| 18 | + height = random.randint(10, 32) * 2 |
| 19 | + width = random.randint(10, 32) * 2 |
| 20 | + oheight = random.randint(5, (height - 2) / 2) * 2 |
| 21 | + owidth = random.randint(5, (width - 2) / 2) * 2 |
| 22 | + clip = torch.randint(0, 256, (numFrames, height, width, 3), dtype=torch.uint8) |
| 23 | + result = transforms.Compose([ |
| 24 | + transforms.ToTensorVideo(), |
| 25 | + transforms.RandomCropVideo((oheight, owidth)), |
| 26 | + ])(clip) |
| 27 | + assert result.size(2) == oheight |
| 28 | + assert result.size(3) == owidth |
| 29 | + |
| 30 | + transforms.RandomCropVideo((oheight, owidth)).__repr__() |
| 31 | + |
| 32 | + def test_random_resized_crop_video(self): |
| 33 | + numFrames = random.randint(4, 128) |
| 34 | + height = random.randint(10, 32) * 2 |
| 35 | + width = random.randint(10, 32) * 2 |
| 36 | + oheight = random.randint(5, (height - 2) / 2) * 2 |
| 37 | + owidth = random.randint(5, (width - 2) / 2) * 2 |
| 38 | + clip = torch.randint(0, 256, (numFrames, height, width, 3), dtype=torch.uint8) |
| 39 | + result = transforms.Compose([ |
| 40 | + transforms.ToTensorVideo(), |
| 41 | + transforms.RandomResizedCropVideo((oheight, owidth)), |
| 42 | + ])(clip) |
| 43 | + assert result.size(2) == oheight |
| 44 | + assert result.size(3) == owidth |
| 45 | + |
| 46 | + transforms.RandomResizedCropVideo((oheight, owidth)).__repr__() |
| 47 | + |
| 48 | + def test_center_crop_video(self): |
| 49 | + numFrames = random.randint(4, 128) |
| 50 | + height = random.randint(10, 32) * 2 |
| 51 | + width = random.randint(10, 32) * 2 |
| 52 | + oheight = random.randint(5, (height - 2) / 2) * 2 |
| 53 | + owidth = random.randint(5, (width - 2) / 2) * 2 |
| 54 | + |
| 55 | + clip = torch.ones((numFrames, height, width, 3), dtype=torch.uint8) * 255 |
| 56 | + oh1 = (height - oheight) // 2 |
| 57 | + ow1 = (width - owidth) // 2 |
| 58 | + clipNarrow = clip[:, oh1:oh1 + oheight, ow1:ow1 + owidth, :] |
| 59 | + clipNarrow.fill_(0) |
| 60 | + result = transforms.Compose([ |
| 61 | + transforms.ToTensorVideo(), |
| 62 | + transforms.CenterCropVideo((oheight, owidth)), |
| 63 | + ])(clip) |
| 64 | + |
| 65 | + msg = "height: " + str(height) + " width: " \ |
| 66 | + + str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth) |
| 67 | + self.assertEqual(result.sum().item(), 0, msg) |
| 68 | + |
| 69 | + oheight += 1 |
| 70 | + owidth += 1 |
| 71 | + result = transforms.Compose([ |
| 72 | + transforms.ToTensorVideo(), |
| 73 | + transforms.CenterCropVideo((oheight, owidth)), |
| 74 | + ])(clip) |
| 75 | + sum1 = result.sum() |
| 76 | + |
| 77 | + msg = "height: " + str(height) + " width: " \ |
| 78 | + + str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth) |
| 79 | + self.assertEqual(sum1.item() > 1, True, msg) |
| 80 | + |
| 81 | + oheight += 1 |
| 82 | + owidth += 1 |
| 83 | + result = transforms.Compose([ |
| 84 | + transforms.ToTensorVideo(), |
| 85 | + transforms.CenterCropVideo((oheight, owidth)), |
| 86 | + ])(clip) |
| 87 | + sum2 = result.sum() |
| 88 | + |
| 89 | + msg = "height: " + str(height) + " width: " \ |
| 90 | + + str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth) |
| 91 | + self.assertTrue(sum2.item() > 1, msg) |
| 92 | + self.assertTrue(sum2.item() > sum1.item(), msg) |
| 93 | + |
| 94 | + @unittest.skipIf(stats is None, 'scipy.stats is not available') |
| 95 | + def test_normalize_video(self): |
| 96 | + def samples_from_standard_normal(tensor): |
| 97 | + p_value = stats.kstest(list(tensor.view(-1)), 'norm', args=(0, 1)).pvalue |
| 98 | + return p_value > 0.0001 |
| 99 | + |
| 100 | + random_state = random.getstate() |
| 101 | + random.seed(42) |
| 102 | + for channels in [1, 3]: |
| 103 | + numFrames = random.randint(4, 128) |
| 104 | + height = random.randint(32, 256) |
| 105 | + width = random.randint(32, 256) |
| 106 | + mean = random.random() |
| 107 | + std = random.random() |
| 108 | + clip = torch.normal(mean, std, size=(channels, numFrames, height, width)) |
| 109 | + mean = [clip[c].mean().item() for c in range(channels)] |
| 110 | + std = [clip[c].std().item() for c in range(channels)] |
| 111 | + normalized = transforms.NormalizeVideo(mean, std)(clip) |
| 112 | + assert samples_from_standard_normal(normalized) |
| 113 | + random.setstate(random_state) |
| 114 | + |
| 115 | + # Checking the optional in-place behaviour |
| 116 | + tensor = torch.rand((3, 128, 16, 16)) |
| 117 | + tensor_inplace = transforms.NormalizeVideo((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)(tensor) |
| 118 | + assert torch.equal(tensor, tensor_inplace) |
| 119 | + |
| 120 | + transforms.NormalizeVideo((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True).__repr__() |
| 121 | + |
| 122 | + def test_to_tensor_video(self): |
| 123 | + numFrames, height, width = 64, 4, 4 |
| 124 | + trans = transforms.ToTensorVideo() |
| 125 | + |
| 126 | + with self.assertRaises(TypeError): |
| 127 | + trans(np.random.rand(numFrames, height, width, 1).tolist()) |
| 128 | + trans(torch.rand((numFrames, height, width, 1), dtype=torch.float)) |
| 129 | + |
| 130 | + with self.assertRaises(ValueError): |
| 131 | + trans(torch.ones((3, numFrames, height, width, 3), dtype=torch.uint8)) |
| 132 | + trans(torch.ones((height, width, 3), dtype=torch.uint8)) |
| 133 | + trans(torch.ones((width, 3), dtype=torch.uint8)) |
| 134 | + trans(torch.ones((3), dtype=torch.uint8)) |
| 135 | + |
| 136 | + trans.__repr__() |
| 137 | + |
| 138 | + @unittest.skipIf(stats is None, 'scipy.stats not available') |
| 139 | + def test_random_horizontal_flip_video(self): |
| 140 | + random_state = random.getstate() |
| 141 | + random.seed(42) |
| 142 | + clip = torch.rand((3, 4, 112, 112), dtype=torch.float) |
| 143 | + hclip = clip.flip((-1)) |
| 144 | + |
| 145 | + num_samples = 250 |
| 146 | + num_horizontal = 0 |
| 147 | + for _ in range(num_samples): |
| 148 | + out = transforms.RandomHorizontalFlipVideo()(clip) |
| 149 | + if torch.all(torch.eq(out, hclip)): |
| 150 | + num_horizontal += 1 |
| 151 | + |
| 152 | + p_value = stats.binom_test(num_horizontal, num_samples, p=0.5) |
| 153 | + random.setstate(random_state) |
| 154 | + assert p_value > 0.0001 |
| 155 | + |
| 156 | + num_samples = 250 |
| 157 | + num_horizontal = 0 |
| 158 | + for _ in range(num_samples): |
| 159 | + out = transforms.RandomHorizontalFlipVideo(p=0.7)(clip) |
| 160 | + if torch.all(torch.eq(out, hclip)): |
| 161 | + num_horizontal += 1 |
| 162 | + |
| 163 | + p_value = stats.binom_test(num_horizontal, num_samples, p=0.7) |
| 164 | + random.setstate(random_state) |
| 165 | + assert p_value > 0.0001 |
| 166 | + |
| 167 | + transforms.RandomHorizontalFlipVideo().__repr__() |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + unittest.main() |
0 commit comments