예제 #1
0
파일: run_vinmec.py 프로젝트: tmquan/COVID
        ds_train = Vinmec(folder=args.data,
                          is_train='train',
                          fname='train.csv',
                          types=args.types,
                          pathology=args.pathology,
                          resize=int(args.shape))
       

        # ds_train = ConcatData([ds_chexpert, ds_vinmec])
        ag_train = [
            imgaug.ColorSpace(mode=cv2.COLOR_GRAY2RGB),
            imgaug.Affine(shear=10),
            imgaug.RandomChooseAug([
                imgaug.Albumentations(AB.Blur(blur_limit=4, p=0.25)),  
                imgaug.Albumentations(AB.MotionBlur(blur_limit=4, p=0.25)),  
                imgaug.Albumentations(AB.MedianBlur(blur_limit=4, p=0.25)),  
            ]),
            imgaug.RandomOrderAug(
                [imgaug.BrightnessScale((0.6, 1.4), clip=False),
                 imgaug.Contrast((0.6, 1.4), clip=False),
                 imgaug.Saturation(0.4, rgb=False),
                 # rgb-bgr conversion for the constants copied from
                 # fb.resnet.torch
                 imgaug.Lighting(0.1,
                                 eigval=np.asarray(
                                     [0.2175, 0.0188, 0.0045][::-1]) * 255.0,
                                 eigvec=np.array(
                                     [[-0.5675, 0.7192, 0.4009],
                                      [-0.5808, -0.0045, -0.8140],
                                      [-0.5836, -0.6948, 0.4203]],
                                     dtype='float32')[::-1, ::-1]
def load_data(fold: int, params: Dict[str, Any]) -> Any:
    torch.multiprocessing.set_sharing_strategy('file_system')
    cudnn.benchmark = True

    logger.info('Options:')
    logger.info(pprint.pformat(opt))

    full_df = pd.read_csv(opt.TRAIN.CSV)
    print('full_df', full_df.shape)
    train_df, val_df = train_val_split(full_df, fold)
    print('train_df', train_df.shape, 'val_df', val_df.shape)
    test_df = pd.read_csv(opt.TEST.CSV)

    # transform_train = transforms.Compose([
    #     # transforms.Resize((opt.MODEL.IMAGE_SIZE)), # smaller edge
    #     transforms.RandomCrop(opt.MODEL.INPUT_SIZE),
    #     transforms.RandomHorizontalFlip(),
    #     # transforms.ColorJitter(brightness=0.2, contrast=0.2),
    #     # transforms.RandomAffine(degrees=20, scale=(0.8, 1.2), shear=10, resample=PIL.Image.BILINEAR),
    #     # transforms.RandomCrop(opt.MODEL.INPUT_SIZE),
    # ])

    augs = []
    augs.append(albu.HorizontalFlip(.5))
    if int(params['vflip']):
        augs.append(albu.VerticalFlip(.5))
    if int(params['rotate90']):
        augs.append(albu.RandomRotate90())

    if params['affine'] == 'soft':
        augs.append(
            albu.ShiftScaleRotate(shift_limit=0.075,
                                  scale_limit=0.15,
                                  rotate_limit=10,
                                  p=.75))
    elif params['affine'] == 'medium':
        augs.append(
            albu.ShiftScaleRotate(shift_limit=0.0625,
                                  scale_limit=0.2,
                                  rotate_limit=45,
                                  p=0.2))
    elif params['affine'] == 'hard':
        augs.append(
            albu.ShiftScaleRotate(shift_limit=0.0625,
                                  scale_limit=0.50,
                                  rotate_limit=45,
                                  p=.75))

    if float(params['noise']) > 0.1:
        augs.append(
            albu.OneOf([
                albu.IAAAdditiveGaussianNoise(),
                albu.GaussNoise(),
            ],
                       p=float(params['noise'])))

    if float(params['blur']) > 0.1:
        augs.append(
            albu.OneOf([
                albu.MotionBlur(p=.2),
                albu.MedianBlur(blur_limit=3, p=0.1),
                albu.Blur(blur_limit=3, p=0.1),
            ],
                       p=float(params['blur'])))

    if float(params['distortion']) > 0.1:
        augs.append(
            albu.OneOf([
                albu.OpticalDistortion(p=0.3),
                albu.GridDistortion(p=.1),
                albu.IAAPiecewiseAffine(p=0.3),
            ],
                       p=float(params['distortion'])))

    if float(params['color']) > 0.1:
        augs.append(
            albu.OneOf([
                albu.CLAHE(clip_limit=2),
                albu.IAASharpen(),
                albu.IAAEmboss(),
                albu.RandomBrightnessContrast(),
            ],
                       p=float(params['color'])))

    transform_train = albu.Compose([
        albu.PadIfNeeded(opt.MODEL.INPUT_SIZE, opt.MODEL.INPUT_SIZE),
        albu.RandomCrop(height=opt.MODEL.INPUT_SIZE,
                        width=opt.MODEL.INPUT_SIZE),
        albu.Compose(augs, p=float(params['aug_global_prob'])),
    ])

    if opt.TEST.NUM_TTAS > 1:
        transform_test = albu.Compose([
            albu.PadIfNeeded(opt.MODEL.INPUT_SIZE, opt.MODEL.INPUT_SIZE),
            albu.RandomCrop(height=opt.MODEL.INPUT_SIZE,
                            width=opt.MODEL.INPUT_SIZE),
            albu.HorizontalFlip(),
        ])
    else:
        transform_test = albu.Compose([
            albu.PadIfNeeded(opt.MODEL.INPUT_SIZE, opt.MODEL.INPUT_SIZE),
            albu.CenterCrop(height=opt.MODEL.INPUT_SIZE,
                            width=opt.MODEL.INPUT_SIZE),
        ])

    train_dataset = Dataset(train_df,
                            path=opt.TRAIN.PATH,
                            mode='train',
                            num_classes=opt.MODEL.NUM_CLASSES,
                            resize=False,
                            augmentor=transform_train)

    val_dataset = Dataset(
        val_df,
        path=opt.TRAIN.PATH,
        mode='val',
        # image_size=opt.MODEL.INPUT_SIZE,
        num_classes=opt.MODEL.NUM_CLASSES,
        resize=False,
        num_tta=1,  # opt.TEST.NUM_TTAS,
        augmentor=transform_test)
    test_dataset = Dataset(
        test_df,
        path=opt.TEST.PATH,
        mode='test',
        # image_size=opt.MODEL.INPUT_SIZE,
        num_classes=opt.MODEL.NUM_CLASSES,
        resize=False,
        num_tta=opt.TEST.NUM_TTAS,
        augmentor=transform_test)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=opt.TRAIN.BATCH_SIZE,
                                               shuffle=True,
                                               num_workers=opt.TRAIN.WORKERS)

    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=opt.TRAIN.BATCH_SIZE,
                                             shuffle=False,
                                             num_workers=opt.TRAIN.WORKERS)

    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=opt.TRAIN.BATCH_SIZE,
                                              shuffle=False,
                                              num_workers=opt.TRAIN.WORKERS)

    return train_loader, val_loader, test_loader
예제 #3
0
def get_transform(size, transform_type="weak", min_visibility=0):
    """Creates transformation for COCO dataset
    Args:
        size (int): image size to return
        transform_type (str):
            'weak': resizes and normalizes image and bbox
            'strong': performs different image effects, resizes and normalizes image and bbox
        min_visibility (int): minimum fraction of area for a bounding box
    Returns:
        albu.core.transforms_interface.BasicTransform: image and bbox transformation
    """
    bbox_params = {
        'format': 'coco',
        'min_visibility': min_visibility,
        'label_fields': ['category_id']
    }

    augs = {
        'strong':
        albu.Compose(
            [
                albu.Resize(size, size),
                albu.HorizontalFlip(),
                # albu.VerticalFlip(p=0.1),
                albu.ShiftScaleRotate(shift_limit=0.05,
                                      scale_limit=0.15,
                                      rotate_limit=20,
                                      p=.4,
                                      border_mode=cv2.BORDER_CONSTANT),
                albu.OneOf([
                    albu.Blur(),
                    albu.MotionBlur(),
                    albu.MedianBlur(),
                    albu.GaussianBlur(),
                ],
                           p=0.2),
                albu.OneOf([
                    albu.GaussNoise(var_limit=(10, 35)),
                    albu.IAAAdditiveGaussianNoise(),
                    albu.JpegCompression(quality_lower=50),
                ],
                           p=0.2),
                albu.OneOf(
                    [
                        albu.RandomRain(),
                        # albu.RandomSunFlare(),
                        albu.RandomShadow()
                    ],
                    p=0.15),
                albu.OneOf([
                    albu.CLAHE(clip_limit=2),
                    albu.IAASharpen(alpha=(0.1, 0.3)),
                    albu.IAAEmboss(alpha=(0.1, 0.4)),
                    albu.RandomGamma(),
                    albu.RandomBrightnessContrast(),
                ],
                           p=0.2),
                # albu.HueSaturationValue(p=0.25),
            ],
            bbox_params=bbox_params),
        'weak':
        albu.Compose(
            [
                albu.Resize(size, size),
                # albu.HorizontalFlip(),
            ],
            bbox_params=bbox_params),
    }

    aug_fn = augs[transform_type]
    normalize = albu.Normalize(mean=[0.485, 0.456, 0.406],
                               std=[0.229, 0.224, 0.225])

    pipeline = albu.Compose([
        aug_fn,
        # ])
        normalize
    ])

    return pipeline
예제 #4
0
import os
import json
import glob
import SimpleITK as sitk
import pandas as pd
import cv2
import albumentations as A
from aug_transforms import aug_transform
a_transform = A.Compose([
    A.OneOf([
        A.IAAAdditiveGaussianNoise(),
        A.GaussNoise(),
    ], p=0.2),
    A.OneOf([
        A.MotionBlur(p=.2),
        A.MedianBlur(blur_limit=3, p=0.1),
        A.Blur(blur_limit=3, p=0.1),
    ],
            p=0.2),
    A.OneOf([
        A.IAAAdditiveGaussianNoise(),
        A.GaussNoise(),
    ], p=0.2),
    A.OneOf([
        A.CLAHE(clip_limit=2),
        A.IAASharpen(),
        A.IAAEmboss(),
        A.RandomBrightnessContrast(),
    ],
            p=0.3)
])
예제 #5
0
    "lookahead":False,
    "k_param":5,
    "alpha_param":0.5,
    "patience":3,
    "DEBUG":False,
}

args['trn_transforms'] = A.Compose([ 
                            # A.Transpose(p=0.5),
                            A.HorizontalFlip(p=0.5), 
                            A.VerticalFlip(p=0.5),
                            # A.RandomBrightness(limit=0.2, p=0.75),
                            # A.RandomContrast(limit=0.2, p=0.75), 
                            A.OneOf([
                                A.MotionBlur(blur_limit=5),
                                A.MedianBlur(blur_limit=5),
                                A.GaussianBlur(blur_limit=5),
                                A.GaussNoise(var_limit=(5.0, 30.0)),
                            ], p=0.7),
                            # A.OneOf([
                            #     A.OpticalDistortion(distort_limit=1.0),
                            #     A.GridDistortion(num_steps=5, distort_limit=1.),
                            #     A.ElasticTransform(alpha=3),
                            # ], p=0.7),

                            A.CLAHE(clip_limit=4.0, p=0.7),
                            # A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=20, val_shift_limit=10, p=0.5),
                            # A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=15, border_mode=0, p=0.85),
                            # A.Cutout(max_h_size=int(256 * 0.375), max_w_size=int(256 * 0.375), num_holes=1, p=0.7),  
                        ])
args['val_transforms'] = A.Compose([
예제 #6
0
N = 2
M = 3

train_transforms = A.Compose([
    A.CenterCrop(img_size[0], img_size[1], p=1),
    # A.RandomResizedCrop(img_size[0], img_size[1], p=1.0),
    # A.Resize(img_size[0], img_size[1], p=1.0),
    RandAugment(N, M),
    A.OneOf(
        [A.RandomBrightness(limit=0.1, p=1),
         A.RandomContrast(limit=0.1, p=1)],
        p=0.5),
    A.OneOf(
        [
            A.MotionBlur(blur_limit=3),
            A.MedianBlur(blur_limit=3),
            A.GaussianBlur(blur_limit=3),
        ],
        p=0.5,
    ),
    A.VerticalFlip(p=0.5),
    A.HorizontalFlip(p=0.5),
    A.ShiftScaleRotate(
        shift_limit=0.2,
        scale_limit=0.2,
        rotate_limit=20,
        interpolation=cv2.INTER_LINEAR,
        border_mode=cv2.BORDER_REFLECT_101,
        p=.75,
    ),
    A.Cutout(num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
예제 #7
0
def get_transforms(image_size):
    '''
    albumentations 라이브러리 사용함
    https://github.com/albumentations-team/albumentations

    TODO: FAST AUTO AUGMENT
    https://github.com/kakaobrain/fast-autoaugment
    DATASET의 AUGMENT POLICY를 탐색해주는 알고리즘

    TODO: Unsupervised Data Augmentation for Consistency Training
    https://github.com/google-research/uda

    TODO: Cutmix vs Mixup vs Gridmask vs Cutout
    https://www.kaggle.com/saife245/cutmix-vs-mixup-vs-gridmask-vs-cutout

    '''
    transforms_train = albumentations.Compose([
        # albumentations.Transpose(p=0.5),
        # albumentations.VerticalFlip(p=0.5),
        albumentations.HorizontalFlip(p=0.5),
        albumentations.RandomBrightness(limit=0.2, p=0.75),
        albumentations.RandomContrast(limit=0.2, p=0.75),

        # one of 의 경우 하나를 랜덤하게 뽑아서 쓰게 해줌
        albumentations.OneOf([
            albumentations.MotionBlur(blur_limit=5),
            albumentations.MedianBlur(blur_limit=5),
            albumentations.GaussianBlur(blur_limit=5),
            albumentations.GaussNoise(var_limit=(5.0, 30.0)),
        ],
                             p=0.7),
        albumentations.OneOf([
            albumentations.OpticalDistortion(distort_limit=1.0),
            albumentations.GridDistortion(num_steps=5, distort_limit=1.),
            albumentations.ElasticTransform(alpha=3),
        ],
                             p=0.7),
        albumentations.CLAHE(clip_limit=4.0, p=0.7),
        albumentations.HueSaturationValue(hue_shift_limit=10,
                                          sat_shift_limit=20,
                                          val_shift_limit=10,
                                          p=0.5),
        albumentations.ShiftScaleRotate(shift_limit=0.1,
                                        scale_limit=0.1,
                                        rotate_limit=15,
                                        border_mode=0,
                                        p=0.85),
        albumentations.Resize(image_size, image_size),
        albumentations.Cutout(max_h_size=int(image_size * 0.375),
                              max_w_size=int(image_size * 0.375),
                              num_holes=1,
                              p=0.7),
        albumentations.Normalize()
    ])

    transforms_val = albumentations.Compose([
        albumentations.Resize(image_size, image_size),
        # albumentations.GaussianBlur(blur_limit=3, p=1.0),
        albumentations.Normalize()
    ])

    return transforms_val, transforms_val
        alb.RandomRotate90(),
        alb.ShiftScaleRotate(),
        alb.RandomGamma(),
        alb.RandomBrightnessContrast(),
        alb.HueSaturationValue(),
        alb.RGBShift(),
        alb.OneOf([
            alb.ElasticTransform(),
            alb.GridDistortion(),
            alb.OpticalDistortion(distort_limit=0.1)
        ],
                  p=0.8),
        alb.OneOf([
            alb.Blur(),
            alb.MotionBlur(),
            alb.MedianBlur(),
            alb.GaussianBlur()
        ]),
        alb.GaussNoise(p=0.25)
    ])

    for batch_idx, (augm_img_batch, augm_mask_batch) in \
        enumerate(AugmentedSequence(root_path=TEST_PATH, img_size=IMG_SIZE, augm_compose=augm, augm_count=3, batch_size=4)):

        print(f"{batch_idx}: {len(augm_img_batch)}")
        print("---")

        for augm_img_idx, augm_img in enumerate(augm_img_batch):
            cv2.imwrite(
                os.path.join(test_augm_img_path,
                             f"batch_{batch_idx}-{augm_img_idx}.png"),
예제 #9
0
import albumentations as A

train_tr = A.Compose([
    A.CLAHE(p=0.2),
    A.Flip(p=0.3),
    A.OneOf([A.GaussianBlur(),
             A.MedianBlur(),
             A.GaussNoise()], p=0.2),
    A.OpticalDistortion(p=0.2),
    A.OneOf([
            A.IAASharpen(),
            A.RandomBrightnessContrast()], p=0.2)
])

val_tr = A.Compose([
])
예제 #10
0
    def get_images(self, imgpath):
        # Pick random clone, crypt or fufi
        u01 = np.random.uniform()
        if u01 < self.cpfr_frac[0]:
            img, mask = self.all_svs_opened[imgpath].fetch_clone(
                prop_displ=0.45)
        elif u01 < np.sum(self.cpfr_frac[0:2]):
            img, mask = self.all_svs_opened[imgpath].fetch_partial(
                prop_displ=0.45)
        elif u01 < np.sum(self.cpfr_frac[0:3]):
            img, mask = self.all_svs_opened[imgpath].fetch_fufi(
                prop_displ=0.45)
        else:
            img, mask = self.all_svs_opened[imgpath].fetch_rndmtile()

        if self.dilate_masks == True:
            n_dil = int(5 / self.um_per_pixel)  # if mpp is one or less than 1
            # dilate if desired
            for i in range(mask.shape[2]):
                mask[:, :, i] = cv2.morphologyEx(mask[:, :, i].copy(),
                                                 cv2.MORPH_DILATE,
                                                 st_3,
                                                 iterations=n_dil)

        if self.aug == True:
            composition = A.Compose([
                A.HorizontalFlip(),
                A.VerticalFlip(),
                A.Rotate(border_mode=cv2.BORDER_CONSTANT),
                A.OneOf([
                    A.ElasticTransform(alpha=1000,
                                       sigma=30,
                                       alpha_affine=30,
                                       border_mode=cv2.BORDER_CONSTANT,
                                       p=1),
                    A.GridDistortion(border_mode=cv2.BORDER_CONSTANT, p=1),
                ],
                        p=0.5),
                A.CLAHE(p=0.2),
                A.HueSaturationValue(hue_shift_limit=12,
                                     sat_shift_limit=12,
                                     val_shift_limit=12,
                                     p=0.3),
                A.RandomBrightnessContrast(p=0.3),
                A.Posterize(p=0.1, num_bits=4),
                A.OneOf([
                    A.JpegCompression(p=1),
                    A.MedianBlur(p=1),
                    A.Blur(p=1),
                    A.GlassBlur(p=1, max_delta=2, sigma=0.4),
                    A.IAASharpen(p=1)
                ],
                        p=0.3)
            ],
                                    p=1)
            transformed = composition(image=img, mask=mask)
            img, mask = transformed['image'], transformed['mask']
        mask_list = [mask[:, :, ii] for ii in range(mask.shape[2])]

        if self.stride_bool:
            mask_list = [cv2.pyrDown(mask_ii.copy()) for mask_ii in mask_list]

        mask_list = [
            cv2.threshold(mask_ii, 120, 255, cv2.THRESH_BINARY)[1]
            for mask_ii in mask_list
        ]

        ## convert to floating point space, normalize and mask non-used clones
        img = img.astype(np.float32) / 255
        mask_list = [mask_ii.astype(np.float32) / 255 for mask_ii in mask_list]
        if self.normalize:
            img = (img - self.norm_mean) / self.norm_std

        return img, np.stack(mask_list, axis=2)
예제 #11
0
from dataset import MyDataset, AlbuWrapper, tensor2img
import albumentations as alb
import albumentations.augmentations.transforms as aat
import albumentations
import pandas as pd
import matplotlib.pyplot as plt
from tqdm.auto import tqdm
import pathlib
from dataset import TRAFFIC_LABELS_TO_NUM

alb_transforms = [
    alb.IAAAdditiveGaussianNoise(p=1),
    alb.GaussNoise(p=1),
    alb.MotionBlur(p=1),
    alb.MedianBlur(blur_limit=3, p=1),
    alb.Blur(blur_limit=3, p=1),
    alb.OpticalDistortion(p=1),
    alb.GridDistortion(p=1),
    alb.IAAPiecewiseAffine(p=1),
    aat.CLAHE(clip_limit=2, p=1),
    alb.IAASharpen(p=1),
    alb.IAAEmboss(p=1),
    aat.HueSaturationValue(p=0.3),
    aat.HorizontalFlip(p=1),
    aat.RGBShift(),
    aat.RandomBrightnessContrast(),
    aat.RandomGamma(p=1),
    aat.Cutout(2, 10, 10, p=1),
    aat.Equalize(mode='cv', p=1),
    aat.FancyPCA(p=1),
    aat.RandomFog(p=1),
예제 #12
0
def get_augmentation_fcn2(mode, p=0.75):
    if not mode or mode.lower() == 'none':
        return None
    augmentation_dict = {
        'no_interpolation_necessary':
        al.OneOf([al.RandomRotate90(p=1.),
                  al.Flip(p=1.)]),
        'interpolation_necessary':
        al.OneOf(
            [al.Rotate(p=1.),
             al.RandomScale(p=1.),
             al.ShiftScaleRotate(p=1.)]),
        'affine':
        al.Compose([al.ShiftScaleRotate(p=1.),
                    al.HorizontalFlip(p=0.5)]),
        'rot':
        al.Rotate(p=1.),
        'rot90':
        al.RandomRotate90(p=1.),
        'flip':
        al.Flip(p=1.),
        'hflip':
        al.HorizontalFlip(p=1.),
        'vflip':
        al.VerticalFlip(p=1.),
        'scale':
        al.RandomScale(p=1.),
        'ssr':
        al.ShiftScaleRotate(p=1.),
        'strong':
        al.Compose(
            [
                # al.RandomRotate90(),
                # al.Flip(),
                # al.Transpose(),
                al.OneOf([
                    al.IAAAdditiveGaussianNoise(),
                    al.GaussNoise(),
                ],
                         p=0.2),
                al.OneOf([
                    al.MotionBlur(p=0.2),
                    al.MedianBlur(blur_limit=3, p=0.1),
                    al.Blur(blur_limit=3, p=0.1),
                ],
                         p=0.2),
                al.ShiftScaleRotate(shift_limit=0.0625,
                                    scale_limit=0.2,
                                    rotate_limit=10,
                                    p=0.2),
                # al.OneOf([
                #     al.OpticalDistortion(p=0.3),
                #     al.GridDistortion(p=0.1),
                #     al.IAAPiecewiseAffine(p=0.3),
                # ], p=0.2),
                al.OneOf(
                    [
                        # al.CLAHE(clip_limit=2),
                        al.IAASharpen(),
                        al.IAAEmboss(),
                        al.RandomBrightnessContrast(),
                    ],
                    p=0.3),
                al.HueSaturationValue(p=0.3),
            ],
            p=p),
        'truss_points':
        al.Compose([
            al.OneOf([
                al.IAAAdditiveGaussianNoise(),
                al.GaussNoise(),
            ], p=0.2),
            al.OneOf([
                al.MotionBlur(p=0.2),
                al.MedianBlur(blur_limit=3, p=0.1),
                al.Blur(blur_limit=3, p=0.1),
            ],
                     p=0.2),
            al.ShiftScaleRotate(
                shift_limit=0.0625, scale_limit=0.2, rotate_limit=10, p=0.2),
            al.OneOf([
                al.IAASharpen(),
                al.IAAEmboss(),
                al.RandomBrightnessContrast(),
            ],
                     p=0.3),
            al.HueSaturationValue(p=0.3),
        ],
                   p=p)
    }

    def aug_fcn(**kwargs):
        # params: image, mask, masks, bboxes, keypoints
        # note keypoints of form (x,y,a,s) ... I think really tangent vectors
        return augmentation_dict[mode](**kwargs)

    return aug_fcn
예제 #13
0
def get_augmentation_fcn(mode, p=0.75):
    if not mode or mode.lower() == 'none':
        return None
    augmentation_dict = {
        'no_interpolation_necessary':
        al.OneOf([al.RandomRotate90(p=1.),
                  al.Flip(p=1.)]),
        'interpolation_necessary':
        al.OneOf(
            [al.Rotate(p=1.),
             al.RandomScale(p=1.),
             al.ShiftScaleRotate(p=1.)]),
        'affine':
        al.Compose([al.ShiftScaleRotate(p=1.),
                    al.HorizontalFlip(p=0.5)]),
        'rot':
        al.Rotate(p=1.),
        'rot90':
        al.RandomRotate90(p=1.),
        'flip':
        al.Flip(p=1.),
        'hflip':
        al.HorizontalFlip(p=1.),
        'vflip':
        al.VerticalFlip(p=1.),
        'scale':
        al.RandomScale(p=1.),
        'ssr':
        al.ShiftScaleRotate(p=1.),
        'strong':
        al.Compose(
            [
                # al.RandomRotate90(),
                # al.Flip(),
                # al.Transpose(),
                al.OneOf([
                    al.IAAAdditiveGaussianNoise(),
                    al.GaussNoise(),
                ],
                         p=0.2),
                al.OneOf([
                    al.MotionBlur(p=0.2),
                    al.MedianBlur(blur_limit=3, p=0.1),
                    al.Blur(blur_limit=3, p=0.1),
                ],
                         p=0.2),
                al.ShiftScaleRotate(shift_limit=0.0625,
                                    scale_limit=0.2,
                                    rotate_limit=10,
                                    p=0.2),
                # al.OneOf([
                #     al.OpticalDistortion(p=0.3),
                #     al.GridDistortion(p=0.1),
                #     al.IAAPiecewiseAffine(p=0.3),
                # ], p=0.2),
                al.OneOf(
                    [
                        # al.CLAHE(clip_limit=2),
                        al.IAASharpen(),
                        al.IAAEmboss(),
                        al.RandomBrightnessContrast(),
                    ],
                    p=0.3),
                al.HueSaturationValue(p=0.3),
            ],
            p=p)
    }

    def aug_fcn(x):
        try:
            return augmentation_dict[mode](image=x)['image']
        except Exception as e:
            print("Exception caught in augmentation stage:", e)
            return x

    return aug_fcn
import json

RESIZE_SIZE = 1024
train_transform = albumentations.Compose([
    albumentations.Resize(RESIZE_SIZE, RESIZE_SIZE),
    albumentations.OneOf([
        albumentations.RandomGamma(gamma_limit=(60, 120), p=0.9),
        albumentations.RandomBrightnessContrast(brightness_limit=0.2,
                                                contrast_limit=0.2,
                                                p=0.9),
        albumentations.CLAHE(clip_limit=4.0, tile_grid_size=(4, 4), p=0.9),
    ]),
    albumentations.OneOf([
        albumentations.Blur(blur_limit=4, p=1),
        albumentations.MotionBlur(blur_limit=4, p=1),
        albumentations.MedianBlur(blur_limit=4, p=1)
    ],
                         p=0.5),
    albumentations.HorizontalFlip(p=0.5),
    albumentations.ShiftScaleRotate(shift_limit=0.2,
                                    scale_limit=0.2,
                                    rotate_limit=20,
                                    interpolation=cv2.INTER_LINEAR,
                                    border_mode=cv2.BORDER_CONSTANT,
                                    p=1),
    albumentations.Normalize(mean=(0.485, 0.456, 0.406),
                             std=(0.229, 0.224, 0.225),
                             max_pixel_value=255.0,
                             p=1.0)
])
예제 #15
0
def get_augmentation(_C, is_train):
    """"""
    if is_train:
        augmentation = [
            # random flip
            A.HorizontalFlip(p=_C.TRANSFORM.TRAIN_HORIZONTAL_FLIP_PROB),
            A.VerticalFlip(p=_C.TRANSFORM.TRAIN_VERTICAL_FLIP_PROB),
            # random rotate
            A.ShiftScaleRotate(
                scale_limit=0.0,
                rotate_limit=_C.TRANSFORM.TRAIN_RANDOM_ROTATE_DEG,
                shift_limit=0.0,
                p=_C.TRANSFORM.TRAIN_RANDOM_ROTATE_PROB,
                border_mode=0,
            ),
            # random crop
            A.RandomCrop(
                width=_C.TRANSFORM.TRAIN_RANDOM_CROP_SIZE[0],
                height=_C.TRANSFORM.TRAIN_RANDOM_CROP_SIZE[1],
                p=_C.TRANSFORM.TRAIN_RANDOM_CROP_PROB,
            ),
            # speckle noise
            A.Lambda(image=functools.partial(
                _random_speckle_noise,
                speckle_std=_C.TRANSFORM.TRAIN_SPECKLE_NOISE_STD,
                p=_C.TRANSFORM.TRAIN_SPECKLE_NOISE_PROB,
            )),
            # blur
            A.OneOf(
                [
                    A.MotionBlur(p=_C.TRANSFORM.TRAIN_BLUR_MOTION_PROB),
                    A.MedianBlur(
                        blur_limit=_C.TRANSFORM.TRAIN_BLUR_MEDIAN_LIMIT,
                        p=_C.TRANSFORM.TRAIN_BLUR_MEDIAN_PROB,
                    ),
                    A.Blur(
                        blur_limit=_C.TRANSFORM.TRAIN_BLUR_LIMIT,
                        p=_C.TRANSFORM.TRAIN_BLUR_PROB,
                    ),
                ],
                p=_C.TRANSFORM.TRAIN_BLUR_ONEOF,
            ),
            # random brightness
            A.Lambda(
                image=functools.partial(
                    _random_brightness,
                    brightness_std=_C.TRANSFORM.TRAIN_RANDOM_BRIGHT_STD_STD,
                    p=_C.TRANSFORM.TRAIN_RANDOM_BRIGHT_STD_PROB,
                ),
                # transforms.ToTensor(),
                # transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
                # ])
            ),
        ]
    else:
        augmentation = [
            A.PadIfNeeded(
                min_width=_C.TRANSFORM.TEST_SIZE[0],
                min_height=_C.TRANSFORM.TEST_SIZE[1],
                always_apply=True,
                border_mode=0,
            )
        ]
    return A.Compose(augmentation)
예제 #16
0
def train(root_path: str = "./data/",
          train_dir: str = "training",
          val_dir: str = "validation",
          img_dir: str = "image",
          mask_dir: str = "mask",
          backbone: str = "efficientnetb4",
          img_size: int = 192,
          augm_count: int = 19,
          batch_size: int = 5,
          LR: float = 0.0001,
          epochs: int = 10,
          model_file: str = "./last_best_model.h5",
          plot_history: bool = False):
    """
    Train unet on provided dataset, saving the best weights.

    Parameters
    ----------
    root_path : string
        The path of the directory which contains the training directory and the validation directory.
    train_dir : string
        The path of the training directory relative to 'root_path'.
    val_dir : string
        The path of the validation directory relative to 'root_path'.
    img_dir : string
        The path of the image directory relative to both 'img_dir' and 'mask_dir'.
    mask_dir : string
        The path of the mask directory relative to both 'img_dir' and 'mask_dir'.
    backbone : string
        The backbone used for unet.
    img_size : int
        Size of the crop for each image fed to the model.
    augm_count : int
        Number of augmentations for each image in the training dataset.
    batch_size : int
        Size of the batch of images fed to the model.
    LR : float
        Learning rate of the model.
    epochs : int
        Number of training epochs.
    model_file : string
        Path of the file in which to save the best weights of the model.
    plot_history : bool
        If True, save the training history to file.
        The file name has the pattern '{number_of_images}i_{img_size}_{augm_count}a_{epochs}e.png'.

    Returns
    -------

    -
    """

    # define augmentation

    augm = alb.Compose([
        alb.Flip(),
        alb.Transpose(),
        alb.RandomRotate90(),
        alb.ShiftScaleRotate(),
        alb.RandomGamma(),
        alb.RandomBrightnessContrast(),
        alb.HueSaturationValue(),
        alb.RGBShift(),
        alb.OneOf([
            alb.ElasticTransform(),
            alb.GridDistortion(),
            alb.OpticalDistortion(distort_limit=0.1)
        ],
                  p=0.8),
        alb.OneOf([
            alb.Blur(),
            alb.MotionBlur(),
            alb.MedianBlur(),
            alb.GaussianBlur()
        ]),
        alb.GaussNoise(p=0.25)
    ])

    # create model
    model = sm.Unet(backbone, encoder_weights="imagenet"
                    )  # activation sigmoid by default, ok for 1 class

    # define optimizer
    optim = keras.optimizers.Adam(learning_rate=LR)

    # define metrics
    metrics = [
        sm.metrics.IOUScore(threshold=0.5),
        sm.metrics.FScore(threshold=0.5)
    ]

    # compile keras model with defined optimizer, loss and metrics
    model.compile(optim, sm.losses.binary_focal_dice_loss, metrics)

    # define train and validation generators

    train_generator = AugmentedSequence(root_path=os.path.join(
        root_path, train_dir),
                                        img_dir=img_dir,
                                        mask_dir=mask_dir,
                                        img_size=img_size,
                                        batch_size=batch_size,
                                        augm_count=augm_count,
                                        augm_compose=augm)

    valid_generator = AugmentedSequence(root_path=os.path.join(
        root_path, val_dir),
                                        img_dir=img_dir,
                                        mask_dir=mask_dir,
                                        img_size=img_size,
                                        batch_size=batch_size,
                                        augm_count=0)

    # check shapes for errors
    assert train_generator[0][0].shape == (batch_size, img_size, img_size, 3)
    assert train_generator[0][1].shape == (batch_size, img_size, img_size,
                                           1), train_generator[0][1].shape

    # define callbacks for learning rate reduction and best checkpoint saving
    callbacks = [
        keras.callbacks.ModelCheckpoint(model_file,
                                        save_best_only=True,
                                        save_weights_only=True,
                                        mode="min"),
        keras.callbacks.ReduceLROnPlateau()
    ]

    # train model
    history = model.fit_generator(
        train_generator,
        epochs=epochs,
        callbacks=callbacks,
        validation_data=valid_generator,
        #use_multiprocessing=True  # crashes, BSODs, the apocalypse
    )

    if plot_history:
        plot = plot_training_history(history)
        plot.savefig(
            f"{len(train_generator.img_files)}i_{img_size}_{augm_count}a_{epochs}e.png"
        )
예제 #17
0
from torch.utils.data import Dataset, DataLoader

from configure import SPLIT_FOLDER, TRAIN_DATA_FOLDER
import albumentations as albu

train_aug = albu.Compose([
    albu.OneOf([
        albu.RandomBrightnessContrast(
            brightness_limit=0.2, contrast_limit=0.2, p=1),
        albu.CLAHE(clip_limit=4.0, tile_grid_size=(4, 4), p=1),
    ],
               p=0.5),
    albu.OneOf([
        albu.Blur(blur_limit=4, p=1),
        albu.MotionBlur(blur_limit=4, p=1),
        albu.MedianBlur(blur_limit=4, p=1)
    ],
               p=0.5),
    albu.OneOf([albu.GridDistortion(p=1),
                albu.OpticalDistortion(p=1)], p=0.5),
    albu.HorizontalFlip(p=0.5),
    albu.VerticalFlip(p=0.5),
    albu.RandomSizedCrop(min_max_height=(160, 320),
                         height=320,
                         width=640,
                         w2h_ratio=2,
                         p=0.5),
    albu.ShiftScaleRotate(shift_limit=0.2,
                          scale_limit=0.2,
                          rotate_limit=45,
                          interpolation=cv2.INTER_LINEAR,
예제 #18
0
        use_validation_loss = False

    if student_learn_from_sfm:
        training_transforms = albu.Compose([
            # Color augmentation
            albu.OneOf([
                albu.Compose([
                    albu.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3, p=0.5),
                    albu.RandomGamma(gamma_limit=(50, 180), p=0.5),
                    albu.HueSaturationValue(hue_shift_limit=30, sat_shift_limit=0, val_shift_limit=0, p=0.5)]),
                albu.HueSaturationValue(hue_shift_limit=30, sat_shift_limit=30, val_shift_limit=30, p=0.5)
            ]),
            # Image quality augmentation
            albu.OneOf([
                albu.Blur(p=0.5),
                albu.MedianBlur(p=0.5),
                albu.MotionBlur(p=0.5),
                albu.JpegCompression(quality_lower=20, quality_upper=100, p=0.5)
            ]),
            # Noise augmentation
            albu.OneOf([
                albu.GaussNoise(var_limit=(10, 50), p=0.5),
                albu.IAAAdditiveGaussianNoise(loc=0, scale=(0.01 * 255, 0.05 * 255))
            ]),
        ], p=1.)
    else:
        training_transforms = albu.Compose(
            [albu.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.5)], p=1.)

    validation_transforms = albu.Compose([
        albu.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), max_pixel_value=255.0, p=1.)], p=1.)
def transform(image, mask=None, opt=None, paired=False):
    assert opt is not None, 'opt is not optional (it cannot be None)'
    #[ ab.Resize(height=opt.load_height, width=opt.load_width)] if opt.load_width!=-1 else [])
    resize = ab.Resize(height=opt.load_height, width=opt.load_width)
    T_stack_pre = ab.Compose([
        #ab.ChannelDropout((1, 4 if paired else 2 )),
        #ab.ChannelDropout(),
        ab.OpticalDistortion(),
        ab.RandomCrop(height=opt.crop_size, width=opt.crop_size),
        # ab.CoarseDropout()
    ])
    T_pair = ab.Compose([
        ab.ShiftScaleRotate(shift_limit=0.005,
                            scale_limit=0.01,
                            rotate_limit=5,
                            interpolation=1),
        ab.OneOf([
            ab.MotionBlur(),
            ab.MedianBlur(5),
            ab.GaussianBlur(),
        ]),
        #ab.ToFloat(),
        #ab.HueSaturationValue(),
        #ab.RandomBrightness(),
        #ab.RandomContrast(),
        ab.Normalize((.5, ) * 3, (.5, ) * 3)
    ])
    #nc = 6 if paired else 3
    T_stack_post = ab.Compose([
        #ab.Normalize((0.5,)*nc, (0.5,)*nc),
        ab.pytorch.ToTensor(),
    ])

    if paired:
        #i = 0
        #print('\n\n')
        #print(str(i), image.mean(), image.std()); i += 1

        aug = resize(image=image, mask=mask)
        image, mask = aug['image'], aug['mask']

        #print(str(i), image.mean(), image.std()); i += 1
        cat = T_stack_pre(
            image=np.concatenate((image, mask), axis=-1))['image']
        image, mask = cat[..., :3], cat[..., 3:]

        #print(str(i), image.mean(), image.std()); i += 1
        aug = T_pair(image=image, mask=mask)
        image, mask = aug['image'], aug['mask']

        #print(str(i), image.mean(), image.std()); i += 1
        cat = T_stack_post(
            image=np.concatenate((image, mask), axis=-1))['image']
        image, mask = cat[:3], cat[3:]
        #print(str(i), image.mean(), image.std()); i += 1
        #print('\n\n')
        return image, mask
    else:
        #print('IMG', type(image))
        #print('1',image.shape, end=' -> ',flush=True)
        image = resize(image=image)['image'],
        #print('\n\n\n\n\n\n\n')
        #print(image)
        #print('\n\n\n\n\n\n\n')
        #print('2',image.shape, end=' -> ', flush=True)
        #print('IMG TYPE',type(image), len(image))
        if type(image) is tuple:
            image = image[0]
        image = T_stack_pre(image=image)['image']
        #print(image.shape, end=' -> ',flush=True)

        image = T_pair(image=image)['image']
        #print(image.shape, end=' -> ')

        image = T_stack_post(image=image)['image']
        #print(image.shape)

        return image
    def _add_augmentation(self, aug_name, params):
        if aug_name == 'hflip':
            return album.HorizontalFlip(p=params['p'])

        elif aug_name == 'vflip':
            return album.VerticalFlip(p=params['p'])

        elif aug_name == 'blur':
            return album.Blur(p=params['p'])

        elif aug_name == 'mblur':
            return album.MedianBlur(p=params['p'])

        elif aug_name == 'rbright':
            return album.RandomBrightness(p=params['p'])

        elif aug_name == 'rotate':
            return album.Rotate(p=params['p'], limit=params['limit'])

        elif aug_name == 'rotate90':
            return album.RandomRotate90(p=params['p'])

        elif aug_name == 'ssrotate':
            return album.ShiftScaleRotate(p=params['p'],
                                          shift_limit=params['shift_limit'],
                                          scale_limit=params['scale_limit'],
                                          rotate_limit=params['rotate_limit'])

        elif aug_name == 'cutout':
            return album.Cutout(p=params['p'],
                                num_holes=params['num_holes'],
                                max_h_size=params['max_h_size'],
                                max_w_size=params['max_w_size'])

        elif aug_name == 'cdropout':
            return album.CoarseDropout(p=params['p'],
                                       max_holes=params['max_holes'],
                                       max_height=params['max_height'],
                                       max_width=params['max_width'],
                                       min_holes=params['min_holes'],
                                       min_height=params['min_height'],
                                       min_width=params['min_width'])

        elif aug_name == 'rrcrop':
            return album.RandomResizedCrop(p=params['p'],
                                           height=params['height'],
                                           width=params['width'],
                                           scale=tuple(params['scale']),
                                           ratio=tuple(params['ratio']))
        elif aug_name == 'elastic':
            return album.ElasticTransform(
                p=params['p'],
                alpha=params['alpha'],
                sigma=params['sigma'],
                alpha_affine=params['alpha_affine'],
                interpolation=params['interpolation'],
                border_mode=params['border_mode'])
        elif aug_name == 'rmorph':
            return RandomMorph(p=params['p'])

        elif aug_name == 'augmix':
            return RandomAugMix(p=params['p'])

        elif aug_name == 'gridmask':
            return GridMask(p=params['p'],
                            num_grid=tuple(params['num_grid']),
                            rotate=params['rotate_limit'],
                            mode=params['mode'])

        else:
            raise (NotImplementedError)
예제 #21
0
 A.Flip(p=1),
 A.RandomRotate90(p=1),
 A.Rotate(p=1, limit=45, interpolation=3),
 A.RandomResizedCrop(input_size[0],
                     input_size[1],
                     scale=(0.7, 1.0),
                     ratio=(1.0, 1.0),
                     interpolation=3,
                     p=1),
 A.OneOf([
     A.IAAAdditiveGaussianNoise(),
     A.GaussNoise(),
 ], p=0.3),
 A.OneOf([
     A.MotionBlur(p=0.25),
     A.MedianBlur(blur_limit=3, p=0.25),
     A.Blur(blur_limit=3, p=0.25),
     A.GaussianBlur(p=0.25)
 ],
         p=0.1),
 A.OneOf([
     A.OpticalDistortion(interpolation=3, p=0.1),
     A.GridDistortion(interpolation=3, p=0.1),
     A.IAAPiecewiseAffine(p=0.5),
 ],
         p=0.05),
 A.OneOf([
     A.CLAHE(clip_limit=2),
     A.IAASharpen(),
     A.IAAEmboss(),
     A.RandomBrightnessContrast(),
예제 #22
0
def get_augs(new_augs, size, size_after_reshape):
    ALBUMENTATIONS_VAL = albu.Compose([
        albu.Normalize(mean=[0.485, 0.456, 0.406],
                       std=[0.229, 0.224, 0.225],
                       max_pixel_value=255.0,
                       p=1.0),
        albu.Resize(size_after_reshape, size_after_reshape)
    ])
    if not new_augs:
        ALBUMENTATIONS_TRAIN = albu.Compose([
            albu.HorizontalFlip(),
            albu.VerticalFlip(),
            albu.RandomRotate90(),
            albu.ShiftScaleRotate(shift_limit=0.0625,
                                  scale_limit=0.2,
                                  rotate_limit=15,
                                  p=0.9,
                                  border_mode=cv2.BORDER_REFLECT),
            albu.OneOf(
                [
                    albu.OpticalDistortion(p=0.3),
                    albu.GridDistortion(p=.1),
                    #albu.IAAPiecewiseAffine(p=0.3),
                ],
                p=0.3),
            albu.OneOf([
                albu.HueSaturationValue(10, 15, 10),
                albu.CLAHE(clip_limit=2),
                albu.RandomBrightnessContrast(),
            ],
                       p=0.3),
            albu.Resize(size_after_reshape, size_after_reshape),
        ])
    else:
        ALBUMENTATIONS_TRAIN = albu.Compose([
            albu.HorizontalFlip(),
            albu.VerticalFlip(),
            albu.RandomRotate90(),
            albu.Cutout(num_holes=10,
                        max_h_size=int(.1 * size),
                        max_w_size=int(.1 * size),
                        p=.25),
            albu.OneOf([
                albu.GaussNoise(0.002, p=.5),
                albu.IAAAffine(p=.5),
            ],
                       p=.25),
            albu.OneOf([
                albu.Blur(blur_limit=3, p=1),
                albu.MedianBlur(blur_limit=3, p=1)
            ],
                       p=.25),
            albu.ShiftScaleRotate(shift_limit=0.0625,
                                  scale_limit=0.2,
                                  rotate_limit=15,
                                  p=0.9,
                                  border_mode=cv2.BORDER_REFLECT),
            albu.HueSaturationValue(20, 30, 20),
            albu.RandomBrightnessContrast(),
            albu.OneOf(
                [
                    albu.OpticalDistortion(1.2, 0.5, p=.3),
                    albu.GridDistortion(5, 0.2, p=.3),
                    # albu.IAAPiecewiseAffine(p=0.3),
                ],
                p=0.3),
            albu.OneOf([
                albu.CLAHE(clip_limit=2),
                albu.RandomGamma(),
            ], p=0.5),
            albu.Normalize(mean=[0.485, 0.456, 0.406],
                           std=[0.229, 0.224, 0.225],
                           max_pixel_value=255.0,
                           p=1.0),
            albu.Resize(size_after_reshape, size_after_reshape),
        ])
    return ALBUMENTATIONS_TRAIN, ALBUMENTATIONS_VAL
예제 #23
0
 def get_transforms(stage: str = None, mode: str = None):
     if mode == 'train':
         return albumentations.Compose([
             # blur
             albumentations.OneOf([
                 albumentations.Blur((1, 4), p=1.0),
                 albumentations.GaussianBlur(3, p=1.0),
                 albumentations.MedianBlur(blur_limit=5, p=1.0),
             ],
                                  p=3 / 4),
             # transformations
             albumentations.ShiftScaleRotate(
                 scale_limit=0.2,
                 rotate_limit=25,
                 border_mode=cv2.BORDER_CONSTANT,
                 value=0,
                 p=1.0),
             # cut and drop
             albumentations.OneOf([
                 albumentations.Cutout(num_holes=10,
                                       max_h_size=SIZE // 6,
                                       max_w_size=SIZE // 6,
                                       p=1.0),
                 albumentations.CoarseDropout(
                     max_holes=8, max_height=10, max_width=10, p=1.0),
             ],
                                  p=2 / 3),
             # distortion
             albumentations.OneOf([
                 albumentations.OpticalDistortion(0.6, p=1.0),
                 albumentations.GridDistortion(
                     8,
                     0.06,
                     border_mode=cv2.BORDER_CONSTANT,
                     value=0,
                     p=1.0),
                 albumentations.ElasticTransform(
                     sigma=10,
                     alpha=1,
                     alpha_affine=10,
                     border_mode=cv2.BORDER_CONSTANT,
                     value=0,
                     p=1.0),
             ],
                                  p=3 / 4),
             # add noise
             albumentations.OneOf([
                 albumentations.GaussNoise((0, 250), p=1.0),
                 albumentations.MultiplicativeNoise(p=1.0),
             ],
                                  p=2 / 3),
             # common
             albumentations.Normalize(TRAIN_MEAN, TRAIN_STD),
             ToTensorV2(),
         ])
     elif mode == 'valid':
         return albumentations.Compose([
             albumentations.Normalize(TRAIN_MEAN, TRAIN_STD),
             ToTensorV2(),
         ])
     else:
         raise ValueError('mode is %s' % mode)
예제 #24
0
    "strong":
    albu.Compose([
        albu.HorizontalFlip(),
        albu.ShiftScaleRotate(shift_limit=0.0,
                              scale_limit=0.2,
                              rotate_limit=30,
                              p=0.4),
        albu.ElasticTransform(),
        albu.GaussNoise(),
        albu.OneOf(
            [
                albu.CLAHE(clip_limit=2),
                albu.IAASharpen(),
                albu.RandomBrightnessContrast(),
                albu.RandomGamma(),
                albu.MedianBlur(),
            ],
            p=0.5,
        ),
        albu.OneOf(
            [albu.RGBShift(), albu.HueSaturationValue()], p=0.5),
    ]),
    "weak":
    albu.Compose([albu.HorizontalFlip()]),
    "horizontal_flip":
    albu.HorizontalFlip(p=0.5),
    "none":
    albu.Compose([]),
}

예제 #25
0
    A.OneOf([
        A.RandomBrightness(limit=(0.8, 1.2), always_apply=True),
        A.RGBShift(r_shift_limit=(-10, 10),
                   g_shift_limit=(-10, 10),
                   b_shift_limit=(-10, 10),
                   always_apply=True),
        A.HueSaturationValue(hue_shift_limit=(-10, 10),
                             sat_shift_limit=(-10, 10),
                             val_shift_limit=(-10, 10),
                             always_apply=True),
        A.RandomContrast(limit=(0.9, 1.1), always_apply=True)
    ],
            p=0.3),
    A.OneOf([
        A.Blur(blur_limit=3, always_apply=True),
        A.MedianBlur(blur_limit=3, always_apply=True),
        A.GaussNoise(var_limit=(10, 50), always_apply=True)
    ])
])

train_ds = D.PKUSingleObjectTrainDataset(
    json_annotations=os.path.join(cfg.CV_DIR, 'fold-1',
                                  'train_objects_both_train.json'),
    images_dir=cfg.TRAIN_IMAGES,
    color_augment_fn=lambda dct: D.augment_fn_albu_color(
        dct, albu_train_transforms),
    geom_augment_fn=lambda dct: D.augment_fn_flip_rotate(D.augment_fn_bbox(
        dct, s=0.02),
                                                         p_flip=0.5,
                                                         p_rotate=0.5,
                                                         rotatelim=(-5, 5)),
예제 #26
0
FINALIZE_T = albumentations.Compose([
    albumentations.Normalize(mean=[0.485, 0.456, 0.406],
                     std=[0.229, 0.224, 0.225]),
    ToTensor(),
])

BASIC_IMAGE_T = albumentations.Compose([
    albumentations.Resize(128, 128),
    FINALIZE_T
])

AUG_IMAGE_T = albumentations.Compose([
    albumentations.GaussNoise(),
    albumentations.OneOf([
        albumentations.MotionBlur(),
        albumentations.MedianBlur(blur_limit=3),
        albumentations.Blur(blur_limit=3),
        albumentations.JpegCompression(quality_lower=90),
    ]),
    albumentations.OneOf([
        albumentations.CLAHE(clip_limit=2),
        albumentations.IAASharpen(),
        albumentations.RandomBrightnessContrast(),
    ]),
    albumentations.HueSaturationValue(p=0.3),
])


TRAIN_IMAGE_T = albumentations.Compose([
    albumentations.Resize(128, 128),
    AUG_IMAGE_T,
예제 #27
0

seed_torch(seed=CFG.seed)

base_transform = A.Compose([
    A.OneOf(
        [
            # 加了hsv
            A.HueSaturationValue(10, 15, 10),
            A.RandomBrightness(limit=.2, p=1),
            A.RandomContrast(limit=.2, p=1),
            A.RandomGamma(p=1)
        ],
        p=.5),
    A.OneOf([A.Blur(blur_limit=3, p=1),
             A.MedianBlur(blur_limit=3, p=1)],
            p=.25),
    A.OneOf([
        A.GaussNoise(0.002, p=.5),
        A.IAAAffine(p=.5),
    ], p=.25),
    # A.OneOf([
    #     A.ElasticTransform(alpha=120, sigma=120 * .05, alpha_affine=120 * .03, p=.5),
    #     A.GridDistortion(p=.5),
    #     A.OpticalDistortion(distort_limit=2, shift_limit=.5, p=1)
    # ], p=.25),
    A.RandomRotate90(p=.5),
    A.HorizontalFlip(p=.5),
    A.VerticalFlip(p=.5),
    A.Cutout(num_holes=10,
             max_h_size=int(.1 * CFG.data),
                                       p=0.7),
     albumentations.RandomBrightnessContrast(brightness_limit=(-0.2, 0.2),
                                             contrast_limit=(-0.2, 0.2),
                                             p=0.7),
     albumentations.CLAHE(clip_limit=(1, 4), p=0.5),
     albumentations.OneOf([
         albumentations.OpticalDistortion(distort_limit=1.0),
         albumentations.GridDistortion(num_steps=5, distort_limit=1.),
         albumentations.ElasticTransform(alpha=3),
     ],
                          p=0.2),
     albumentations.OneOf([
         albumentations.GaussNoise(var_limit=[10, 50]),
         albumentations.GaussianBlur(),
         albumentations.MotionBlur(),
         albumentations.MedianBlur(),
     ],
                          p=0.2),
     albumentations.Resize(CFG.img_size, CFG.img_size),
     albumentations.OneOf([
         albumentations.ImageCompression(),
         albumentations.Downscale(scale_min=0.1, scale_max=0.15),
     ],
                          p=0.2),
     albumentations.IAAPiecewiseAffine(p=0.2),
     albumentations.IAASharpen(p=0.2),
     albumentations.CoarseDropout(max_height=int(CFG.img_size * 0.1),
                                  max_width=int(CFG.img_size * 0.1),
                                  max_holes=5,
                                  p=0.5),
 ])
예제 #29
0
    def __call__(self, example):
        if self.train:
            x, y = example
        else:
            x = example
        # --- Augmentation ---
        if self.affine:
            x = affine_image(x)

        # --- Train/Test common preprocessing ---
        if self.crop:
            x = crop_char_image(x, threshold=self.threshold)
        if self.size is not None:
            x = resize(x, size=self.size)
        if self.sigma > 0.:
            x = add_gaussian_noise(x, sigma=self.sigma)

        # albumentations...
        x = x.astype(np.float32)
        assert x.ndim == 2
        # 1. blur
        if _evaluate_ratio(self.blur_ratio):
            r = np.random.uniform()
            if r < 0.25:
                x = apply_aug(A.Blur(p=1.0), x)
            elif r < 0.5:
                x = apply_aug(A.MedianBlur(blur_limit=5, p=1.0), x)
            elif r < 0.75:
                x = apply_aug(A.GaussianBlur(p=1.0), x)
            else:
                x = apply_aug(A.MotionBlur(p=1.0), x)

        if _evaluate_ratio(self.noise_ratio):
            r = np.random.uniform()
            if r < 0.50:
                x = apply_aug(A.GaussNoise(var_limit=5. / 255., p=1.0), x)
            else:
                x = apply_aug(A.MultiplicativeNoise(p=1.0), x)

        if _evaluate_ratio(self.cutout_ratio):
            # A.Cutout(num_holes=2,  max_h_size=2, max_w_size=2, p=1.0)  # Deprecated...
            x = apply_aug(
                A.CoarseDropout(max_holes=8, max_height=8, max_width=8, p=1.0),
                x)

        if _evaluate_ratio(self.grid_distortion_ratio):
            x = apply_aug(A.GridDistortion(p=1.0), x)

        if _evaluate_ratio(self.elastic_distortion_ratio):
            x = apply_aug(
                A.ElasticTransform(sigma=50, alpha=1, alpha_affine=10, p=1.0),
                x)

        if _evaluate_ratio(self.random_brightness_ratio):
            # A.RandomBrightness(p=1.0)  # Deprecated...
            # A.RandomContrast(p=1.0)    # Deprecated...
            x = apply_aug(A.RandomBrightnessContrast(p=1.0), x)

        if _evaluate_ratio(self.piece_affine_ratio):
            x = apply_aug(A.IAAPiecewiseAffine(p=1.0), x)

        if _evaluate_ratio(self.ssr_ratio):
            x = apply_aug(
                A.ShiftScaleRotate(shift_limit=0.0625,
                                   scale_limit=0.1,
                                   rotate_limit=30,
                                   p=1.0), x)

        if self.normalize:
            x = (x.astype(np.float32) - 0.0692) / 0.2051
        if x.ndim == 2:
            x = x[None, :, :]
        x = x.astype(np.float32)
        if self.train:
            y = y.astype(np.int64)
            return x, y
        else:
            return x
    def __init__(self, folds, img_height, img_width, mean, std):
        df = pd.read_csv('../input/dataset/train_folds.csv')
        df = df[['Image', 'label', 'kfold']]

        df = df[df.kfold.isin(folds)].reset_index(drop=True)

        self.image_ids = df.Image.values

        self.labels = df.label.values

        if len(folds) == 1:
            self.aug = albumentations.Compose([
                albumentations.Resize(img_height, img_width),
                albumentations.Normalize(mean, std, always_apply=True)
            ])
        else:
            self.aug = albumentations.Compose([
                albumentations.Resize(img_height, img_width),
                albumentations.OneOf([
                    albumentations.ShiftScaleRotate(shift_limit=0.0625,
                                                    scale_limit=0.1,
                                                    rotate_limit=45),
                    albumentations.Rotate(limit=5),
                    albumentations.RandomGamma(),
                    albumentations.RandomShadow(),
                    albumentations.RandomGridShuffle(),
                    albumentations.ElasticTransform(),
                    albumentations.RGBShift(),
                ]),
                albumentations.OneOf([
                    albumentations.OneOf([
                        albumentations.Blur(),
                        albumentations.MedianBlur(),
                        albumentations.MotionBlur(),
                        albumentations.GaussianBlur(),
                    ]),
                    albumentations.OneOf([
                        albumentations.GaussNoise(),
                        albumentations.IAAAdditiveGaussianNoise(),
                        albumentations.ISONoise()
                    ]),
                ]),
                albumentations.OneOf([
                    albumentations.RandomBrightness(),
                    albumentations.RandomContrast(),
                    albumentations.RandomBrightnessContrast(),
                ]),
                albumentations.OneOf([
                    albumentations.OneOf([
                        albumentations.Cutout(),
                        albumentations.CoarseDropout(),
                        albumentations.GridDistortion(),
                        albumentations.GridDropout(),
                        albumentations.OpticalDistortion()
                    ]),
                    albumentations.OneOf([
                        albumentations.HorizontalFlip(),
                        albumentations.VerticalFlip(),
                        albumentations.RandomRotate90(),
                        albumentations.Transpose()
                    ]),
                ]),

                # albumentations.OneOf([
                #         albumentations.RandomSnow(),
                #         albumentations.RandomRain(),
                #         albumentations.RandomFog(),
                #     ]),
                albumentations.Normalize(mean, std, always_apply=True)
            ])