Пример #1
0
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 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(),
    ])
Пример #3
0
def medium_3(image_size, p=1.0):
    cutout_crop = int(0.25 * image_size)
    return A.Compose(
        [
            # RandomCrop(input_size) / RandomResizedCrop (0.08, 1)
            A.HorizontalFlip(p=0.5),  # vflip
            A.VerticalFlip(p=0.5),  # hflip
            A.ShiftScaleRotate(
                shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.3),
            A.OneOf([
                A.RandomFog(
                    fog_coef_lower=0.3, fog_coef_upper=1.0, alpha_coef=.1),
                A.ImageCompression(quality_lower=20, quality_upper=99),
            ],
                    p=0.3),
            A.RandomBrightnessContrast(brightness_limit=0.125,
                                       contrast_limit=0.2,
                                       p=0.5),  # contrast_limit=0.5
            A.HueSaturationValue(hue_shift_limit=5,
                                 sat_shift_limit=30,
                                 val_shift_limit=20,
                                 p=0.2),
            A.GaussNoise(var_limit=(1, 50), p=0.4),
            A.CoarseDropout(min_holes=1,
                            max_holes=2,
                            max_height=cutout_crop,
                            max_width=cutout_crop,
                            p=0.5),
        ],
        p=p)
Пример #4
0
    def augment(self, img0, img1):
        transform = A.Compose([
            A.IAAAdditiveGaussianNoise(p=0.05),
            A.OneOf([
                A.IAASharpen(p=1.0),
                A.Blur(blur_limit=3, p=1.0),
            ],
                    p=0.5),
            A.OneOf([
                A.RandomBrightnessContrast(p=1.0),
                A.HueSaturationValue(p=1.0),
                A.RandomGamma(p=1.0),
            ],
                    p=0.5),
            A.OneOf([
                A.RandomFog(p=1.0),
                A.RandomRain(p=1.0),
                A.RandomShadow(p=1.0),
                A.RandomSnow(p=1.0),
                A.RandomSunFlare(p=1.0)
            ],
                    p=0.05),
        ],
                              additional_targets={'img1': 'image'})
        transformed = transform(image=img0, img1=img1)
        img0 = transformed["image"]
        img1 = transformed["img1"]

        return img0, img1
def get_transformations(aug_name='soft', image_size=448):
    all_transforms = {
        'no_aug' : A.Compose([
            A.Resize(image_size, image_size),
            A.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            ToTensorV2()
        ]),
        'super_soft' : A.Compose([
            A.Resize(image_size, image_size),
            A.HorizontalFlip(p=0.5),
            A.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            ToTensorV2()
        ]),
        'soft_rtsd' : A.Compose([
            A.Resize(image_size, image_size),
            A.RandomBrightnessContrast(brightness_limit=(-0.3,0.3), contrast_limit=(-0.3,0.3),p=0.5),
            A.Cutout(num_holes=5, max_h_size=image_size//10, max_w_size=image_size//10, p=0.25),
            A.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            ToTensorV2()
        ]),
        'medium_rtsd' : A.Compose([
            A.Resize(image_size, image_size),
            A.RandomBrightnessContrast(brightness_limit=(-0.3,0.3), contrast_limit=(-0.3,0.3),p=0.5),
            A.Blur(p=0.25, blur_limit=(3, 5)),
            A.GaussNoise(p=0.25, var_limit=(10.0, 50.0)),
            A.RGBShift(p=0.25, r_shift_limit=(-20, 20), g_shift_limit=(-20, 20), b_shift_limit=(-20, 20)),
            A.RandomFog(p=0.1, fog_coef_lower=0.1, fog_coef_upper=0.44, alpha_coef=0.16),
            A.Cutout(num_holes=5, max_h_size=image_size//10, max_w_size=image_size//10, p=0.25),
            A.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            ToTensorV2()
        ]),
        'soft' : A.Compose([
            A.Resize(image_size, image_size),
            A.HorizontalFlip(p=0.5),
            A.ShiftScaleRotate(shift_limit=0.1, scale_limit=1.1, rotate_limit=15, p=0.5),
            A.RandomBrightnessContrast(brightness_limit=(-0.2,0.2), contrast_limit=(-0.2,0.2),p=0.5),
            A.Cutout(num_holes=8, max_h_size=image_size//5, max_w_size=image_size//5, p=0.5),
            A.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            ToTensorV2()
        ]),
        'transforms_without_aug' : A.Compose([
            A.Resize(image_size, image_size),
            A.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            ToTensorV2()
        ])
    }

    image_transforms = {
        'train': all_transforms[aug_name],
        'valid': all_transforms['transforms_without_aug'],
        'test': all_transforms['transforms_without_aug']
    }
    return image_transforms
Пример #6
0
def generate_transforms2(img_size):
    train_transform = Compose([
        # A.RandomCrop(p=1, height=img_size, width=img_size),
        A.Resize(height=img_size, width=img_size),
        A.RandomSunFlare(p=1),
        A.RandomFog(p=1),
        A.RandomBrightness(p=1),
        A.Rotate(p=1, limit=90),
        A.RGBShift(p=1),
        A.RandomSnow(p=1),
        A.HorizontalFlip(p=1),
        A.VerticalFlip(p=1),
        A.RandomContrast(limit=0.5, p=1),
        A.HueSaturationValue(p=1,
                             hue_shift_limit=20,
                             sat_shift_limit=30,
                             val_shift_limit=50),
        # A.Cutout(p=1),
        # A.Transpose(p=1),
        A.JpegCompression(p=1),
        A.CoarseDropout(p=1),
        A.IAAAdditiveGaussianNoise(loc=0,
                                   scale=(2.5500000000000003, 12.75),
                                   per_channel=False,
                                   p=1),
        A.IAAAffine(scale=1.0,
                    translate_percent=None,
                    translate_px=None,
                    rotate=0.0,
                    shear=0.0,
                    order=1,
                    cval=0,
                    mode='reflect',
                    p=1),
        A.IAAAffine(rotate=90., p=1),
        A.IAAAffine(rotate=180., p=1),
        Normalize(mean=(0.485, 0.456, 0.406),
                  std=(0.229, 0.224, 0.225),
                  max_pixel_value=255.0,
                  p=1.0),
        ToTensorV2()
    ])
    val_transform = Compose([
        Resize(height=img_size, width=img_size),
        Normalize(mean=(0.485, 0.456, 0.406),
                  std=(0.229, 0.224, 0.225),
                  max_pixel_value=255.0,
                  p=1.0),
        ToTensorV2(),
    ])

    return {"train": train_transform, "val": val_transform}
Пример #7
0
def get_training_augmentation(min_area=0., min_visibility=0.):
    train_transform = [
        albu.OneOf([
            albu.MotionBlur(p=.2),
            albu.MedianBlur(blur_limit=3, p=0.1),
            albu.Blur(blur_limit=3, p=0.1),
        ],
                   p=0.2),
        albu.ShiftScaleRotate(shift_limit=0,
                              scale_limit=0,
                              rotate_limit=15,
                              p=0.5),
        albu.OneOf([
            albu.CLAHE(clip_limit=2),
            albu.IAASharpen(),
            albu.IAAEmboss(),
        ],
                   p=0.3),
        albu.OneOf([
            albu.RandomFog(fog_coef_lower=0.1, fog_coef_upper=0.15, p=0.1),
            albu.RandomShadow(p=0.1),
            albu.RandomBrightness(limit=0.3, p=0.2),
            albu.RandomRain(slant_lower=0,
                            slant_upper=8,
                            drop_length=0,
                            blur_value=4,
                            brightness_coefficient=0.8,
                            rain_type='heavy',
                            p=0.1),
            albu.RandomSunFlare(p=0.2),
        ]),
        albu.OneOf([
            albu.RGBShift(p=0.1),
            albu.HueSaturationValue(p=0.3),
        ]),
        albu.OneOf([
            albu.HorizontalFlip(p=0.5),
            albu.RandomSizedCrop(min_max_height=(720, 1380),
                                 height=1380,
                                 width=720,
                                 interpolation=cv2.INTER_AREA)
        ],
                   p=0.2)
    ]
    return albu.Compose(train_transform,
                        bbox_params={
                            'format': 'coco',
                            '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 setup_pipeline(self, dict_transform):

        tranform_list = []
        if 'shadow' in dict_transform:
            tranform_list.append(
                A.RandomShadow(shadow_roi=(0, 0.5, 1, 1),
                               num_shadows_upper=1,
                               p=0.2))
        if 'scale' in dict_transform:
            tranform_list.append(
                A.RandomScale(scale_limit=float(dict_transform['scale'])))
        if 'rotate' in dict_transform:
            tranform_list.append(
                A.Rotate(limit=float(dict_transform['rotate']), p=0.8))
        if 'shift' in dict_transform:
            tranform_list.append(
                A.ShiftScaleRotate(shift_limit=float(dict_transform['shift']),
                                   scale_limit=0.0,
                                   rotate_limit=0,
                                   interpolation=1,
                                   border_mode=4,
                                   p=0.8))
        if 'brightness' in dict_transform:
            tranform_list.append(
                A.RandomBrightness(limit=float(dict_transform['brightness']),
                                   p=0.8))
        if 'contrast' in dict_transform:
            tranform_list.append(
                A.RandomContrast(limit=float(dict_transform['contrast']),
                                 p=0.8))
        if 'motion_blur' in dict_transform:
            tranform_list.append(A.MotionBlur(p=0.5, blur_limit=7))
        if 'fog' in dict_transform:
            tranform_list.append(
                A.RandomFog(fog_coef_lower=0.0,
                            fog_coef_upper=float(dict_transform['fog']),
                            alpha_coef=0.05,
                            p=0.7))
        if 'rain' in dict_transform:
            tranform_list.append(
                A.RandomRain(brightness_coefficient=0.95,
                             drop_width=1,
                             blur_value=1,
                             p=0.7))
        if 'occlusion' in dict_transform:
            tranform_list.append(
                A.CoarseDropout(max_holes=5, max_height=8, max_width=8, p=0.5))
        self.transform = A.Compose(tranform_list)
Пример #10
0
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
Пример #11
0
    def __init__(self, cfg):
        self.cfg = cfg
        self.data = self.prepare()
        self.mean = self.cfg.mean
        self.std = self.cfg.std

        self.normal_transform = A.Compose([
            A.Resize(384, 288, p=1.0),
            A.HorizontalFlip(p=0.5),
            A.Normalize(p=1.0, mean=self.mean, std=self.std)
        ])

        self.augment_transform = A.Compose([
            A.Resize(384, 288, p=1.0),
            A.HorizontalFlip(p=0.7),
            A.GaussNoise(p=0.5),
            A.ShiftScaleRotate(shift_limit=0.1,
                               scale_limit=0.25,
                               rotate_limit=20,
                               p=0.6,
                               border_mode=0),
            A.OneOf([
                A.CLAHE(p=0.5),
                A.Compose([
                    A.RandomBrightness(limit=0.5, p=0.6),
                    A.RandomContrast(limit=0.4, p=0.6),
                    A.RandomGamma(p=0.6),
                ])
            ],
                    p=0.65),
            A.OneOf([
                A.HueSaturationValue(10, 20, 10, p=1.0),
                A.RGBShift(p=1.0),
                A.Emboss(p=1.0),
            ],
                    p=0.5),
            A.RandomFog(fog_coef_lower=0.3, fog_coef_upper=0.3, p=0.3),
            A.OneOf([
                A.Perspective(p=1.0, scale=(0.05, 0.1)),
                A.GridDistortion(p=1.0, distort_limit=0.25, border_mode=0),
                A.OpticalDistortion(
                    p=1.0, shift_limit=0.1, distort_limit=0.1, border_mode=0)
            ],
                    p=0.65),
            A.Normalize(p=1.0, mean=self.mean, std=self.std),
        ])
Пример #12
0
 def __init__(self):
     self.policy = A.Compose([
         A.OneOf([
             A.Rotate(180),
             A.Flip(),
         ], p=0.3),
         A.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.5, rotate_limit=0, p=0.2),
         A.OneOf([
             A.CoarseDropout(max_holes=16, max_height=16, max_width=16, p=0.3),
             A.GridDropout(ratio=0.3, p=0.3),
         ]),
         A.OneOf([
             A.ElasticTransform(sigma=10, alpha_affine=25, p=0.3),
             A.RandomFog(fog_coef_lower=0.2, fog_coef_upper=0.7, p=0.2),
         ], p=0.2),
         A.OneOf([
             A.IAAAdditiveGaussianNoise(),
             A.GaussNoise(),
             A.ISONoise()
         ], p=0.2),
         A.OneOf([
             A.MotionBlur(p=.3),
             A.MedianBlur(blur_limit=5, p=0.3),
             A.Blur(blur_limit=5, p=0.3),
             A.GaussianBlur(p=0.3)
         ], p=0.2),
         A.OneOf([
             A.ChannelShuffle(p=.3),
             A.HueSaturationValue(p=0.3),
             A.ToGray(p=0.3),
             A.ChannelDropout(p=0.3),
             A.InvertImg(p=0.1)
         ], p=0.2),
         A.OneOf([
             A.OpticalDistortion(p=0.3),
             A.GridDistortion(p=.2),
             A.IAAPiecewiseAffine(p=0.3),
         ], p=0.2),
         A.OneOf([
             A.CLAHE(clip_limit=2),
             A.IAASharpen(),
             A.IAAEmboss(),
         ], p=0.2),
         A.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3, p=0.3),
         A.Solarize(p=0.2),
     ])
Пример #13
0
def noise_transforms(p=0.5):
    """
    Applies GaussNoise or RandomFog random with a probability p.

    Args:
        p (float, optional): probability. Defaults to 0.5.

    Returns:
        albumentation transforms: transforms.
    """
    return albu.OneOf(
        [
            albu.GaussNoise(var_limit=(1.0, 50.0), always_apply=True),
            albu.RandomFog(
                fog_coef_lower=0.01, fog_coef_upper=0.25, always_apply=True),
        ],
        p=p,
    )
Пример #14
0
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 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(),
    ])
Пример #16
0
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 augmentation_function(n_augment: int) -> A.core.composition.Compose:
    """Performs data augmentation on images with realistic parameters
    Returns a Albumentation composition function"""
    proba_transfo = 1 / n_augment

    def random_proba(proba_transfo: float) -> float:
        return max(0.4, min(1, proba_transfo + (2 * np.random.random() - 1) / 3))

    return A.Compose(
        [
            A.HorizontalFlip(p=1),
            A.Rotate(limit=25, p=random_proba(proba_transfo)),
            A.RandomBrightness(limit=0.1, p=random_proba(proba_transfo)),
            A.RandomSnow(brightness_coeff=0.95, snow_point_lower=0.1,
                         snow_point_upper=0.3, p=random_proba(proba_transfo)),
            A.RandomFog(fog_coef_lower=0.1, fog_coef_upper=0.4,
                        alpha_coef=0.1, p=random_proba(proba_transfo)),

        ],
        p=1.0,
        bbox_params=A.BboxParams(format='yolo', label_fields=['category_ids'])
    )
Пример #18
0
    def augment(self, img, mask, do_affine_transform = True):
        if do_affine_transform:
            afine_transform = A.Compose([
                A.HorizontalFlip(p=0.4),
                A.OneOf([
                    A.GridDistortion(interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0),
                    A.ElasticTransform(interpolation=cv2.INTER_NEAREST, alpha_affine=10, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0),
                    A.ShiftScaleRotate(interpolation=cv2.INTER_NEAREST, shift_limit=0.03, rotate_limit=4, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0),
                    A.OpticalDistortion(interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0)
                ], p=0.6),
            ], additional_targets={'mask': 'image'})
            afine_transformed = afine_transform(image=img, mask=mask)
            img = afine_transformed["image"]
            mask = afine_transformed["mask"]

        transform = A.Compose([
            A.IAAAdditiveGaussianNoise(p=0.05),
            A.OneOf([
                A.IAASharpen(p=1.0),
                A.Blur(blur_limit=3, p=1.0),
            ] , p=0.5),
            A.OneOf([
                A.RandomBrightnessContrast(p=1.0),
                A.HueSaturationValue(p=1.0),
                A.RandomGamma(p=1.0),
            ], p=0.5),
            A.OneOf([
                A.RandomFog(p=1.0),
                A.RandomRain(p=1.0),
                A.RandomShadow(p=1.0),
                A.RandomSnow(p=1.0),
                A.RandomSunFlare(p=1.0)
            ], p=0.05),
        ])
        transformed = transform(image=img)
        img = transformed["image"]
        return img, mask
Пример #19
0
def hard_augmentations(mask_dropout=True) -> List[A.DualTransform]:
    return [
        # D4 Augmentations
        A.RandomRotate90(p=1),
        A.Transpose(p=0.5),
        # Spatial augmentations
        A.OneOf(
            [
                A.ShiftScaleRotate(scale_limit=0.2, rotate_limit=45, border_mode=cv2.BORDER_REFLECT101),
                A.ElasticTransform(border_mode=cv2.BORDER_REFLECT101, alpha_affine=5),
            ]
        ),
        # Color augmentations
        A.OneOf(
            [
                A.RandomBrightnessContrast(brightness_by_max=True),
                A.CLAHE(),
                A.FancyPCA(),
                A.HueSaturationValue(),
                A.RGBShift(),
                A.RandomGamma(),
            ]
        ),
        # Dropout & Shuffle
        A.OneOf(
            [
                A.RandomGridShuffle(),
                A.CoarseDropout(),
                A.MaskDropout(max_objects=2, mask_fill_value=0) if mask_dropout else A.NoOp(),
            ]
        ),
        # Add occasion blur
        A.OneOf([A.GaussianBlur(), A.GaussNoise(), A.IAAAdditiveGaussianNoise()]),
        # Weather effects
        A.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1),
    ]
Пример #20
0
def get_transforms(aug_type: str):
    """
    Data augmentation 객체 생성

    Args:
        aug_type(str) : augmentation타입 지정

    Returns :
        list :: train, validation, test데이터 셋에 대한 transform
    """
    # TODO: Normalize
    if False:
        pass
    else:
        norm_mean = (0, 0, 0)
        norm_std = (1, 1, 1)

    if aug_type == 'no':
        train_transform = A.Compose(
            [A.Normalize(mean=norm_mean, std=norm_std),
             ToTensorV2()])
        val_transform = A.Compose(
            [A.Normalize(mean=norm_mean, std=norm_std),
             ToTensorV2()])
        test_transform = A.Compose(
            [A.Normalize(mean=norm_mean, std=norm_std),
             ToTensorV2()])
    elif aug_type == 'dev':
        pass
    elif aug_type == 'final':
        train_transform = A.Compose([
            A.HorizontalFlip(p=0.5),
            A.RandomResizedCrop(512,
                                512,
                                p=0.8,
                                scale=(0.7, 1.0),
                                ratio=(0.5, 1.5)),
            A.Rotate(limit=30, p=0.8),
            A.Cutout(num_holes=4, max_h_size=16, max_w_size=16, p=0.8),
            A.ElasticTransform(alpha=40, p=0.8),
            A.CLAHE(clip_limit=3.0, p=0.8),
            A.Normalize(mean=norm_mean, std=norm_std),
            ToTensorV2()
        ])
        val_transform = A.Compose(
            [A.Normalize(mean=norm_mean, std=norm_std),
             ToTensorV2()])
        test_transform = A.Compose(
            [A.Normalize(mean=norm_mean, std=norm_std),
             ToTensorV2()])
    # TODO: 아래 코드 정리 중
    elif aug_type == 'basic':
        train_transform = A.Compose([
            A.Normalize(mean=(0.46009655, 0.43957878, 0.41827092),
                        std=(0.2108204, 0.20766491, 0.21656131),
                        max_pixel_value=255.0,
                        p=1.0),
            ToTensorV2()
        ])

        val_transform = A.Compose([
            A.Normalize(mean=(0.46009655, 0.43957878, 0.41827092),
                        std=(0.2108204, 0.20766491, 0.21656131),
                        max_pixel_value=255.0,
                        p=1.0),
            ToTensorV2()
        ])

        test_transform = A.Compose([ToTensorV2()])
    elif aug_type == "aug1":
        train_transform = A.Compose([
            A.OneOf([
                A.ElasticTransform(alpha=120,
                                   sigma=120 * 0.05,
                                   alpha_affine=120 * 0.03,
                                   p=0.5),
                A.GridDistortion(p=1.0),
                A.OpticalDistortion(distort_limit=2, shift_limit=0.5, p=1)
            ],
                    p=0.8),
            A.VerticalFlip(p=0.5),
            A.RandomBrightnessContrast(p=0.8),
            A.RandomGamma(p=0.8),
            A.RandomRotate90(p=0.5),
            ToTensorV2()
        ])

        val_transform = A.Compose([ToTensorV2()])

        test_transform = A.Compose([ToTensorV2()])

    elif aug_type == "aug2":
        train_transform = A.Compose([
            A.OneOf([
                A.RandomShadow(p=1),
                A.RandomSunFlare(num_flare_circles_lower=1,
                                 num_flare_circles_upper=5,
                                 src_radius=250,
                                 p=1),
                A.RandomRain(p=1),
                A.RandomSnow(brightness_coeff=1.5, p=1),
                A.RandomFog(
                    fog_coef_lower=0.8, fog_coef_upper=1, alpha_coef=0.08, p=1)
            ],
                    p=0.8),
            ToTensorV2()
        ])

        val_transform = A.Compose([ToTensorV2()])

        test_transform = A.Compose([ToTensorV2()])

    elif aug_type == "aug3":
        train_transform = A.Compose([
            A.OneOf([
                A.HueSaturationValue(hue_shift_limit=0.2,
                                     sat_shift_limit=0.2,
                                     val_shift_limit=0.2,
                                     p=1),
                A.MultiplicativeNoise(
                    multiplier=(0.9, 1.1), per_channel=True, p=1),
                A.RGBShift(r_shift_limit=0.1,
                           g_shift_limit=0.1,
                           b_shift_limit=0.1,
                           p=1),
                A.ChannelShuffle(0.05)
            ],
                    p=0.8),
            ToTensorV2()
        ])

        val_transform = A.Compose([ToTensorV2()])

        test_transform = A.Compose([ToTensorV2()])

    elif aug_type == "aug4":
        train_transform = A.Compose([
            A.HorizontalFlip(p=0.5),
            A.VerticalFlip(p=0.5),
            A.RandomRotate90(p=0.5),
            ToTensorV2()
        ])

        val_transform = A.Compose([ToTensorV2()])

        test_transform = A.Compose([ToTensorV2()])

    elif aug_type == "aug5":
        train_transform = A.Compose([
            A.VerticalFlip(p=0.5),
            A.RandomRotate90(p=0.5),
            #A.ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03),
            #A.GridDistortion(p=1.0),
            #A.OpticalDistortion(distort_limit=2, shift_limit=0.5, p=1)
            #A.VerticalFlip(p=0.5),
            #A.RandomBrightnessContrast(p=0.8),
            #A.RandomGamma(p=0.8),
            #A.RandomRotate90(p=0.5),
            #A.MultiplicativeNoise(multiplier=(0.9, 1.1), per_channel=True, p=0.8),
            ToTensorV2()
        ])

        val_transform = A.Compose([ToTensorV2()])

        test_transform = A.Compose([ToTensorV2()])

    return train_transform, val_transform, test_transform
Пример #21
0
def train_function(gpu, world_size, node_rank, gpus):
    import torch.multiprocessing
    torch.multiprocessing.set_sharing_strategy('file_system')

    torch.manual_seed(25)
    np.random.seed(25)

    rank = node_rank * gpus + gpu
    dist.init_process_group(
        backend='nccl',
        init_method='env://',
        world_size=world_size,
        rank=rank
    )

    width_size = 512
    batch_size = 32
    accumulation_step = 5
    device = torch.device("cuda:{}".format(gpu) if torch.cuda.is_available() else "cpu")

    if rank == 0:
        wandb.init(project='inception_v3', group=wandb.util.generate_id())
        wandb.config.width_size = width_size
        wandb.config.aspect_rate = 1
        wandb.config.batch_size = batch_size
        wandb.config.accumulation_step = accumulation_step

        shutil.rmtree('tensorboard_runs', ignore_errors=True)
        writer = SummaryWriter(log_dir='tensorboard_runs', filename_suffix=str(time.time()))

    ranzcr_df = pd.read_csv('train_folds.csv')
    ranzcr_train_df = ranzcr_df[ranzcr_df['fold'] != 1]

    chestx_df = pd.read_csv('chestx_pseudolabeled_data_lazy_balancing.csv')
    train_image_transforms = alb.Compose([
        alb.ImageCompression(quality_lower=65, p=0.5),
        alb.HorizontalFlip(p=0.5),
        alb.CLAHE(p=0.5),
        alb.OneOf([
            alb.GridDistortion(
                num_steps=8,
                distort_limit=0.5,
                p=1.0
            ),
            alb.OpticalDistortion(
                distort_limit=0.5,
                shift_limit=0.5,
                p=1.0,
            ),
            alb.ElasticTransform(alpha=3, p=1.0)],
            p=0.7
        ),
        alb.RandomResizedCrop(
            height=width_size,
            width=width_size,
            scale=(0.8, 1.2),
            p=0.7
        ),
        alb.RGBShift(p=0.5),
        alb.RandomSunFlare(p=0.5),
        alb.RandomFog(p=0.5),
        alb.RandomBrightnessContrast(p=0.5),
        alb.HueSaturationValue(
            hue_shift_limit=20,
            sat_shift_limit=20,
            val_shift_limit=20,
            p=0.5
        ),
        alb.ShiftScaleRotate(shift_limit=0.025, scale_limit=0.1, rotate_limit=20, p=0.5),
        alb.CoarseDropout(
            max_holes=12,
            min_holes=6,
            max_height=int(width_size / 6),
            max_width=int(width_size / 6),
            min_height=int(width_size / 6),
            min_width=int(width_size / 20),
            p=0.5
        ),
        alb.IAAAdditiveGaussianNoise(loc=0, scale=(2.5500000000000003, 12.75), per_channel=False, p=0.5),
        alb.IAAAffine(scale=1.0, translate_percent=None, translate_px=None, rotate=0.0, shear=0.0, order=1, cval=0,
                      mode='reflect', p=0.5),
        alb.IAAAffine(rotate=90., p=0.5),
        alb.IAAAffine(rotate=180., p=0.5),
        alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2()
    ])
    train_set = NoisyStudentDataset(ranzcr_train_df, chestx_df, train_image_transforms,
                                    '../ranzcr/train', '../data', width_size=width_size)
    train_sampler = DistributedSampler(train_set, num_replicas=world_size, rank=rank, shuffle=True)
    train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False, num_workers=4, sampler=train_sampler)

    ranzcr_valid_df = ranzcr_df[ranzcr_df['fold'] == 1]
    valid_image_transforms = alb.Compose([
        alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2()
    ])
    valid_set = ImageDataset(ranzcr_valid_df, valid_image_transforms, '../ranzcr/train', width_size=width_size)
    valid_loader = DataLoader(valid_set, batch_size=batch_size, num_workers=4, pin_memory=False, drop_last=False)

    # ranzcr_valid_df = ranzcr_df[ranzcr_df['fold'] == 1]
    # valid_image_transforms = alb.Compose([
    #     alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
    #     ToTensorV2()
    # ])
    # valid_set = ImageDataset(ranzcr_valid_df, valid_image_transforms, '../ranzcr/train', width_size=width_size)
    # valid_sampler = DistributedSampler(valid_set, num_replicas=world_size, rank=rank)
    # valid_loader = DataLoader(valid_set, batch_size=batch_size, num_workers=4, sampler=valid_sampler)

    checkpoints_dir_name = 'inception_v3_noisy_student_{}'.format(width_size)
    os.makedirs(checkpoints_dir_name, exist_ok=True)

    # model = EfficientNetNoisyStudent(11, pretrained_backbone=True,
    #                                  mixed_precision=True, model_name='tf_efficientnet_b7_ns')
    model = Inception(11, pretrained_backbone=True, mixed_precision=False, model_name='inception_v3')
    model = SyncBatchNorm.convert_sync_batchnorm(model)
    model.to(device)
    model = DistributedDataParallel(model, device_ids=[gpu])

    # class_weights = [354.625, 23.73913043478261, 2.777105767812362, 110.32608695652173,
    #                  52.679245283018865, 9.152656621728786, 4.7851333032083145,
    #                  8.437891632878731, 2.4620064899945917, 0.4034751151063363, 31.534942820838626]
    class_names = ['ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal',
                   'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal',
                   'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal', 'Swan Ganz Catheter Present']
    scaler = GradScaler()
    criterion = torch.nn.BCEWithLogitsLoss()

    lr_start = 1e-4
    lr_end = 1e-6
    weight_decay = 0
    epoch_num = 20
    if rank == 0:
        wandb.config.model_name = checkpoints_dir_name
        wandb.config.lr_start = lr_start
        wandb.config.lr_end = lr_end
        wandb.config.weight_decay = weight_decay
        wandb.config.epoch_num = epoch_num
        wandb.config.optimizer = 'adam'
        wandb.config.scheduler = 'CosineAnnealingLR'
        wandb.config.is_loss_weights = 'no'

    optimizer = Adam(model.parameters(), lr=lr_start, weight_decay=weight_decay)
    scheduler = CosineAnnealingLR(optimizer, T_max=epoch_num, eta_min=lr_end, last_epoch=-1)

    max_val_auc = 0

    for epoch in range(epoch_num):
        train_loss, train_avg_auc, train_auc, train_rocs, train_data_pr, train_duration = one_epoch_train(
            model, train_loader, optimizer, criterion, device, scaler,
            iters_to_accumulate=accumulation_step, clip_grads=False)
        scheduler.step()

        if rank == 0:
            val_loss, val_avg_auc, val_auc, val_rocs, val_data_pr, val_duration = eval_model(
                model, valid_loader, device, criterion, scaler)

            wandb.log({'train_loss': train_loss, 'val_loss': val_loss,
                       'train_auc': train_avg_auc, 'val_auc': val_avg_auc, 'epoch': epoch})
            for class_name, auc1, auc2 in zip(class_names, train_auc, val_auc):
                wandb.log({'{} train auc'.format(class_name): auc1,
                           '{} val auc'.format(class_name): auc2, 'epoch': epoch})

            if val_avg_auc > max_val_auc:
                max_val_auc = val_avg_auc
                wandb.run.summary["best_accuracy"] = val_avg_auc

            print('EPOCH %d:\tTRAIN [duration %.3f sec, loss: %.3f, avg auc: %.3f]\t\t'
                  'VAL [duration %.3f sec, loss: %.3f, avg auc: %.3f]\tCurrent time %s' %
                  (epoch + 1, train_duration, train_loss, train_avg_auc,
                   val_duration, val_loss, val_avg_auc, str(datetime.now(timezone('Europe/Moscow')))))

            torch.save(model.module.state_dict(),
                       os.path.join(checkpoints_dir_name, '{}_epoch{}_val_auc{}_loss{}_train_auc{}_loss{}.pth'.format(
                           checkpoints_dir_name, epoch + 1, round(val_avg_auc, 3), round(val_loss, 3),
                           round(train_avg_auc, 3), round(train_loss, 3))))
    if rank == 0:
        wandb.finish()
Пример #22
0
    建议把所有加噪声的方法加入此类
    """

    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, img):
        n = len(self.transforms)
        idx = np.random.randint(0, n - 1)
        t = self.transforms[idx]
        return t(image=img)


rand_weather = OneOf([
    am.RandomRain(),
    am.RandomFog(),
    am.RandomSnow(),
])  # 策略2,随机天气,自然界可能随机发生一些天气现象,增强模型对天气现象的抗干扰能力,参考链接https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

sharpOrBlur = OneOf([
    am.GaussianBlur(),  # 高斯模糊在mnist中我已经提供了一个自己实现的版本,这里选择调库
    am.IAASharpen(),
])

noiser = OneOf([
    GaussNoise(),
    SaltAndPepper(),
])

strategy = [eraser, rand_weather, sharpOrBlur, noiser]
Пример #23
0
    def __init__(self, args, main_dir='/opt/ml/', mode='train'):
        self.postfix = args.postfix
        self.main_dir = Path(main_dir)
        self.data_dir = self.main_dir / 'input/data'
        self.image_folder = self.data_dir / mode / 'cropped_images'
        self.meta_dir = self.data_dir / mode / str(mode + '.csv')
        
        self.gridshuffle = True if args.gridshuffle == 1 else False
        self.mixed_precision = True if args.mixed_precision == 1 else False
        self.n_fold = args.n_fold
        self.s_epoch = args.s_epoch
        self.t_epoch = args.t_epoch
        self.weight_path = None
        self.weighed_sampler = True if args.weighted_sampler == 1 else False
        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        
        
        self.nosiy_elimination = True if args.nosiy_elimination == 1 else False
        
        # criterion
        self.clipping = True if "nfnet" in args.model_type else False
        self.crit = args.crit
        self.arcface_crit = args.arcface_crit
        self.focal_type = args.focal_type
        self.cls_weight = True if args.cls_weight == 1 else False
        self.focal_gamma = 5.0
        
        # optimizer 
        self.optim = args.optim
        self.lr = args.lr
        self.weight_decay = args.decay
        
        # scheduler
        self.sched_type = args.sched_type
        self.sched_T_0 = args.T_max if args.T_max != 0 else self.t_epoch
        self.eta_min = args.eta_min
        
        # model
        self.cls_num = 18
        self.backbone_name = args.model_type
        self.checkpoint = self.main_dir / 'checkpoints' / str(self.backbone_name + "_" + args.postfix)
        if not os.path.exists(self.checkpoint):
            os.makedirs(self.checkpoint, exist_ok=True)
        
        self.backbone_pretrained = True if mode == 'train' else False
        self.embed_size = args.embed_size
        
        self.pool = args.pool
        self.p_trainable = True
        self.neck = args.neck
        
        self.multi_dropout = True if args.multi_dropout  == 1 else False
        self.multi_dropout_num = 16
        self.multi_dropout_prob = 0.2

        # pseudo label
        self.pseudo_label = True if args.pseudo_label == 1 else False
        self.pseudo_label_data = self.main_dir / 'submission' / args.pseudo_label_path
        
        
        # logging
        self.log_interval = 50
        self.log_dir = self.main_dir / 'logs' 
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir, exist_ok=True)
        self.log_dir = self.log_dir / (self.backbone_name + "_" + args.postfix + '.txt')
        
        self.mean = [0.56019358, 0.52410121, 0.501457]
        self.std  = [0.23318603, 0.24300033, 0.24567522]
        
        # transforms
        self.trn_tfms = A.Compose([
            A.Resize(384, 288, p=1.0),
            A.HorizontalFlip(p=0.7),
            A.GaussNoise(p=0.5),
            A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.25, rotate_limit=20, p=0.6, border_mode=0),
           
            A.OneOf([
                A.CLAHE(p=0.5),
                A.Compose([
                    A.RandomBrightness(limit=0.5, p=0.6),
                    A.RandomContrast(limit=0.4, p=0.6),
                    A.RandomGamma(p=0.6),        
                ])    
            ], p=0.65),
            
            A.OneOf([
                A.HueSaturationValue(10, 20, 10, p=1.0),
                A.RGBShift(p=1.0),
                A.Emboss(p=1.0),
            ], p=0.5),
            
            A.RandomFog(fog_coef_lower=0.3, fog_coef_upper=0.3, p=0.3),
            
            A.OneOf([
                A.Perspective(p=1.0, scale=(0.05, 0.1)),
                A.GridDistortion(p=1.0, distort_limit=0.25, border_mode=0),
                A.OpticalDistortion(p=1.0, shift_limit=0.1, distort_limit=0.1, border_mode=0)
            ], p=0.65),

            A.Normalize(p=1.0, mean=self.mean, std=self.std),
        ])
        
        self.val_tfms = A.Compose([
            A.Resize(384, 288),
            A.Normalize(p=1.0, mean=self.mean, std=self.std),
        ])
Пример #24
0
def get_config(runner,
               raw_uri,
               processed_uri,
               root_uri,
               test=False,
               external_model=False,
               external_loss=False,
               augment=False):
    debug = False
    train_scene_info = get_scene_info(join(processed_uri, 'train-scenes.csv'))
    val_scene_info = get_scene_info(join(processed_uri, 'val-scenes.csv'))
    log_tensorboard = True
    run_tensorboard = True
    class_config = ClassConfig(names=['no_building', 'building'])

    if test:
        debug = True
        train_scene_info = train_scene_info[0:1]
        val_scene_info = val_scene_info[0:1]

    def make_scene(scene_info):
        (raster_uri, label_uri) = scene_info
        raster_uri = join(raw_uri, raster_uri)
        label_uri = join(processed_uri, label_uri)
        aoi_uri = join(raw_uri, aoi_path)

        if test:
            crop_uri = join(processed_uri, 'crops',
                            os.path.basename(raster_uri))
            label_crop_uri = join(processed_uri, 'crops',
                                  os.path.basename(label_uri))

            save_image_crop(raster_uri,
                            crop_uri,
                            label_uri=label_uri,
                            label_crop_uri=label_crop_uri,
                            size=600,
                            min_features=20,
                            class_config=class_config)
            raster_uri = crop_uri
            label_uri = label_crop_uri

        id = os.path.splitext(os.path.basename(raster_uri))[0]
        raster_source = RasterioSourceConfig(channel_order=[0, 1, 2],
                                             uris=[raster_uri])
        label_source = ChipClassificationLabelSourceConfig(
            vector_source=GeoJSONVectorSourceConfig(uri=label_uri,
                                                    default_class_id=1,
                                                    ignore_crs_field=True),
            ioa_thresh=0.5,
            use_intersection_over_cell=False,
            pick_min_class_id=False,
            background_class_id=0,
            infer_cells=True)

        return SceneConfig(id=id,
                           raster_source=raster_source,
                           label_source=label_source,
                           aoi_uris=[aoi_uri])

    chip_sz = 200
    train_scenes = [make_scene(info) for info in train_scene_info]
    val_scenes = [make_scene(info) for info in val_scene_info]
    dataset = DatasetConfig(class_config=class_config,
                            train_scenes=train_scenes,
                            validation_scenes=val_scenes)

    if external_model:
        model = ClassificationModelConfig(external_def=ExternalModuleConfig(
            github_repo='lukemelas/EfficientNet-PyTorch',
            # uri='s3://raster-vision-ahassan/models/EfficientNet-PyTorch.zip',
            name='efficient_net',
            entrypoint='efficientnet_b0',
            force_reload=False,
            entrypoint_kwargs={
                'num_classes': len(class_config.names),
                'pretrained': 'imagenet'
            }))
    else:
        model = ClassificationModelConfig(backbone=Backbone.resnet50)

    if external_loss:
        external_loss_def = ExternalModuleConfig(
            github_repo='AdeelH/pytorch-multi-class-focal-loss',
            name='focal_loss',
            entrypoint='focal_loss',
            force_reload=False,
            entrypoint_kwargs={
                'alpha': [.75, .25],
                'gamma': 2
            })
    else:
        external_loss_def = None

    solver = SolverConfig(lr=1e-4,
                          num_epochs=20,
                          test_num_epochs=4,
                          batch_sz=32,
                          one_cycle=True,
                          external_loss_def=external_loss_def)

    if augment:
        mu = np.array((0.485, 0.456, 0.406))
        std = np.array((0.229, 0.224, 0.225))

        aug_transform = A.Compose([
            A.Flip(),
            A.Transpose(),
            A.RandomRotate90(),
            A.ShiftScaleRotate(),
            A.OneOf([
                A.ChannelShuffle(),
                A.CLAHE(),
                A.FancyPCA(),
                A.HueSaturationValue(),
                A.RGBShift(),
                A.ToGray(),
                A.ToSepia(),
            ]),
            A.OneOf([
                A.RandomBrightness(),
                A.RandomGamma(),
            ]),
            A.OneOf([
                A.GaussNoise(),
                A.ISONoise(),
                A.RandomFog(),
            ]),
            A.OneOf([
                A.Blur(),
                A.MotionBlur(),
                A.ImageCompression(),
                A.Downscale(),
            ]),
            A.CoarseDropout(max_height=32, max_width=32, max_holes=5)
        ])
        base_transform = A.Normalize(mean=mu.tolist(), std=std.tolist())
        plot_transform = A.Normalize(mean=(-mu / std).tolist(),
                                     std=(1 / std).tolist(),
                                     max_pixel_value=1.)
    else:
        aug_transform = None
        base_transform = None
        plot_transform = None

    backend = PyTorchChipClassificationConfig(
        model=model,
        solver=solver,
        log_tensorboard=log_tensorboard,
        run_tensorboard=run_tensorboard,
        test_mode=test,
        base_transform=A.to_dict(base_transform),
        aug_transform=A.to_dict(aug_transform),
        plot_options=PlotOptions(transform=A.to_dict(plot_transform)))

    config = ChipClassificationConfig(root_uri=root_uri,
                                      dataset=dataset,
                                      backend=backend,
                                      train_chip_sz=chip_sz,
                                      predict_chip_sz=chip_sz)
    return config
Пример #25
0
    A.RandomRotate90(),
    A.ShiftScaleRotate(scale_limit = 0.2, rotate_limit = 20, p = 0.9, border_mode=cv2.BORDER_REFLECT),
    A.OneOf([
        A.IAAPiecewiseAffine(),
        A.GridDistortion(),
        A.OpticalDistortion(),
    ], p = 1),
    A.OneOf([
        A.RandomBrightnessContrast(),
        A.HueSaturationValue(),
        A.CLAHE()
    ], p = 1),
    A.OneOf([
        A.RandomRain(),
        A.RandomSnow(),
        A.RandomFog()
    ], p = 1),
    A.Resize(size, size),
    A.Normalize()
], p = 1)
valid_trans = A.Compose([
    A.Resize(size, size),
    A.Normalize()
])

split = StratifiedKFold(n_folds, shuffle = True, random_state = seed)
model = None
for fold in range(fold, n_folds):
    METRIC = 0.
    logger(f"\n*******START: seed {seed}, fold {fold}*******")
Пример #26
0
    def augment(self,
        img1, bbox1 = [], keypoints1 = [], filtered_objs1 = [],
        img0 = None, bbox0 = [], keypoints0 = [], filtered_objs0 = [],
        do_img_aug = True, do_affine_aug = False, img1_to_img0 = False
    ):
        if img0 is None:
            img0 = img1.copy()

        replay_img_aug = []

        keypoints = keypoints1 + keypoints0
        bboxes = bbox1 + bbox0

        # Afine augmentations
        # --------------------------------------
        if do_affine_aug:
            afine_transform = A.ReplayCompose(
                [
                    A.HorizontalFlip(p=0.4),
                    A.OneOf([
                        # A.GridDistortion(interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0),
                        # A.ElasticTransform(interpolation=cv2.INTER_NEAREST, alpha_affine=10, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0),
                        A.ShiftScaleRotate(interpolation=cv2.INTER_NEAREST, shift_limit=0.035, rotate_limit=5, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0),
                        # A.OpticalDistortion(interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=1.0),
                    ], p=0.65),
                ],
                additional_targets={"img0": "image"},
                keypoint_params=A.KeypointParams(format="xy", remove_invisible=False),
                bbox_params=A.BboxParams(format='coco', label_fields=["bbox_ids"]), # coco format: [x-min, y-min, width, height]
            )
            transformed = afine_transform(image=img1, img0=img0, keypoints=keypoints, bboxes=bboxes, bbox_ids=np.arange(len(bboxes)))
            replay_img_aug.append(transformed["replay"])
            img1 = transformed["image"]
            img0 = transformed["img0"]
            transformed_keypoints = transformed['keypoints']
            transformed_bboxes = transformed['bboxes']
            bbox_ids = transformed["bbox_ids"]
            # it can happend that bounding boxes are removed, we have to account for that
            # and also remove the objects and keypoints in question
            idx_offset = len(bbox1)
            keypoints1 = []
            keypoints0 = []
            bbox1 = []
            bbox0 = []
            tmp_filtered_objs1 = []
            tmp_filtered_objs0 = []
            for i, bbox_id in enumerate(bbox_ids):
                kp_idx_start = bbox_id * 8
                kp_idx_end = bbox_id * 8 + 8
                if bbox_id < idx_offset:
                    tmp_filtered_objs1.append(filtered_objs1[i])
                    bbox1.append(transformed_bboxes[i])
                    keypoints1 += transformed_keypoints[kp_idx_start:kp_idx_end]
                else:
                    tmp_filtered_objs0.append(filtered_objs0[i])
                    bbox0.append(transformed_bboxes[i])
                    keypoints0 += transformed_keypoints[kp_idx_start:kp_idx_end]
            filtered_objs1 = tmp_filtered_objs1
            filtered_objs0 = tmp_filtered_objs0

        # Translate img0 + bbox0/keypoints0 for single track centernet
        # --------------------------------------
        if img1_to_img0:
            transform = A.Compose(
                [A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.15, rotate_limit=0, always_apply=True, border_mode=cv2.BORDER_CONSTANT)],
                keypoint_params=A.KeypointParams(format="xy", remove_invisible=False),
                bbox_params=A.BboxParams(format='coco', label_fields=[])
            )
            keypoints = list(np.array(piped_params["gt_2d_info"])[:,:2] * self.params.R)
            transformed = transform(image=img1, keypoints=keypoints1, bboxes=bboxes1)
            img0 = transformed["image"]
            bboxes0 = transformed['bboxes']
            keypoints0 = transformed['keypoints']

        # Augmentation for images
        # --------------------------------------
        if do_img_aug:
            transform = A.ReplayCompose([
                A.IAAAdditiveGaussianNoise(p=0.02),
                A.OneOf([
                    A.IAASharpen(p=1.0),
                    A.Blur(blur_limit=3, p=1.0),
                ] , p=0.5),
                A.OneOf([
                    A.RandomBrightnessContrast(p=1.0),
                    A.HueSaturationValue(p=1.0),
                    A.RandomGamma(p=1.0),
                ], p=0.6),
                A.OneOf([
                    A.RandomFog(p=1.0),
                    A.RandomRain(p=1.0),
                    A.RandomShadow(p=1.0),
                    A.RandomSnow(p=1.0)
                ], p=0.02),
            ], additional_targets={"img0": "image"})
            transformed = transform(image=img1, img0=img0)
            img1 = transformed["image"]
            img0 = transformed["img0"]

        return img1, bbox1, keypoints1, filtered_objs1, img0, bbox0, keypoints0, filtered_objs0, replay_img_aug
Пример #27
0
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
Пример #28
0
def getdata(img, count, disp=False):
    #randomly transform the template image to generate sample data
    #need to add a new function that generates data by registering real -
    #input images with template and thus generating transform matrix
    xtrain = []
    ytrain = []
    d = 20

    pts1 = np.float32([[0, 0], [width, 0], [width, height]])
    '''
    cv2.circle(img, tuple(pts1[0]), 5, (0, 0, 255), -1)
    cv2.circle(img, tuple(pts1[1]), 5, (0, 0, 255), -1)
    cv2.circle(img, tuple(pts1[2]), 5, (0, 0, 255), -1)
    cv2.imshow("Image", img)
    '''
    aug = A.Compose(
        [
            A.RandomBrightnessContrast(p=.5),
            A.RandomGamma(p=.5),
            A.RandomRain(
                brightness_coefficient=0.9, drop_width=1, blur_value=5, p=.5),
            A.RandomShadow(num_shadows_lower=1,
                           num_shadows_upper=1,
                           shadow_dimension=5,
                           shadow_roi=(0, 0.5, 1, 1),
                           p=.5),
            A.RandomFog(
                fog_coef_lower=0.1, fog_coef_upper=0.5, alpha_coef=0.1, p=.5),
            #A.RandomSunFlare(flare_roi=(0, 0, 0.2, 0.2), angle_lower=0.2, p=.5),
            #A.CLAHE(p=1),
            A.HueSaturationValue(hue_shift_limit=3,
                                 sat_shift_limit=50,
                                 val_shift_limit=50,
                                 p=.5),
        ],
        p=.8)

    for i in range(count):

        d0 = random.sample(range(-d, d), 2)
        d1 = random.sample(range(-d, d), 2)
        d2 = random.sample(range(-d, d), 2)
        #import pdb;pdb.set_trace()

        pts2 = np.float32([pts1[0] + d0, pts1[1] + d1, pts1[2] + d2])

        matrix = cv2.getAffineTransform(pts1, pts2)
        result = cv2.warpAffine(img, matrix, (width, height))
        #augmentations
        result = aug(image=result)['image']

        #import pdb;pdb.set_trace()
        matrix = cv2.invertAffineTransform(matrix)
        matrix = matrix.flatten()

        xtrain.append(result)
        ytrain.append(matrix)

        if disp == True:
            cv2.imshow("Affine transformation", result)
            cv2.waitKey(30)
    cv2.destroyAllWindows()
    xtrain = np.array(xtrain, dtype=np.float32)
    ytrain = np.array(ytrain, dtype=np.float32)
    return (xtrain / 255.0, ytrain)
Пример #29
0
import albumentations as A

transform_fn = A.Compose([
    A.OneOf([
        A.IAAAdditiveGaussianNoise(),
        A.GaussNoise(),
    ], p=.2),
    A.OneOf([
        A.MotionBlur(p=.2),
        A.MedianBlur(blur_limit=3, p=.2),
        A.Blur(blur_limit=3, p=.2),
    ], p=.2),
    A.OneOf([
        A.CLAHE(clip_limit=2),
        A.IAASharpen(),
        A.IAAEmboss(),
        A.RandomBrightnessContrast(),
    ], p=0.2),
    A.OneOf([
        A.ChannelShuffle(),
        A.HueSaturationValue(),
        A.RGBShift(),
        A.ToSepia(),
    ], p=0.2),
    A.OneOf([
        A.RandomSunFlare(flare_roi=(0, 0, 1., 1.), src_radius=100, p=.1),
        A.RandomFog(fog_coef_lower=0.2, fog_coef_upper=0.3, p=.1),
        A.RandomShadow(p=.1),
        A.RandomRain(p=.1, blur_value=1),
    ], p=0.1)
])
Пример #30
0
    def __call__(self, data):
        rgb, thermal, depth, audio, label, id = data

        rgb = rgb.astype(np.float32)
        height, width, _ = rgb.shape
        albumentations_transform_pixel = {
            'Blur': albumentations.Blur(),
            #'CLAHE':albumentations.CLAHE(),
            'ChannelDropout': albumentations.ChannelDropout(),
            'ChannelShuffle': albumentations.ChannelShuffle(),
            'CoarseDropout': albumentations.CoarseDropout(),
            #'Equalize':albumentations.Equalize(),
            #'FancyPCA':albumentations.FancyPCA(),
            'GaussNoise': albumentations.GaussNoise(),
            'GaussianBlur': albumentations.GaussianBlur(),
            #'GlassBlur':albumentations.GlassBlur(),
            'HueSaturationValue': albumentations.HueSaturationValue(),
            'IAAAdditiveGaussianNoise':
            albumentations.IAAAdditiveGaussianNoise(),
            #'ISONoise':albumentations.ISONoise(),
            'RGBShift': albumentations.RGBShift(),
            'RandomBrightnessContrast':
            albumentations.RandomBrightnessContrast(),
            'RandomFog': albumentations.RandomFog(),
            #'RandomGamma':albumentations.RandomGamma(),
            'RandomRain': albumentations.RandomRain(),
            'RandomShadow': albumentations.RandomShadow(),
            'RandomSnow': albumentations.RandomSnow(),
            'RandomSunFlare': albumentations.RandomSunFlare(),
            'Solarize': albumentations.Solarize(),
        }
        albumentations_transform_bbox = {
            #'HorizontalFlip':albumentations.HorizontalFlip(),
            #'VerticalFlip':albumentations.VerticalFlip(),
            #'CenterCrop':albumentations.CenterCrop(height=height-10, width=width-10, p=0.5),
            #'RandomCropNearBBox':albumentations.RandomCropNearBBox(p=0.5),
            #'Crop':albumentations.Crop(x_min=10, y_min =10, y_max=height-10, x_max=width-10, p=0.5),
            #'ElasticTransform':albumentations.ElasticTransform(),
            #'ShiftScaleRotate':albumentations.ShiftScaleRotate(),
        }
        transform = np.random.choice(
            ['None'] + list(albumentations_transform_pixel.keys()) +
            list(albumentations_transform_bbox.keys()))

        if transform in albumentations_transform_pixel:
            aug = albumentations.Compose(
                [albumentations_transform_pixel[transform]],
                bbox_params={
                    'format': 'pascal_voc',
                    'label_fields': ['labels']
                })
            try:
                annots = np.array(annots).astype(np.float32)
                aug_result = aug(image=rgb,
                                 bboxes=annots[:, :4],
                                 labels=annots[:, 4])
                rgb = aug_result['image']
                annots = np.hstack([
                    aug_result['bboxes'],
                    np.array(aug_result['labels']).reshape(-1, 1)
                ])
            except Exception as e:
                print(
                    f"transform={transform} aug_result['bboxes']={aug_result['bboxes']}            aug_result['labels']={aug_result['labels']}"
                )
                raise Exception(e)

        elif transform in albumentations_transform_bbox:
            aug = albumentations.Compose(
                [albumentations_transform_bbox[transform]],
                bbox_params={
                    'format': 'pascal_voc',
                    'label_fields': ['labels']
                })
            try:
                annots = np.array(annots).astype(np.float32)
                aug_result = aug(image=rgb,
                                 bboxes=annots[:, :4],
                                 labels=annots[:, 4])
                rgb = aug_result['image']
                label = np.hstack([
                    aug_result['bboxes'],
                    np.array(aug_result['labels']).reshape(-1, 1)
                ])
            except Exception as e:
                print(
                    f"transform={transform} aug_result['bboxes']={aug_result['bboxes']}            aug_result['labels']={aug_result['labels']}"
                )
                raise Exception(e)

        return rgb, thermal, depth, audio, label, id