Beispiel #1
0
def get_tta_aug(tta_index=0):
    tta_augs = {
        1: [HorizontalFlip(always_apply=True)],
        2: [VerticalFlip(always_apply=True)],
        3:
        [HorizontalFlip(always_apply=True),
         VerticalFlip(always_apply=True)],
        4: [Rotate90(always_apply=True)],
        5: [Rotate90(always_apply=True),
            HorizontalFlip(always_apply=True)],
        6: [VerticalFlip(always_apply=True),
            Rotate90(always_apply=True)],
        7: [
            HorizontalFlip(always_apply=True),
            VerticalFlip(always_apply=True),
            Rotate90(always_apply=True)
        ],
    }
    if tta_index == 0:
        return Compose([Resize(img_sz, img_sz)], p=1.)
    else:
        return Compose(tta_augs[tta_index],
                       preprocessing_transforms=[
                           Resize(img_sz, img_sz, always_apply=True)
                       ],
                       p=1.0)
Beispiel #2
0
    def __init__(self, num_classes, input_size=None):
        super(HSRInhandObjectsDataTransform, self).__init__()

        random.seed(1000)

        if input_size is not None:
            height, width = input_size
            self._train_transform_list.append(Resize(height, width))
            self._val_transform_list.append(Resize(height, width))

        self._train_transform_list = self._train_transform_list + [
            HorizontalFlip(p=0.5),
            Rotate(p=0.5, limit=(-15, 15)),
            GaussNoise(p=0.5),
            RandomBrightnessContrast(p=0.5),
            RandomShadow(p=0.5),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor(num_classes=num_classes),
        ]

        self._val_transform_list = self._val_transform_list + [
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor(num_classes=num_classes),
        ]

        self._initialize_transform_dict()
def get_transforms(CONFIG):
    train_transform = Compose([
        Resize(CONFIG.input_size, CONFIG.input_size),
        HorizontalFlip(p=0.5),
        VerticalFlip(p=0.5),
        Normalize(mean=[0.485, 0.456, 0.406],
                  std=[0.229, 0.224, 0.225],
                  max_pixel_value=255.0,
                  p=1.0),
        ToTensorV2(p=1.0),
    ],
                              p=1.)
    val_transform = Compose([
        Resize(CONFIG.input_size, CONFIG.input_size),
        Normalize(mean=[0.485, 0.456, 0.406],
                  std=[0.229, 0.224, 0.225],
                  max_pixel_value=255.0,
                  p=1.0),
        ToTensorV2(p=1.0),
    ],
                            p=1.)

    test_transform = Compose([
        Resize(CONFIG.input_size, CONFIG.input_size),
        Normalize(mean=[0.485, 0.456, 0.406],
                  std=[0.229, 0.224, 0.225],
                  max_pixel_value=255.0,
                  p=1.0),
        ToTensorV2(p=1.0),
    ],
                             p=1.)

    return train_transform, val_transform, test_transform
Beispiel #4
0
def generate_transforms(image_size):

    train_transform = Compose(
        [
            Resize(height=image_size[0], width=image_size[1]),
            OneOf([RandomBrightness(limit=0.1, p=1), RandomContrast(limit=0.1, p=1)]),
            OneOf([MotionBlur(blur_limit=3), MedianBlur(blur_limit=3), GaussianBlur(blur_limit=3)], p=0.5),
            VerticalFlip(p=0.5),
            HorizontalFlip(p=0.5),
            ShiftScaleRotate(
                shift_limit=0.2,
                scale_limit=0.2,
                rotate_limit=20,
                interpolation=cv2.INTER_LINEAR,
                border_mode=cv2.BORDER_REFLECT_101,
                p=1,
            ),
            Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, p=1.0),
        ]
    )

    val_transform = Compose(
        [
            Resize(height=image_size[0], width=image_size[1]),
            Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, p=1.0),
        ]
    )

    return {"train_transforms": train_transform, "val_transforms": val_transform}
Beispiel #5
0
    def __init__(self, dataset_path, image_size, transform=None):

        """
        BreastPathQ dataset: supervised fine-tuning on downstream task
        """

        self.image_size = image_size
        self.transform = transform

        # Resize images
        self.transform1 = Compose([Resize(image_size, image_size, interpolation=2)])  # 256

        # Data augmentations
        self.transform4 = Compose([Rotate(limit=(-90, 90), interpolation=2), CenterCrop(image_size, image_size)])
        self.transform5 = Compose(
            [Rotate(limit=(-90, 90), interpolation=2), RandomScale(scale_limit=(0.8, 1.2), interpolation=2),
             Resize(image_size + 20, image_size + 20, interpolation=2),
             RandomCrop(image_size, image_size)])

        self.datalist = []
        data_paths = glob.glob(dataset_path + "*.h5")
        with tqdm(enumerate(sorted(data_paths)), disable=True) as t:
            for wj, data_path in t:
                data = h5py.File(data_path)
                data_patches = data['x'][:]
                cls_id = data['y'][:]
                for idx in range(len(data_patches)):
                    self.datalist.append((data_patches[idx], cls_id[idx]))
Beispiel #6
0
    def __init__(self, folds, img_height, img_width, mean, std):
        df = pd.read_csv(BengaliConstants.TRAIN_FOLDS_CSV_FILE)
        # df = pd.read_csv("../input/bengaliai-cv19/train_folds.csv")
        df = df[["image_id", "grapheme_root", "vowel_diacritic", "consonant_diacritic", "kfold"]]

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

        self.image_ids = df.image_id.values
        self.grapheme_root = df.grapheme_root.values
        self.vowel_diacritic = df.vowel_diacritic.values
        self.consonant_diacritic = df.consonant_diacritic.values

        # validation phase transformations (only 1 fold)
        if len(folds) == 1:
            self.aug = Compose([
                Resize(img_height, img_width, always_apply=True),
                Normalize(mean, std, always_apply=True)
            ])
        else:   # Training phase transformations
            self.aug = Compose([
                Resize(img_height, img_width, always_apply=True),
                ShiftScaleRotate(shift_limit=0.0625,
                                 scale_limit=0.1,
                                 rotate_limit=5,
                                 p=0.9),
                Normalize(mean, std, always_apply=True)
            ])
Beispiel #7
0
    def __init__(self, is_train: bool, to_pytorch: bool):
        preprocess = OneOf([
            Resize(height=DATA_HEIGHT, width=DATA_WIDTH),
            Compose([
                Resize(height=int(DATA_HEIGHT * 1.2),
                       width=int(DATA_WIDTH * 1.2)),
                RandomCrop(height=DATA_HEIGHT, width=DATA_WIDTH)
            ])
        ],
                           p=1)

        if is_train:
            self._aug = Compose(
                [
                    preprocess,
                    HorizontalFlip(p=0.5),
                    GaussNoise(p=0.3),
                    # OneOf([
                    #     RandomBrightnessContrast(),
                    #     RandomGamma(),
                    # ], p=0.3),
                    # OneOf([
                    #     ElasticTransform(alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03),
                    #     GridDistortion(),
                    #     OpticalDistortion(distort_limit=2, shift_limit=0.5),
                    # ], p=0.3),
                    Rotate(limit=20),
                ],
                p=1)
        else:
            self._aug = preprocess

        self._need_to_pytorch = to_pytorch
Beispiel #8
0
def fer_train_aug(input_size, crop_residual_pix=16):
    aug = Compose([
        Resize(height=input_size + crop_residual_pix,
               width=input_size + crop_residual_pix),
        OneOf([
            RandomCrop(height=input_size, width=input_size),
            Resize(height=input_size, width=input_size)
        ],
              p=1.0),
        HorizontalFlip(p=0.5),
        OneOf([
            Blur(blur_limit=7, p=0.5),
            GaussianBlur(blur_limit=7, p=0.5),
            MedianBlur(blur_limit=7, p=0.5)
        ]),
        OneOf([
            HueSaturationValue(hue_shift_limit=30,
                               sat_shift_limit=30,
                               val_shift_limit=30,
                               p=0.5),
            RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3)
        ]),
        Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
    ],
                  p=1.0)

    return aug
Beispiel #9
0
def get_transform(model_name):
    if 'TAPNet' in model_name:
        # transform for sequences of images is very tricky
        # TODO: more transforms should be adopted for better results
        train_transform_ops = [
            PadIfNeeded(min_height=args.input_height, min_width=args.input_width, p=1),
            Normalize(p=1),
            # optional transforms
            Resize(height=args.input_height, width=args.input_width, p=1),
            # CenterCrop(height=args.input_height, width=args.input_width, p=1)
        ]
    else:
        train_transform_ops = [
            VerticalFlip(p=0.5),
            HorizontalFlip(p=0.5),
            PadIfNeeded(min_height=args.input_height, min_width=args.input_width, p=1),
            Normalize(p=1),
            # optional transforms
            # Resize(height=args.input_height, width=args.input_width, p=1),
            # CenterCrop(height=args.input_height, width=args.input_width, p=1)
            RandomCrop(height=args.input_height, width=args.input_width, p=1),
        ]

    valid_transform_ops = [
        Normalize(p=1),
        PadIfNeeded(min_height=args.input_height, min_width=args.input_width, p=1),
        # optional transforms
        Resize(height=args.input_height, width=args.input_width, p=1),
        # CenterCrop(height=args.input_height, width=args.input_width, p=1)
    ]
    return Compose(train_transform_ops, p=1), Compose(valid_transform_ops, p=1)
Beispiel #10
0
def transform_v2(config):
    train_transforms = Compose([
        HorizontalFlip(p=0.5),
        ImageCompression(quality_lower=99, quality_upper=100),
        ShiftScaleRotate(shift_limit=0.25,
                         scale_limit=0.25,
                         rotate_limit=10,
                         border_mode=0,
                         p=0.7),
        Resize(config.image_size, config.image_size),
        Cutout(max_h_size=int(config.image_size * 0.6),
               max_w_size=int(config.image_size * 0.6),
               num_holes=1,
               p=0.5),
        Normalize(),
        ToTensor()
    ])

    test_transforms = Compose([
        Resize(config.image_size, config.image_size),
        Normalize(),
        ToTensor()
    ])

    return train_transforms, test_transforms
Beispiel #11
0
def transform_v3(config):
    train_transforms = Compose([
        ImageCompression(quality_lower=60, quality_upper=100, p=0.5),
        GaussNoise(p=1),
        GaussianBlur(blur_limit=3, p=1),
        HorizontalFlip(),
        Resize(config.image_size, config.image_size),
        OneOf([RandomBrightnessContrast(),
               FancyPCA(),
               HueSaturationValue()],
              p=1),
        ShiftScaleRotate(shift_limit=0.1,
                         scale_limit=0.2,
                         rotate_limit=10,
                         border_mode=cv2.BORDER_CONSTANT,
                         p=1),
        ToTensor()
    ])

    test_transforms = Compose([
        GaussNoise(p=1),
        GaussianBlur(blur_limit=3, p=1),
        Resize(config.image_size, config.image_size),
        OneOf([RandomBrightnessContrast(),
               FancyPCA(),
               HueSaturationValue()],
              p=1),
        ToTensor()
    ])

    return train_transforms, test_transforms
Beispiel #12
0
def get_raw_tta():
    transform_test_raw = Compose(
        [Resize(RAW_CROP_SIZE, RAW_CROP_SIZE),
         ToTensor()])

    transform_test_hf = Compose([
        Resize(RAW_CROP_SIZE, RAW_CROP_SIZE),
        HorizontalFlip(p=1, always_apply=True),
        ToTensor()
    ])

    transform_test_vf = Compose([
        Resize(RAW_CROP_SIZE, RAW_CROP_SIZE),
        VerticalFlip(p=1, always_apply=True),
        ToTensor()
    ])

    transform_test_tr = Compose(
        [Resize(RAW_CROP_SIZE, RAW_CROP_SIZE),
         Transpose(),
         ToTensor()])

    return [
        transform_test_raw, transform_test_hf, transform_test_vf,
        transform_test_tr
    ]
Beispiel #13
0
def transform_v0(config):
    """ default transforms

    Args:
        config: CFG

    Returns: train_tranforms, test_transforms
    """
    train_transforms = Compose([
        Resize(config.image_size, config.image_size),
        Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225],
        ),
        ToTensor()
    ],
                               p=1)

    test_transforms = Compose([
        Resize(config.image_size, config.image_size),
        Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225],
        ),
        ToTensor()
    ],
                              p=1)

    return train_transforms, test_transforms
Beispiel #14
0
def get_transforms(config, mode):
    if (mode == "val") or (mode =="test"):
        transform = [
            Resize(config["model"]["image_size"], config["model"]["image_size"]),
            # Normalize(mean=phase_config.mean, std=phase_config.std, p=1),
            ToTensor(),
        ]
        return transform

    elif mode == "train":
        list_transforms = []
        config_tarnsform = config[mode]["transform"]
        if config_tarnsform["HorizontalFlip"]:
            list_transforms.append(HorizontalFlip())
        if config_tarnsform["VerticalFlip"]:
            list_transforms.append(VerticalFlip())
        if config_tarnsform["Contrast"]:
            list_transforms.append(
                OneOf([
                    RandomContrast(0.5),
                    RandomBrightness(),
                ], p=0.5),
            )
        if config_tarnsform["ShiftScaleRotate"]:
            list_transforms.append(ShiftScaleRotate(shift_limit=(-0.05, 0.05), scale_limit=(-0.05, 0.05), rotate_limit=(-30,30), p=0.5))

        list_transforms.extend(
            [
                Resize(config["model"]["image_size"], config["model"]["image_size"]),
                # Normalize(mean=phase_config.mean, std=phase_config.std, p=1),
                ToTensor(),
            ]
        )

        return list_transforms
    def transform_aug(self, p=1, im_height=480, im_width=640):
        dropout_w = int(im_width / 8)
        dropout_h = int(im_height / 9.)
        strong = Compose(
            OneOf([
                Compose([
                    Rotate(limit=1, p=0.5),
                    RandomCrop(height=int(im_height * 0.95),
                               width=int(im_width * 0.9),
                               p=0.3),
                    ElasticTransform(p=0.8),
                    HorizontalFlip(p=0.5),
                    CoarseDropout(max_holes=8,
                                  max_height=dropout_w,
                                  max_width=dropout_h,
                                  min_holes=1,
                                  min_height=5,
                                  min_width=5,
                                  fill_value=0,
                                  always_apply=False,
                                  p=0.85),
                    OneOf([
                        MotionBlur(p=0.8),
                        Blur(blur_limit=20, p=0.8),
                    ],
                          p=0.35),
                    Resize(height=256, width=256, p=1)
                ]),
                Resize(height=256, width=256, p=1)
            ],
                  p=1))

        return strong
Beispiel #16
0
def get_transforms(*, data_type):
    if data_type == "light_train":
        return Compose([
            Resize(CFG.size, CFG.size),
            Transpose(p=0.5),
            HorizontalFlip(p=0.5),
            VerticalFlip(p=0.5),
            ShiftScaleRotate(scale_limit=(0, 0), p=0.5),
            ToTensorV2(),
        ])

    if data_type == "train":
        return Compose([
            Resize(CFG.size, CFG.size),
            Transpose(p=0.5),
            HorizontalFlip(p=0.5),
            VerticalFlip(p=0.5),
            albumentations.OneOf([
                albumentations.ElasticTransform(
                    alpha=1, sigma=20, alpha_affine=10),
                albumentations.GridDistortion(num_steps=6, distort_limit=0.1),
                albumentations.OpticalDistortion(distort_limit=0.05,
                                                 shift_limit=0.05),
            ],
                                 p=0.2),
            albumentations.core.composition.PerChannel(albumentations.OneOf([
                albumentations.MotionBlur(p=.05),
                albumentations.MedianBlur(blur_limit=3, p=.05),
                albumentations.Blur(blur_limit=3, p=.05),
            ]),
                                                       p=1.0),
            albumentations.OneOf([
                albumentations.CoarseDropout(max_holes=16,
                                             max_height=CFG.size // 16,
                                             max_width=CFG.size // 16,
                                             fill_value=0,
                                             p=0.5),
                albumentations.GridDropout(ratio=0.09, p=0.5),
                albumentations.Cutout(num_holes=8,
                                      max_h_size=CFG.size // 16,
                                      max_w_size=CFG.size // 16,
                                      p=0.2),
            ],
                                 p=0.5),
            albumentations.ShiftScaleRotate(
                shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.5),
            ToTensorV2(),
        ],
                       additional_targets={
                           'r': 'image',
                           'g': 'image',
                           'b': 'image',
                           'y': 'image',
                       })

    elif data_type == 'valid':
        return Compose([
            Resize(CFG.size, CFG.size),
            ToTensorV2(),
        ])
    def __call__(self, original_image):
        self.augmentation_pipeline = Compose(
            [
                Resize(650, 650, always_apply=True),
                HorizontalFlip(p=0.5),
                VerticalFlip(p=0.5),
                ShiftScaleRotate(rotate_limit=25.0, p=0.7),
                OneOf([IAAEmboss(p=1),
                       IAASharpen(p=1),
                       Blur(p=1)], p=0.5),
                IAAPiecewiseAffine(p=0.5),
                Resize(self.height, self.width, always_apply=True),
                Normalize(
                    mean=[0.485, 0.456, 0.406],
                    std=[0.229, 0.224, 0.225],
                    always_apply=True
                ),
                ToTensor()
            ]
        )

        augmented = self.augmentation_pipeline(
            image=original_image
        )
        image = augmented["image"]
        return image
Beispiel #18
0
def seg_transforms(phase, resize=(512, 512), mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
    """ Get segmentation albumentation tansforms
    Args:
        phase: train or valid
        resize: input image shape into model

    Returns:
        albu compose transforms
    Raises:
        IOError: An error occurred accessing ablumentation object.
    """
    assert (phase in ['train', 'valid', 'test'])
    transforms_list = []
    if phase == 'train':
        transforms_list.extend([
            # Rotate
            VerticalFlip(p=0.5),
            HorizontalFlip(p=0.5),
            ShiftScaleRotate(rotate_limit=20, border_mode=0, p=0.2),
            RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.2),
            Resize(resize[0] + 64, resize[1] + 64, interpolation=Image.BILINEAR),
            Normalize(mean=mean, std=std, p=1),
            RandomCrop(resize[0], resize[1]),
            ToTensor(),
        ])
    else:
        transforms_list.extend([
            Resize(resize[0], resize[1], interpolation=Image.BILINEAR),
            Normalize(mean=mean, std=std, p=1),
            ToTensor(),
        ])
    transforms = Compose(transforms_list)
    return transforms
    def __init__(self,
                 df,
                 rgb_channel=False,
                 img_size=512,
                 grid_size=32,
                 is_train=True):
        self.df = df
        self.is_train = is_train
        self.img_size = img_size
        self.rgb_channel = rgb_channel
        self.clahe = cv2.createCLAHE(clipLimit=2.0,
                                     tileGridSize=(grid_size, grid_size))

        # Transforms
        if self.is_train:
            self.transforms = Compose([
                Resize(img_size, img_size),
                HorizontalFlip(p=0.5),
                ShiftScaleRotate(shift_limit=0,
                                 scale_limit=0.1,
                                 rotate_limit=10,
                                 p=0.5,
                                 border_mode=cv2.BORDER_CONSTANT),
                GaussNoise(),
                MultiplicativeNoise(),
                ToTensor()
            ])
        else:
            self.transforms = Compose([Resize(img_size, img_size), ToTensor()])
Beispiel #20
0
    def __get_transformations(self):
        norm_mean = [0.3456, 0.2281, 0.2233]
        norm_std = [0.2528, 0.2135, 0.2104]
        normalize = Normalize(mean=norm_mean, std=norm_std)
        training_augmentation = Compose([
            ShiftScaleRotate(shift_limit=0.1,
                             scale_limit=(-0.2, 0.5),
                             rotate_limit=15,
                             border_mode=0,
                             value=0,
                             p=0.7),
        ])

        data_transformations = {}
        data_transformations["train"] = Compose([
            Resize(height=self.input_height, width=self.input_width),
            training_augmentation,
            normalize,
            ToTensorV2(),
        ])
        data_transformations["val"] = Compose([
            Resize(height=self.input_height, width=self.input_width),
            normalize,
            ToTensorV2(),
        ])
        data_transformations["test"] = data_transformations["val"]
        return data_transformations
 def __init__(self, mean, std, imgdim=64, augmentations=True):
     if augmentations:
         self.albumentations_transform = Compose([
             Resize(imgdim,
                    imgdim,
                    interpolation=1,
                    always_apply=False,
                    p=1),
             Normalize(
                 mean=mean,
                 std=std,
             ),
             ToTensor()
         ])
     else:
         self.albumentations_transform = Compose([
             Resize(imgdim,
                    imgdim,
                    interpolation=1,
                    always_apply=False,
                    p=1),
             Normalize(
                 mean=mean,
                 std=std,
             ),
             ToTensor()
         ])
    def init_augmentations(self):
        common = [
            HorizontalFlip(),
            Rotate(limit=10),
            RandomBrightnessContrast(),
            ToGray(p=0.05)
        ]

        random_crop_aug = [
            RandomResizedCrop(height=self.params.input_height,
                              width=self.params.input_width,
                              scale=(0.35, 1.0))
        ]
        random_crop_aug.extend(common)

        simple_resize_aug = [
            Resize(height=self.params.input_height,
                   width=self.params.input_width)
        ]
        simple_resize_aug.extend(common)

        crop = self.get_aug(random_crop_aug, min_visibility=0.5)

        resize = self.get_aug(simple_resize_aug, min_visibility=0.5)

        just_resize = self.get_aug([
            Resize(height=self.params.input_height,
                   width=self.params.input_width)
        ])

        self.crop_aug = crop
        self.resize_aug = resize
        self.just_resize = just_resize
def load_img_dataset(data_path,
                     batch_size,
                     resize=None,
                     crop=None,
                     num_samples=None):
    transform = Compose([
        Resize(256, 256),
        strong_aug(),
        Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225],
        ),
        ToTensor(),
    ])

    if resize is not None:
        transform = Compose([
            Resize(resize, resize),
            strong_aug(),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor(),
        ])

    if crop is not None:
        transform = Compose([
            Resize(256, 256),
            RandomCrop(crop, crop),
            strong_aug(),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor(),
        ])

    train_dataset = ImageFolderAlbum(root=data_path, transform=transform)

    sampler = None

    if num_samples is not None:
        sampler = ImbalancedDatasetSampler(train_dataset,
                                           num_samples=num_samples)
    else:
        sampler = ImbalancedDatasetSampler(train_dataset)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=batch_size,
                                               num_workers=1,
                                               drop_last=True,
                                               sampler=sampler,
                                               pin_memory=True,
                                               shuffle=False)
    return train_loader
Beispiel #24
0
 def __init__(self, df, aug=True, mode='train'):
     self.data = df
     self.mode = mode
     if mode is 'train':
         self.images = df.ImageId.unique()
         self._aug = Compose([Normalize(), Flip(), Resize(224, 224)])
     elif mode is 'test' or mode is 'val':
         self.images = df.ImageId.unique()
         self._aug = Compose([Normalize(), Resize(224, 224)])
     else:
         raise RuntimeError()
    def _transformed(self, img, pad_side=0, pad_factor=0):
        side_percents = [0] * 4
        side_percents[pad_side] = pad_factor

        w, h = img.shape[:2]

        aug = iaa.CropAndPad(percent=tuple(side_percents), pad_cval=self.value)
        img = aug.augment_image(img)

        resize = Resize(w, h)
        return resize.apply(img)
def train_val_dataloaders(train_path: str, val_path: str, augment: bool,
                          batch_size: int):
    """Form the dataloaders for training and validation and store them in the dictionary.

    :param train_path: path to images for trainin
    :param val_path: path to images for validation
    :param batch_size: size of the batch
    :return: the dictionary with dataloaders
    """
    if augment:
        train_transform = Compose([
            Blur(p=0.1),
            ChannelDropout(p=0.1),
            Flip(p=0.5),
            GaussNoise((10.0, 30.0), 25.0, p=0.1),
            HueSaturationValue(p=0.1),
            RandomBrightnessContrast(brightness_limit=(-0.20, 0.50), p=0.1),
            RandomGamma(p=0.1),
            RandomRotate90(p=0.5),
            RGBShift(p=0.1),
            Transpose(p=0.25),
            Resize(224, 224, p=1.0),
            Normalize(),
            ToTensorV2(),
        ])
    else:
        train_transform = Compose(
            [Resize(224, 224), Normalize(),
             ToTensorV2()])

    val_transforms = Compose([Resize(224, 224), Normalize(), ToTensorV2()])

    train_dataset = AlbumentationsImageFolder(train_path, train_transform)
    val_dataset = AlbumentationsImageFolder(val_path, val_transforms)

    dataloader = dict()

    dataloader["train"] = torch.utils.data.DataLoader(
        dataset=train_dataset,
        batch_size=batch_size,
        shuffle=True,
        num_workers=4,
        drop_last=True,
    )

    dataloader["val"] = torch.utils.data.DataLoader(
        dataset=val_dataset,
        batch_size=batch_size,
        shuffle=True,
        num_workers=4,
        drop_last=True,
    )

    return dataloader
Beispiel #27
0
 def compose_transform(self, image_size):
     self.train_transform = Compose([
         Resize(*image_size, p=1.0),
         HorizontalFlip(p=0.5),
         Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], p=1.0),
         ToTensorV2()
     ])
     self.validation_transform = Compose([
         Resize(*image_size, p=1.0),
         Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], p=1.0),
         ToTensorV2()
     ])
Beispiel #28
0
    def __init__(self,
                 path,
                 input_size,
                 batch_size,
                 augmentations,
                 mode='train'):
        '''
        Initialises the attributes of the class

        Args:
            path (str): Path to the dataset directory
            input_size (int): Height/Width of the input image
            batch_size (int): Batch size to be used
            augmentations (str): If set to "train", image augmentations are applied
        
        Returns:
            None
        '''

        random.seed(42)

        labels = {
            name: index
            for index in range(1) for name in glob.glob(path + '/*.JPG')
        }
        l = list(labels.items())
        labels = dict(l)

        self.path = path
        self.names = list(labels.keys())
        self.labels = list(labels.values())
        self.input_size = input_size
        self.batch_size = batch_size

        AUGMENTATIONS_TRAIN = Compose([
            HorizontalFlip(p=0.5),
            RandomContrast(limit=0.2, p=0.5),
            RandomGamma(gamma_limit=(80, 120), p=0.5),
            RandomBrightness(limit=0.2, p=0.5),
            HueSaturationValue(hue_shift_limit=5,
                               sat_shift_limit=20,
                               val_shift_limit=10,
                               p=.9),
            RandomRotate90(),
            Resize(input_size, input_size),
            ToFloat(max_value=255)
        ])

        AUGMENTATIONS_TEST = Compose(
            [Resize(input_size, input_size),
             ToFloat(max_value=255)])

        self.augment = AUGMENTATIONS_TRAIN if augmentations == 'train' else AUGMENTATIONS_TEST
Beispiel #29
0
    def __init__(self, args=None):
        super().__init__()

        self.opt = None
        self.sched = None

        if args is not None:
            for k in vars(args):
                self.hparams[k] = getattr(args, k)

        if args is not None:
            fold = args.fold
        else:
            fold = 0

        df = pd.read_csv(
            '/home/lyan/Documents/kaggle/isic_melanoma/group_fold_train_512.csv'
        )
        self.train_df = df[df.fold != fold].reset_index().drop('index',
                                                               axis=1).copy()
        self.valid_df = df[df.fold == fold].reset_index().drop('index',
                                                               axis=1).copy()
        self.path = '/var/ssd_2t_1/kaggle_isic/ds_512_2/512x512-dataset-melanoma/512x512-dataset-melanoma'

        if args is not None:
            self.batch_size = args.batch_size
        else:
            self.batch_size = 1

        self.model = get_mel_model(mdl=args.model)

        self.train_aug = Compose([
            Resize(width=400, height=400, always_apply=True),
            RandomResizedCrop(width=384,
                              height=384,
                              scale=(0.5, 0.9),
                              ratio=(0.5, 1),
                              always_apply=True),
            CLAHE(),
            ShiftScaleRotate(),
            Normalize(always_apply=True),
            ToTensorV2(always_apply=True),
        ],
                                 p=0.3)

        self.valid_aug = Compose([
            Resize(width=384, height=384, always_apply=True),
            Normalize(always_apply=True),
            ToTensorV2(always_apply=True),
        ])
        self.val_y_hat = []
        self.val_y = []
Beispiel #30
0
def data_transform(size=256, normalize=True):
    if normalize:
        t = Compose([
            Resize(size, size),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor()
        ])
    else:
        t = Compose([Resize(size, size), ToTensor()])
    return t