def get_light_augmentations(image_size): return A.Compose([ A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1, rotate_limit=15, border_mode=cv2.BORDER_CONSTANT, value=0), A.RandomSizedCrop(min_max_height=(int(image_size[0] * 0.85), image_size[0]), height=image_size[0], width=image_size[1], p=0.3), ZeroTopAndBottom(p=0.3), # Brightness/contrast augmentations A.OneOf([ A.RandomBrightnessContrast(brightness_limit=0.25, contrast_limit=0.2), IndependentRandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1), A.RandomGamma(gamma_limit=(75, 125)), A.NoOp() ]), A.OneOf([ChannelIndependentCLAHE(p=0.5), A.CLAHE(), A.NoOp()]), A.HorizontalFlip(p=0.5), ])
def get_medium_augmentations(image_size): return A.Compose([ A.OneOf([ A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1, rotate_limit=15, 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.NoOp() ]), 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.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=20, b_shift_limit=15, g_shift_limit=15), A.HueSaturationValue(hue_shift_limit=5, sat_shift_limit=5), A.NoOp() ]), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5) ])
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 medium_augmentations(): return A.Compose([ A.HorizontalFlip(), A.ShiftScaleRotate(scale_limit=0.1, rotate_limit=15, 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), A.Normalize(), ])
def get_individual_transforms(): transforms = A.Compose([ A.OneOf( [ A.Transpose(p=1.0), A.VerticalFlip(p=1.0), A.HorizontalFlip(p=1.0), A.RandomRotate90(p=1.0), A.NoOp(), ], p=1.0, ), A.OneOf( [ A.ElasticTransform(p=1.0), A.GridDistortion(p=1.0), A.OpticalDistortion(p=1.0), A.NoOp(), ], p=1.0, ), A.OneOf( [ A.GaussNoise(p=1.0), A.GaussianBlur(p=1.0), A.ISONoise(p=1.0), A.CoarseDropout( p=1.0, max_holes=16, max_height=16, max_width=16), A.NoOp(), ], p=1.0, ), ]) return transforms
def get_training_augmentation(): train_transform = [ A.RandomSizedCrop(min_max_height=(300, 360), height=320, width=320, always_apply=True), A.HorizontalFlip(p=0.5), A.OneOf([ A.CLAHE(), A.RandomBrightnessContrast(), A.RandomGamma(), A.HueSaturationValue(), A.NoOp() ]), A.OneOf([ A.IAAAdditiveGaussianNoise(p=0.2), A.IAASharpen(), A.Blur(blur_limit=3), A.MotionBlur(blur_limit=3), A.NoOp() ]), A.OneOf([ A.RandomFog(), A.RandomSunFlare(), A.RandomRain(), A.RandomSnow(), A.NoOp() ]), A.Normalize(), ] return A.Compose(train_transform)
def get_medium_augmentations(image_size): min_size = min(image_size[0], image_size[1]) return A.Compose([ A.OneOf([A.RandomSizedCrop(min_max_height=(int(min_size* 0.85), min_size), height=image_size[0], width=image_size[1]), A.Resize(image_size[0], image_size[1]), A.CenterCrop(image_size[0], image_size[1]) ], p = 1.0), A.OneOf([ A.RandomBrightnessContrast(brightness_limit=0.15, contrast_limit=0.5), A.RandomGamma(gamma_limit=(50, 150)), A.NoOp() ], p = 1.0), A.OneOf([A.CLAHE(p=0.5, clip_limit=(10, 10), tile_grid_size=(3, 3)), A.FancyPCA(alpha=0.4), A.NoOp(), ], p = 1.0), A.OneOf([A.HorizontalFlip(), A.VerticalFlip(), A.RandomRotate90(), A.NoOp() ], p = 1.0) ])
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 get_augumentation(phase, width=512, height=512, min_area=0., min_visibility=0.): list_transforms = [] if phase == 'train': list_transforms.extend([ albu.augmentations.transforms.LongestMaxSize(max_size=width, always_apply=True), albu.PadIfNeeded(min_height=height, min_width=width, always_apply=True, border_mode=0, value=[0, 0, 0]), albu.augmentations.transforms.RandomResizedCrop(height=height, width=width, p=0.3), albu.augmentations.transforms.Flip(), albu.augmentations.transforms.Transpose(), albu.OneOf([ albu.RandomBrightnessContrast(brightness_limit=0.5, contrast_limit=0.4), albu.RandomGamma(gamma_limit=(50, 150)), albu.NoOp() ]), albu.OneOf([ albu.RGBShift(r_shift_limit=20, b_shift_limit=15, g_shift_limit=15), albu.HueSaturationValue(hue_shift_limit=5, sat_shift_limit=5), albu.NoOp() ]), albu.CLAHE(p=0.8), albu.HorizontalFlip(p=0.5), albu.VerticalFlip(p=0.5), ]) if (phase == 'test' or phase == 'valid'): list_transforms.extend([albu.Resize(height=height, width=width)]) list_transforms.extend([ albu.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), p=1), ToTensor() ]) if (phase == 'test'): return albu.Compose(list_transforms) return albu.Compose(list_transforms, bbox_params=albu.BboxParams( format='pascal_voc', min_area=min_area, min_visibility=min_visibility, label_fields=['category_id']))
def hard_color_augmentations(): return A.Compose([ A.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3, brightness_by_max=True), A.RandomGamma(gamma_limit=(90, 110)), A.OneOf( [A.NoOp(), A.MultiplicativeNoise(), A.GaussNoise(), A.ISONoise()]), A.OneOf([A.RGBShift(), A.HueSaturationValue(), A.NoOp()]), A.RandomFog(fog_coef_lower=0.05, fog_coef_upper=0.3), ])
def test_compose_with_keypoint_noop(keypoints, keypoint_format, labels): image = np.ones((100, 100, 3)) if labels is not None: aug = A.Compose([A.NoOp(p=1.0)], keypoint_params={ "format": keypoint_format, "label_fields": ["labels"] }) transformed = aug(image=image, keypoints=keypoints, labels=labels) else: aug = A.Compose([A.NoOp(p=1.0)], keypoint_params={"format": keypoint_format}) transformed = aug(image=image, keypoints=keypoints) assert np.array_equal(transformed["image"], image) assert transformed["keypoints"] == keypoints
def __init__(self, scale_range: float = (0.35, 0.65), input_size: int = (416, 416), augmentation: bool = False) -> None: if augmentation: self.crop_func = RandomCropAndResize(scale_range, input_size) self.aug_func = alb.Compose([ alb.OneOf([ alb.RGBShift(), alb.ToGray(), alb.NoOp(), ]), alb.RandomBrightnessContrast(), alb.OneOf([ alb.GaussNoise(), alb.IAAAdditiveGaussianNoise(), alb.CoarseDropout(fill_value=100), ]) ]) else: scale = (scale_range[0] + scale_range[1]) / 2. self.crop_func = CenterCropAndResize(scale, input_size) self.aug_func = None self.heatmap_stride = 4 self.heatmap_size = (input_size[0] // self.heatmap_stride, input_size[1] // self.heatmap_stride)
def sequence_augmentation(self, p_apply=0.5, limit_rotation=40, limit_translation=0.1, limit_scale=(-0.2, 0.2)): if self.rand_choice == 1: augm = A.Lambda(image=self.aug_dilate, keypoint=self.aug_keypoints) elif self.rand_choice == 2: augm = A.Lambda(image=self.aug_erode, keypoint=self.aug_keypoints) else: augm = A.NoOp() transform = A.Compose([ A.Lambda(image=self.aug_morph_close, keypoint=self.aug_keypoints, p=1.0), augm, A.Downscale(scale_min=0.5, scale_max=0.9, p=p_apply, interpolation=cv2.INTER_NEAREST_EXACT), A.ShiftScaleRotate(limit_translation, limit_scale, limit_rotation, p=p_apply, border_mode=cv2.BORDER_REFLECT101, value=-1.0) ], additional_targets={ 'image1': 'image', 'image2': 'image' }, keypoint_params=A.KeypointParams( "xy", remove_invisible=False)) return transform
def augmentation(p_apply=0.5, limit_rotation=40, limit_translation=0.1, limit_scale=(-0.2, 0.2)): transform = A.Compose( [ A.Lambda(image=aug_morph_close, keypoint=aug_keypoints, p=1.0), A.OneOf([ A.Lambda(image=aug_dilate, keypoint=aug_keypoints), A.Lambda(image=aug_erode, keypoint=aug_keypoints), A.NoOp() ], p=p_apply), # A.Lambda(image=aug_erode_or_dilate, keypoint=aug_keypoints, p=p_apply), A.Downscale(scale_min=0.5, scale_max=0.9, p=p_apply, interpolation=cv2.INTER_NEAREST_EXACT), A.ShiftScaleRotate(limit_translation, limit_scale, limit_rotation, p=p_apply, border_mode=cv2.BORDER_REFLECT101, value=-1.0), A.Lambda(image=cropout, keypoint=aug_keypoints, p=p_apply), ], keypoint_params=A.KeypointParams("xy", remove_invisible=False)) return transform
def get_aug_from_config(config): config = copy.deepcopy(config) if config is None: return A.NoOp() if isinstance(config, str): return name2factory(config)() if isinstance(config, list): return A.Compose([get_aug_from_config(c) for c in config]) # Augmentation name name = list(config.keys())[0] config = config[name] if config[name] else {} # Args for Compose/OneOf args = config.pop("args", None) args = args if args is not None else [] if name == "Compose": return A.Compose([get_aug_from_config(c) for c in args], **config) elif name == "OneOf": return A.OneOf([get_aug_from_config(c) for c in args], **config) else: return name2factory(name)(*args, **config)
def get_augumentation(phase, width=512, height=512, min_area=0., min_visibility=0.): list_transforms = [] if phase == 'train': list_transforms.extend([ albu.OneOf([ albu.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1, rotate_limit=15, border_mode=cv2.BORDER_CONSTANT, value=0), albu.NoOp() ]), albu.augmentations.transforms.RandomResizedCrop(height=height, width=width, p=1.0), albu.OneOf([ albu.RandomBrightnessContrast(brightness_limit=0.5, contrast_limit=0.4), albu.RandomGamma(gamma_limit=(50, 150)), albu.NoOp() ]), albu.OneOf([ albu.RGBShift(r_shift_limit=20, b_shift_limit=15, g_shift_limit=15), albu.HueSaturationValue(hue_shift_limit=5, sat_shift_limit=5), albu.NoOp() ]), albu.OneOf([albu.CLAHE(), albu.NoOp()]), albu.HorizontalFlip(p=0.5), albu.VerticalFlip(p=0.5) ]) if (phase == 'test'): list_transforms.extend([albu.Resize(height=height, width=width)]) list_transforms.extend([ToTensor()]) if (phase == 'test'): return albu.Compose(list_transforms) return albu.Compose(list_transforms, bbox_params=albu.BboxParams( format='pascal_voc', min_area=min_area, min_visibility=min_visibility, label_fields=['category_id']))
def get_transforms(config, key='transforms', imagenet=False, norm=True, to_tensor=True): if norm: normalize = A.Normalize( [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) if imagenet else A.Normalize(0, 1) else: normalize = A.NoOp() return A.Compose([ A.Resize(config['data']['height'], config['data']['width']), normalize, A.NoOp() if config['data'][key] == 'default' else get_aug_from_config( config['data'][key]), ToTensorV2() if to_tensor else A.NoOp() ])
def medium_training_transforms(): return A.Compose([ A.OneOf([ A.Transpose(), A.VerticalFlip(), A.HorizontalFlip(), A.RandomRotate90(), A.NoOp() ], p=1.0), A.OneOf([ A.CoarseDropout(max_holes=16, max_height=16, max_width=16), A.NoOp() ], p=1.0), A.Normalize() ])
def __init__(self, df, aug=A.NoOp(), fold=1): """ df.iloc[:, 0]: (C, H, W) float32 df.iloc[:, 1]: (H, W) float32 """ self.df = pd.DataFrame(dict(image=df.iloc[:, 0], mask=df.iloc[:, 1])) self.aug = A.Compose([aug, ToTensorV2()]) self.fold = fold
def medium_training_transforms_xray(crop_size=256): return A.Compose([ A.RandomResizedCrop(height=crop_size, width=crop_size), A.OneOf([ A.Transpose(), A.VerticalFlip(), A.HorizontalFlip(), A.RandomRotate90(), A.NoOp() ], p=1.0), A.OneOf([ A.CoarseDropout(max_holes=16, max_height=16, max_width=16), A.NoOp() ], p=1.0), ])
def __init__(self, root, mode='train', mask_file='mask.tif', scales=(1.0, )): self.scales = scales super().__init__(root, mode, mask_file, transform=alb.NoOp()) self.scales = scales
def __init__(self, df, aug=A.NoOp(), fold=1): """ df.iloc[:, 0]: (C, H, W) float32 df.iloc[:, 1]: (C, H, W) float32 """ self.df = pd.DataFrame(dict(image=df.iloc[:, 0], target=df.iloc[:, 1])) self.aug = A.Compose([aug, ToTensorV2()], additional_targets=dict(target="image")) self.fold = fold
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 test_compose_with_keypoint_noop_error_label_fields(keypoints, keypoint_format): image = np.ones((100, 100, 3)) aug = A.Compose([A.NoOp(p=1.0)], keypoint_params={ "format": keypoint_format, "label_fields": "class_id" }) with pytest.raises(Exception): aug(image=image, keypoints=keypoints, cls_id=[0])
def get_tta_transforms(config): return A.Compose( [ A.NoOp(), A.NoOp(), A.Rotate(limit=80, p=1.0), A.OneOf([A.NoOp(), A.NoOp(), A.NoOp()]), # A.RandomSizedCrop(min_max_height=(int(resolution*0.7), input_res), # height=resolution, width=resolution, p=1.0), A.NoOp(), A.NoOp(), A.GaussianBlur(p=0.3) if config.gaussian_blur else A.NoOp(), A.OneOf([A.NoOp(), A.HueSaturationValue(hue_shift_limit=0)]), A.NoOp(), A.Normalize(), ToTensorV2(), ], p=1.0, )
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 train_transform(width=512, height=512, min_area=0.0, min_visibility=0.0, lamda_norm=False): list_transforms = [] augment = albu.Compose([ albu.OneOf( [ albu.RandomSizedBBoxSafeCrop(p=1.0, height=height, width=width), albu.HorizontalFlip(p=1.0), albu.VerticalFlip(p=1.0), albu.RandomRotate90(p=1.0), albu.NoOp(p=1.0) ] ), albu.OneOf( [ albu.RandomBrightnessContrast(p=1.0), albu.RandomGamma(p=1.0), albu.NoOp(p=1.0) ] ), albu.OneOf( [ albu.MotionBlur(p=1.0), albu.RandomFog(p=1.0), albu.RandomRain(p=1.0), albu.CLAHE(p=1.0), albu.ToGray(p=1.0), albu.NoOp(p=1.0) ] ) ]) list_transforms.extend([augment]) if lamda_norm: list_transforms.extend([albu.Lambda(image=lamda_norm_tran)]) else: list_transforms.extend([albu.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255., p=1.0)]) list_transforms.extend([albu.Resize(height=height, width=width, p=1.0)]) return albu.Compose(list_transforms, bbox_params=albu.BboxParams(format='pascal_voc', min_area=min_area, min_visibility=min_visibility, label_fields=['label']))
def create_transform(self, input_dtype): weights = self.weights.detach().cpu().numpy().tolist() probabilities = [op.probability.item() for op in self.operations] true_probabilities = [w * p for (w, p) in zip(weights, probabilities)] assert sum(true_probabilities) <= 1.0 transforms = [] p_sum = 0 for operation, p in zip(self.operations, true_probabilities): transforms.append(operation.create_transform(input_dtype, p)) p_sum += p transforms.append(A.NoOp(p=1.0 - p_sum)) return OneOf(transforms, p=1)
def light_training_transforms(): return A.Compose([ A.OneOf([ A.Transpose(), A.VerticalFlip(), A.HorizontalFlip(), A.RandomRotate90(), A.NoOp() ], p=1.0), A.Normalize() ])
def light_training_transforms_xray(crop_size=256): return A.Compose([ A.RandomResizedCrop(height=crop_size, width=crop_size), A.OneOf([ A.Transpose(), A.VerticalFlip(), A.HorizontalFlip(), A.RandomRotate90(), A.NoOp() ], p=1.0), ])