def augmentation(mode, target_size, prob=0.5, aug_m=2):
    '''
    description: augmentation
    mode: 'train' 'test'
    target_size: int or list, the shape of image ,
    aug_m: Strength of transform
    '''
    high_p = prob
    low_p = high_p / 2.0
    M = aug_m
    first_size = [int(x / 0.7) for x in target_size]

    if mode == 'train':
        return composition.Compose([
            transforms.Resize(first_size[0], first_size[1], interpolation=3),
            transforms.Flip(p=0.5),
            composition.OneOf([
                RandomCenterCut(scale=0.1 * M),
                transforms.ShiftScaleRotate(shift_limit=0.05 * M,
                                            scale_limit=0.1 * M,
                                            rotate_limit=180,
                                            border_mode=cv2.BORDER_CONSTANT,
                                            value=0),
                albumentations.imgaug.transforms.IAAAffine(
                    shear=(-10 * M, 10 * M), mode='constant')
            ],
                              p=high_p),
            transforms.RandomBrightnessContrast(
                brightness_limit=0.1 * M, contrast_limit=0.03 * M, p=high_p),
            transforms.HueSaturationValue(hue_shift_limit=5 * M,
                                          sat_shift_limit=15 * M,
                                          val_shift_limit=10 * M,
                                          p=high_p),
            transforms.OpticalDistortion(distort_limit=0.03 * M,
                                         shift_limit=0,
                                         border_mode=cv2.BORDER_CONSTANT,
                                         value=0,
                                         p=low_p),
            composition.OneOf([
                transforms.Blur(blur_limit=7),
                albumentations.imgaug.transforms.IAASharpen(),
                transforms.GaussNoise(var_limit=(2.0, 10.0), mean=0),
                transforms.ISONoise()
            ],
                              p=low_p),
            transforms.Resize(target_size[0], target_size[1], interpolation=3),
            transforms.Normalize(mean=(0.5, 0.5, 0.5),
                                 std=(0.5, 0.5, 0.5),
                                 max_pixel_value=255.0)
        ],
                                   p=1)

    else:
        return composition.Compose([
            transforms.Resize(target_size[0], target_size[1], interpolation=3),
            transforms.Normalize(mean=(0.5, 0.5, 0.5),
                                 std=(0.5, 0.5, 0.5),
                                 max_pixel_value=255.0)
        ],
                                   p=1)
Exemple #2
0
def get_presize_combine_transforms_V4():
    transforms_presize = A.Compose([
        transforms.PadIfNeeded(600, 800),
        geometric.Perspective(
            scale=[0, .1],
            pad_mode=cv2.BORDER_REFLECT,
            interpolation=cv2.INTER_AREA, p = .3),
        transforms.Flip(),
        geometric.ShiftScaleRotate(interpolation=cv2.INTER_LANCZOS4, p = 0.95, scale_limit=0.0),
        crops.RandomResizedCrop(
            TARGET_SIZE, TARGET_SIZE,
            scale=(config['rrc_scale_min'], config['rrc_scale_max']),
            ratio=(.70, 1.4),
            interpolation=cv2.INTER_CUBIC,
            p=1.0),
        transforms.Transpose()
        #rotate.Rotate(interpolation=cv2.INTER_LANCZOS4, p = 0.99),
    ])
    
    transforms_postsize = A.Compose([
        #imgaug.IAAPiecewiseAffine(),

        transforms.CoarseDropout(),
        transforms.CLAHE(p=.1),
        transforms.RandomToneCurve(scale=.1, p=0.2),
        transforms.RandomBrightnessContrast(
            brightness_limit=.1, 
            contrast_limit=0.4,
            p=.8),
        transforms.HueSaturationValue(
            hue_shift_limit=20, 
            sat_shift_limit=50,
            val_shift_limit=0, 
            p=0.5),
        transforms.Equalize(p=0.05),
        transforms.FancyPCA(p=0.05),
        transforms.RandomGridShuffle(p=0.1),
        A.OneOf([
                transforms.MotionBlur(blur_limit=(3, 9)),
                transforms.GaussianBlur(),
                transforms.MedianBlur()
            ], p=0.1),
        transforms.ISONoise(p=.2),
        transforms.GaussNoise(var_limit=127., p=.3),
        A.OneOf([
            transforms.GridDistortion(interpolation=cv2.INTER_AREA, distort_limit=[0.7, 0.7], p=0.5),
            transforms.OpticalDistortion(interpolation=cv2.INTER_AREA, p=.3),
        ], p=.3),
        geometric.ElasticTransform(alpha=4, sigma=4, alpha_affine=4, interpolation=cv2.INTER_AREA, p=0.3),
        transforms.CoarseDropout(),
        transforms.Normalize(),
        ToTensorV2()
    ])
    return transforms_presize, transforms_postsize
Exemple #3
0
def get_presize_combine_tune_transforms():
    transforms_presize = A.Compose([
        transforms.Transpose(),
        transforms.Flip(),
        #transforms.PadIfNeeded(600, 800),
        crops.RandomResizedCrop(
            TARGET_SIZE, TARGET_SIZE,
            scale=(.75, 1),
            interpolation=cv2.INTER_CUBIC,
            p=1.0),
        rotate.Rotate(interpolation=cv2.INTER_LANCZOS4, p = 0.99),
    ])
    
    transforms_postsize = A.Compose([
        transforms.CoarseDropout(),
        # transforms.CLAHE(p=.1),
        transforms.RandomToneCurve(scale=.1),
        transforms.RandomBrightnessContrast(
            brightness_limit=.1, 
            contrast_limit=0.2,
            p=.7),
        transforms.HueSaturationValue(
            hue_shift_limit=20, 
            sat_shift_limit=60,
            val_shift_limit=0, 
            p=0.6),
        #transforms.Equalize(p=0.1),
        #transforms.FancyPCA(p=0.05),
        #transforms.RandomGridShuffle(p=0.1),
        #A.OneOf([
        #        transforms.MotionBlur(blur_limit=(3, 9)),
        #        transforms.GaussianBlur(),
        #        transforms.MedianBlur()
        #    ], p=0.2),
        transforms.ISONoise(p=.3),
        transforms.GaussNoise(var_limit=255., p=.3),
        #A.OneOf([
        #     transforms.GridDistortion(interpolation=cv2.INTER_AREA, distort_limit=[0.7, 0.7], p=0.5),
        #     transforms.OpticalDistortion(interpolation=cv2.INTER_AREA, p=.3),
        # ], p=.3),
        geometric.ElasticTransform(alpha=4, sigma=100, alpha_affine=100, interpolation=cv2.INTER_AREA, p=0.3),
        transforms.CoarseDropout(),
        transforms.Normalize(),
        ToTensorV2()
    ])
    return transforms_presize, transforms_postsize
def get_augmentations():
    """Get a list of 'major' and 'minor' augmentation functions for the pipeline in a dictionary."""
    return {
        "major": {
            "shift-scale-rot":
            trans.ShiftScaleRotate(
                shift_limit=0.05,
                rotate_limit=35,
                border_mode=cv2.BORDER_REPLICATE,
                always_apply=True,
            ),
            "crop":
            trans.RandomResizedCrop(100,
                                    100,
                                    scale=(0.8, 0.95),
                                    ratio=(0.8, 1.2),
                                    always_apply=True),
            # "elastic": trans.ElasticTransform(
            #     alpha=0.8,
            #     alpha_affine=10,
            #     sigma=40,
            #     border_mode=cv2.BORDER_REPLICATE,
            #     always_apply=True,
            # ),
            "distort":
            trans.OpticalDistortion(0.2, always_apply=True),
        },
        "minor": {
            "blur":
            trans.GaussianBlur(7, always_apply=True),
            "noise":
            trans.GaussNoise((20.0, 40.0), always_apply=True),
            "bright-contrast":
            trans.RandomBrightnessContrast(0.4, 0.4, always_apply=True),
            "hsv":
            trans.HueSaturationValue(30, 40, 50, always_apply=True),
            "rgb":
            trans.RGBShift(always_apply=True),
            "flip":
            trans.HorizontalFlip(always_apply=True),
        },
    }
Exemple #5
0
def get_train_transforms():
    return A.Compose([
        transforms.PadIfNeeded(600, 800),
        geometric.ShiftScaleRotate(interpolation=cv2.INTER_LANCZOS4, p = 0.99, scale_limit=0.8),
        geometric.Perspective(pad_mode=cv2.BORDER_REFLECT,interpolation=cv2.INTER_AREA),
        crops.RandomResizedCrop(
            TARGET_SIZE, TARGET_SIZE,
            scale=(config['rrc_scale_min'], config['rrc_scale_max']),
            interpolation=cv2.INTER_CUBIC,
            p=1.0),
        transforms.Transpose(),
        transforms.Flip(),
        transforms.CoarseDropout(),
        transforms.CLAHE(p=.1),
        transforms.RandomToneCurve(scale=.1),
        transforms.RandomBrightnessContrast(
            brightness_limit=.1, 
            contrast_limit=0.3,
            p=.7),
        transforms.HueSaturationValue(
            hue_shift_limit=20, 
            sat_shift_limit=60,
            val_shift_limit=0, 
            p=0.6),
        transforms.RandomGridShuffle(p=0.1),
        A.OneOf([
                transforms.MotionBlur(blur_limit=(3, 9)),
                transforms.GaussianBlur(),
                transforms.MedianBlur()
            ], p=0.2),
        transforms.ISONoise(p=.3),
        transforms.GaussNoise(var_limit=255., p=.3),
        A.OneOf([
            transforms.GridDistortion(interpolation=cv2.INTER_AREA, distort_limit=[0.7, 0.7], p=0.5),
            transforms.OpticalDistortion(interpolation=cv2.INTER_AREA, p=.3),
        ], p=.3),
        geometric.ElasticTransform(alpha=4, sigma=100, alpha_affine=100, interpolation=cv2.INTER_AREA, p=0.3),
        transforms.CoarseDropout(),
        transforms.Normalize(),
        ToTensorV2()
    ])
Exemple #6
0
 def __init__(self, im_paths=None, labels=None, phase=None, resize=False):
     """
     Args:
         im_paths (numpy): image_data
         y (numpy): label data
         transform: pytorch transforms for transforms and tensor conversion
     """
     self.im_paths = im_paths
     self.labels = labels
     self.resize = resize
     self.albumentations_transform = {
         'train':
         Compose([
             ab_transforms.HorizontalFlip(p=0.2),
             ab_transforms.VerticalFlip(p=0.2),
             ab_transforms.Rotate(limit=180, p=0.2),
             ab_transforms.HueSaturationValue(p=0.1),
             ab_transforms.RandomContrast(p=0.1),
             ab_transforms.GaussianBlur(blur_limit=3, p=0.2),
             ab_transforms.GaussNoise(p=0.05),
             ab_transforms.CLAHE(p=0.2),
             ab_transforms.Normalize(mean=[0.5944, 0.4343, 0.5652],
                                     std=[0.2593, 0.2293, 0.2377]),
             ToTensor()
         ]),
         'val':
         Compose([
             ab_transforms.Normalize(mean=[0.5944, 0.4343, 0.5652],
                                     std=[0.2593, 0.2293, 0.2377]),
             ToTensor()
         ]),
     }
     if phase == 'train':
         self.transform = self.albumentations_transform['train']
     else:
         self.transform = self.albumentations_transform['val']
def GaussNoise(var_limit=(10.0, 50.0), mean=0, p=0.5):
    return transforms.GaussNoise(var_limit=var_limit, mean=mean, p=p)