Exemple #1
0
def get_train_aug(config):
    train_size = config.data["train_size"]
    if config.data["train_size"]==config.data["input_size"]:
        aug = get_aug([
              A.RandomSizedCrop(min_max_height=(800, 800), height=1024, width=1024, p=0.5),
            A.OneOf([
                    A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit= 0.2, 
                                         val_shift_limit=0.2, p=0.9),
                    A.RandomBrightnessContrast(brightness_limit=0.2, 
                                               contrast_limit=0.2, p=0.9),]),
              A.HorizontalFlip(p=0.5),
              A.VerticalFlip(p=0.5),
              A.Resize(train_size, train_size, p=1),
              A.Cutout(num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
              ToTensorV2(p=1.0),])
    else:
        # define augumentation
        aug = get_aug([
              A.RandomSizedCrop(min_max_height=(800, 800), height=1024, width=1024, p=0.5),
              A.OneOf([
                    A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit= 0.2, 
                                         val_shift_limit=0.2, p=0.9),
                    A.RandomBrightnessContrast(brightness_limit=0.2, 
                                               contrast_limit=0.2, p=0.9),]),
              A.HorizontalFlip(p=0.5),
              A.VerticalFlip(p=0.5),
              A.RandomCrop(train_size, train_size, p=1),
              A.Cutout(num_holes=8, max_h_size=32, max_w_size=32, fill_value=0, p=0.5),
              ToTensorV2(p=1.0),])
    return aug
    def composeAugmentation(self):
        if self.source == 'train':
            self.total = 10
            self.augment = albumentations.Compose([
                albumentations.Rotate(3, always_apply=True),
                albumentations.RandomSizedCrop((self.sizeY // 2, 700),
                                               self.sizeY,
                                               self.sizeX,
                                               1,
                                               always_apply=True),
                albumentations.HorizontalFlip(),
                #albumentations.GridDistortion(always_apply=False),
                #albumentations.IAAAffine(rotate=2, shear=5, always_apply=False),

                #albumentations.OpticalDistortion(),
                albumentations.ElasticTransform(alpha=64,
                                                sigma=24,
                                                always_apply=False,
                                                alpha_affine=0),
                albumentations.RandomBrightnessContrast(0.1,
                                                        0.1,
                                                        always_apply=False),
                #albumentations.Blur(always_apply=False)
            ])
        else:
            self.total = 4
            self.augment = albumentations.Compose([
                albumentations.RandomSizedCrop((self.sizeY, self.sizeY),
                                               self.sizeY,
                                               self.sizeX,
                                               1,
                                               always_apply=True),
            ])
    def __init__(self, dataset):

        if dataset == 'brats':
            prob = 0.5
            self.augs = A.Compose([
                A.Blur(blur_limit=1, p=prob),
                A.RandomSizedCrop(min_max_height=(90, 90),
                                  height=240,
                                  width=240,
                                  p=prob),
                A.RandomGamma(gamma_limit=(80, 120), p=prob),
            ])
        elif dataset == 'ACDC_8':
            prob = 0.5
            self.augs = A.Compose([
                # A.HorizontalFlip(p=prob),
                # A.VerticalFlip(p=prob),
                A.Rotate(limit=5, p=prob),
                A.ElasticTransform(alpha=0.05, p=prob),
                A.RandomSizedCrop(min_max_height=(140, 140),
                                  height=154,
                                  width=154,
                                  p=prob),
                A.RandomGamma(gamma_limit=(80, 120), p=prob),
            ])
def test_force_apply():
    """
    Unit test for https://github.com/albu/albumentations/issues/189
    """
    aug = A.Compose(
        [
            A.OneOrOther(
                A.Compose(
                    [
                        A.RandomSizedCrop(min_max_height=(256, 1025), height=512, width=512, p=1),
                        A.OneOf(
                            [
                                A.RandomSizedCrop(min_max_height=(256, 512), height=384, width=384, p=0.5),
                                A.RandomSizedCrop(min_max_height=(256, 512), height=512, width=512, p=0.5),
                            ]
                        ),
                    ]
                ),
                A.Compose(
                    [
                        A.RandomSizedCrop(min_max_height=(256, 1025), height=256, width=256, p=1),
                        A.OneOf([A.HueSaturationValue(p=0.5), A.RGBShift(p=0.7)], p=1),
                    ]
                ),
            ),
            A.HorizontalFlip(p=1),
            A.RandomBrightnessContrast(p=0.5),
        ]
    )

    res = aug(image=np.zeros((1248, 1248, 3), dtype=np.uint8))
    assert res["image"].shape[0] in (256, 384, 512)
    assert res["image"].shape[1] in (256, 384, 512)
Exemple #5
0
    def __augment_image(self, image):
        random.seed()
        np.random.seed()

        train_aug = A.Compose(
            transforms = [
                A.RandomSizedCrop(min_max_height=(256, 512), height=512, width=512, p=1),
                A.RandomRotate90(p=1),
                A.HorizontalFlip(p=0.5),
                A.VerticalFlip(p=0.5),
                A.IAAAffine(shear=15, p=0.5),
                A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1, rotate_limit=10, p=.3),
                A.Blur(blur_limit=2, p=.33),
                A.OpticalDistortion(p=.33),
                A.RandomBrightnessContrast(p=.33)
            ],
            p=1
        )

        test_aug = A.Compose(
            transforms = [
                A.RandomSizedCrop(min_max_height=(256, 512), height=512, width=512, p=1),
                A.RandomRotate90(p=1),
                A.HorizontalFlip(p=0.5),
                A.VerticalFlip(p=0.5),
            ],
            p=1
        )

        if self.mode == 'train':
            augmented_image = train_aug(image=image)['image']
        else:
            augmented_image = test_aug(image=image)['image']
        return augmented_image
Exemple #6
0
def test_transform_pipeline_serialization(seed, image, mask):
    aug = A.Compose([
        A.OneOrOther(
            A.Compose([
                A.Resize(1024, 1024),
                A.RandomSizedCrop(min_max_height=(256, 1024), height=512, width=512, p=1),
                A.OneOf([
                    A.RandomSizedCrop(min_max_height=(256, 512), height=384, width=384, p=0.5),
                    A.RandomSizedCrop(min_max_height=(256, 512), height=512, width=512, p=0.5),
                ])
            ]),
            A.Compose([
                A.Resize(1024, 1024),
                A.RandomSizedCrop(min_max_height=(256, 1025), height=256, width=256, p=1),
                A.OneOf([
                    A.HueSaturationValue(p=0.5),
                    A.RGBShift(p=0.7)
                ], p=1),
            ])
        ),
        A.HorizontalFlip(p=1),
        A.RandomBrightnessContrast(p=0.5)
    ])
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    random.seed(seed)
    aug_data = aug(image=image, mask=mask)
    random.seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, mask=mask)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
    assert np.array_equal(aug_data['mask'], deserialized_aug_data['mask'])
Exemple #7
0
def get_training_augmentation(max_dim, crop=True, flip=False, light=False):
    train_transform = [
        A.PadIfNeeded(min_height=max_dim, min_width=max_dim, border_mode=0)
    ]

    if crop:
        train_transform.append(
            A.OneOf([
                A.RandomSizedCrop(min_max_height=(min(max_dim,
                                                      256), min(max_dim, 256)),
                                  height=SHAPE,
                                  width=SHAPE,
                                  p=1),
                A.RandomSizedCrop(min_max_height=(max_dim, max_dim),
                                  height=SHAPE,
                                  width=SHAPE,
                                  p=1)
            ],
                    p=1))
    else:
        train_transform.append(
            A.Resize(height=SHAPE,
                     width=SHAPE,
                     interpolation=1,
                     always_apply=True,
                     p=1))

    if flip:
        train_transform.append(A.VerticalFlip(p=.5))
        train_transform.append(A.RandomRotate90(p=.5))
    if light:
        train_transform.append(A.CLAHE(p=0.8))
        train_transform.append(A.RandomBrightnessContrast(p=0.8))
        train_transform.append(A.RandomGamma(p=0.8))
    return A.Compose(train_transform)
Exemple #8
0
def medium_augmentations(image_size: Tuple[int, int], rot_angle=15):
    return A.Compose([
        A.OneOf([
            A.RandomSizedCrop((image_size[0], int(image_size[0] * 1.25)),
                              image_size[0],
                              image_size[1],
                              p=0.05),
            A.RandomSizedCrop((image_size[0], int(image_size[0] * 1.5)),
                              image_size[0],
                              image_size[1],
                              p=0.10),
            A.RandomSizedCrop((image_size[0], int(image_size[0] * 2)),
                              image_size[0],
                              image_size[1],
                              p=0.15),
            A.Resize(image_size[0], image_size[1], p=0.7),
        ],
                p=1.0),
        A.HorizontalFlip(p=0.5),
        A.ShiftScaleRotate(shift_limit=0.1,
                           scale_limit=0.05,
                           rotate_limit=15,
                           border_mode=cv2.BORDER_REFLECT,
                           p=0.3),
        A.RandomBrightnessContrast(brightness_limit=0.2,
                                   contrast_limit=0.2,
                                   p=0.2),
        A.RandomGamma(gamma_limit=(85, 115), p=0.2),
        A.HueSaturationValue(p=0.2),
        A.CLAHE(p=0.2),
        A.JpegCompression(quality_lower=50, p=0.2),
        A.Normalize(),
        ToTensor()
    ])
    def __init__(self, dataset):

        prob = 0.5
        if dataset == 'HIST':
            self.augs = A.Compose(
                [
                    # A.HorizontalFlip(p=prob),
                    # A.VerticalFlip(p=prob),
                    A.Rotate(limit=5, p=prob),
                    # # A.GlassBlur(sigma=1),
                    # # A.GridDistortion(distort_limit=0.3),
                    # # A.ElasticTransform(alpha=0.05, p=prob),
                    A.RandomSizedCrop(min_max_height=(65, 80), height=96, width=96, p=prob),
                    A.RandomGamma(gamma_limit=(80, 120), p=prob),
                    # # A.RandomBrightness(limit=0.2, p=prob)
                ]
            )
        elif dataset == 'APTOS':
            self.augs = A.Compose(
                [
                    # A.HorizontalFlip(p=prob),
                    # A.VerticalFlip(p=prob),
                    A.Rotate(limit=5, p=prob),
                    # A.GlassBlur(sigma=1),
                    # A.GridDistortion(distort_limit=0.3),
                    # A.ElasticTransform(alpha=0.05, p=prob),
                    A.RandomSizedCrop(min_max_height=(180, 220), height=256, width=256, p=prob),
                    A.RandomGamma(gamma_limit=(80, 120), p=prob),
                ]
            )
Exemple #10
0
def hard_augmentations(image_size: Tuple[int, int], rot_angle=30):
    pad_h, pad_w = padding_for_rotation(image_size, rot_angle)
    crop_height = int(image_size[0] + pad_h * 2)
    crop_width = int(image_size[1] + pad_w * 2)
    crop_transform = A.Compose([
        A.RandomSizedCrop((int(crop_height * 0.75), int(crop_height * 1.25)), crop_height, crop_width),
        A.ShiftScaleRotate(shift_limit=0, scale_limit=0, rotate_limit=rot_angle, border_mode=cv2.BORDER_CONSTANT),
        A.CenterCrop(image_size[0], image_size[1]),
    ])

    return A.Compose([
        # spatial transform
        A.PadIfNeeded(int(crop_height * 1.25), int(crop_height * 1.25)),
        A.OneOf([
            crop_transform,
            A.RandomSizedCrop((image_size[0], int(image_size[0] * 1.25)), image_size[0], image_size[1], p=0.25),
            A.RandomSizedCrop((image_size[0], int(image_size[0] * 1.5)), image_size[0], image_size[1], p=0.25),
            A.RandomSizedCrop((image_size[0], int(image_size[0] * 2)), image_size[0], image_size[1], p=0.25),
            A.Resize(image_size[0], image_size[1], p=0.75)
        ], p=1.0),

        # add occasion blur/sharpening
        A.OneOf([
            A.GaussianBlur(),
            A.MotionBlur(),
            A.IAASharpen(),
            A.JpegCompression(quality_lower=75, p=0.25),
        ]),

        # D4 augmentations
        A.Compose([
            A.HorizontalFlip(),
        ]),

        # spatial-preserving augmentations
        A.OneOf([
            A.Cutout(),
            A.GaussNoise(),
        ]),
        A.OneOf([
            A.RandomBrightnessContrast(),
            A.CLAHE(),
            A.HueSaturationValue(),
            A.RGBShift(),
            A.RandomGamma()
        ]),

        A.Normalize(),
        ToTensor()
    ])
Exemple #11
0
def get_train_transform():
    return A.Compose([
        A.RandomSizedCrop(
            min_max_height=(800, 800), height=1024, width=1024, p=0.5),
        A.OneOf([
            A.HueSaturationValue(hue_shift_limit=0.2,
                                 sat_shift_limit=0.2,
                                 val_shift_limit=0.2,
                                 p=0.9),
            A.RandomBrightnessContrast(
                brightness_limit=0.2, contrast_limit=0.2, p=0.9),
        ],
                p=0.9),
        A.ToGray(p=0.01),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.Resize(height=our_image_size, width=our_image_size, p=1),
        A.Cutout(num_holes=8,
                 max_h_size=our_image_size // 8,
                 max_w_size=our_image_size // 8,
                 fill_value=0,
                 p=0.5)
    ],
                     bbox_params={
                         'format': 'pascal_voc',
                         'label_fields': ['labels']
                     })
Exemple #12
0
def get_medium_augmentations(image_size):
    min_size = min(image_size[0], image_size[1])

    return A.Compose([
        A.OneOf([A.RandomSizedCrop(min_max_height=(int(min_size* 0.85), min_size),
                          height=image_size[0],
                          width=image_size[1]),
                A.Resize(image_size[0], image_size[1]),
                A.CenterCrop(image_size[0], image_size[1])
                ], p = 1.0),

        A.OneOf([
            A.RandomBrightnessContrast(brightness_limit=0.15,
                                       contrast_limit=0.5),
            A.RandomGamma(gamma_limit=(50, 150)),
            A.NoOp()
        ], p = 1.0),
        
        A.OneOf([A.CLAHE(p=0.5, clip_limit=(10, 10), tile_grid_size=(3, 3)),
                A.FancyPCA(alpha=0.4),
                A.NoOp(),
                ], p = 1.0),
        
        A.OneOf([A.HorizontalFlip(),
                A.VerticalFlip(),
                A.RandomRotate90(),
                A.NoOp()
                ], p = 1.0)
    ])
Exemple #13
0
def get_train_transforms():
    return A.Compose([
        A.RandomSizedCrop((140, 140), width=160, height=160, p=0.25),
        A.OneOf([
            A.HueSaturationValue(hue_shift_limit=0.2,
                                 sat_shift_limit=0.2,
                                 val_shift_limit=0.2,
                                 p=0.9),
            A.RandomBrightnessContrast(
                brightness_limit=0.2, contrast_limit=0.2, p=0.9),
        ],
                p=0.9),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.Rotate(limit=45, p=1),
        A.ChannelShuffle(p=0.05),
        A.FancyPCA(),
        A.GaussNoise(p=0.25),
        A.Blur(blur_limit=4, p=0.1),
        A.Cutout(num_holes=8, max_h_size=4, max_w_size=4, fill_value=0, p=0.1),
        A.Resize(
            160,
            160,
        ),
        A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2(p=1.0),
    ],
                     p=1.0)
def get_train_transforms(cfg):
    image_size = cfg["image_size"]
    return A.Compose([
        A.RandomSizedCrop(min_max_height=(image_size - 200, image_size - 200),
                          height=image_size,
                          width=image_size,
                          p=0.5),
        A.OneOf([
            A.HueSaturationValue(hue_shift_limit=0.2,
                                 sat_shift_limit=0.2,
                                 val_shift_limit=0.2,
                                 p=0.9),
            A.RandomBrightnessContrast(
                brightness_limit=0.2, contrast_limit=0.2, p=0.9),
            A.GaussNoise(
                var_limit=(0.01, .005), mean=0, always_apply=False, p=0.6),
        ],
                p=0.9),
        A.ToGray(p=0.01),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.Resize(height=image_size, width=image_size, p=1),
        A.CoarseDropout(
            max_holes=8, max_height=32, max_width=128, fill_value=0, p=0.3),
        A.CoarseDropout(
            max_holes=8, max_height=128, max_width=32, fill_value=0, p=0.3),
        ToTensorV2(p=1.0),
    ],
                     p=1.0,
                     bbox_params=A.BboxParams(format='pascal_voc',
                                              min_area=0,
                                              min_visibility=0,
                                              label_fields=['labels']))
Exemple #15
0
def get_train_transforms():
    return A.Compose([
        A.JpegCompression(p=0.5),
        A.Rotate(limit=80, p=1.0),
        A.OneOf([
            A.OpticalDistortion(),
            A.GridDistortion(),
            A.IAAPiecewiseAffine(),
        ]),
        A.RandomSizedCrop(min_max_height=(int(resolution * 0.7), input_res),
                          height=resolution,
                          width=resolution,
                          p=1.0),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.GaussianBlur(p=0.3),
        A.OneOf([
            A.RandomBrightnessContrast(),
            A.HueSaturationValue(),
        ]),
        A.Cutout(num_holes=8,
                 max_h_size=resolution // 8,
                 max_w_size=resolution // 8,
                 fill_value=0,
                 p=0.3),
        A.Normalize(),
        ToTensorV2(),
    ],
                     p=1.0)
Exemple #16
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)
Exemple #17
0
def get_dataloader(batch_size, patch_size, db_path):
    # +

    def id_collate(batch):
        new_batch = []
        ids = []
        for _batch in batch:
            new_batch.append(_batch[:-1])
            ids.append(_batch[-1])
        return default_collate(new_batch), ids

    # +
    img_transform = albmt.Compose([
        albmt.RandomSizedCrop((patch_size, patch_size), patch_size,
                              patch_size),
        ToTensor()
    ])

    if db_path[0] != '/':
        db_path = f'./{db_path}'

    # more workers do not seem no improve perfomance
    dataset = Dataset(db_path, img_transform=img_transform)
    dataLoader = DataLoader(dataset,
                            batch_size=batch_size,
                            shuffle=False,
                            num_workers=0,
                            pin_memory=True,
                            collate_fn=id_collate)

    print(f"dataset size:\t{len(dataset)}")
    # -
    return dataLoader, dataset
Exemple #18
0
def get_train_transforms():
    # noinspection PyTypeChecker
    return A.Compose([
        A.RandomSizedCrop(
            min_max_height=(850, 850), height=1024, width=1024, p=0.3),
        A.OneOf([
            A.HueSaturationValue(hue_shift_limit=0.2,
                                 sat_shift_limit=0.2,
                                 val_shift_limit=0.2,
                                 p=0.8),
            A.RandomBrightnessContrast(
                brightness_limit=0.2, contrast_limit=0.2, p=0.9)
        ],
                p=0.5),
        A.OneOf([
            A.RandomRain(rain_type='drizzle', p=0.2),
            A.GaussianBlur(blur_limit=7, p=0.5),
            A.GaussNoise((0.2, 0.25), p=0.3),
            A.RandomShadow(p=0.2)
        ],
                p=0.4),
        A.ToGray(p=0.01),
        A.Flip(p=0.5),
        A.CoarseDropout(max_height=64,
                        max_width=64,
                        min_holes=3,
                        min_height=32,
                        min_width=32,
                        p=0.5),
        A.Resize(Config.Train.img_size, Config.Train.img_size, p=1.0),
        ToTensorV2(p=1.0),
    ],
                     bbox_params=BboxParams('pascal_voc',
                                            label_fields=['labels'],
                                            min_visibility=0.0))
def get_light_augmentations(image_size):
    return A.Compose([
        A.ShiftScaleRotate(shift_limit=0.05,
                           scale_limit=0.1,
                           rotate_limit=15,
                           border_mode=cv2.BORDER_CONSTANT,
                           value=0),
        A.RandomSizedCrop(min_max_height=(int(image_size[0] * 0.85),
                                          image_size[0]),
                          height=image_size[0],
                          width=image_size[1],
                          p=0.3),
        ZeroTopAndBottom(p=0.3),
        # Brightness/contrast augmentations
        A.OneOf([
            A.RandomBrightnessContrast(brightness_limit=0.25,
                                       contrast_limit=0.2),
            IndependentRandomBrightnessContrast(brightness_limit=0.1,
                                                contrast_limit=0.1),
            A.RandomGamma(gamma_limit=(75, 125)),
            A.NoOp()
        ]),
        A.OneOf([ChannelIndependentCLAHE(p=0.5),
                 A.CLAHE(),
                 A.NoOp()]),
        A.HorizontalFlip(p=0.5),
    ])
Exemple #20
0
def get_train_transforms(cfg):
    return A.Compose(
        [
            A.RandomSizedCrop(min_max_height=cfg.INPUT.RSC_MIN_MAX_HEIGHT,
                              height=cfg.INPUT.RSC_HEIGHT,
                              width=cfg.INPUT.RSC_WIDTH,
                              p=cfg.INPUT.RSC_PROB),
            A.OneOf([
                A.HueSaturationValue(hue_shift_limit=cfg.INPUT.HSV_H,
                                     sat_shift_limit=cfg.INPUT.HSV_S,
                                     val_shift_limit=cfg.INPUT.HSV_V,
                                     p=cfg.INPUT.HSV_PROB),
                A.RandomBrightnessContrast(brightness_limit=cfg.INPUT.BC_B,
                                           contrast_limit=cfg.INPUT.BC_C,
                                           p=cfg.INPUT.BC_PROB),
            ],
                    p=cfg.INPUT.COLOR_PROB),
            A.ToGray(p=cfg.INPUT.TOFGRAY_PROB),
            A.HorizontalFlip(p=cfg.INPUT.HFLIP_PROB),
            A.VerticalFlip(p=cfg.INPUT.VFLIP_PROB),
            # A.Resize(height=512, width=512, p=1),
            A.Cutout(num_holes=cfg.INPUT.COTOUT_NUM_HOLES,
                     max_h_size=cfg.INPUT.COTOUT_MAX_H_SIZE,
                     max_w_size=cfg.INPUT.COTOUT_MAX_W_SIZE,
                     fill_value=cfg.INPUT.COTOUT_FILL_VALUE,
                     p=cfg.INPUT.COTOUT_PROB),
            ToTensorV2(p=1.0),
        ],
        p=1.0,
        bbox_params=A.BboxParams(format='pascal_voc',
                                 min_area=0,
                                 min_visibility=0,
                                 label_fields=['labels']))
Exemple #21
0
def get_train_transform():
    crop_height = 256
    crop_width = 256

    return albu.Compose([
        albu.PadIfNeeded(min_height=crop_height, min_width=crop_width, p=1),
        albu.RandomSizedCrop((int(0.3 * crop_height), 288), crop_height, crop_width, p=1),
        albu.HorizontalFlip(p=0.5),
        albu.OneOf([
            albu.IAAAdditiveGaussianNoise(p=0.5),
            albu.GaussNoise(p=0.5),
        ], p=0.2),
        albu.OneOf([
            albu.MotionBlur(p=0.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.0625, scale_limit=0, rotate_limit=20, p=0.1),
        albu.OneOf([
            albu.OpticalDistortion(p=0.3),
            albu.GridDistortion(p=0.1),
            albu.IAAPiecewiseAffine(p=0.3),
        ], p=0.2),
        albu.OneOf([
            albu.CLAHE(clip_limit=2, p=0.5),
            albu.IAASharpen(p=0.5),
            albu.IAAEmboss(p=0.5),
            albu.RandomBrightnessContrast(p=0.5),
        ], p=0.3),
        albu.HueSaturationValue(p=0.3),
        albu.JpegCompression(p=0.2, quality_lower=20, quality_upper=99),
        albu.ElasticTransform(p=0.1),
        albu.Normalize(p=1)
    ], p=1)
Exemple #22
0
def get_train_transforms():
    return A.Compose(
        [
            A.RandomSizedCrop(min_max_height=(800, 800), height=1024, width=1024, p=0.5),
            A.OneOf([
                A.HueSaturationValue(hue_shift_limit=0.1, sat_shift_limit= 0.3,
                                     val_shift_limit=0.3, p=0.9),
                A.RandomBrightnessContrast(brightness_limit=0.4,
                                           contrast_limit=0.3, p=0.9),
            ],p=0.9),
            A.ToGray(p=0.01),
            A.HorizontalFlip(p=0.5),
            A.VerticalFlip(p=0.5),
            A.Resize(height=512, width=512, p=1),
            A.Cutout(num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
            ToTensorV2(p=1.0),
        ],
        p=1.0,
        bbox_params=A.BboxParams(
            format='pascal_voc',
            min_area=0,
            min_visibility=0,
            label_fields=['labels']
        )
    )
Exemple #23
0
def augmentation(train: bool) -> Callable:

    initial_size = 2048
    crop_min_max_height = (400, 533)
    crop_width = 512
    crop_height = 384
    if train:
        aug = [
            albu.LongestMaxSize(max_size=initial_size),
            albu.RandomSizedCrop(min_max_height=crop_min_max_height,
                                 width=crop_width,
                                 height=crop_height,
                                 w2h_ratio=crop_width / crop_height),
            albu.HueSaturationValue(hue_shift_limit=7,
                                    sat_shift_limit=10,
                                    val_shift_limit=10),
            albu.RandomBrightnessContrast(),
            albu.RandomGamma()
        ]
    else:
        test_size = int(initial_size * crop_height /
                        np.mean(crop_min_max_height))
        print('Test image max sizes is {} pixels'.format(test_size))
        # for tta probably
        aug = [albu.LongestMaxSize(max_size=test_size)]

    aug.extend([ToTensor()])
    return albu.Compose(aug,
                        bbox_params={
                            'format': 'coco',
                            'min_area': 0,
                            'min_visibility': 0.5,
                            'label_fields': ['labels']
                        })
Exemple #24
0
    def __init__(self, outputs=5):
        super().__init__()
        self.net = EfficientNet.from_pretrained('efficientnet-b0')
        self.linear = Sequential(ReLU(), Dropout(), Linear(1000, outputs))

        df = pd.read_csv("../input/prostate-cancer-grade-assessment/train.csv")
        self.train_df, self.valid_df = train_test_split(df, test_size=0.2)
        self.data_dir = "/datasets/panda/train_128_100"

        self.train_transforms = A.Compose([
            A.InvertImg(p=1),
            A.RandomSizedCrop([100, 128], 128, 128),
            A.Transpose(),
            A.Flip(),
            A.Rotate(90),
            # A.RandomBrightnessContrast(0.02, 0.02),
            # A.HueSaturationValue(0, 10, 10),
            A.Normalize(mean, std, 1),
        ])
        self.valid_transforms = A.Compose([
            A.InvertImg(p=1),
            A.Normalize(mean, std, 1),
        ])

        self.criterion = BCEWithLogitsLoss()
Exemple #25
0
def get_train_transforms():
    return A.Compose([
        A.RandomSizedCrop(min_max_height=(800, 800),
                          height=args["image_size"],
                          width=args["image_size"],
                          p=0.5),
        A.OneOf([
            A.HueSaturationValue(hue_shift_limit=0.2,
                                 sat_shift_limit=0.2,
                                 val_shift_limit=0.2,
                                 p=0.9),
            A.RandomBrightnessContrast(
                brightness_limit=0.2, contrast_limit=0.2, p=0.9),
        ],
                p=0.9),
        A.RandomBrightnessContrast(brightness_limit=0.3,
                                   contrast_limit=0.3,
                                   brightness_by_max=True,
                                   p=1.0),
        A.RandomGamma(gamma_limit=(90, 110)),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.Resize(height=args["image_size"], width=args["image_size"], p=1),
        A.Cutout(
            num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
        ToTensorV2(p=1.0),
    ],
                     p=1.0,
                     bbox_params=A.BboxParams(format='pascal_voc',
                                              min_area=0,
                                              min_visibility=0,
                                              label_fields=['labels']))
Exemple #26
0
def get_transform(train, augmentation_ver=None, imageclassidentifier=None):
    if augmentation_ver is None or augmentation_ver == 0:
        data_transforms = albumentations.Compose([
            albumentations.Flip(),
            #albumentations.RandomBrightness(0.2),
            albumentations.ShiftScaleRotate(rotate_limit=90, scale_limit=0.10),
            #ToTensorV2()
            ], bbox_params=albumentations.BboxParams(format='pascal_voc', label_fields=['class_labels'])) # min_visibility=0.2
    elif augmentation_ver == 1:
        if imageclassidentifier is None:
            imageclassidentifier = preprocess.ImageClassIdentifier()

        def ifpred(**kwargs):
            img = kwargs['image']
            predicted_class = imageclassidentifier.detect(np.array(img))
            if predicted_class == 2:
                return True
            return False

        data_transforms = albumentations.Compose([
            IfThenElse(ifpred, albumentations.Compose([
                    albumentations.RandomSizedCrop((256, 1024), 1024, 1024)
                    #albumentations.RandomSizedBBoxSafeCrop(height=1024, width=1024)
                ]),
                albumentations.Compose([
                    # do nothing
                ])),
            albumentations.Flip(),
            albumentations.VerticalFlip(),
            albumentations.RandomRotate90(p=0.5),
            albumentations.ShiftScaleRotate(rotate_limit=15, scale_limit=0.10),
            #ToTensorV2()
            ], bbox_params=albumentations.BboxParams(format='pascal_voc', label_fields=['class_labels'])) # min_visibility=0.2
    return data_transforms
def get_train_transforms():
    return A.Compose([
        A.RandomSizedCrop(
            min_max_height=(800, 1024), height=1024, width=1024, p=0.5),
        A.OneOf([
            A.HueSaturationValue(hue_shift_limit=0.2,
                                 sat_shift_limit=0.2,
                                 val_shift_limit=0.2,
                                 p=0.9),
            A.RandomBrightnessContrast(
                brightness_limit=0.2, contrast_limit=0.2, p=0.9),
        ],
                p=0.9),
        A.ToGray(p=0.01),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.RandomRotate90(p=0.5),
        A.Transpose(p=0.5),
        A.JpegCompression(quality_lower=85, quality_upper=95, p=0.2),
        A.OneOf(
            [A.Blur(blur_limit=3, p=1.0),
             A.MedianBlur(blur_limit=3, p=1.0)],
            p=0.1),
        A.Resize(height=1024, width=1024, p=1),
        A.Cutout(
            num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
        ToTensorV2(p=1.0),
    ],
                     p=1.0,
                     bbox_params=A.BboxParams(format='pascal_voc',
                                              min_area=0,
                                              min_visibility=0,
                                              label_fields=['labels']))
def get_train_transforms():
    return A.Compose([
            A.RandomSizedCrop(min_max_height=(400, 400), height=512, width=512, p=0.5),
            # A.ShiftScaleRotate(p=0.5),
            A.RandomRotate90(p=0.5),
            A.HorizontalFlip(p=0.5),
            A.VerticalFlip(p=0.5),
            # A.RandomBrightness(p=0.5),
            # A.RandomContrast(p=0.5),
            # A.RandomGamma(p=0.5),
       #     A.RGBShift(),
         #   A.CLAHE(p=1),
            # A.ToGray(),
            # A.OneOf([
            #     A.RandomContrast(),
            #     A.RandomGamma(),
            #     A.RandomBrightness(),
            # ], p=0.5),
            # A.HueSaturationValue(p=0.5),
            #A.Resize(height=512, width=512, p=1),
            # ImageNetPolicy(),
            # A.ChannelShuffle(p=0.5),
            # A.Resize(height=384, width=384, interpolation=cv2.INTER_AREA, p=1),
            #A.Cutout(num_holes=64, max_h_size=24, max_w_size=24, fill_value=0, p=0.5),
            A.Cutout(num_holes=64, max_h_size=32, max_w_size=32, fill_value=0, p=0.5),
            ToTensorV2(p=1.0),                  
        ], p=1.0)
Exemple #29
0
def get_medium_augmentations(image_size):
    return A.Compose([
        A.OneOf([
            A.ShiftScaleRotate(shift_limit=0.05,
                               scale_limit=0.1,
                               rotate_limit=15,
                               border_mode=cv2.BORDER_CONSTANT,
                               value=0),
            A.OpticalDistortion(distort_limit=0.11,
                                shift_limit=0.15,
                                border_mode=cv2.BORDER_CONSTANT,
                                value=0),
            A.NoOp()
        ]),
        A.RandomSizedCrop(min_max_height=(int(image_size[0] * 0.75),
                                          image_size[0]),
                          height=image_size[0],
                          width=image_size[1],
                          p=0.3),
        A.OneOf([
            A.RandomBrightnessContrast(brightness_limit=0.5,
                                       contrast_limit=0.4),
            IndependentRandomBrightnessContrast(brightness_limit=0.25,
                                                contrast_limit=0.24),
            A.RandomGamma(gamma_limit=(50, 150)),
            A.NoOp()
        ]),
        A.OneOf([
            A.RGBShift(r_shift_limit=20, b_shift_limit=15, g_shift_limit=15),
            A.HueSaturationValue(hue_shift_limit=5, sat_shift_limit=5),
            A.NoOp()
        ]),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5)
    ])
Exemple #30
0
def get_transform(train: bool, im_size: int = 400):
    if train:
        aug = A.Compose([
            A.OneOf([
                A.RandomSizedCrop(min_max_height=(224, 720),
                                  height=im_size,
                                  width=im_size,
                                  p=0.5),
                A.Resize(height=im_size,
                         width=im_size,
                         interpolation=cv2.INTER_CUBIC,
                         p=0.5)
            ],
                    p=1),
            A.ChannelShuffle(p=0.5),
            A.HorizontalFlip(p=0.5),
            A.ColorJitter(p=0.5),
            A.Blur(p=0.5),
            A.Normalize(),
            ToTensorV2(),
        ])
    else:
        aug = A.Compose([
            A.Resize(height=im_size,
                     width=im_size,
                     interpolation=cv2.INTER_CUBIC),
            A.Normalize(),
            ToTensorV2(),
        ])
    return aug