def test_forward(self, shape, padding, patchwise_apply, same_on_batch, keepdim, random_apply, device, dtype): torch.manual_seed(11) try: # skip wrong param settings. seq = K.PatchSequential( K.color.RgbToBgr(), K.ColorJitter(0.1, 0.1, 0.1, 0.1), K.ImageSequential( K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5), K.RandomPerspective(0.2, p=0.5), K.RandomSolarize(0.1, 0.1, p=0.5), ), K.RandomMixUp(p=1.0), grid_size=(2, 2), padding=padding, patchwise_apply=patchwise_apply, same_on_batch=same_on_batch, keepdim=keepdim, random_apply=random_apply, ) # TODO: improve me and remove the exception. except Exception: return input = torch.randn(*shape, device=device, dtype=dtype) out = seq(input) if seq.return_label: out, _ = out assert out.shape[-3:] == input.shape[-3:] reproducibility_test(input, seq)
def test_forward(self, return_transform, random_apply, device, dtype): inp = torch.randn(1, 3, 30, 30, device=device, dtype=dtype) aug = K.ImageSequential( K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0), kornia.filters.MedianBlur((3, 3)), K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0, return_transform=True), K.ImageSequential( K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0) ), K.ImageSequential( K.RandomAffine(360, p=1.0) ), K.RandomAffine(360, p=1.0), K.RandomMixUp(p=1.0), return_transform=return_transform, random_apply=random_apply, ) out = aug(inp) if aug.return_label: out, _ = out if isinstance(out, (tuple,)): assert out[0].shape == inp.shape else: assert out.shape == inp.shape aug.inverse(inp) reproducibility_test(inp, aug)
def test_mixup(self, inp, random_apply, same_on_batch, device, dtype): inp = torch.as_tensor(inp, device=device, dtype=dtype) aug = K.AugmentationSequential( K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0)), K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0), K.RandomMixUp(p=1.0), data_keys=["input"], random_apply=random_apply, same_on_batch=same_on_batch, ) out = aug(inp) if aug.return_label: out, _ = out assert out.shape[-3:] == inp.shape[-3:] reproducibility_test(inp, aug)
def test_forward(self, random_apply, device, dtype): inp = torch.randn(1, 3, 30, 30, device=device, dtype=dtype) aug = K.ImageSequential( K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), kornia.filters.MedianBlur((3, 3)), K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)), K.ImageSequential(K.RandomAffine(360, p=1.0)), K.RandomAffine(360, p=1.0), K.RandomMixUp(p=1.0), random_apply=random_apply, ) out = aug(inp) if aug.return_label: out, _ = out assert out.shape == inp.shape aug.inverse(inp) reproducibility_test(inp, aug)
def test_construction(self, same_on_batch, keepdim, random_apply): aug = K.ImageSequential( K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0), K.RandomMixUp(p=1.0), same_on_batch=same_on_batch, keepdim=keepdim, random_apply=random_apply, ) c = 0 for a in aug.get_forward_sequence(): if isinstance(a, (MixAugmentationBase, )): c += 1 assert c < 2 aug.same_on_batch = True aug.keepdim = True for m in aug.children(): assert m.same_on_batch is True, m.same_on_batch assert m.keepdim is True, m.keepdim
def test_mixup(self, inp, return_transform, random_apply, same_on_batch, device, dtype): inp = torch.as_tensor(inp, device=device, dtype=dtype) aug = K.AugmentationSequential( K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0), K.RandomMixUp(p=1.0), data_keys=["input"], random_apply=random_apply, return_transform=return_transform, same_on_batch=same_on_batch, ) out = aug(inp) if aug.return_label: out, label = out if return_transform and isinstance(out, (tuple, list)): out = out[0] assert out.shape[-3:] == inp.shape[-3:] reproducibility_test(inp, aug)
def __init__(self, N_TFMS: int, MAGN: int, mean: Union[tuple, list, torch.tensor], std: Union[tuple, list, torch.tensor], transform_list: list = None, use_resize: int = None, image_size: tuple = None, use_mix: int = None, mix_p: float = .5): super().__init__() self.N_TFMS, self.MAGN = N_TFMS, MAGN self.use_mix, self.mix_p = use_mix, mix_p self.image_size = image_size if not isinstance(mean, torch.Tensor): mean = torch.Tensor(mean) if not isinstance(std, torch.Tensor): std = torch.Tensor(std) if self.use_mix is not None: self.mix_list = [ K.RandomCutMix(self.image_size[0], self.image_size[1], p=1), K.RandomMixUp(p=1) ] self.use_resize = use_resize if use_resize is not None: assert len( image_size ) == 2, 'Invalid `image_size`. Must be a tuple of form (h, w)' self.resize_list = [ K.RandomResizedCrop(image_size), K.RandomCrop(image_size), K.CenterCrop(image_size) ] if self.use_resize < 3: self.resize = self.resize_list[use_resize] self.normalize = K.Normalize(mean, std) self.transform_list = transform_list if transform_list is None: self.transform_list = kornia_list(MAGN)
class TestVideoSequential: @pytest.mark.parametrize('shape', [(3, 4), (2, 3, 4), (2, 3, 5, 6), (2, 3, 4, 5, 6, 7)]) @pytest.mark.parametrize('data_format', ["BCTHW", "BTCHW"]) def test_exception(self, shape, data_format, device, dtype): aug_list = K.VideoSequential(K.ColorJitter(0.1, 0.1, 0.1, 0.1), data_format=data_format, same_on_frame=True) with pytest.raises(AssertionError): img = torch.randn(*shape, device=device, dtype=dtype) aug_list(img) @pytest.mark.parametrize( 'augmentation', [ K.RandomAffine(360, p=1.0), K.CenterCrop((3, 3), p=1.0), K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomCrop((5, 5), p=1.0), K.RandomErasing(p=1.0), K.RandomGrayscale(p=1.0), K.RandomHorizontalFlip(p=1.0), K.RandomVerticalFlip(p=1.0), K.RandomPerspective(p=1.0), K.RandomResizedCrop((5, 5), p=1.0), K.RandomRotation(360.0, p=1.0), K.RandomSolarize(p=1.0), K.RandomPosterize(p=1.0), K.RandomSharpness(p=1.0), K.RandomEqualize(p=1.0), K.RandomMotionBlur(3, 35.0, 0.5, p=1.0), K.Normalize(torch.tensor([0.5, 0.5, 0.5]), torch.tensor([0.5, 0.5, 0.5]), p=1.0), K.Denormalize(torch.tensor([0.5, 0.5, 0.5]), torch.tensor([0.5, 0.5, 0.5]), p=1.0), ], ) @pytest.mark.parametrize('data_format', ["BCTHW", "BTCHW"]) def test_augmentation(self, augmentation, data_format, device, dtype): input = torch.randint(255, (1, 3, 3, 5, 6), device=device, dtype=dtype).repeat(2, 1, 1, 1, 1) / 255.0 torch.manual_seed(21) aug_list = K.VideoSequential(augmentation, data_format=data_format, same_on_frame=True) reproducibility_test(input, aug_list) @pytest.mark.parametrize( 'augmentations', [ [ K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0) ], [ K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0), K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0) ], [K.RandomAffine(360, p=1.0), kornia.color.BgrToRgb()], [ K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.0), K.RandomAffine(360, p=0.0) ], [K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.0)], [K.RandomAffine(360, p=0.0)], [ K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0), K.RandomMixUp(p=1.0) ], ], ) @pytest.mark.parametrize('data_format', ["BCTHW", "BTCHW"]) @pytest.mark.parametrize('random_apply', [1, (1, 1), (1, ), 10, True, False]) def test_same_on_frame(self, augmentations, data_format, random_apply, device, dtype): aug_list = K.VideoSequential(*augmentations, data_format=data_format, same_on_frame=True, random_apply=random_apply) if data_format == 'BCTHW': input = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1) output = aug_list(input) if aug_list.return_label: output, _ = output assert (output[:, :, 0] == output[:, :, 1]).all() assert (output[:, :, 1] == output[:, :, 2]).all() assert (output[:, :, 2] == output[:, :, 3]).all() if data_format == 'BTCHW': input = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1) output = aug_list(input) if aug_list.return_label: output, _ = output assert (output[:, 0] == output[:, 1]).all() assert (output[:, 1] == output[:, 2]).all() assert (output[:, 2] == output[:, 3]).all() reproducibility_test(input, aug_list) @pytest.mark.parametrize( 'augmentations', [ [K.RandomAffine(360, p=1.0)], [K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0)], [ K.RandomAffine(360, p=0.0), K.ImageSequential(K.RandomAffine(360, p=0.0)) ], ], ) @pytest.mark.parametrize('data_format', ["BCTHW", "BTCHW"]) def test_against_sequential(self, augmentations, data_format, device, dtype): aug_list_1 = K.VideoSequential(*augmentations, data_format=data_format, same_on_frame=False) aug_list_2 = torch.nn.Sequential(*augmentations) if data_format == 'BCTHW': input = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1) if data_format == 'BTCHW': input = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1) torch.manual_seed(0) output_1 = aug_list_1(input) torch.manual_seed(0) if data_format == 'BCTHW': input = input.transpose(1, 2) output_2 = aug_list_2(input.reshape(-1, 3, 5, 6)) output_2 = output_2.view(2, 4, 3, 5, 6) if data_format == 'BCTHW': output_2 = output_2.transpose(1, 2) assert (output_1 == output_2).all(), dict(aug_list_1._params) @pytest.mark.jit @pytest.mark.skip(reason="turn off due to Union Type") def test_jit(self, device, dtype): B, C, D, H, W = 2, 3, 5, 4, 4 img = torch.ones(B, C, D, H, W, device=device, dtype=dtype) op = K.VideoSequential(K.ColorJitter(0.1, 0.1, 0.1, 0.1), same_on_frame=True) op_jit = torch.jit.script(op) assert_close(op(img), op_jit(img))