def hard_spatial_augmentations(image_size: Tuple[int, int], rot_angle=45): return A.Compose([ A.OneOf([ A.NoOp(), A.RandomGridShuffle(grid=(4, 4)), A.RandomGridShuffle(grid=(3, 3)), A.RandomGridShuffle(grid=(2, 2)), ]), A.MaskDropout(max_objects=10), A.OneOf([ A.ShiftScaleRotate(scale_limit=0.1, rotate_limit=rot_angle, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0), A.NoOp(), ]), A.OneOf([ A.ElasticTransform(border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0), A.GridDistortion(border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0), A.NoOp(), ]), # D4 A.Compose([A.Transpose(), A.RandomRotate90()]), ])
def subcase3_cls_train_augs(name, **kwargs): return [ A.Compose([ Colorout(fill=255, same=True), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), A.Compose([ Colorout(fill=255, same=False), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), A.Compose([ A.RandomBrightness(0.1), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), A.Compose([ A.RandomGamma((100, 150)), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), A.Compose([ A.HueSaturationValue(), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), ]
def __init__(self, outputs=6): super().__init__() self.net = models.resnet34(True) self.linear = Sequential(ReLU(), Dropout(), Linear(1000, outputs)) df = pd.read_csv( "/home/dipet/kaggle/prostate/input/prostate-cancer-grade-assessment/train.csv" ) self.train_df, self.valid_df = train_test_split(df, test_size=0.2) self.data_dir = "/datasets/panda/train_128_100" self.train_transforms = A.Compose([ A.InvertImg(p=1), A.RandomGridShuffle(grid=(10, 10)), A.RandomScale(0.1), A.PadIfNeeded(1280, 1280), A.RandomSizedCrop([1000, 1280], 1280, 1280), A.Flip(), A.Rotate(90), A.RandomBrightnessContrast(0.02, 0.02), A.HueSaturationValue(0, 10, 10), A.Normalize(mean, std, 1), ]) self.valid_transforms = A.Compose([ A.InvertImg(p=1), A.Normalize(mean, std, 1), ])
def __init__(self, outputs=6): super().__init__() self.net = models.resnet34(True) self.linear = Linear(1000, outputs) df = pd.read_csv("/home/dipet/kaggle/prostate/input/prostate-cancer-grade-assessment/train.csv") self.train_df, self.valid_df = train_test_split(df, test_size=0.2) self.data_dir = "/datasets/panda/train_64_100" self.train_transforms = A.Compose( [ A.Compose( [ A.OneOf([A.GaussNoise(), A.MultiplicativeNoise(elementwise=True)]), A.RandomBrightnessContrast(0.02, 0.02), A.HueSaturationValue(0, 10, 10), A.Flip(), A.RandomGridShuffle(grid=(10, 10)), A.GridDistortion(), A.Rotate() ], p=0.5, ), A.ToFloat(), ] ) self.valid_transforms = A.Compose([A.ToFloat()])
def on_epoch_start(self, trainer, pl_module): if pl_module.hparams.progressive: ind = int(trainer.current_epoch / 5) if trainer.current_epoch < 5*3 else 3 prog = [512, 1024, 1536, 2048] #batch = [32, 16, 8, 4] batch = [64, 32, 16, 8] # For Progressive Resizing train_transform = A.Compose([ A.RandomResizedCrop(height=prog[ind], width=prog[ind], scale=(0.8, 1.0), ratio=(1, 1), interpolation=1, always_apply=False, p=1.0), A.Flip(always_apply=False, p=0.5), A.RandomGridShuffle(grid=(4, 4), always_apply=False, p=1.0), A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=10, val_shift_limit=10, always_apply=False, p=0.5), A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, brightness_by_max=True, always_apply=False, p=0.5), A.GaussNoise(var_limit=(10.0, 50.0), mean=0, always_apply=False, p=0.5), #A.Rotate(limit=90, interpolation=1, border_mode=4, value=None, mask_value=None, always_apply=False, p=0.5), A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.0, rotate_limit=45, interpolation=1, border_mode=4, value=255, mask_value=None, always_apply=False, p=0.5), A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, always_apply=False, p=1.0), A.Cutout(num_holes=8, max_h_size=8, max_w_size=8, fill_value=0, always_apply=False, p=0.5), ]) valid_transform = A.Compose([A.Resize(height=prog[ind], width=prog[ind], interpolation=1, always_apply=False, p=1.0), A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, always_apply=False, p=1.0) ]) pl_module.train_dataset = PANDADataset(pl_module.train_df, pl_module.hparams.data_dir, pl_module.hparams.image_format, transform=train_transform, tile=pl_module.hparams.tile, layer=pl_module.hparams.image_layer) pl_module.val_dataset = PANDADataset(pl_module.val_df, pl_module.hparams.data_dir, pl_module.hparams.image_format, transform=valid_transform, tile=pl_module.hparams.tile, layer=pl_module.hparams.image_layer) trainer.train_dataloader = DataLoader(pl_module.train_dataset, batch_size=batch[ind], shuffle=True, num_workers=4, drop_last=True) trainer.val_dataloaders = [DataLoader(pl_module.val_dataset, batch_size=batch[ind], shuffle=True, num_workers=4, drop_last=True)] trainer.num_training_batches = len(trainer.train_dataloader)#float('inf') trainer.num_val_batches = len(trainer.val_dataloaders[0])#float('inf') trainer.val_check_batch = trainer.num_training_batches
def get_hard_augmentations(image_size): return A.Compose([ A.OneOf([ A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1, rotate_limit=45, border_mode=cv2.BORDER_CONSTANT, value=0), A.ElasticTransform(alpha_affine=0, alpha=35, sigma=5, border_mode=cv2.BORDER_CONSTANT, value=0), A.OpticalDistortion(distort_limit=0.11, shift_limit=0.15, border_mode=cv2.BORDER_CONSTANT, value=0), A.GridDistortion(border_mode=cv2.BORDER_CONSTANT, value=0), A.NoOp() ]), A.OneOf([ A.RandomSizedCrop(min_max_height=(int(image_size[0] * 0.75), image_size[0]), height=image_size[0], width=image_size[1], p=0.3), A.NoOp() ]), A.ISONoise(p=0.5), # Brightness/contrast augmentations A.OneOf([ A.RandomBrightnessContrast(brightness_limit=0.5, contrast_limit=0.4), IndependentRandomBrightnessContrast(brightness_limit=0.25, contrast_limit=0.24), A.RandomGamma(gamma_limit=(50, 150)), A.NoOp() ]), A.OneOf([ A.RGBShift(r_shift_limit=40, b_shift_limit=30, g_shift_limit=30), A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=10), A.ToGray(p=0.2), A.NoOp() ]), A.ChannelDropout(), A.RandomGridShuffle(p=0.3), # D4 A.Compose([ A.RandomRotate90(), A.Transpose() ]) ])
def medium_spatial_augmentations(image_size: Tuple[int, int], no_mask_dropout=False): return A.Compose([ A.OneOf([ A.NoOp(p=0.8), A.RandomGridShuffle(grid=(4, 4), p=0.2), A.RandomGridShuffle(grid=(3, 3), p=0.2), A.RandomGridShuffle(grid=(2, 2), p=0.2), ], p=1), A.ShiftScaleRotate(scale_limit=0.1, rotate_limit=10, border_mode=cv2.BORDER_CONSTANT), # D4 Augmentations A.Compose([A.Transpose(), A.RandomRotate90()]), # Spatial-preserving augmentations: A.RandomBrightnessContrast(), A.NoOp() if no_mask_dropout else A.MaskDropout(max_objects=10), ])
def subcase5_cls_train_augs(name, **kwargs): return [ A.Compose([ A.RandomBrightness(), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), A.Compose([ A.RandomGamma(), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), A.Compose([ A.HueSaturationValue(), A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), A.Compose([ A.RandomGridShuffle(grid=(4, 4)), ], p=1.0), ]
def __init__(self, size): if isinstance(size,int): size = (size,size) self.A_Compose = A.Compose([A.RandomGridShuffle(always_apply=False, p=0.05, grid=(2, 2)), A.transforms.GridDistortion(num_steps=5, distort_limit=0.4, p=0.5), A.HorizontalFlip(p=0.5), A.RandomResizedCrop(always_apply=True, p=1.0, height=size[0], width=size[0], scale=(0.75, 1), ratio=(0.98, 1.02), interpolation=3),#ONLY 3 SEEMS TO WORK A.transforms.ColorJitter(brightness=0.05, contrast=0.07, saturation=0.04, hue=0.07, always_apply=False, p=0.6),#HSV random A.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ToTensorV2() ])
def __init__(self, cfg, data, transform=None, train=True): self.cfg = cfg self.train = train self.data = data self.transform = transform if self.cfg.gridshuffle: self.gridshuffle = A.OneOf( [ A.RandomGridShuffle((2, 2), p=1.0), # A.RandomGridShuffle((4, 2), p=1.0), ], p=0.5) else: self.gridshuffle = None
def __init__(self, mode=None, resize=224): # assert type(resize) == list, f'resize type is not list ' if mode == 'train_tfms_mask': self.transform = A.Compose([ A.OneOf([ A.Perspective(p=1.0), A.Rotate(limit=20, p=1.0, border_mode=1), ], p=0.5), A.OneOf([ A.RandomBrightness(p=1.0), A.HueSaturationValue(p=1.0), A.RandomContrast(p=1.0), ], p=0.5), A.Compose([ A.Resize(resize, resize), A.Normalize(), ]) ]) elif mode == 'train_age_gender': self.transform = A.Compose([ A.Rotate(limit=20, p=0.5, border_mode=1), A.OneOf( [ A.RandomGridShuffle(grid=(2, 2), p=1.0), # not using for gender # A.RandomGridShuffle(grid=(4, 2), p=1.0), A.Perspective(p=1.0) ], p=0.5), A.GaussNoise(p=0.5), A.Compose([ A.Resize(resize, resize), A.Normalize(), ]) ]) # elif mode = elif mode == 'valid_tfms': self.transform = A.Compose([ A.Resize(resize, resize), A.Normalize(), ])
def get_train_transforms_v2(): return A.Compose( [ A.Resize(config.SIZE, config.SIZE), A.OneOf( [ A.GaussNoise(var_limit=1.15), A.MultiplicativeNoise(multiplier=1.1), ], p=0.2, ), A.RandomBrightnessContrast( contrast_limit=0.12, brightness_limit=0.12, p=0.2 ), A.OpticalDistortion(distort_limit=0.07, shift_limit=0.07, p=0.25), A.GaussianBlur(p=0.15), A.RandomGridShuffle(grid=(4, 4), p=0.2), ToTensorV2(), ] )
def hard_augmentations(): return A.Compose([ A.RandomRotate90(), A.Transpose(), A.RandomGridShuffle(), A.ShiftScaleRotate(scale_limit=0.1, rotate_limit=45, border_mode=cv2.BORDER_CONSTANT, mask_value=0, value=0), A.ElasticTransform(border_mode=cv2.BORDER_CONSTANT, alpha_affine=5, mask_value=0, value=0), # Add occasion blur A.OneOf([ A.GaussianBlur(), A.GaussNoise(), A.IAAAdditiveGaussianNoise(), A.NoOp() ]), # D4 Augmentations A.OneOf([A.CoarseDropout(), A.MaskDropout(max_objects=10), A.NoOp()]), # Spatial-preserving augmentations: A.OneOf([ A.RandomBrightnessContrast(brightness_by_max=True), A.CLAHE(), A.HueSaturationValue(), A.RGBShift(), A.RandomGamma(), A.NoOp(), ]), # Weather effects A.OneOf([ A.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), A.NoOp() ]), A.Normalize(), ])
def hard_augmentations(mask_dropout=True) -> List[A.DualTransform]: return [ # D4 Augmentations A.RandomRotate90(p=1), A.Transpose(p=0.5), # Spatial augmentations A.OneOf( [ A.ShiftScaleRotate(scale_limit=0.2, rotate_limit=45, border_mode=cv2.BORDER_REFLECT101), A.ElasticTransform(border_mode=cv2.BORDER_REFLECT101, alpha_affine=5), ] ), # Color augmentations A.OneOf( [ A.RandomBrightnessContrast(brightness_by_max=True), A.CLAHE(), A.FancyPCA(), A.HueSaturationValue(), A.RGBShift(), A.RandomGamma(), ] ), # Dropout & Shuffle A.OneOf( [ A.RandomGridShuffle(), A.CoarseDropout(), A.MaskDropout(max_objects=2, mask_fill_value=0) if mask_dropout else A.NoOp(), ] ), # Add occasion blur A.OneOf([A.GaussianBlur(), A.GaussNoise(), A.IAAAdditiveGaussianNoise()]), # Weather effects A.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), ]
#scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.33, patience=2, verbose=True) scheduler = OneCycleLRWithWarmup(optimizer, num_steps=EPOCHS, lr_range=(0.003, 0.0001), warmup_steps=1) LOGDIR = os.path.join(BASE_LOGDIR, f"fold{i}") train_files = image_files[train_inds] valid_files = image_files[test_inds] train_ds = tu.ITSDataset(train_files, train_transforms=[ albu.VerticalFlip(), albu.HorizontalFlip(), albu.ShiftScaleRotate(), albu.RandomGamma(), albu.RandomGridShuffle(), albu.HueSaturationValue() ]) val_ds = tu.ITSDataset(valid_files) train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE, num_workers=6, shuffle=True) val_loader = DataLoader(val_ds, batch_size=BATCH_SIZE, num_workers=6, shuffle=False) loaders = OrderedDict() loaders["train"] = train_loader
def get_transform_imagenet(use_albu_aug): if use_albu_aug: train_transform = al.Compose([ # al.Flip(p=0.5), al.Resize(256, 256, interpolation=2), al.RandomResizedCrop(224, 224, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=2), al.HorizontalFlip(), al.OneOf( [ al.OneOf( [ al.ShiftScaleRotate( border_mode=cv2.BORDER_CONSTANT, rotate_limit=30), # , p=0.05), al.OpticalDistortion( border_mode=cv2.BORDER_CONSTANT, distort_limit=5.0, shift_limit=0.1), # , p=0.05), al.GridDistortion(border_mode=cv2.BORDER_CONSTANT ), # , p=0.05), al.ElasticTransform( border_mode=cv2.BORDER_CONSTANT, alpha_affine=15), # , p=0.05), ], p=0.1), al.OneOf( [ al.RandomGamma(), # p=0.05), al.HueSaturationValue(), # p=0.05), al.RGBShift(), # p=0.05), al.CLAHE(), # p=0.05), al.ChannelShuffle(), # p=0.05), al.InvertImg(), # p=0.05), ], p=0.1), al.OneOf( [ al.RandomSnow(), # p=0.05), al.RandomRain(), # p=0.05), al.RandomFog(), # p=0.05), al.RandomSunFlare(num_flare_circles_lower=1, num_flare_circles_upper=2, src_radius=110), # p=0.05, ), al.RandomShadow(), # p=0.05), ], p=0.1), al.RandomBrightnessContrast(p=0.1), al.OneOf( [ al.GaussNoise(), # p=0.05), al.ISONoise(), # p=0.05), al.MultiplicativeNoise(), # p=0.05), ], p=0.1), al.OneOf( [ al.ToGray(), # p=0.05), al.ToSepia(), # p=0.05), al.Solarize(), # p=0.05), al.Equalize(), # p=0.05), al.Posterize(), # p=0.05), al.FancyPCA(), # p=0.05), ], p=0.1), al.OneOf( [ # al.MotionBlur(blur_limit=1), al.Blur(blur_limit=[3, 5]), al.MedianBlur(blur_limit=[3, 5]), al.GaussianBlur(blur_limit=[3, 5]), ], p=0.1), al.OneOf( [ al.CoarseDropout(), # p=0.05), al.Cutout(), # p=0.05), al.GridDropout(), # p=0.05), al.ChannelDropout(), # p=0.05), al.RandomGridShuffle(), # p=0.05), ], p=0.1), al.OneOf( [ al.Downscale(), # p=0.1), al.ImageCompression(quality_lower=60), # , p=0.1), ], p=0.1), ], p=0.5), al.Normalize(), ToTensorV2() ]) else: train_transform = transforms.Compose([ transforms.Resize(256), transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) test_transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), ]) if use_albu_aug: train_transform = MultiDataTransformAlbu(train_transform) else: train_transform = MultiDataTransform(train_transform) return train_transform, test_transform
def get_aug(aug_type="val", size=256): """aug_type (str): one of `val`, `test`, `light`, `medium`, `hard` size (int): final size of the crop""" NORM_TO_TENSOR = albu.Compose([albu.Normalize(), ToTensor()]) CROP_AUG = albu.RandomResizedCrop(size, size, scale=(0.05, 0.4)) VAL_AUG = albu.Compose([ albu.CenterCrop(size, size), NORM_TO_TENSOR, ]) TEST_AUG = albu.Compose([ albu.Resize(size, size), NORM_TO_TENSOR, ]) LIGHT_AUG = albu.Compose([ CROP_AUG, albu.Flip(), albu.RandomRotate90(), NORM_TO_TENSOR, ]) MEDIUM_AUG = albu.Compose([ CROP_AUG, albu.Flip(), albu.ShiftScaleRotate(), # border_mode=cv2.BORDER_CONSTANT # Add occasion blur/sharpening albu.OneOf([albu.GaussianBlur(), albu.IAASharpen(), albu.NoOp()]), # Spatial-preserving augmentations: # albu.OneOf([albu.CoarseDropout(), albu.MaskDropout(max_objects=5), albu.NoOp()]), albu.GaussNoise(), albu.OneOf([ albu.RandomBrightnessContrast(), albu.CLAHE(), albu.HueSaturationValue(), albu.RGBShift(), albu.RandomGamma(), ]), # Weather effects albu.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), NORM_TO_TENSOR, ]) HARD_AUG = albu.Compose([ CROP_AUG, albu.RandomRotate90(), albu.Transpose(), albu.RandomGridShuffle(p=0.2), albu.ShiftScaleRotate(scale_limit=0.1, rotate_limit=45, p=0.2), albu.ElasticTransform(alpha_affine=5, p=0.2), # Add occasion blur albu.OneOf([ albu.GaussianBlur(), albu.GaussNoise(), albu.IAAAdditiveGaussianNoise(), albu.NoOp() ]), # D4 Augmentations albu.OneOf([albu.CoarseDropout(), albu.NoOp()]), # Spatial-preserving augmentations: albu.OneOf([ albu.RandomBrightnessContrast(brightness_by_max=True), albu.CLAHE(), albu.HueSaturationValue(), albu.RGBShift(), albu.RandomGamma(), albu.NoOp(), ]), # Weather effects albu.OneOf([ albu.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), albu.NoOp() ]), NORM_TO_TENSOR, ]) types = { "val": VAL_AUG, "test": TEST_AUG, "light": LIGHT_AUG, "medium": MEDIUM_AUG, "hard": HARD_AUG, } return types[aug_type]
def set_transforms(self) -> dict: d4_tansforms = [ A.SmallestMaxSize(self.img_size[0], interpolation=0, p=1.), # D4 Group augmentations A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.RandomRotate90(p=0.5), A.Transpose(p=0.5), ] tensor_norm = [ ToTensor(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ] geometric = [ A.SmallestMaxSize(self.img_size[0], interpolation=0, p=1.), A.ShiftScaleRotate(shift_limit=self.shift_limit, self.scale_limit, rotate_limit=self.rotate_limit, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=0.5), # crop and resize A.RandomSizedCrop( (self.crop_size[0], min(self.crop_size[1], self.img_size[0], self.img_size[1])), self.img_size[0], self.img_size[1], w2h_ratio=1.0, interpolation=cv2.INTER_LINEAR, always_apply=False, p=0.2), ] resize_crop = [ A.SmallestMaxSize(max(self.img_size[0], self.img_size[1]), interpolation=0, p=1.), A.RandomCrop(self.img_size[0], self.img_size[1], p=1.), ] train_light = [ A.SmallestMaxSize(self.img_size[0], interpolation=0, p=1.), A.RandomCrop(self.img_size[0], self.img_size[1], p=1.), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(p=0.5), ] train_medium = train_light.append([ A.ShiftScaleRotate(shift_limit=self.shift_limit, self.scale_limit, rotate_limit=self.rotate_limit, p=0.5), A.OneOf([ A.RandomBrightnessContrast(p=0.7), A.Equalize(p=0.3), A.HueSaturationValue(p=0.5), A.RGBShift(p=0.5), A.RandomGamma(p=0.4), A.ChannelShuffle(p=0.05), ], p=0.9), A.OneOf([ A.GaussNoise(p=0.5), A.ISONoise(p=0.5), A.MultiplicativeNoise(0.5), ], p=0.2), ]) valid_ade = [ A.SmallestMaxSize(self.img_size, p=1.), A.Lambda(name="Pad32", image=pad_x32, mask=pad_x32), ] # from bloodaxe # https://github.com/BloodAxe/Catalyst-Inria-Segmentation-Example/blob/master/inria/augmentations.py crop = [ #(image_size: Tuple[int, int], min_scale=0.75, max_scale=1.25, input_size=5000): A.OneOrOther( A.RandomSizedCrop((self.crop_size[0], min(self.crop_size[1], self.img_size[0], self.img_size[1])), self.img_size[0], self.img_size[1]), A.CropNonEmptyMaskIfExists(self.img_size[0], self.img_size[1]), ) ] safe_augmentations = [A.HorizontalFlip(), A.RandomBrightnessContrast()] light_augmentations = [ A.HorizontalFlip(), A.RandomBrightnessContrast(), A.OneOf([ A.ShiftScaleRotate(scale_limit=self.scale_limit, rotate_limit=self.rotate_limit, border_mode=cv2.BORDER_CONSTANT), A.IAAAffine(), A.IAAPerspective(), A.NoOp() ]), A.HueSaturationValue(), ] medium_augmentations = [ A.HorizontalFlip(), A.ShiftScaleRotate(scale_limit=self.scale_limit, rotate_limit=self.rotate_limit, border_mode=cv2.BORDER_CONSTANT), # Add occasion blur/sharpening A.OneOf([A.GaussianBlur(), A.IAASharpen(), A.NoOp()]), # Spatial-preserving augmentations: A.OneOf( [A.CoarseDropout(), A.MaskDropout(max_objects=5), A.NoOp()]), A.GaussNoise(), A.OneOf([ A.RandomBrightnessContrast(), A.CLAHE(), A.HueSaturationValue(), A.RGBShift(), A.RandomGamma() ]), # Weather effects A.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), ] hard_augmentations = [ A.RandomRotate90(), A.Transpose(), A.RandomGridShuffle(), A.ShiftScaleRotate(scale_limit=self.scale_limit, rotate_limit=self.rotate_limit, border_mode=cv2.BORDER_CONSTANT, mask_value=0, value=0), A.ElasticTransform(border_mode=cv2.BORDER_CONSTANT, alpha_affine=5, mask_value=0, value=0), # Add occasion blur A.OneOf([ A.GaussianBlur(), A.GaussNoise(), A.IAAAdditiveGaussianNoise(), A.NoOp() ]), # D4 Augmentations A.OneOf( [A.CoarseDropout(), A.MaskDropout(max_objects=10), A.NoOp()]), # Spatial-preserving augmentations: A.OneOf([ A.RandomBrightnessContrast(brightness_by_max=True), A.HueSaturationValue(), A.RGBShift(), A.RandomGamma(), A.NoOp(), ]), # Weather effects A.OneOf([ A.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), A.NoOp() ]), ] TRANSFORMS = { "d4": d4_tansforms, "resize_crop": resize_crop, "geometric": geometric, "light": train_light, "medium": train_medium, "ade_valid": valid_ade, "flip_bright": safe_augmentations, "crop": crop, "inria_light": light_augmentations, "inria_medium": medium_augmentations, "inria_hard": hard_augmentations, "inria_valid": safe_augmentations, } return TRANSFORMS def get_transforms(self): augs = [] if self.augs_name: augs = self.set_transforms[self.augs_name] augs.append( A.Resize(height=self.img_size[0], width=self.img_size[1], p=1.0)) if self.use_d4: augs.append(self.set_transforms["d4"]) if self.vflip: augs.append(A.VerticalFlip(p=0.5)) if self.hflip: augs.append(A.HorizontalFlip(p=0.5)) if self.normalise: augs.append(A.Normalize()) return A.Compose(augs)
A.GaussNoise(), A.OneOf([ A.RandomBrightnessContrast(), A.CLAHE(), A.HueSaturationValue(), A.RGBShift(), A.RandomGamma() ]), # Weather effects A.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), ]) hard_augmentations = A.Compose([ A.RandomRotate90(), A.Transpose(), A.RandomGridShuffle(), A.ShiftScaleRotate(scale_limit=0.3, rotate_limit=7, border_mode=cv2.BORDER_CONSTANT, mask_value=0, value=0), A.ElasticTransform(border_mode=cv2.BORDER_CONSTANT, alpha_affine=5, mask_value=0, value=0), # Add occasion blur A.OneOf([ A.GaussianBlur(), A.GaussNoise(), A.IAAAdditiveGaussianNoise(), A.NoOp()
def get_train_transforms_mmdetection(input_size, use_crop=False, use_no_color_aug=False, use_center_crop=False, center_crop_ratio=0.9, use_gray=False): if isinstance(input_size, int): input_size = (input_size[0], input_size[1]) return al.Compose([ al.RandomResizedCrop(height=input_size[0], width=input_size[1], scale=(0.4, 1.0), interpolation=0, p=0.5), al.Resize(input_size[0], input_size[1], p=1.0), al.HorizontalFlip(p=0.5), al.OneOf([ al.ShiftScaleRotate(border_mode=0, shift_limit=(-0.2, 0.2), scale_limit=(-0.2, 0.2), rotate_limit=(-20, 20)), al.OpticalDistortion(border_mode=0, distort_limit=[-0.5, 0.5], shift_limit=[-0.5, 0.5]), al.GridDistortion( num_steps=5, distort_limit=[-0., 0.3], border_mode=0), al.ElasticTransform(border_mode=0), al.IAAPerspective(), al.RandomGridShuffle() ], p=0.1), al.Rotate(limit=(-25, 25), border_mode=0, p=0.1), al.OneOf([ al.RandomBrightnessContrast(brightness_limit=(-0.2, 0.2), contrast_limit=(-0.2, 0.2)), al.HueSaturationValue(hue_shift_limit=(-20, 20), sat_shift_limit=(-30, 30), val_shift_limit=(-20, 20)), al.RandomGamma(gamma_limit=(30, 150)), al.RGBShift(), al.CLAHE(clip_limit=(1, 15)), al.ChannelShuffle(), al.InvertImg(), ], p=0.1), al.RandomSnow(p=0.05), al.RandomRain(p=0.05), al.RandomFog(p=0.05), al.RandomSunFlare(num_flare_circles_lower=1, num_flare_circles_upper=2, src_radius=110, p=0.05), al.RandomShadow(p=0.05), al.GaussNoise(var_limit=(10, 20), p=0.05), al.ISONoise(color_shift=(0, 15), p=0.05), al.MultiplicativeNoise(p=0.05), al.OneOf([ al.ToGray(p=1. if use_gray else 0.05), al.ToSepia(p=0.05), al.Solarize(p=0.05), al.Equalize(p=0.05), al.Posterize(p=0.05), al.FancyPCA(p=0.05), ], p=0.05), al.OneOf([ al.MotionBlur(blur_limit=(3, 7)), al.Blur(blur_limit=(3, 7)), al.MedianBlur(blur_limit=3), al.GaussianBlur(blur_limit=3), ], p=0.05), al.CoarseDropout(p=0.05), al.Cutout(num_holes=30, max_h_size=37, max_w_size=37, fill_value=0, p=0.05), al.GridDropout(p=0.05), al.ChannelDropout(p=0.05), al.Downscale(scale_min=0.5, scale_max=0.9, p=0.1), al.ImageCompression(quality_lower=60, p=0.2), al.Normalize(), ToTensorV2() ])
def get_hard_augmentations_v2(image_size): return A.Compose([ A.OneOf([ A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1, rotate_limit=45, border_mode=cv2.BORDER_CONSTANT, value=0), A.ElasticTransform(alpha_affine=0, alpha=35, sigma=5, border_mode=cv2.BORDER_CONSTANT, value=0), A.OpticalDistortion(distort_limit=0.11, shift_limit=0.15, border_mode=cv2.BORDER_CONSTANT, value=0), A.GridDistortion(border_mode=cv2.BORDER_CONSTANT, value=0), A.NoOp() ]), A.OneOf([ ZeroTopAndBottom(p=0.3), A.RandomSizedCrop(min_max_height=(int(image_size[0] * 0.75), image_size[0]), height=image_size[0], width=image_size[1], p=0.3), A.NoOp() ]), A.ISONoise(p=0.5), A.JpegCompression(p=0.3, quality_lower=75), # Brightness/contrast augmentations A.OneOf([ A.RandomBrightnessContrast(brightness_limit=0.5, contrast_limit=0.4), IndependentRandomBrightnessContrast(brightness_limit=0.25, contrast_limit=0.24), A.RandomGamma(gamma_limit=(50, 150)), A.NoOp() ]), A.OneOf([ FancyPCA(alpha_std=6), A.RGBShift(r_shift_limit=40, b_shift_limit=30, g_shift_limit=30), A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=10), A.ToGray(p=0.2), A.NoOp() ]), # Intentionally destroy image quality and assign 0 class in this case # A.Compose([ # BrightnessContrastDestroy(p=0.1), # A.OneOf([ # MakeTooBlurry(), # MakeTooBlurryMedian(), # A.NoOp() # ], p=0.1), # ]), # Add preprocessing method as an augmentation ChannelIndependentCLAHE(p=0.5), A.OneOf([ A.ChannelDropout(p=0.2), A.CoarseDropout(p=0.1, max_holes=2, max_width=256, max_height=256, min_height=16, min_width=16), A.NoOp() ]), A.RandomGridShuffle(p=0.3), DiagnosisNoise(p=0.2), # D4 A.Compose([A.RandomRotate90(), A.Transpose()]) ])
def get_train_transforms_atopy(input_size, use_crop=False, use_no_color_aug=False): if use_crop: resize = [ al.Resize(int(input_size * 1.2), int(input_size * 1.2)), al.RandomSizedCrop(min_max_height=(int(input_size * 0.6), int(input_size * 1.2)), height=input_size, width=input_size) ] else: resize = [al.Resize(input_size, input_size)] return al.Compose(resize + [ al.Flip(p=0.5), al.OneOf([ al.RandomRotate90(), al.Rotate(limit=180), ], p=0.5), al.OneOf([ al.ShiftScaleRotate(), al.OpticalDistortion(), al.GridDistortion(), al.ElasticTransform(), ], p=0.3), al.RandomGridShuffle(p=0.05), al.OneOf([ al.RandomGamma(), al.HueSaturationValue(), al.RGBShift(), al.CLAHE(), al.ChannelShuffle(), al.InvertImg(), ], p=0.1), al.RandomSnow(p=0.05), al.RandomRain(p=0.05), al.RandomFog(p=0.05), al.RandomSunFlare(p=0.05), al.RandomShadow(p=0.05), al.RandomBrightnessContrast(p=0.05), al.GaussNoise(p=0.2), al.ISONoise(p=0.2), al.MultiplicativeNoise(p=0.2), al.ToGray(p=0.05), al.ToSepia(p=0.05), al.Solarize(p=0.05), al.Equalize(p=0.05), al.Posterize(p=0.05), al.FancyPCA(p=0.05), al.OneOf([ al.MotionBlur(blur_limit=3), al.Blur(blur_limit=3), al.MedianBlur(blur_limit=3), al.GaussianBlur(blur_limit=3), ], p=0.05), al.CoarseDropout(p=0.05), al.Cutout(p=0.05), al.GridDropout(p=0.05), al.ChannelDropout(p=0.05), al.Downscale(p=0.1), al.ImageCompression(quality_lower=60, p=0.2), al.Normalize(), ToTensorV2() ])
def __init__(self, folds, img_height, img_width, mean, std): df = pd.read_csv('../input/dataset/train_folds.csv') df = df[['Image', 'label', 'kfold']] df = df[df.kfold.isin(folds)].reset_index(drop=True) self.image_ids = df.Image.values self.labels = df.label.values if len(folds) == 1: self.aug = albumentations.Compose([ albumentations.Resize(img_height, img_width), albumentations.Normalize(mean, std, always_apply=True) ]) else: self.aug = albumentations.Compose([ albumentations.Resize(img_height, img_width), albumentations.OneOf([ albumentations.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.1, rotate_limit=45), albumentations.Rotate(limit=5), albumentations.RandomGamma(), albumentations.RandomShadow(), albumentations.RandomGridShuffle(), albumentations.ElasticTransform(), albumentations.RGBShift(), ]), albumentations.OneOf([ albumentations.OneOf([ albumentations.Blur(), albumentations.MedianBlur(), albumentations.MotionBlur(), albumentations.GaussianBlur(), ]), albumentations.OneOf([ albumentations.GaussNoise(), albumentations.IAAAdditiveGaussianNoise(), albumentations.ISONoise() ]), ]), albumentations.OneOf([ albumentations.RandomBrightness(), albumentations.RandomContrast(), albumentations.RandomBrightnessContrast(), ]), albumentations.OneOf([ albumentations.OneOf([ albumentations.Cutout(), albumentations.CoarseDropout(), albumentations.GridDistortion(), albumentations.GridDropout(), albumentations.OpticalDistortion() ]), albumentations.OneOf([ albumentations.HorizontalFlip(), albumentations.VerticalFlip(), albumentations.RandomRotate90(), albumentations.Transpose() ]), ]), # albumentations.OneOf([ # albumentations.RandomSnow(), # albumentations.RandomRain(), # albumentations.RandomFog(), # ]), albumentations.Normalize(mean, std, always_apply=True) ])
image = glue_to_one_picture_from_coord( filepath, regions, window=window, K=K, layer=layer ) else: image = glue_to_one_picture_from_coord_lowlayer( filepath, regions, window=window, K=K, layer=layer ) image = cv2.resize(image, (outsize * outgrid, outsize * outgrid)) return image # %% [code] """ DataLoader """ augops = A.Compose([ A.RandomGridShuffle(grid=(4, 4)), A.Normalize() ], p=1.0) def augment_fn(img): ret = augops(force_apply=False, image=img) return ret['image'] def GetImage(tdir, idn, image_size, scale_factor, jitter=0): # 想定: scale_factor = 2.0 gen_images = 16 num_images = 16 num_column = int(np.sqrt(num_images)) imgpath = os.path.join(tdir, idn + '.tiff') img = LoadImage(imgpath, gen_images, scale_factor, layer=1, auto_ws=True, window=image_size // 4, stride=image_size // 4, jitter=jitter) img = 255 - img
def prepare_data(self): df = pd.read_csv(os.path.join(self.hparams.data_dir, 'train.csv')) skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=2020) for fold, (train_index, val_index) in enumerate( skf.split(df.values, df['isup_grade'])): df.loc[val_index, 'fold'] = int(fold) df['fold'] = df['fold'].astype(int) #print(df) self.train_df = df[df['fold'] != self.hparams.fold] self.val_df = df[df['fold'] == self.hparams.fold] #train_df, val_df = train_test_split(train_df, stratify=train_df['isup_grade']) train_transform = A.Compose([ A.RandomResizedCrop(height=self.hparams.image_size, width=self.hparams.image_size, scale=(0.8, 1.0), ratio=(1, 1), interpolation=1, always_apply=False, p=1.0), A.Flip(always_apply=False, p=0.5), A.RandomGridShuffle(grid=(4, 4), always_apply=False, p=1.0), A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=10, val_shift_limit=10, always_apply=False, p=0.5), A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, brightness_by_max=True, always_apply=False, p=0.5), A.GaussNoise(var_limit=(10.0, 50.0), mean=0, always_apply=False, p=0.5), #A.Rotate(limit=90, interpolation=1, border_mode=4, value=None, mask_value=None, always_apply=False, p=0.5), A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.0, rotate_limit=45, interpolation=1, border_mode=4, value=255, mask_value=None, always_apply=False, p=0.5), A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, always_apply=False, p=1.0), A.Cutout(num_holes=8, max_h_size=8, max_w_size=8, fill_value=0, always_apply=False, p=0.5), ]) valid_transform = A.Compose([ A.Resize(height=self.hparams.image_size, width=self.hparams.image_size, interpolation=1, always_apply=False, p=1.0), A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, always_apply=False, p=1.0) ]) self.train_dataset = PANDADataset(self.train_df, self.hparams.data_dir, self.hparams.image_format, transform=train_transform, tile=self.hparams.tile, layer=self.hparams.image_layer) self.val_dataset = PANDADataset(self.val_df, self.hparams.data_dir, self.hparams.image_format, transform=valid_transform, tile=self.hparams.tile, layer=self.hparams.image_layer)
def get_augmentation(version): if version == "v1": return { "train": A.Compose([ A.Flip(p=0.5), A.Rotate(p=0.5), A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v2": return { "train": A.Compose([ A.Flip(p=0.5), A.Rotate(p=0.5), A.OneOf([ A.RandomCrop(height=256, width=256, p=1), A.RandomCrop(height=384, width=384, p=1), A.RandomCrop(height=512, width=512, p=1), A.RandomCrop(height=640, width=640, p=1), ], p=0.5), A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v3": return { "train": A.Compose([ A.RandomCrop(height=1024, width=1024, p=1), A.Flip(p=0.5), A.Rotate(p=0.5), A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v4": # for 3x-scaled dataset | 256x256 return { "train": A.Compose([ A.RandomCrop(height=512, width=512, p=1), A.Flip(p=0.5), A.Rotate(p=0.5), A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v5": # for 3x-scaled dataset | 512x512 return { "train": A.Compose([ A.RandomCrop(height=512, width=512, p=1), A.Flip(p=0.5), A.Rotate(p=0.5), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v6": # for 3x-scaled dataset | 512x512 return { "train": A.Compose([ A.Flip(p=0.5), A.Rotate(p=0.5), A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, always_apply=False, p=0.25), A.RandomBrightness(limit=0.4, always_apply=False, p=0.25), A.RandomContrast(limit=0.2, always_apply=False, p=0.25), A.RandomShadow(shadow_roi=(0, 0, 1, 1), num_shadows_lower=1, num_shadows_upper=50, shadow_dimension=5, always_apply=False, p=0.25), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v7": # for 3x-scaled dataset | 512x512 return { "train": A.Compose([ A.Flip(p=0.5), A.Rotate(p=0.5), A.RandomCrop(height=768, width=768, p=1), A.Resize(height=256, width=256, p=1.0), A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, always_apply=False, p=0.25), A.RandomBrightness(limit=0.4, always_apply=False, p=0.25), A.RandomContrast(limit=0.2, always_apply=False, p=0.25), A.RandomShadow(shadow_roi=(0, 0, 1, 1), num_shadows_lower=1, num_shadows_upper=50, shadow_dimension=5, always_apply=False, p=0.25), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Resize(height=256, width=256, p=1.0), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v8": # for 3x-scaled dataset | 512x512 return { "train": A.Compose([ A.Rotate(p=0.5), A.RandomCrop(height=512, width=512, p=1), A.Flip(p=0.5), # color A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, always_apply=False, p=0.25), A.RandomBrightness(limit=0.4, always_apply=False, p=0.25), A.RandomContrast(limit=0.2, always_apply=False, p=0.25), A.RandomShadow(shadow_roi=(0, 0, 1, 1), num_shadows_lower=1, num_shadows_upper=50, shadow_dimension=5, always_apply=False, p=0.25), # transform A.ElasticTransform(p=1, alpha=120, sigma=6, alpha_affine=0.25), A.GridDistortion(p=0.25), A.OpticalDistortion(distort_limit=2, shift_limit=0.5, p=0.25), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v9": # for 3x-scaled dataset | 512x512 return { "train": A.Compose([ A.Flip(p=0.5), A.Rotate(p=0.5), A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, always_apply=False, p=0.25), A.RandomBrightness(limit=0.4, always_apply=False, p=0.25), A.RandomContrast(limit=0.2, always_apply=False, p=0.25), A.RandomShadow(shadow_roi=(0, 0, 1, 1), num_shadows_lower=1, num_shadows_upper=50, shadow_dimension=5, always_apply=False, p=0.25), # transform A.OneOf([ A.ElasticTransform( alpha=120, sigma=6, alpha_affine=0.25, p=0.25), A.GridDistortion(p=0.25), A.OpticalDistortion( distort_limit=2, shift_limit=0.5, p=0.25) ], p=0.75), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v10": # for 3x-scaled dataset | 512x512 return { "train": A.Compose([ A.Flip(p=0.5), A.Rotate(p=0.5), # size A.OneOf([ A.CropNonEmptyMaskIfExists(height=128, width=128, p=1.0), A.CropNonEmptyMaskIfExists(height=256, width=256, p=1.0), A.CropNonEmptyMaskIfExists(height=384, width=384, p=1.0) ], p=0.25), A.PadIfNeeded(min_height=512, min_width=512, border_mode=4, p=1), # array shuffle A.OneOf([A.MaskDropout(p=1), A.RandomGridShuffle(p=1)], p=0.5), # quality A.Downscale(scale_min=0.25, scale_max=0.75, interpolation=0, always_apply=False, p=0.5), A.GaussNoise(p=0.5), A.OneOf([ A.GlassBlur(p=1), A.GaussianBlur(p=1), ], p=0.5), # colors A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, always_apply=False, p=0.5), A.RandomBrightness(limit=0.4, always_apply=False, p=0.5), A.RandomContrast(limit=0.2, always_apply=False, p=0.5), A.RandomShadow(shadow_roi=(0, 0, 1, 1), num_shadows_lower=1, num_shadows_upper=50, shadow_dimension=5, always_apply=False, p=0.5), A.CLAHE(p=0.5), A.Equalize(p=0.5), A.ChannelShuffle(p=0.5), # transform A.OneOf([ A.ElasticTransform( alpha=120, sigma=6, alpha_affine=0.25, p=1), A.GridDistortion(p=1), A.OpticalDistortion(distort_limit=2, shift_limit=0.5, p=1) ], p=0.5), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v11": # custom normalization (see other/img_normalization.py) return { "train": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } elif version == "v12": # for 3x-scaled dataset | 512x512 return { "train": A.Compose([ A.Flip(p=0.5), A.Rotate(p=0.5), # size / quality A.OneOf([ A.CropNonEmptyMaskIfExists(height=128, width=128, p=1.0), A.CropNonEmptyMaskIfExists(height=256, width=256, p=1.0), A.CropNonEmptyMaskIfExists(height=384, width=384, p=1.0), A.Downscale(scale_min=0.25, scale_max=0.75, interpolation=0, always_apply=False, p=1.0), ], p=0.25), A.PadIfNeeded(min_height=512, min_width=512, border_mode=4, p=1), # array shuffle A.OneOf([A.MaskDropout(p=1), A.RandomGridShuffle(p=1)], p=0.15), # noise A.OneOf([ A.GaussNoise(p=1), A.GlassBlur(p=1), A.GaussianBlur(p=1), ], p=0.15), # colors A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, always_apply=False, p=0.15), A.RandomBrightness(limit=0.4, always_apply=False, p=0.15), A.RandomContrast(limit=0.2, always_apply=False, p=0.15), A.RandomShadow(shadow_roi=(0, 0, 1, 1), num_shadows_lower=1, num_shadows_upper=50, shadow_dimension=5, always_apply=False, p=0.15), A.OneOf([ A.CLAHE(p=1), A.Equalize(p=1), A.ChannelShuffle(p=1), ], p=0.15), # transform A.OneOf([ A.ElasticTransform( alpha=120, sigma=6, alpha_affine=0.25, p=1), A.GridDistortion(p=1), A.OpticalDistortion(distort_limit=2, shift_limit=0.5, p=1) ], p=0.15), A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), "valid": A.Compose([ A.Normalize(mean=(0.623, 0.520, 0.650), std=(0.278, 0.305, 0.274), max_pixel_value=255.0, p=1.0) ]), } else: raise Exception(f"Augmentation version '{version}' is UNKNOWN!")