def test_forward(self, shape, padding, patchwise_apply, same_on_batch, keepdim, device, dtype): seq = K.PatchSequential( 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.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.ColorJitter(0.1, 0.1, 0.1, 0.1), grid_size=(2, 2), padding=padding, patchwise_apply=patchwise_apply, same_on_batch=same_on_batch, keepdim=keepdim, ) input = torch.randn(*shape, device=device, dtype=dtype) trans = torch.randn(shape[0], 3, 3, device=device, dtype=dtype) out = seq(input) assert out.shape[-3:] == input.shape[-3:] out = seq((input, trans)) assert out[0].shape[-3:] == input.shape[-3:] assert out[1].shape == trans.shape
def test_intensity_only(self): seq = K.PatchSequential( 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.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.ColorJitter(0.1, 0.1, 0.1, 0.1), grid_size=(2, 2), ) assert not seq.is_intensity_only() seq = K.PatchSequential( K.ImageSequential(K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5)), K.ColorJitter(0.1, 0.1, 0.1, 0.1), K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=0.5), K.ColorJitter(0.1, 0.1, 0.1, 0.1), grid_size=(2, 2), ) assert seq.is_intensity_only()
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 get_train_augmentation_transforms( p_flip_vertical=0.5, p_flip_horizontal=0.5, max_rotation=10.0, max_zoom=1.1, max_warp=0.2, p_affine=0.75, max_lighting=0.2, p_lighting=0.75, ): """ Build a set of pytorch image Transforms to use during training: p_flip_vertical: probability of a vertical flip p_flip_horizontal: probability of a horizontal flip max_rotation: maximum rotation angle in degrees max_zoom: maximum zoom level max_warp: perspective warping scale (from 0.0 to 1.0) p_affine: probility of rotation, zoom and perspective warping max_lighting: maximum scaling of brightness and contrast """ return [ kaug.RandomVerticalFlip(p=p_flip_vertical), kaug.RandomHorizontalFlip(p=p_flip_horizontal), kaug.RandomAffine(p=p_affine, degrees=max_rotation, scale=(1.0, max_zoom)), kaug.RandomPerspective(p=p_affine, distortion_scale=max_warp), kaug.ColorJitter(p=p_lighting, brightness=max_lighting, contrast=max_lighting), # TODO: the kornia transforms work on batches and so by default they # add a batch dimension. Until I work out how to apply transform by # batches (and only while training) I will just keep this here to # remove the batch dimension again GetItemTransform(), ]
def kornia_list(MAGN: int = 4): """ Returns standard list of kornia transforms, each with magnitude `MAGN`. Args: MAGN (int): Magnitude of each transform in the returned list. """ transform_list = [ # SPATIAL K.RandomHorizontalFlip(p=1), K.RandomRotation(degrees=90., p=1), K.RandomAffine(degrees=MAGN * 5., shear=MAGN / 5, translate=MAGN / 20, p=1), K.RandomPerspective(distortion_scale=MAGN / 25, p=1), # PIXEL-LEVEL K.ColorJitter(brightness=MAGN / 30, p=1), # brightness K.ColorJitter(saturation=MAGN / 30, p=1), # saturation K.ColorJitter(contrast=MAGN / 30, p=1), # contrast K.ColorJitter(hue=MAGN / 30, p=1), # hue K.ColorJitter(p=0), # identity K.RandomMotionBlur(kernel_size=2 * (MAGN // 3) + 1, angle=MAGN, direction=1., p=1), K.RandomErasing(scale=(MAGN / 100, MAGN / 50), ratio=(MAGN / 20, MAGN), p=1), ] return transform_list
def test_exception(self, error_param): with pytest.raises(Exception): # AssertError and NotImplementedError K.PatchSequential( 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.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.ColorJitter(0.1, 0.1, 0.1, 0.1), **error_param, )
def __init__(self, cutn): super().__init__() self.cutn = cutn self.augs = nn.Sequential( K.RandomHorizontalFlip(p=0.5), K.ColorJitter(hue=0.01, saturation=0.01, p=0.7), #K.RandomSolarize(0.01, 0.01, p=0.7), K.RandomSharpness(0.3, p=0.4), K.RandomAffine(degrees=30, translate=0.1, p=0.8, padding_mode='border'), K.RandomPerspective(0.2, p=0.4), ) self.noise_fac = 0.1
def test_forward(self, shape, padding, patchwise_apply, same_on_batch, keepdim, random_apply, device, dtype): try: # skip wrong param settings. seq = K.PatchSequential( 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.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.ColorJitter(0.1, 0.1, 0.1, 0.1), grid_size=(2, 2), padding=padding, patchwise_apply=patchwise_apply, same_on_batch=same_on_batch, keepdim=keepdim, random_apply=random_apply, ) except: return input = torch.randn(*shape, device=device, dtype=dtype) trans = torch.randn(shape[0], 3, 3, device=device, dtype=dtype) out = seq(input) assert out.shape[-3:] == input.shape[-3:] out = seq((input, trans)) assert out[0].shape[-3:] == input.shape[-3:] assert out[1].shape == trans.shape reproducibility_test(input, seq)
def __init__(self, viz: bool = False): super().__init__() self.viz = viz '''self.geometric = [ K.augmentation.RandomAffine(60., p=0.75), ]''' self.augmentations = nn.Sequential( augmentation.RandomRotation(degrees=30.), augmentation.RandomPerspective(distortion_scale=0.4), augmentation.RandomResizedCrop((224, 224)), augmentation.RandomHorizontalFlip(p=0.5), augmentation.RandomVerticalFlip(p=0.5), # K.augmentation.GaussianBlur((3, 3), (0.1, 2.0), p=1.0), # K.augmentation.ColorJitter(0.01, 0.01, 0.01, 0.01, p=0.25), ) self.denorm = augmentation.Denormalize(Tensor(DATASET_IMAGE_MEAN), Tensor(DATASET_IMAGE_STD))
def __init__(self, cut_size, cutn, cut_pow=1.): super().__init__() self.cut_size = cut_size self.cutn = cutn self.cut_pow = cut_pow self.augs = nn.Sequential( # K.RandomHorizontalFlip(p=0.5), # K.RandomVerticalFlip(p=0.5), # K.RandomSolarize(0.01, 0.01, p=0.7), # K.RandomSharpness(0.3, p=0.4), # K.RandomResizedCrop( # size=(self.cut_size, self.cut_size), # scale=(0.1, 1), ratio=(0.75, 1.333), # cropping_mode="resample", p=0.5 # ), # K.RandomCrop( # size=(self.cut_size, self.cut_size), p=0.5 # ), K.RandomAffine( degrees=15, translate=0.1, p=0.7, padding_mode="border" ), K.RandomPerspective(0.7, p=0.7), K.ColorJitter(hue=0.1, saturation=0.1, p=0.7), K.RandomErasing( (.1, .4), (.3, 1/.3), same_on_batch=True, p=0.7 ), ) self.noise_fac = 0.1 self.av_pool = nn.AdaptiveAvgPool2d( (self.cut_size, self.cut_size) ) self.max_pool = nn.AdaptiveMaxPool2d( (self.cut_size, self.cut_size) )
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))
transform = { "per_sample_transform": nn.Sequential( ApplyToKeys( DataKeys.INPUT, nn.Sequential( torchvision.transforms.ToTensor(), Kg.Resize((196, 196)), # SPATIAL Ka.RandomHorizontalFlip(p=0.25), Ka.RandomRotation(degrees=90.0, p=0.25), Ka.RandomAffine(degrees=1 * 5.0, shear=1 / 5, translate=1 / 20, p=0.25), Ka.RandomPerspective(distortion_scale=1 / 25, p=0.25), # PIXEL-LEVEL Ka.ColorJitter(brightness=1 / 30, p=0.25), # brightness Ka.ColorJitter(saturation=1 / 30, p=0.25), # saturation Ka.ColorJitter(contrast=1 / 30, p=0.25), # contrast Ka.ColorJitter(hue=1 / 30, p=0.25), # hue Ka.RandomMotionBlur(kernel_size=2 * (4 // 3) + 1, angle=1, direction=1.0, p=0.25), Ka.RandomErasing(scale=(1 / 100, 1 / 50), ratio=(1 / 20, 1), p=0.25), ), ), ApplyToKeys(DataKeys.TARGET, torch.as_tensor),
'vgg16': models.vgg16, 'vgg16_bn': models.vgg16_bn, 'squeezenet': models.squeezenet1_0, 'densenet': models.densenet161, 'shufflenet': models.shufflenet_v2_x1_0, 'mobilenet': models.mobilenet_v2, 'resnext50_32x4d': models.resnext50_32x4d, 'mnasnet': models.mnasnet1_0, } DS_NAME_TO_CORRUPTION_PATH = { 'cifar10': '/mnt/ssd/datasets/CIFAR-10-C', 'cifar10_boosted': '/mnt/ssd/datasets/CIFAR-10-boosted-C' } THREE_D_CORRUPTIONS = nn.Sequential(K.RandomPerspective(p=0.5)) SCENE_DICT = lambda custom_file: { "type": "scene", "myintegrator": { "type": "aov", "aovs": "chungus:uv", "sub_integrator": { "type": "direct" } }, "myemitter": { "type": "constant", "radiance": 0.7 }, "myobject": {