def __init__(self, n, m): self.n = n self.m = m m_ratio = self.m / 30.0 self.augment_list = ( A.CLAHE(always_apply=True), A.Equalize(always_apply=True), A.InvertImg(always_apply=True), A.Rotate(limit=30 * m_ratio, always_apply=True), A.Posterize(num_bits=int(4 * m_ratio), always_apply=True), A.Solarize(threshold=m_ratio, always_apply=True), A.RGBShift(r_shift_limit=110 * m_ratio, g_shift_limit=110 * m_ratio, b_shift_limit=110 * m_ratio, always_apply=True), A.HueSaturationValue(hue_shift_limit=20 * m_ratio, sat_shift_limit=30 * m_ratio, val_shift_limit=20 * m_ratio, always_apply=True), A.RandomContrast(limit=m_ratio, always_apply=True), A.RandomBrightness(limit=m_ratio, always_apply=True), # A.Sharpen(always_apply=True), 0.1, 1.9), A.ShiftScaleRotate(shift_limit=0.3 * m_ratio, shift_limit_y=0, rotate_limit=0, always_apply=True), A.ShiftScaleRotate(shift_limit=0.3 * m_ratio, shift_limit_x=0, rotate_limit=0, always_apply=True), A.Cutout(num_holes=int(8 * m_ratio), always_apply=True), A.IAAAffine(shear=0.3 * m_ratio, always_apply=True)) assert self.n <= len(self.augment_list)
def __init__(self,fold_file, pkl_file_path, folds, image_height, image_width, mean, std): self.pkl_file_path = pkl_file_path self.fold_file = fold_file df = pd.read_csv(self.fold_file) df = df[['image_id','labels','kfold']] df = df[df['kfold'].isin(folds)].reset_index(drop= True) class_map = {'A':0,'B':1,'C':2} self.img_id = df['image_id'].apply(lambda x: x.split('.')[0]).values self.labels = df['labels'].apply(lambda x: x[-1]).map(class_map).values # training set self.aug = albumentations.Compose([ albumentations.Resize(image_height, image_width, always_apply=True), albumentations.Cutout(num_holes=8,max_h_size=30,max_w_size=60,always_apply=False,p=0.9), albumentations.Equalize(always_apply=False, p=0.9), albumentations.RandomShadow(shadow_roi=(0, 0.85, 1, 1), p=0.9), albumentations.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.9), albumentations.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.1, rotate_limit=5, p=0.9), albumentations.Normalize(mean, std, always_apply=True) ])
def test_equalize(): aug = A.Equalize(p=1) img = np.random.randint(0, 256, 256 * 256 * 3, np.uint8).reshape((256, 256, 3)) a = aug(image=img)["image"] b = F.equalize(img) assert np.all(a == b) mask = np.random.randint(0, 2, 256 * 256, np.uint8).reshape((256, 256)) aug = A.Equalize(mask=mask, p=1) a = aug(image=img)["image"] b = F.equalize(img, mask=mask) assert np.all(a == b) def mask_func(image, test): return mask aug = A.Equalize(mask=mask_func, mask_params=["test"], p=1) assert np.all(aug(image=img, test=mask)["image"] == F.equalize(img, mask=mask))
def augment_and_save(path_to_get_data, path_to_save_data, number_of_tranformation=10): """ Function defined to apply an image / rounding boxes transformation pipeline and save the corresponding files. Args: ----- - path_to_get_data: str, the folder path where untouched data is - path_to_save_data: str, the folder path where to save augmented data - number_of_tranformation: int, number of transformation to perform Returns: -------- - None """ images_names, yolo_names = get_images_and_box_files_names(path_to_get_data) augmentation_pipeline = A.Compose([ A.Resize(416, 416), A.Equalize(by_channels=True), A.RGBShift(r_shift_limit=(-30, 30), g_shift_limit=(-30, 30), b_shift_limit=(-30, 30), p=0.25), A.HorizontalFlip(p=0.35), A.VerticalFlip(p=0.35), A.ShiftScaleRotate(border_mode=cv2.BORDER_REPLICATE, p=0.35), A.RandomSnow(brightness_coeff=2.0, p=0.2) ], A.BboxParams('yolo', ['class_labels'])) # Iterate through each image for idx, name in enumerate(images_names): image_path = path_to_get_data + '/' + name image = cv2.imread(image_path) yolo_file_path = path_to_get_data + '/' + yolo_names[idx] labels, coordinates = get_labels_and_coordinates(yolo_file_path) # Generate x tranformation of the images for i in tqdm(range(number_of_tranformation)): new_image_name, new_yolos_name = set_new_files_names( name, i, "jpg", "txt") # Catch error due to unproper labelling try: new_image, new_coordinates, labels = get_data_from_pipeline( augmentation_pipeline, image, coordinates, labels) except ValueError as e: print("**** Error Message ****\n") print(f"{e}\n") print(f"""Invalid transformation of box: {str(new_coordinates)}\n""") print(f"Image: {new_image_name}\n") continue # Same each image to jpg with its corresponding coordinates save_image_bbox_data(path_to_save_data, new_image, new_image_name, new_coordinates, labels, new_yolos_name)
def get_train_augm(size=Tuple[int, int], p=0.5): return albu.Compose([ albu.Resize(*size), albu.OneOf([albu.CLAHE(6, (4, 4), always_apply=True), albu.Equalize(always_apply=True)], p=0.99), albu.HorizontalFlip(p=p), albu.VerticalFlip(p=p), albu.ToFloat(255), ToTensor() # albu.Lambda(image=to_tensor) ])
def get_augmentation(save_path=None, load_path=None): if load_path: return A.load(load_path) else: aug_seq1 = A.OneOf([ A.Rotate(limit=(-90, 90), p=1.0), A.Flip(p=1.0), A.OpticalDistortion(always_apply=False, p=1.0, distort_limit=(-0.3, 0.3), shift_limit=(-0.05, 0.05), interpolation=3, border_mode=3, value=(0, 0, 0), mask_value=None), ], p=1.0) aug_seq2 = A.OneOf([ # A.ChannelDropout(always_apply=False, p=1.0, channel_drop_range=(1, 1), fill_value=0), A.RGBShift(r_shift_limit=15, g_shift_limit=15, b_shift_limit=15, p=1.0), A.RandomBrightnessContrast(always_apply=False, p=1.0, brightness_limit=( -0.2, 0.2), contrast_limit=(-0.2, 0.2), brightness_by_max=True) ], p=1.0) aug_seq3 = A.OneOf([ A.GaussNoise(always_apply=False, p=1.0, var_limit=(10, 50)), A.ISONoise(always_apply=False, p=1.0, intensity=( 0.1, 1.0), color_shift=(0.01, 0.3)), A.MultiplicativeNoise(always_apply=False, p=1.0, multiplier=( 0.8, 1.6), per_channel=True, elementwise=True), ], p=1.0) aug_seq4 = A.OneOf([ A.Equalize(always_apply=False, p=1.0, mode='pil', by_channels=True), A.InvertImg(always_apply=False, p=1.0), A.MotionBlur(always_apply=False, p=1.0, blur_limit=(3, 7)), A.RandomFog(always_apply=False, p=1.0, fog_coef_lower=0.01, fog_coef_upper=0.2, alpha_coef=0.2) ], p=1.0) aug_seq = A.Compose([ # A.Resize(self.img_size, self.img_size), # aug_seq1, aug_seq2, aug_seq3, aug_seq4, # A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), ]) # aug_path = '/home/jitesh/prj/classification/test/bolt/aug/aug_seq.json' if save_path: A.save(aug_seq, save_path) # loaded_transform = A.load(aug_path) return aug_seq
def __getitem__(self, index): """Return a data point and its metadata information. Parameters: index - - a random integer for data indexing Returns a dictionary that contains A, B, A_paths and B_paths A (tensor) - - an image in the input domain B (tensor) - - its corresponding image in the target domain A_paths (str) - - image paths B_paths (str) - - image paths (same as A_paths) """ # read a image given a random integer index AB_path = self.AB_paths[index] AB = Image.open(AB_path).convert('RGB') # split AB image into A and B w, h = AB.size w2 = int(w / 2) A = AB.crop((0, 0, w2, h)) B = AB.crop((w2, 0, w, h)) # apply the same transform to both A and B # transform_params = get_params(self.opt, A.size) transformOutput = Al.Compose([ Al.Crop(x_max=256, y_max=256), Al.RandomRotate90(), Al.Flip(), ]) transformInput = Al.Compose([ Al.Blur(blur_limit=3), Al.Equalize(always_apply=True), Al.RandomBrightnessContrast(), transformOutput, ]) random.seed(42) A = transformInput(image=np.array(A))['image'] B = transformOutput(image=np.array(B))['image'] # A_transform = get_transform(self.opt, transform_params, grayscale=(self.input_nc == 1)) # B_transform = get_transform(self.opt, transform_params, grayscale=(self.output_nc == 1)) # A = A_transform(A) # B = B_transform(B) return {'A': A, 'B': B, 'A_paths': AB_path, 'B_paths': AB_path}
def __init__(self, k: int = 5, always_apply: bool = True, p: float = 1.0): super(RandAugmentAlb, self).__init__(always_apply, p) self.k = k self.candidates = [ AutoContrast(p=1.0), A.Equalize(p=1.0), A.InvertImg(p=1.0), Rotate(30., p=1.0), A.Posterize([4, 8], p=1.0), A.Solarize([0, 256], p=1.0), A.RandomBrightnessContrast(brightness_limit=0., contrast_limit=(0.05, 0.95), p=1.0), A.RandomBrightnessContrast(brightness_limit=(0.05, 0.95), contrast_limit=0., p=1.0), ShearX(0.3), ShearY(0.3), Translate(0.45), ]
def albumentations_list(MAGN: int = 4): """ Returns standard list of albumentations transforms, each of mangitude `MAGN`. Args: MAGN (int): Magnitude of each transform in the returned list. """ M = MAGN transform_list = [ # PIXEL-LEVEL A.RandomContrast(limit=M * .1, always_apply=True), A.RandomBrightness(limit=M * .1, always_apply=True), A.Equalize(always_apply=True), A.OpticalDistortion(distort_limit=M * .2, shift_limit=M * .1, always_apply=True), A.RGBShift(r_shift_limit=M * 10, g_shift_limit=M * 10, b_shift_limit=M * 10, always_apply=True), A.ISONoise(color_shift=(M * .01, M * .1), intensity=(M * .02, M * .2), always_apply=True), A.RandomFog(fog_coef_lower=M * .01, fog_coef_upper=M * .1, always_apply=True), A.CoarseDropout(max_holes=M * 10, always_apply=True), A.GaussNoise(var_limit=(M, M * 50), always_apply=True), # SPATIAL A.Rotate(always_apply=True), A.Transpose(always_apply=True), A.NoOp(always_apply=True), A.ElasticTransform(alpha=M * .25, sigma=M * 3, alpha_affine=M * 3, always_apply=True), A.GridDistortion(distort_limit=M * .075, always_apply=True) ] return transform_list
def __init__(self, folds, img_height, img_width, mean, std): df = pd.read_csv('../input/train_folds.csv') df = df[[ 'image_id', 'grapheme_root', 'vowel_diacritic', 'consonant_diacritic', 'kfold' ]] df = df[df.kfold.isin(folds)].reset_index(drop=True) self.image_ids = df.image_id.values self.grapheme_root = df.grapheme_root.values self.vowel_diacritic = df.vowel_diacritic.values self.consonant_diacritic = df.consonant_diacritic 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.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.1, rotate_limit=5, p=0.9), albumentations.Rotate(limit=5), albumentations.RandomContrast(limit=0.2), albumentations.GaussianBlur(blur_limit=7), albumentations.RandomGamma(), albumentations.RandomShadow(), albumentations.GaussNoise(), albumentations.ChannelShuffle(), #albumentations.Cutout(), albumentations.Equalize(), albumentations.MultiplicativeNoise(), albumentations.Normalize(mean, std, always_apply=True) ])
def __init__( self, transforms=None, mean=(0, 0, 0), std=(1, 1, 1), width=3, depth=-1, alpha=1., p=1., ): self.transforms = transforms self.mean = mean self.std = std self.width = width self.depth = depth self.alpha = alpha self.p = p if self.transforms is None: self.transforms = [ AutoContrast(cutoff=0, p=1), albu.Equalize(mode='pil', p=1), albu.Posterize(num_bits=(3, 4), p=1), albu.ShiftScaleRotate(shift_limit=0, scale_limit=0, rotate_limit=5, border_mode=cv2.BORDER_CONSTANT, value=(0, 0, 0), p=1), # rotate albu.Solarize(threshold=77, p=1), RandomShear(shear_x=0.09, shear_y=0, border_mode=cv2.BORDER_CONSTANT, value=(0, 0, 0), p=1), RandomShear(shear_x=0, shear_y=0.09, border_mode=cv2.BORDER_CONSTANT, value=(0, 0, 0), p=1), VerticalShift(shift_limit=0.09, border_mode=cv2.BORDER_CONSTANT, value=(0, 0, 0), p=1), HorizontalShift(shift_limit=0.09, border_mode=cv2.BORDER_CONSTANT, value=(0, 0, 0), p=1), # ImageNet-C albu.HueSaturationValue(hue_shift_limit=0, sat_shift_limit=(-36, 0), val_shift_limit=0, p=1), # saturation albu.RandomContrast(limit=(-0.36, 0), p=1), albu.RandomBrightness(limit=(-0.36, 0), p=1), albu.OneOf([ # sharpness albu.IAASharpen(alpha=(0.1, 0.5), lightness=0, p=1), albu.Blur(blur_limit=7, p=1), ], p=0.5), ]
import albumentations as A from albumentations.pytorch import ToTensorV2 train_transformation = A.Compose([ A.HorizontalFlip(p=0.5), A.ShiftScaleRotate(rotate_limit=(-15, -15), border_mode=1, p=0.5), A.OneOf([ A.RandomBrightnessContrast(p=0.5, brightness_limit=(-0.1, 0.1), contrast_limit=(-0.1, 0.1), brightness_by_max=True), A.Equalize(p=0.5, mode='cv', by_channels=True), ], p=0.5), A.RandomCrop(224, 224, p=1.0), ToTensorV2(p=1.0) ]) test_transformation = A.Compose([A.Resize(224, 224, p=1.0), ToTensorV2(p=1.0)])
alb.OneOf([ alb.OpticalDistortion(p=0.3), alb.GridDistortion(p=0.1), alb.IAAAffine(p=0.1), alb.ShiftScaleRotate(rotate_limit=15, p=0.1) ], p=0.2), alb.OneOf([alb.CLAHE(clip_limit=2), alb.IAASharpen(), alb.IAAEmboss()], p=0.3), alb.OneOf([ alb.HueSaturationValue(p=0.3), alb.RGBShift(), alb.RandomBrightnessContrast(), alb.Equalize(mode='cv', p=1) ], p=0.3), alb.HorizontalFlip(), alb.RandomGamma(p=0.2), alb.OneOf([ alb.CoarseDropout(2, 10, 10, p=0.5), alb.RandomRain(blur_value=3, p=1) ], p=0.5) ])) class MyDataset(Dataset): def __init__(self, data_dir,
def augment_and_save_images(self): _, _, files = next(os.walk(self.img_dir_path)) counter = 1 rotate_and_flip = A.Compose( [A.VerticalFlip(p=1), A.RandomRotate90(p=1)]) elastic_aug = A.ElasticTransform(p=1, alpha=400, sigma=20, alpha_affine=0.5, border_mode=cv2.BORDER_REPLICATE, interpolation=cv2.INTER_LINEAR) clahe_aug = A.CLAHE(clip_limit=(4.0, 16.0), tile_grid_size=(4, 4), always_apply=False, p=1) blur_aug = A.Blur(blur_limit=(5, 8), always_apply=False, p=1) downscale_aug = A.Downscale(scale_min=0.25, scale_max=0.4, interpolation=0, always_apply=False, p=1) equalize_aug = A.Equalize(mode='cv', by_channels=True, mask=None, always_apply=False, p=1) gaussnoise_aug = A.GaussNoise(var_limit=(50, 100), mean=0, always_apply=False, p=1) saturate_aug = A.HueSaturationValue(hue_shift_limit=30, sat_shift_limit=30, val_shift_limit=30, p=1) embos_aug = A.IAAEmboss(alpha=(0.5, 0.8), strength=(0.5, 0.8), always_apply=False, p=1) sharpen_aug = A.IAASharpen(alpha=(0.5, 0.8), lightness=(0.8, 1), always_apply=False, p=1) solarize_aug = A.Solarize(threshold=(100, 200), always_apply=False, p=1) aug_dict = { 'rotate': rotate_and_flip, 'elastic': elastic_aug, 'clahe': clahe_aug, 'blur': blur_aug, 'downscale': downscale_aug, 'equalize': equalize_aug, 'noise': gaussnoise_aug, 'saturate': saturate_aug, 'embos': embos_aug, 'sharpen': sharpen_aug, 'solarize': solarize_aug } required_aug_list = [ 'rotate', 'elastic', 'noise', 'saturate', 'solarize' ] for filename in files: image_path = self.img_dir_path + '/' + filename image = imageio.imread(image_path) label_path = self.label_dir_path + '/' + filename label = np.asarray(Image.open(label_path)) for aug_type in required_aug_list: self.save_augmented_images(aug_name=aug_type, augmented_items=aug_dict[aug_type]( image=image, mask=label), image_name=counter) #Following 3 if loops are separated to get extra randomness if bool(random.getrandbits(1)): self.save_augmented_images(aug_name='clahe', augmented_items=aug_dict['clahe']( image=image, mask=label), image_name=counter) else: self.save_augmented_images( aug_name='equalize', augmented_items=aug_dict['equalize'](image=image, mask=label), image_name=counter) if bool(random.getrandbits(1)): self.save_augmented_images(aug_name='blur', augmented_items=aug_dict['blur']( image=image, mask=label), image_name=counter) else: self.save_augmented_images( aug_name='downscale', augmented_items=aug_dict['downscale'](image=image, mask=label), image_name=counter) if bool(random.getrandbits(1)): self.save_augmented_images(aug_name='embos', augmented_items=aug_dict['embos']( image=image, mask=label), image_name=counter) else: self.save_augmented_images(aug_name='sharpen', augmented_items=aug_dict['sharpen']( image=image, mask=label), image_name=counter) counter += 1
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
aug_seq1 = A.OneOf([ A.Rotate(limit=(-90, 90), p=1.0), A.Flip(p=1.0), ], p=1.0) aug_seq2 = A.OneOf([ # A.ChannelDropout(always_apply=False, p=1.0, channel_drop_range=(1, 1), fill_value=0), A.RGBShift(r_shift_limit=15, g_shift_limit=15, b_shift_limit=15, p=1.0), A.RandomBrightnessContrast(always_apply=False, p=1.0, brightness_limit=(-0.2, 0.2), contrast_limit=(-0.2, 0.2), brightness_by_max=True) ], p=1.0) aug_seq3 = A.OneOf([ A.GaussNoise(always_apply=False, p=1.0, var_limit=(10, 100)), A.ISONoise(always_apply=False, p=1.0, intensity=(0.1, 1.0), color_shift=(0.01, 0.3)), A.MultiplicativeNoise(always_apply=False, p=1.0, multiplier=(0.8, 1.6), per_channel=True, elementwise=True), ], p=1.0) aug_seq4 = A.OneOf([ A.Equalize(always_apply=False, p=1.0, mode='pil', by_channels=True), A.InvertImg(always_apply=False, p=1.0), A.MotionBlur(always_apply=False, p=1.0, blur_limit=(3, 7)), A.OpticalDistortion(always_apply=False, p=1.0, distort_limit=(-0.3, 0.3), shift_limit=(-0.05, 0.05), interpolation=0, border_mode=0, value=(0, 0, 0), mask_value=None), A.RandomFog(always_apply=False, p=1.0, fog_coef_lower=0.1, fog_coef_upper=0.45, alpha_coef=0.5) ], p=1.0) aug_seq = A.Compose([ A.Resize(IMG_SIZE, IMG_SIZE), aug_seq1, aug_seq2, aug_seq3, aug_seq4, A.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) aug_path = '/home/jitesh/prj/classification/test/bolt/aug/aug_seq.json' A.save(aug_seq, aug_path)
def get_train_aug(RESOLUTION=300): return A.Compose([ A.LongestMaxSize(max_size=RESOLUTION*2, interpolation=cv2.INTER_CUBIC, \ always_apply=True), A.PadIfNeeded(min_height=RESOLUTION*2, min_width=RESOLUTION*2, always_apply=True, border_mode=cv2.BORDER_CONSTANT), A.RandomResizedCrop(RESOLUTION,RESOLUTION, scale=(0.7, 1), \ interpolation=cv2.INTER_CUBIC), A.Resize(RESOLUTION, RESOLUTION, p=1.0, interpolation=cv2.INTER_CUBIC), A.FancyPCA(p=0.8, alpha=0.5), # A.Transpose(p=0.7), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.1), A.ShiftScaleRotate(p=0.4, rotate_limit=12), A.HueSaturationValue( always_apply=False, p=0.3, hue_shift_limit=(-20, 20), sat_shift_limit=(-30, 30), val_shift_limit=(-20, 20)), # A.HueSaturationValue( # hue_shift_limit=0.4, #.3 # sat_shift_limit=0.4, #.3 # val_shift_limit=0.4, #.3 # p=0.7 # ), A.RandomBrightnessContrast( brightness_limit=(-0.5,0.5), #-.2,.2 contrast_limit=(-0.4, 0.4), #-.2,.2 #p=0.6 ), A.CoarseDropout(p=0.8, max_holes=30), # A.Cutout(p=0.8, max_h_size=40, max_w_size=40), A.Cutout(p=1, max_h_size=60, max_w_size=30, num_holes=6, fill_value=[106,87,55]), A.Cutout(p=1, max_h_size=30, max_w_size=60, num_holes=6, fill_value=[106,87,55]), A.OneOf([ A.OpticalDistortion(always_apply=False, p=1.0, distort_limit=(-0.6599999666213989, 0.6800000071525574), shift_limit=(-0.6699999570846558, 0.4599999785423279), interpolation=0, border_mode=0, value=(0, 0, 0), mask_value=None), # A.OpticalDistortion(p=0.5, distort_limit=0.15, shift_limit=0.15), # A.GridDistortion(p=0.5, distort_limit=0.5), A.GridDistortion(always_apply=False, p=1.0, num_steps=6, distort_limit=(-0.4599999785423279, 0.5), interpolation=0, border_mode=0, value=(0, 0, 0), mask_value=None), # A.IAAPiecewiseAffine(p=0.5, scale=(0.1, 0.14)), ], p=0.6), A.Sharpen(p=1.0, alpha=(0.1,0.3), lightness=(0.3, 0.9)), A.GaussNoise(var_limit=(300.0, 500.0), p=0.4), A.ISONoise(always_apply=False, p=0.4, intensity=(0.10000000149011612, 1.399999976158142), color_shift=(0.009999999776482582, 0.4000000059604645)), A.OneOf([ A.Equalize(always_apply=False, p=1.0, mode='cv', by_channels=True), A.Solarize(always_apply=False, p=1.0, threshold=(67, 120)), # A.IAAAdditiveGaussianNoise(p=1.0), A.GaussNoise(p=1.0), A.MotionBlur(always_apply=False, p=1.0, blur_limit=(5, 20)) ], p=0.5), ], p=1.0)
rotate_limit=18, p=1, border_mode=cv.BORDER_CONSTANT), A.IAAAffine(shear=10, p=1, mode="constant"), #A.Perspective(scale=(0.05, 0.15), keep_size=True, pad_mode=0, pad_val=0, # mask_pad_val=0, fit_output=False, interpolation=1, always_apply=False, p=1), ], p=1.0, ), A.OneOf( [ A.FancyPCA(alpha=0.1, always_apply=False, p=1), A.Blur(p=1), A.ToGray(p=0.8), A.ColorJitter( brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1, p=1), A.ChannelDropout((1, 1), fill_value=0, always_apply=False, p=1), ], p=0.3, ), A.OneOf( [ #A.GaussNoise (var_limit=(10.0, 50.0), mean=0, per_channel=True, always_apply=False, p=0.5), A.Equalize(mode='cv', by_channels=True, mask=None, mask_params=(), always_apply=False, p=0.8), A.MotionBlur(blur_limit=4, p=1), ], p=0.1, ) ])
def __call__(self, image, boxes=None, labels=None): #initialize the format for lib albumentations if boxes.shape[0] == 0: return image, boxes, labels bbox = [] for i in boxes: bbox.append(list(i)) #create annotations annotations = { 'image': image, 'bboxes': boxes, 'category_id': list(labels) } #create translation #Color_Level Change if self.cfg.DATA_LOADER.AUGMENTATION_WEATHER: trans_color_level = A.Compose([ A.Cutout(num_holes=20, max_h_size=64, max_w_size=64, fill_value=255, always_apply=False, p=0.8), A.Equalize(p=1), A.HueSaturationValue(hue_shift_limit=50, sat_shift_limit=50, val_shift_limit=50, always_apply=False, p=0.8), A.OneOf([ A.RandomFog(fog_coef_lower=0.3, fog_coef_upper=0.7, alpha_coef=0.08, always_apply=False, p=0.5), A.RandomSnow(snow_point_lower=0.1, snow_point_upper=0.3, brightness_coeff=2.5, always_apply=False, p=0.5), A.RandomSunFlare(flare_roi=(0, 0, 1, 0.5), angle_lower=0, angle_upper=1, num_flare_circles_lower=6, num_flare_circles_upper=10, src_radius=400, src_color=(255, 255, 255), always_apply=False, p=0.5), A.RandomRain(slant_lower=-10, slant_upper=10, drop_length=20, drop_width=1, drop_color=(200, 200, 200), blur_value=7, brightness_coefficient=0.7, rain_type=None, always_apply=False, p=0.5) ]), A.OneOf([ A.RandomSizedBBoxSafeCrop(720, 960, erosion_rate=0.0, interpolation=1, always_apply=False, p=0.5), A.RandomSizedBBoxSafeCrop(480, 640, erosion_rate=0.0, interpolation=1, always_apply=False, p=0.5), A.RandomSizedBBoxSafeCrop(240, 320, erosion_rate=0.0, interpolation=1, always_apply=False, p=0.5), ]), ]) else: trans_color_level = A.Compose([ A.Cutout(num_holes=20, max_h_size=64, max_w_size=64, fill_value=255, always_apply=False, p=0.5), A.Equalize(p=1), A.HueSaturationValue(hue_shift_limit=50, sat_shift_limit=50, val_shift_limit=50, always_apply=False, p=0.5), A.OneOf([ A.RandomSizedBBoxSafeCrop(720, 960, erosion_rate=0.0, interpolation=1, always_apply=False, p=0.5), A.RandomSizedBBoxSafeCrop(480, 640, erosion_rate=0.0, interpolation=1, always_apply=False, p=0.5), A.RandomSizedBBoxSafeCrop(240, 320, erosion_rate=0.0, interpolation=1, always_apply=False, p=0.5), ]), ]) #Spatial_Level if self.cfg.DATA_LOADER.AUGMENTATION_SPATIAL_LEVEL: trans_rotate_level = A.Compose([ A.OneOf([ A.Rotate(limit=90, interpolation=1, border_mode=4, value=None, mask_value=None, always_apply=False, p=0.5), A.RandomRotate90(always_apply=False, p=0.5), A.VerticalFlip(always_apply=False, p=0.5), A.HorizontalFlip(always_apply=False, p=0.5) ]), ]) #Apply the trans aug = get_aug(trans_color_level) augmented = aug(**annotations) img = augmented['image'] bbox = augmented['bboxes'] bbox = np.array(bbox) label = augmented['category_id'] #try rotate if self.cfg.DATA_LOADER.AUGMENTATION_SPATIAL_LEVEL: aug1 = get_aug(trans_rotate_level) augmented1 = aug1(**augmented) img1 = augmented1['image'] bbox1 = augmented1['bboxes'] bbox1 = np.array(bbox1) label1 = augmented1['category_id'] #if rotate fail if bbox1.shape[0] == 0: return img, bbox.astype(np.float32), np.array(label) else: return img1, bbox1.astype(np.float32), np.array(label1) else: return img, bbox.astype(np.float32), np.array(label)
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)
def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0.0, rank=-1): self.img_size = img_size self.augment = augment self.hyp = hyp self.image_weights = image_weights self.rect = False if image_weights else rect self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) self.mosaic_border = [-img_size // 2, -img_size // 2] self.stride = stride self.albuAug = A.Compose([ A.Blur((3, 6), p=0.07), A.CLAHE((1, 4), (8, 8), p=0.07), A.ChannelShuffle(p=0), A.Equalize(always_apply=False, p=0.07, mode='cv', by_channels=True), A.GaussNoise(always_apply=False, p=0.07, var_limit=(144.739990234375, 266.4499816894531)), A.HueSaturationValue(always_apply=False, p=0.07, hue_shift_limit=(-20, 20), sat_shift_limit=(-30, 30), val_shift_limit=(-20, 20)), A.ISONoise(always_apply=False, p=0.07, intensity=(0.1, 0.5), color_shift=(0.01, 0.05)), A.ImageCompression(always_apply=False, p=0.07, quality_lower=25, quality_upper=66, compression_type=1), A.JpegCompression(always_apply=False, p=0.07, quality_lower=23, quality_upper=45), A.MotionBlur(always_apply=False, p=0.07, blur_limit=(3, 7)), A.MultiplicativeNoise(always_apply=False, p=0.07, multiplier=(0.9, 1.1), per_channel=True, elementwise=True), A.RandomBrightness(always_apply=False, p=0.07, limit=(-0.2, 0.2)), A.RandomBrightnessContrast(always_apply=False, p=0.07, brightness_limit=(-0.2, 0.2), contrast_limit=(-0.2, 0.2), brightness_by_max=True) ]) try: f = [] # image files for p in path if isinstance(path, list) else [path]: p = Path(p) # os-agnostic if p.is_dir(): # dir f += glob.glob(str(p / '**' / '*.*'), recursive=True) elif p.is_file(): # file with open(p, 'r') as t: t = t.read().strip().splitlines() parent = str(p.parent) + os.sep f += [ x.replace('./', parent) if x.startswith('./') else x for x in t ] # local to global path else: raise Exception('%s does not exist' % p) self.img_files = sorted([ x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats ]) assert self.img_files, 'No images found' except Exception as e: raise Exception('Error loading data from %s: %s\nSee %s' % (path, e, help_url)) # Check cache self.label_files = img2label_paths(self.img_files) # labels cache_path = Path(self.label_files[0]).parent.with_suffix( '.cache') # cached labels if cache_path.is_file(): cache = torch.load(cache_path) # load if cache['hash'] != get_hash( self.label_files + self.img_files) or 'results' not in cache: # changed cache = self.cache_labels(cache_path) # re-cache else: cache = self.cache_labels(cache_path) # cache # Display cache [nf, nm, ne, nc, n] = cache.pop('results') # found, missing, empty, corrupted, total desc = f"Scanning '{cache_path}' for images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupted" tqdm(None, desc=desc, total=n, initial=n) assert nf > 0 or not augment, f'No labels found in {cache_path}. Can not train without labels. See {help_url}' # Read cache cache.pop('hash') # remove hash labels, shapes = zip(*cache.values()) self.labels = list(labels) self.shapes = np.array(shapes, dtype=np.float64) self.img_files = list(cache.keys()) # update self.label_files = img2label_paths(cache.keys()) # update if single_cls: for x in self.labels: x[:, 0] = 0 n = len(shapes) # number of images bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index nb = bi[-1] + 1 # number of batches self.batch = bi # batch index of image self.n = n self.indices = range(n) # Rectangular Training if self.rect: # Sort by aspect ratio s = self.shapes # wh ar = s[:, 1] / s[:, 0] # aspect ratio irect = ar.argsort() self.img_files = [self.img_files[i] for i in irect] self.label_files = [self.label_files[i] for i in irect] self.labels = [self.labels[i] for i in irect] self.shapes = s[irect] # wh ar = ar[irect] # Set training image shapes shapes = [[1, 1]] * nb for i in range(nb): ari = ar[bi == i] mini, maxi = ari.min(), ari.max() if maxi < 1: shapes[i] = [maxi, 1] elif mini > 1: shapes[i] = [1, 1 / mini] self.batch_shapes = np.ceil( np.array(shapes) * img_size / stride + pad).astype( np.int) * stride # Cache images into memory for faster training (WARNING: large datasets may exceed system RAM) self.imgs = [None] * n if cache_images: gb = 0 # Gigabytes of cached images self.img_hw0, self.img_hw = [None] * n, [None] * n results = ThreadPool(8).imap(lambda x: load_image(*x), zip(repeat(self), range(n))) # 8 threads pbar = tqdm(enumerate(results), total=n) for i, x in pbar: self.imgs[i], self.img_hw0[i], self.img_hw[ i] = x # img, hw_original, hw_resized = load_image(self, i) gb += self.imgs[i].nbytes pbar.desc = 'Caching images (%.1fGB)' % (gb / 1E9)
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!")
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 equalize(m): return albu.Equalize( mode='pil', p=1)
image = self.transforms(image=image)['image'] return image, torch.LongTensor([label]) df = pd.read_excel('SFEW.xlsx') df = df.sample(frac=1.0, random_state=random_seed) transforms_train = A.Compose([ A.HorizontalFlip(p=0.5), A.Rotate(limit=15, p=0.5), A.RandomResizedCrop(height=img_size, width=img_size, scale=(0.9, 1.0), p=1.0), A.Equalize(p=0.5), A.Normalize(p=1.0), ToTensorV2(p=1.0), ]) transforms_eval = A.Compose([ A.Resize(height=img_size, width=img_size, p=1.0), A.Normalize(p=1.0), ToTensorV2(p=1.0), ]) train_index = int(len(df) * train_portion) valid_index = train_index + int(len(df) * valid_portion) train = df.iloc[:train_index] valid = df.iloc[train_index:valid_index] test = df.iloc[valid_index:]
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() ])