Ejemplo n.º 1
0
def tr_da_fn(height, width):
    train_transform = [
        A.HorizontalFlip(p=0.5),
        A.ShiftScaleRotate(scale_limit=0.10, rotate_limit=7, shift_limit=0.10, border_mode=cv2.BORDER_CONSTANT, p=1.0),
        A.Perspective(scale=(0.025, 0.04), p=0.3),
        A.RandomResizedCrop(height=height, width=width, scale=(0.9, 1.0), p=0.3),

        A.OneOf(
            [
                A.CLAHE(p=1),
                A.RandomBrightness(p=1),
                A.RandomGamma(p=1),
                A.RandomContrast(limit=0.2, p=1.0),
            ],
            p=0.5,
        ),

        A.OneOf(
            [
                A.Sharpen(alpha=(0.2, 0.5), lightness=(0.5, 1.0), p=1.0),
                A.Blur(blur_limit=[2, 3], p=1.0),
                A.GaussNoise(var_limit=(5, 25), p=1.0),
                # A.MotionBlur(blur_limit=3, p=1.0),
            ],
            p=0.5,
        ),

        A.Lambda(image=_da_negative, p=0.2),

        A.LongestMaxSize(max_size=max(height, width), always_apply=True),
        A.PadIfNeeded(min_height=height, min_width=width, border_mode=cv2.BORDER_CONSTANT, always_apply=True),
    ]
    return A.Compose(train_transform)
def Albumentations(params):
    augmentation = A.Compose([
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.Transpose(p=0.5),
        A.OneOf([
            A.CLAHE(p=1.),
            A.RandomBrightnessContrast(0.15, 0.15, p=1.),
            A.RandomGamma(p=1.),
            A.HueSaturationValue(p=1.)],
            p=0.5),
        A.OneOf([
            A.Blur(3, p=1.),
            A.MotionBlur(3, p=1.),
            A.Sharpen(p=1.)],
            p=0.5),
        A.ShiftScaleRotate(
            shift_limit=0.05, scale_limit=0.05, rotate_limit=15,
            border_mode=cv2.BORDER_CONSTANT, p=0.75)
        
        # IMPORTANT: You MUST add A.Normalize(...) in the list.
        A.Normalize(mean=params.mean, std=params.std, max_pixel_value=255., always_apply=True)
    ], bbox_params=A.BboxParams(format='coco', label_fields=['category_ids'], min_visibility=0.2))
    
    return augmentation
Ejemplo n.º 3
0
def get_valid_aug(RESOLUTION=380): 
    return A.Compose([
#         A.SmallestMaxSize(max_size=RESOLUTION*2, interpolation=cv2.INTER_CUBIC, \
#                      always_apply=True),
#         A.OneOf([
#             A.CenterCrop(RESOLUTION,RESOLUTION, always_apply=True),
#             A.RandomResizedCrop(RESOLUTION,RESOLUTION, scale=(0.4, 1.0), \
#                                 always_apply=True, interpolation=cv2.INTER_CUBIC),
#             ], p=1.0),
        A.LongestMaxSize(max_size=RESOLUTION, interpolation=cv2.INTER_CUBIC, \
                         always_apply=True),
        A.PadIfNeeded(min_height=RESOLUTION, min_width=RESOLUTION, always_apply=True, border_mode=cv2.BORDER_CONSTANT),
#         A.Resize(RESOLUTION, RESOLUTION, p=1.0, interpolation=cv2.INTER_CUBIC),  
        A.HorizontalFlip(p=0.5),
        A.FancyPCA(p=1.0, alpha=0.5),
        A.HueSaturationValue(
            hue_shift_limit=0.1, 
            sat_shift_limit=0.1, 
            val_shift_limit=0.1, 
            p=0.5
            ),
        A.RandomBrightnessContrast(
            brightness_limit=(-0.1,0.1), 
            contrast_limit=(-0.1, 0.1), 
            p=0.5
            ),
        A.Sharpen(p=1.0, alpha=(0.1, 0.3), lightness=(0.3, 0.9))
        ], p=1.0)
Ejemplo n.º 4
0
 def train_augs(self):
     augs = []
     if self.image_size is not None:
         augs.append(A.Resize(self.image_size, self.image_size))
     if self.normalize:
         # imagenet normalization
         augs.append(
             A.Normalize(
                 mean=[0.485, 0.456, 0.406],
                 std=[0.229, 0.224, 0.225],
                 max_pixel_value=255.0,
                 p=1.0,
             )
         )
     if self.apply_augs:
         img_augs = [
             A.HorizontalFlip(p=0.5),
             A.ChannelShuffle(p=0.1),
             A.OneOf(
                 [
                     A.HueSaturationValue(
                         hue_shift_limit=0.2,
                         sat_shift_limit=0.2,
                         val_shift_limit=0.2,
                         p=0.5,
                     ),
                     A.RandomBrightnessContrast(
                         brightness_limit=0.2, contrast_limit=0.2, p=0.5
                     ),
                 ],
                 p=0.5,
             ),
             A.ToGray(p=0.01),
             A.RandomGamma(p=0.1),
             A.Sharpen(p=0.1),
             A.Cutout(
                 num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.2
             ),
         ]
         augs = augs + img_augs
     if self.bbox_format is not None:
         return A.Compose(
             augs,
             bbox_params=A.BboxParams(
                 format=self.bbox_format,
                 min_area=0,
                 min_visibility=0,
                 label_fields=["labels"],
             ),
         )
     return A.Compose(augs)
Ejemplo n.º 5
0
def get_albumentations_transforms(mode ):
    """
    Composes albumentations transforms.
    Returns the full list of transforms when mode is "train".
    mode should be one of "train", "val".
    """
    # compose validation transforms
    if mode == "val":
        transforms = compo(
            [],
            bbox_params=BboxParams(
                format="pascal_voc",
                min_area=0.0,
                min_visibility=0.0,
                label_fields=["category_id"],
            ),
        )
    # compose train transforms
    # TODO: make transformation parameters configurable from yml
    elif mode == "train":
        transforms = compo(
            [
        # A.Normalize(),
        # A.Blur(p=0.5),
        # A.ColorJitter(p=0.5),
        # A.Downscale(p=0.3),
        # A.Superpixels(p=0.3),
        A.RandomContrast(p=0.5),
        A.ShiftScaleRotate(p=0.8),

        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.RandomBrightnessContrast(p=0.5),
        A.Sharpen(p = 0.5),

        # A.RGBShift(p=0.5),
        # A.RandomRain(p=0.3),
        # A.RandomFog(p=0.3)
            ],
            bbox_params=BboxParams(
                format="pascal_voc",
                min_area=0.0,
                min_visibility=0.0,
                label_fields=["category_id"],
            ),
        )
    return transforms
Ejemplo n.º 6
0
def get_training_augmentation():
    train_transform = [

        albu.HorizontalFlip(p=0.5),

        albu.ShiftScaleRotate(scale_limit=0.5, rotate_limit=0, shift_limit=0.1, p=1, border_mode=0),

        albu.PadIfNeeded(min_height=320, min_width=320, always_apply=True, border_mode=0),
        albu.RandomCrop(height=320, width=320, always_apply=True),

        albu.GaussNoise(p=0.2),
        albu.Perspective(p=0.5),

        albu.OneOf(
            [
                albu.CLAHE(p=1),
                albu.RandomBrightnessContrast(p=1), #RandomBrightness(p=1),
                albu.RandomGamma(p=1)
            ],
            p=0.9,
        ),

        albu.OneOf(
            [
                albu.Sharpen(p=1),
                albu.Blur(blur_limit=3, p=1),
                albu.MotionBlur(blur_limit=3, p=1),
            ],
            p=0.9,
        ),

        albu.OneOf(
            [
                albu.RandomBrightnessContrast(p=1), #RandomContrast(p=1),
                albu.HueSaturationValue(p=1),
            ],
            p=0.9,
        ),
    ]
    return albu.Compose(train_transform)
Ejemplo n.º 7
0
Archivo: utils.py Proyecto: gadm21/AI
def my_augmentation():

    transforms = A.Compose(
        [
            # A.Normalize(),
            # A.Blur(p=0.5),
            # A.ColorJitter(p=0.5),
            # A.Downscale(p=0.3),
            # A.Superpixels(p=0.3),
            A.RandomContrast(p=0.5),
            A.ShiftScaleRotate(p=0.8),
            A.HorizontalFlip(p=0.5),
            A.VerticalFlip(p=0.5),
            A.RandomBrightnessContrast(p=0.5),
            A.Sharpen(p=0.5),

            # A.RGBShift(p=0.5),
            # A.RandomRain(p=0.3),
            # A.RandomFog(p=0.3)
        ],
        bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels']))

    return transforms
Ejemplo n.º 8
0
def get_train_aug(RESOLUTION=300): 
    return A.Compose([
        A.SmallestMaxSize(max_size=RESOLUTION*2, interpolation=cv2.INTER_CUBIC, \
                         always_apply=True),
        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.7),
        A.HueSaturationValue(
            hue_shift_limit=0.3, 
            sat_shift_limit=0.3, 
            val_shift_limit=0.3, 
            p=0.7
        ),
        A.RandomBrightnessContrast(
            brightness_limit=(-0.2,0.2), 
            contrast_limit=(-0.2, 0.2), 
            p=0.7
        ),
        A.CoarseDropout(p=0.8, max_holes=30),
        A.Cutout(p=0.8, max_h_size=40, max_w_size=40),
        A.OneOf([
                A.OpticalDistortion(p=0.3),
                A.GridDistortion(p=.1),
                A.IAAPiecewiseAffine(p=0.3),
                ], p=0.6),
        A.Sharpen(p=1.0, alpha=(0.1,0.3), lightness=(0.3, 0.9)),
        A.OneOf([
            A.IAAAdditiveGaussianNoise(p=1.0),
            A.GaussNoise(p=1.0),
            A.MotionBlur(always_apply=False, p=1.0, blur_limit=(3, 3))
            ], p=0.5),
        ], p=1.0)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
def randAugment(N=2, M=4, p=1.0, mode="all", cut_out=False):
    """
    Examples:
        >>> # M from 0 to 20
        >>> transforms = randAugment(N=3, M=8, p=0.8, mode='all', cut_out=False)
    """
    # Magnitude(M) search space
    scale = np.linspace(0, 0.4, 20)
    translate = np.linspace(0, 0.4, 20)
    rot = np.linspace(0, 30, 20)
    shear_x = np.linspace(0, 20, 20)
    shear_y = np.linspace(0, 20, 20)
    contrast = np.linspace(0.0, 0.4, 20)
    bright = np.linspace(0.0, 0.4, 20)
    sat = np.linspace(0.0, 0.2, 20)
    hue = np.linspace(0.0, 0.2, 20)
    shar = np.linspace(0.0, 0.9, 20)
    blur = np.linspace(0, 0.2, 20)
    noise = np.linspace(0, 1, 20)
    cut = np.linspace(0, 0.6, 20)
    # Transformation search space
    Aug = [  # geometrical
        albumentations.Affine(scale=(1.0 - scale[M], 1.0 + scale[M]), p=p),
        albumentations.Affine(translate_percent=(-translate[M], translate[M]),
                              p=p),
        albumentations.Affine(rotate=(-rot[M], rot[M]), p=p),
        albumentations.Affine(shear={'x': (-shear_x[M], shear_x[M])}, p=p),
        albumentations.Affine(shear={'y': (-shear_y[M], shear_y[M])}, p=p),
        # Color Based
        albumentations.RandomContrast(limit=contrast[M], p=p),
        albumentations.RandomBrightness(limit=bright[M], p=p),
        albumentations.ColorJitter(brightness=0.0,
                                   contrast=0.0,
                                   saturation=sat[M],
                                   hue=0.0,
                                   p=p),
        albumentations.ColorJitter(brightness=0.0,
                                   contrast=0.0,
                                   saturation=0.0,
                                   hue=hue[M],
                                   p=p),
        albumentations.Sharpen(alpha=(0.1, shar[M]), lightness=(0.5, 1.0),
                               p=p),
        albumentations.core.composition.PerChannel(albumentations.OneOf([
            albumentations.MotionBlur(p=0.5),
            albumentations.MedianBlur(blur_limit=3, p=1),
            albumentations.Blur(blur_limit=3, p=1),
        ]),
                                                   p=blur[M] * p),
        albumentations.GaussNoise(var_limit=(8.0 * noise[M], 64.0 * noise[M]),
                                  per_channel=True,
                                  p=p)
    ]
    # Sampling from the Transformation search space
    if mode == "geo":
        transforms = albumentations.SomeOf(Aug[0:5], N)
    elif mode == "color":
        transforms = albumentations.SomeOf(Aug[5:], N)
    else:
        transforms = albumentations.SomeOf(Aug, N)

    if cut_out:
        cut_trans = albumentations.OneOf([
            albumentations.CoarseDropout(
                max_holes=8, max_height=16, max_width=16, fill_value=0, p=1),
            albumentations.GridDropout(ratio=cut[M], p=1),
            albumentations.Cutout(
                num_holes=8, max_h_size=16, max_w_size=16, p=1),
        ],
                                         p=cut[M])
        transforms = albumentations.Compose([transforms, cut_trans])

    return transforms
Ejemplo n.º 11
0
def augmentation_train():
    train_transform = [
        albu.HorizontalFlip(p=0.5),
        albu.VerticalFlip(p=0.5),
        albu.OneOf([
            albu.InvertImg(p=0.5),
            albu.RandomBrightnessContrast(brightness_limit=(-0.5, 0.3),
                                          contrast_limit=(-0.5, 0.3),
                                          brightness_by_max=False,
                                          p=0.5),
            albu.RandomGamma(gamma_limit=(50, 120), p=.5),
            albu.RandomToneCurve(scale=0.4, p=.5),
            albu.HueSaturationValue(hue_shift_limit=20,
                                    sat_shift_limit=20,
                                    val_shift_limit=20,
                                    p=.5),
            albu.ChannelShuffle(p=.5),
            albu.RGBShift(
                r_shift_limit=20, g_shift_limit=20, b_shift_limit=20, p=.5),
        ],
                   p=0.5),
        albu.OneOf([
            albu.RandomFog(
                fog_coef_lower=0.1, fog_coef_upper=.4, alpha_coef=0.06, p=0.5),
            albu.MotionBlur(blur_limit=7, p=0.5),
            albu.MedianBlur(blur_limit=7, p=0.5),
            albu.GlassBlur(sigma=0.5, max_delta=2, p=0.5),
            albu.Sharpen(alpha=(0.1, 0.3), lightness=(0.7, 1.1), p=0.5)
        ],
                   p=0.5),
        albu.OneOf([
            albu.GaussNoise(var_limit=0.03, mean=0, p=0.5),
            albu.MultiplicativeNoise(multiplier=(0.98, 1.02), p=0.5),
            albu.ISONoise(
                color_shift=(0.01, 0.02), intensity=(0.1, 0.3), p=0.5),
        ],
                   p=0.3),
        albu.OneOf([
            albu.ElasticTransform(border_mode=cv2.BORDER_CONSTANT,
                                  interpolation=cv2.INTER_CUBIC,
                                  alpha=1,
                                  sigma=50,
                                  alpha_affine=50,
                                  p=0.5),
            albu.GridDistortion(border_mode=cv2.BORDER_CONSTANT,
                                interpolation=cv2.INTER_CUBIC,
                                distort_limit=(-0.3, 0.3),
                                num_steps=5,
                                p=0.5),
            albu.OpticalDistortion(border_mode=cv2.BORDER_CONSTANT,
                                   interpolation=cv2.INTER_CUBIC,
                                   distort_limit=(-.05, .05),
                                   shift_limit=(-0.05, 0.05),
                                   p=0.5),
            albu.ShiftScaleRotate(border_mode=cv2.BORDER_CONSTANT,
                                  interpolation=cv2.INTER_CUBIC,
                                  shift_limit=(0.05, 0.02),
                                  scale_limit=(-.1, 0),
                                  rotate_limit=2,
                                  p=0.5),
        ],
                   p=0.5),
    ]
    return albu.Compose(train_transform)