def __init__(self, root_dir, partition, augment):
        self.root_dir = root_dir
        self.list_IDs = os.listdir(os.path.join(self.root_dir, 'y_{}'.format(partition)))
        self.partition = partition
        self.augment = augment
        self.to_tensor = transforms.ToTensor()
        self.augmentator = Compose([
                    # Non destructive transformations
                        VerticalFlip(p=0.6),
                        HorizontalFlip(p=0.6),
                        RandomRotate90(),
                        Transpose(p=0.6),
                        ShiftScaleRotate(p=0.45, scale_limit=(0.1, 0.3)),

                    #     # Non-rigid transformations
                        # ElasticTransform(p=0.25, alpha=160, sigma=180 * 0.05, alpha_affine=120 * 0.03),

                        Blur(blur_limit=3, p=0.2),

                    #     Color augmentation
                        RandomBrightness(p=0.5),
                        RandomContrast(p=0.5),
                        RGBShift(p=0.3),
                        RandomGamma(p=0.5),
                        CLAHE(p=0.5)

                        ]
                    )
def aug_image(image):
    from albumentations import (
        HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90, RandomGamma, VerticalFlip,
        Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue, 
        IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, RandomBrightnessContrast, IAAPiecewiseAffine,
        IAASharpen, IAAEmboss, Flip, OneOf, Compose, Rotate, RandomContrast, RandomBrightness, RandomCrop, Resize, OpticalDistortion
    )

    transforms = Compose([
            #Rotate(limit=30, p=0.5),
            #Rotate(limit=180, p=0.5),
            #RandomRotate90(p=1.0)
            #Transpose(p=1.0)
            # Resize(248,248, p=1),     # resize 후 크롭
            # RandomCrop(224,224, p=1),  # 위에꺼랑 세트
            
            OneOf([
            RandomContrast(p=1, limit=(-0.5,2)),   # -0.5 ~ 2 까지가 현장과 가장 비슷함  -- RandomBrightnessContrast
            RandomBrightness(p=1, limit=(-0.2,0.4)),
            RandomGamma(p=1, gamma_limit=(80,200)),
            ], p=1),
                
            # OneOf([
            #     Rotate(limit=30, p=0.3),
            #     RandomRotate90(p=0.3),
            #     VerticalFlip(p=0.3)
            # ], p=0.3),
        
            MotionBlur(p=0.2),   # 움직일때 흔들리는 것 같은 이미지
            #ShiftScaleRotate(shift_limit=0.001, scale_limit=0.1, rotate_limit=30, p=0.3, border_mode=1),
            #Resize(224,224, p=1),
            ],
            p=1)
    return transforms(image=image)['image']
Пример #3
0
    def augmentation(image,
                     mask,
                     noise=False,
                     transform=False,
                     clahe=True,
                     r_bright=True,
                     r_gamma=True):
        aug_list = [
            VerticalFlip(p=0.5),
            HorizontalFlip(p=0.5),
            RandomRotate90(p=0.5),
        ]
        if r_bright:
            aug_list += [RandomBrightnessContrast(p=.5)]
        if r_gamma:
            aug_list += [RandomGamma(p=.5)]
        if clahe:
            aug_list += [CLAHE(p=1., always_apply=True)]
        if noise:
            aug_list += [GaussNoise(p=.5, var_limit=1.)]
        if transform:
            aug_list += [
                ElasticTransform(p=.5,
                                 sigma=1.,
                                 alpha_affine=20,
                                 border_mode=0)
            ]
        aug = Compose(aug_list)

        augmented = aug(image=image, mask=mask)
        image_heavy = augmented['image']
        mask_heavy = augmented['mask']
        return image_heavy, mask_heavy
Пример #4
0
def create_generator(
    mode="train",
    augmenter=True,
    batch_size=32,
    labels_encoding_mode="smooth",
    smooth_factor=0.1,
    h=227,
    w=227,
):

    my_augmenter = (
        Compose([
            HorizontalFlip(p=0.5),
            RandomGamma(gamma_limit=(80, 120), p=0.5),
            Resize(h, w, cv2.INTER_AREA),
            # ToFloat(max_value=255)
        ]) if augmenter else False)

    gen = HDF5ImageGenerator(
        src="/storage/datasets/mnist_test.h5",
        scaler=True,
        labels_encoding=labels_encoding_mode,
        smooth_factor=smooth_factor,
        batch_size=batch_size,
        augmenter=my_augmenter,
        mode=mode,
    )

    return gen
Пример #5
0
    def __init__(self, is_train: bool, to_pytorch: bool):
        if is_train:
            self._aug = Compose([
                OneOf([
                    Compose([
                        SmallestMaxSize(
                            max_size=min(data_height, data_width) * 1.1, p=1),
                        RandomCrop(height=data_height, width=data_width, p=1)
                    ],
                            p=1),
                    Resize(height=data_height, width=data_width, p=1)
                ],
                      p=1),
                GaussNoise(p=0.5),
                RandomGamma(p=0.5),
                RandomBrightnessContrast(p=0.5),
                HorizontalFlip(p=0.5)
            ],
                                p=1)
        else:
            self._aug = Compose([
                SmallestMaxSize(max_size=min(data_height, data_width), p=1),
                CenterCrop(height=data_height, width=data_width, p=1)
            ],
                                p=1)

        self._need_to_pytorch = to_pytorch
 def box_segmentation_aug():
     return Compose([
         OneOf([
             RandomBrightnessContrast(brightness_limit=0.2, p=0.5),
             RandomGamma(gamma_limit=50, p=0.5),
             ChannelShuffle(p=0.5)
         ]),
         OneOf([
             ImageCompression(quality_lower=0, quality_upper=20, p=0.5),
             MultiplicativeNoise(multiplier=(0.3, 0.8),
                                 elementwise=True,
                                 per_channel=True,
                                 p=0.5),
             Blur(blur_limit=(15, 15), p=0.5)
         ]),
         OneOf([
             CenterCrop(height=1000, width=1000, p=0.1),
             RandomGridShuffle(grid=(3, 3), p=0.2),
             CoarseDropout(max_holes=20,
                           max_height=100,
                           max_width=100,
                           fill_value=53,
                           p=0.2)
         ]),
         OneOf([
             GridDistortion(p=0.5, num_steps=2, distort_limit=0.2),
             ElasticTransform(alpha=157, sigma=80, alpha_affine=196, p=0.5),
             OpticalDistortion(distort_limit=0.5, shift_limit=0.5, p=0.5)
         ]),
         OneOf([
             VerticalFlip(p=0.5),
             HorizontalFlip(p=0.5),
             Rotate(limit=44, p=0.5)
         ])
     ])
Пример #7
0
def aug_with_crop(width=640, height=480, crop_prob=1):
    return Compose(
        [
            # RandomCrop(width=480, height=640, p=crop_prob),
            HorizontalFlip(p=0.5),
            VerticalFlip(p=0.5),
            RandomRotate90(p=0.5),
            Transpose(p=0.5),
            ShiftScaleRotate(
                shift_limit=0.01, scale_limit=0.04, rotate_limit=0, p=0.25),
            RandomBrightnessContrast(p=0.5),
            RandomGamma(p=0.25),
            IAAEmboss(p=0.25),
            Blur(p=0.01, blur_limit=3),
            OneOf([
                ElasticTransform(p=0.5,
                                 alpha=120,
                                 sigma=120 * 0.05,
                                 alpha_affine=120 * 0.03),
                GridDistortion(p=0.5),
                OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5)
            ],
                  p=0.8)
        ],
        p=1)
def get_augmenter(p=1.0):
    return Compose([
        ElasticTransform(
            p=0.8, alpha_affine=10, border_mode=cv2.BORDER_REPLICATE),
        RandomGamma(p=0.8, gamma_limit=(50, 150)),
    ],
                   p=p)
Пример #9
0
def hard_transform(image_size=224, p=0.5):
    transforms = [
        Cutout(
            num_holes=4,
            max_w_size=image_size // 4,
            max_h_size=image_size // 4,
            p=p
        ),
        ShiftScaleRotate(
            shift_limit=0.1,
            scale_limit=0.1,
            rotate_limit=15,
            border_mode=cv2.BORDER_REFLECT,
            p=p
        ),
        IAAPerspective(scale=(0.02, 0.05), p=p),
        OneOf(
            [
                HueSaturationValue(p=p),
                ToGray(p=p),
                RGBShift(p=p),
                ChannelShuffle(p=p),
            ]
        ),
        RandomBrightnessContrast(
            brightness_limit=0.5, contrast_limit=0.5, p=p
        ),
        RandomGamma(p=p),
        CLAHE(p=p),
        JpegCompression(quality_lower=50, p=p),
    ]
    transforms = Compose(transforms)
    return transforms
Пример #10
0
  def __init__(
      self,
      image_ids,
      transform=True,
      preprocessing_fn=None
  ):
    """
    Dataset class for segmentation problem
    :param image_ids: ids of the images, list
    :param transform: True/False, no transform in validation :param preprocessing_fn: a function for preprocessing image
    """
    # we create a empty dictionary to store iamge # and mask paths
    self.data = defaultdict(dict)
    # for augmentations
    self.transform = transform
    # preprocessing function to normalize
    # images
    self.preprocessing_fn = preprocessing_fn

    # albumentation augmentations
    # we have shift, scale & rotate
    # applied with 80% probability
    # and then one of gamma and brightness/contrast # is applied to the image
    # albumentation takes care of which augmentation # is applied to image and mask
    self.aug = Compose(
      [
        ShiftScaleRotate(
          shift_limit=0.0625,
          scale_limit=0.1,
          rotate_limit=10,
          p=0.8
        ),
        OneOf(
          [
              RandomGamma(
                gamma_limit=(90, 110)
              ),
              RandomBrightnessContrast(
                  brightness_limit=0.1,
                  contrast_limit=0.1
              ), 
          ],
          p=0.5,
      ),
      ]
    )
    # going over all image_ids to store # image and mask paths
    for imgid in image_ids:
      TRAIN_PATH="/content/drive/MyDrive/Colab_Notebooks/AAMPL/image_classification/Classification/input/train_png"
      # files = glob.glob(os.path.join(TRAIN_PATH, imgid, "*.png"))
      files = glob.glob("/content/drive/MyDrive/Colab_Notebooks/AAMPL/image_classification/Classification/input/train_png/" + imgid + "*.png")
      self.data[counter] = {
        "img_path": os.path.join(
            TRAIN_PATH, imgid + ".png"
        ),
        "mask_path": os.path.join(
          TRAIN_PATH, imgid + "_mask.png"
        ),
      }
Пример #11
0
def flow(data_dir, Timage, Tmask, batch, size1, size2, augument=False):

    images_ = os.listdir(data_dir + Timage)
    shuffle(images_)
    ids_int = list(range(len(images_)))
    NORMALIZE = 127.5
    while True:
        try:
            for start in range(0, len(ids_int), batch):
                x_batch = []
                y_batch = []
                end = min(start + batch, len(images_))
                batch_create = ids_int[start:end]
                jbs = dict()
                for loads in batch_create:
                    try:
                        img = cv2.imread(
                            os.path.join(data_dir, Timage, images_[loads]))
                        height_o_image, width_o_image = img.shape[
                            0], img.shape[1]
                        if height_o_image % 2 != 0:
                            height_o_image = height_o_image - 1
                        if width_o_image % 2 != 0:
                            width_o_image = width_o_image - 1
                        jbs["width"] = width_o_image * 2
                        jbs["height"] = height_o_image * 2
                        img = cv2.resize(img, (width_o_image, height_o_image))
                        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                        masks = cv2.imread(
                            os.path.join(data_dir, Tmask, images_[loads]))
                        masks = cv2.resize(masks,
                                           (jbs["width"], jbs["height"]))
                    except:
                        continue
                    if augument:
                        aug = Compose([
                            VerticalFlip(p=0.1),
                            Transpose(p=0.01),
                            RandomGamma(p=0.06),
                            OpticalDistortion(p=0.00,
                                              distort_limit=0.7,
                                              shift_limit=0.3)
                        ])
                        augmented = aug(image=img, mask=masks)
                        img = augmented['image']
                        masks = augmented['mask']
                        x_batch.append(img)
                        y_batch.append(masks)
                    else:
                        x_batch.append(img)
                        y_batch.append(masks)
                x_batch = np.array(x_batch) / NORMALIZE
                x_batch = x_batch - 1
                y_batch = np.array(y_batch) / NORMALIZE
                y_batch = y_batch - 1
                yield x_batch, y_batch

        except:
            continue
def color_aug(p=0.5):
    return Compose(
        [
            RandomBrightness(p=0.5),
            RandomGamma(p=0.5),
            RGBShift(p=0.5),
        ],
        p=p)
Пример #13
0
 def __init__(self):
     self.aug = Compose([
         ShiftScaleRotate(p=1,
                          shift_limit=0.05,
                          scale_limit=0.05,
                          rotate_limit=5,
                          border_mode=cv2.BORDER_CONSTANT),
         RandomGamma(p=1, gamma_limit=(80, 120)),
         #             RandomBrightnessContrast(p=1, brightness_limit=0.3, contrast_limit=0.3),
     ])
Пример #14
0
 def __init__(self, p=0.5):
     self.aug = Compose([
         RandomBrightnessContrast(),
         RandomGamma(),
         CLAHE(),
         OneOf([
             GaussNoise(),
             Cutout(num_holes=10, max_h_size=5, max_w_size=5)
         ])
     ])
Пример #15
0
def strong_tta(p=0.9):
    return Compose(
        [
            Blur(blur_limit=3),
            OneOf([RandomContrast(p=.5),
                   RandomBrightness(p=.5)]),
            RandomGamma(),
            #RandomSizedCrop(p=0.9, min_max_height=(65, 101), height=101, width=101),
        ],
        p=p)
Пример #16
0
    def _transform_generic_np(self, npimages, prob):
        """So, we assume these images are batched numpy values, scaled from 0..255"""
        batchsz, height, width, channels = npimages.shape
        assert height == width, "We assume squares as inputs."
        assert channels == 3, "We assume RGB images."
        assert npimages.dtype == np.uint8
        assert 0 <= npimages.min() and npimages.max() <= 255

        # make these the images that albumentation requires.
        outputs = np.zeros((batchsz, self.outsz, self.outsz, channels),
                           dtype=np.uint8)

        ops = Compose( \
            [ \
                Compose( \
                    [ \
                        OneOf([ \
                            IAAAdditiveGaussianNoise(p=1.0), \
                            GaussNoise(p=1.0), \
                        ], p=0.5), \
                        OneOf([ \
                            MotionBlur(p=1.0), \
                            MedianBlur(blur_limit=3, p=1.0), \
                            Blur(blur_limit=3, p=1.0), \
                        ], p=0.5), \
                        RandomGamma(p=0.5), \
                        Rotate(limit=45, interpolation=cv2.INTER_CUBIC, p=0.5), \
                        ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, interpolation=cv2.INTER_CUBIC, p=0.5), \
                        OneOf([ \
                            OpticalDistortion(interpolation=cv2.INTER_CUBIC, p=1.0), \
                            GridDistortion(interpolation=cv2.INTER_CUBIC, p=1.0), \
                            IAAPiecewiseAffine(p=1.0), \
                        ], p=0.5), \
                        OneOf([ \
                            CLAHE(clip_limit=2, p=1.0), \
                            IAASharpen(p=1.0), \
                            IAAEmboss(p=1.0), \
                            RandomContrast(p=1.0), \
                            RandomBrightness(p=1.0), \
                        ], p=0.5), \
                        HueSaturationValue(p=0.5), \
                    ], \
                    p=prob \
                ), \
                Resize(self.outsz, self.outsz, interpolation=cv2.INTER_CUBIC), \
            ], \
            p=1.0 \
        )

        # So, the output of ops, should be a dictionary containing an image
        for idx in range(0, batchsz):
            vvv = ops(image=npimages[idx])["image"]
            outputs[idx] = vvv

        return outputs
Пример #17
0
def get_transforms(*, data):

    if data == 'train':
        return Compose([
            HorizontalFlip(p=0.5),
            VerticalFlip(p=0.5),
            RandomRotate90(p=0.5),
            Transpose(p=0.5),
            ShiftScaleRotate(scale_limit=0.2,
                             rotate_limit=0,
                             shift_limit=0.2,
                             p=0.2,
                             border_mode=0),
            IAAAdditiveGaussianNoise(p=0.2),
            IAAPerspective(p=0.5),
            OneOf(
                [
                    CLAHE(p=1),
                    RandomBrightness(p=1),
                    RandomGamma(p=1),
                ],
                p=0.9,
            ),
            OneOf(
                [
                    IAASharpen(p=1),
                    Blur(blur_limit=3, p=1),
                    MotionBlur(blur_limit=3, p=1),
                ],
                p=0.9,
            ),
            OneOf(
                [
                    RandomContrast(p=1),
                    HueSaturationValue(p=1),
                ],
                p=0.9,
            ),
            Compose([VerticalFlip(p=0.5),
                     RandomRotate90(p=0.5)]),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensorV2(),  #ToTensor(num_classes=2),
        ])
    elif data == 'valid':
        return Compose([
            Resize(256, 256),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensorV2(),
        ])
Пример #18
0
    def __getitem__(self, index):
        '''
            The method to get one data from dataset.
            index: int, the index of the sample in dataset.
        '''
        #get data from dataset
        img_path = self.img_paths[index]
        mask_path = self.mask_paths[index]
        img = cv2.imread(img_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        #modify the mask to satisfied our requirments for the mask
        if not os.path.exists(mask_path):
            mask = np.zeros((img.shape[0], img.shape[1]), dtype=np.float32)
        else:
            mask = cv2.imread(mask_path, 0)
            mask[mask > 1] = 2
            mask[mask < 1] = 1
            mask[mask > 1] = 0

        if isinstance(self.img_size, tuple):
            height = self.img_size[1]
            width = self.img_size[0]
        elif isinstance(self.img_size, int):
            width = height = self.img_size

        if self.augumentation:
            #augmentation methods
            task = Compose([
                RandomBrightnessContrast(),
                RandomGamma(),
                HorizontalFlip(),
                VerticalFlip(),
                ChannelShuffle(),
                PadIfNeeded(height, width),
            ])
            #augmentation
            augument_data = task(image=img, mask=mask)
            img = augument_data["image"]
            mask = augument_data["mask"]
        resize = Compose([
            Resize(height=height, width=width, always_apply=True),
            Normalize(mean=(0.5, 0.5, 0.5),
                      std=(0.5, 0.5, 0.5),
                      always_apply=True)
        ])
        #resize data
        resize_data = resize(image=img, mask=mask)
        img, mask = resize_data["image"], resize_data["mask"]
        if img.ndim > 2:
            img = np.transpose(img, axes=[2, 0, 1])
        elif img.ndim == 2:
            img = np.expand_dims(img, axis=0)
        return torch.from_numpy(img.astype(np.float32)), torch.from_numpy(
            mask.astype(np.float32))
Пример #19
0
def data_augmentation(original_image, original_mask, mode):
    """进行样本和掩膜的随机增强

    Args:
        original_image: 原始图片
        original_mask: 原始掩膜
    Return:
        image_aug: 增强后的图片
        mask_aug: 增强后的掩膜
    """
    original_height, original_width = original_image.shape[:2]
    augmentations = Compose([
        RandomRotate90(p=0.3),
        HorizontalFlip(p=0.3),
        Rotate(limit=15, p=0.3),
        CLAHE(p=0.3),
        HueSaturationValue(20, 5, 5, p=0.7),
        # 亮度、对比度
        RandomGamma(gamma_limit=(80, 120), p=0.4),
        RandomBrightnessContrast(p=0.4),
        #
        # # 模糊
        # OneOf([
        #     # MotionBlur(p=0.1),
        #     MedianBlur(blur_limit=3, p=0.1),
        #     Blur(blur_limit=3, p=0.1),
        # ], p=0.3),
        #
        # OneOf([
        #     IAAAdditiveGaussianNoise(),
        #     GaussNoise(),
        # ], p=0.2)
    ])

    augmentations2 = Compose([
        # HorizontalFlip(p=0.2),
        # HueSaturationValue(p=1),
        Rotate(limit=15, p=0.2),
        # CenterCrop(p=0.3, height=original_height, width=original_width),
        # 直方图均衡化
        # CLAHE(p=0.4),
    ])

    if mode == 'train':
        augmented = augmentations(image=original_image, mask=original_mask)
        image_aug = augmented['image']
        mask_aug = augmented['mask']
        return image_aug, mask_aug

    elif mode == 'validation':
        augmented = augmentations2(image=original_image, mask=original_mask)
        image_aug = augmented['image']
        mask_aug = augmented['mask']
        return image_aug, mask_aug
def aug():
    return Compose_alb([
        HorizontalFlip(),
        ShiftScaleRotate(rotate_limit=10, border_mode=0),
        RandomGamma(),
        RandomBrightness(),
        RandomContrast(),
        ElasticTransform(border_mode=0),
        GaussianBlur()
    ],
                       p=1)
Пример #21
0
def get_medium_augmentations(width, height):
    return [
        HorizontalFlip(p=1),
        VerticalFlip(p=1),
        RandomSizedCrop((height - 4, height - 2), height, width, p=1),
        RandomContrast(p=1),
        RandomBrightness(p=1),
        RandomGamma(p=1),
        ShiftScaleRotate(p=1),
        Blur(blur_limit=3, p=1)
    ]
def soft_aug(p=0.5):
    return Compose([
        HorizontalFlip(p=0.5),
        ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=30, p=0.2),
        OneOf([
            RandomContrast(),
            RandomGamma(),
            RandomBrightness()
            # RandomBrightnessContrast(),
            ], p=0.3)
        ], p=p)
Пример #23
0
def hard_transforms(image_size):
    min_holes, max_holes = 1, 2
    size = 30

    return [
        # Random shifts, stretches and turns with a 50% probability
        ShiftScaleRotate(
            shift_limit=0.2,
            scale_limit=0.2,
            rotate_limit=180,
            border_mode=BORDER_CONSTANT,
            p=0.1
        ),

        IAAPerspective(scale=(0.02, 0.05), p=0.1),

        # Random brightness / contrast with a 30% probability
        RandomBrightnessContrast(
            brightness_limit=0.2, contrast_limit=0.2, p=0.1
        ),

        OneOf([
            GaussNoise(var_limit=1.0, p=1.0),
            MultiplicativeNoise(multiplier=(0.9, 1), p=1.0)
        ], p=0.1),

        OneOf([
            GaussianBlur(blur_limit=3, p=1.0),
            Blur(p=1.0),
        ], p=0.1),

        # CoarseDropout(
        #     min_holes=min_holes,
        #     max_holes=max_holes,
        #     # min_height=image_height // 4,
        #     # max_height=image_height // 4,
        #     # min_width=image_width // 4,
        #     # max_width=image_width // 4,
        #     min_height=size,
        #     max_height=size,
        #     min_width=size,
        #     max_width=size,
        #     fill_value=0,
        #     p=1.0
        # ),

        # Random gamma changes with a 30% probability
        RandomGamma(gamma_limit=(85, 115), p=0.1),
        ImageCompression(
            quality_lower=70,
            quality_upper=100,
            p=0.1
        ),
    ]
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
Пример #25
0
def aug_with_crop(image_size=384, crop_prob=0.5):
    return Compose([
        RandomCrop(width=image_size, height=image_size, p=crop_prob),
        HorizontalFlip(p=0.5),
        VerticalFlip(p=0.5),
        RandomRotate90(p=0.5),
        Transpose(p=0.5),
        ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=45, p=0.5),
        RandomGamma(p=0.25),
        Resize(height=image_size, width=image_size)
    ], p=1)
Пример #26
0
def train_transform(size=28):
    return Compose([
        RandomCrop(size, size),
        ShiftScaleRotate(shift_limit=0.1,
                         scale_limit=(0.8, 1.2),
                         rotate_limit=10,
                         border_mode=cv2.BORDER_REPLICATE,
                         p=0.3),
        RandomGamma(gamma_limit=(95, 105), p=0.3),
        RandomBrightnessContrast(0.1, p=0.3),
        post_transform()
    ])
Пример #27
0
    def __init__(self,
                 image_ids,
                 arguments,
                 transform=True,
                 preprocessing_fn=None):
        """
        Dataset class for segmentation problem
        -> image_ids: ids of the image, list
        -> transform: True/False, no transform in validation
        -> preprocessing_fn: a function for preprocessing image
        """
        self.data = defaultdict(dict)
        self.counter = 0

        # get the arguments
        self.args = arguments

        # for augmentation
        self.transform = transform

        # preprocessing function
        self.preprocessing_fn = preprocessing_fn

        # albumentation augmentation
        # have shift, scale & rotate features
        # and is applied with 80% probability.
        # Gamma controls the and brightness /
        # contrast i.e. applied to the image.
        # Albumentation takes care of which augmentation
        # is applied to image and mask
        self.aug = Compose([
            ShiftScaleRotate(shift_limit=0.0625,
                             scale_limit=0.1,
                             rotate_limit=10,
                             p=0.8),
            OneOf(
                [
                    RandomGamma(gamma_limit=(90, 110)),
                    RandomBrightnessContrast(brightness_limit=0.1,
                                             contrast_limit=0.1),
                ],
                p=0.5,
            ),
        ])
        # going pver all image_ids to store
        # image and mask paths
        for imgid in image_ids:
            imgid = imgid.split('.')[0]
            self.data[self.counter] = {
                "img_path": os.path.join(self.args.image_path, imgid + ".png"),
                "mask_path": os.path.join(self.args.mask_path, imgid + ".png"),
            }
            self.counter += 1
Пример #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
Пример #29
0
def aug_with_crop(image_size):
    return Compose([
        RandomCrop(width=image_size, height=image_size, p=1.),
        HorizontalFlip(p=0.5),
        VerticalFlip(p=0.5),
        RandomRotate90(p=0.5),
        Transpose(p=0.5),
        ShiftScaleRotate(shift_limit=0.01, scale_limit=0.04, rotate_limit=0, p=0.25),
        RandomBrightnessContrast(p=0.5),
        RandomGamma(p=0.25),
        GridDistortion(p=0.5)
    ], p=1)
def TTA(img, model, model_name, seed=88, niter=4):

    input_size = int(model.get_input_at(0).get_shape()[1])

    AUGMENTATIONS = Compose([
        HorizontalFlip(p=0.25),
        RandomSizedCrop(min_max_height=(int(input_size * 0.75), input_size),
                        height=input_size,
                        width=input_size,
                        p=0.5),
        OneOf([
            ShiftScaleRotate(rotate_limit=25),
            ElasticTransform(
                alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03),
            GridDistortion(),
            OpticalDistortion(distort_limit=2, shift_limit=0.5),
        ],
              p=0.5),
        OneOf([RandomContrast(),
               RandomGamma(),
               RandomBrightness()], p=0.5),
        OneOf(
            [Blur(), MedianBlur(),
             GaussNoise(), GaussianBlur()], p=0.5)
    ],
                            p=0.5)

    np.random.seed(seed)
    original_img = img.copy()
    inverted_img = np.invert(img.copy())
    hflipped_img = np.fliplr(img.copy())
    original_img_array = np.empty(
        (niter + 1, img.shape[0], img.shape[1], img.shape[2]))
    inverted_img_array = original_img_array.copy()
    hflipped_img_array = original_img_array.copy()
    original_img_array[0] = original_img
    inverted_img_array[0] = inverted_img
    hflipped_img_array[0] = hflipped_img
    for each_iter in range(niter):
        original_img_array[each_iter +
                           1] = AUGMENTATIONS(image=original_img)['image']
        inverted_img_array[each_iter +
                           1] = AUGMENTATIONS(image=inverted_img)['image']
        hflipped_img_array[each_iter +
                           1] = AUGMENTATIONS(image=hflipped_img)['image']
    tmp_array = np.vstack(
        (original_img_array, inverted_img_array, hflipped_img_array))
    tmp_array = preprocess_input(tmp_array, model_name)

    prediction = np.mean(model.predict(tmp_array), axis=0)

    return prediction