Exemplo n.º 1
0
def get_transforms(*, data):

    if data == 'train':
        return Compose([
            LongestMaxSize(CFG.size),
            PadIfNeeded(min_height=CFG.size, min_width=CFG.size,
                        border_mode=1),
            #             Resize(CFG.size,CFG.size),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensorV2(),
        ])

    elif data == 'valid':
        return Compose([
            LongestMaxSize(CFG.size),
            PadIfNeeded(min_height=CFG.size, min_width=CFG.size,
                        border_mode=1),
            #             Resize(CFG.size,CFG.size),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensorV2(),
        ])
Exemplo n.º 2
0
def get_augmentations(IMG_SZ):
    train_aug = Compose([
        LongestMaxSize(IMG_SZ, p=1.0, always_apply=True),
        ShiftScaleRotate(p=1.0,
                         border_mode=cv2.BORDER_CONSTANT,
                         rotate_limit=10),
    ])

    val_aug = Compose([
        LongestMaxSize(IMG_SZ, p=1.0, always_apply=True),
    ])

    return train_aug, val_aug
Exemplo n.º 3
0
 def __init__(self, width, height):
     self.aug = Compose([
         LongestMaxSize(max_size=width if width > height else height),
         PadIfNeeded(min_height=height,
                     min_width=width,
                     border_mode=cv2.BORDER_CONSTANT)
     ])
 def __init__(self, data_root='data', split_file='', size=(256, 256), fold=0, resize=False):
     self.data_root = data_root
     pkl_data = pickle.load(open(split_file, 'rb'))
     if fold == -1:
         self.path_list = pkl_data[0]['train']
         self.path_list.extend(pkl_data[0]['val'])
     else:
         self.path_list = pkl_data[fold]['train']
     self.len = len(self.path_list)
     if resize:
         self.transforms = Compose([Resize(height=size[0], width=size[1], interpolation=cv2.INTER_NEAREST),
                                    ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=20, p=0.7,
                                                     border_mode=cv2.BORDER_CONSTANT, value=0),
                                    HorizontalFlip(p=0.5),
                                    OneOf([ElasticTransform(p=1, alpha=50, sigma=30, alpha_affine=30,
                                                            border_mode=cv2.BORDER_CONSTANT, value=0),
                                           OpticalDistortion(p=1, distort_limit=0.5, shift_limit=0.1,
                                                             border_mode=cv2.BORDER_CONSTANT, value=0)], p=0.5),
                                    RandomGamma(gamma_limit=(80, 120), p=0.5),
                                    GaussNoise(var_limit=(0.02, 0.1), mean=0, p=0.5)
                                    ])
     else:
         self.transforms = Compose([LongestMaxSize(max_size=max(size)),
                                    PadIfNeeded(min_height=size[0], min_width=size[1], value=0,
                                                border_mode=cv2.BORDER_CONSTANT),
                                    ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=20, p=0.7,
                                                     border_mode=cv2.BORDER_CONSTANT, value=0),
                                    HorizontalFlip(p=0.5),
                                    OneOf([ElasticTransform(p=1, alpha=50, sigma=30, alpha_affine=30,
                                                            border_mode=cv2.BORDER_CONSTANT, value=0),
                                           OpticalDistortion(p=1, distort_limit=0.5, shift_limit=0.1,
                                                             border_mode=cv2.BORDER_CONSTANT, value=0)], p=0.5),
                                    RandomGamma(gamma_limit=(80, 120), p=0.5),
                                    GaussNoise(var_limit=(0.02, 0.1), mean=0, p=0.5)
                                    ])
Exemplo n.º 5
0
def resize_transforms(image_size=224):
    pre_size = int(image_size * 1.5)

    random_crop = Compose([
      SmallestMaxSize(pre_size, p=1),
      RandomCrop(
          image_size, image_size, p=1
      )

    ])

    resize = Compose([
        Resize(image_size, image_size, p=1)
    ])

    random_crop_big = Compose([
      LongestMaxSize(pre_size, p=1),
      RandomCrop(
          image_size, image_size, p=1
      )

    ])

    # Converts the image to a square of size image_size x image_size
    result = [
      OneOf([
          random_crop,
          resize,
          random_crop_big
      ], p=1)
    ]

    return result
Exemplo n.º 6
0
def valid_transform(image_size=224):
    transforms = [
        LongestMaxSize(max_size=image_size),
        PadIfNeeded(image_size, image_size, border_mode=cv2.BORDER_CONSTANT),
        post_transform()
    ]
    transforms = Compose(transforms)
    return transforms
Exemplo n.º 7
0
def get_pred_label(image, solver, crop_size, stay_size, mode):
    '''
    params: 要求crop_size与stay_size为同奇偶
    img_path: 测试图片的路径
    crop_size: 图片太大需要裁切的大小;为一个整数c,代表(c,c)区域。
    stay_size: 最终保存结果的size,是crop_size的中间部分;为一个整数s,代表(s,s)区域
    '''
    pad_sts_row = int(stay_size - image.shape[0] % stay_size)
    pad_sts_column = int(stay_size - image.shape[1] % stay_size)

    pad_cs_row = (crop_size - stay_size) // 2
    pad_cs_column = (crop_size - stay_size) // 2

    pad = ((pad_cs_row, pad_sts_row + pad_cs_row),
           (pad_cs_column, pad_sts_column + pad_cs_column), (0, 0))
    pad_img = np.pad(image, pad, mode='reflect')
    pred_label = np.zeros(shape=(image.shape[0] + pad_sts_row,
                                 image.shape[1] + pad_sts_column))
    # predict and get the label
    for row_idx in range(0, pad_img.shape[0], stay_size):
        if row_idx > pad_img.shape[0] - crop_size:
            break
        for column_idx in range(0, pad_img.shape[1], stay_size):
            if column_idx > pad_img.shape[1] - crop_size:
                break
            crop_img = pad_img[row_idx:row_idx + crop_size,
                               column_idx:column_idx + crop_size, :]
            if mode == 'gray':
                resize_img = LongestMaxSize(512)(image=crop_img)['image']
                crop_result = solver.test_one_img_from_path(resize_img)
                crop_mask = LongestMaxSize(256)(
                    image=crop_result)['image']  #这里是mask的resize
                # crop_mask[crop_mask > 0.45] = 1.0
                # crop_mask[crop_mask <= 0.45] = 0.0
            elif mode == 'rgb':
                crop_mask = solver.test_one_img_from_path(crop_img)
                # crop_mask[crop_mask > 0.5] = 1.0
                # crop_mask[crop_mask <= 0.5] = 0.0

            pred_label[row_idx:row_idx + stay_size, column_idx:column_idx +
                       stay_size] = crop_mask[pad_cs_row:pad_cs_row +
                                              stay_size,
                                              pad_cs_column:pad_cs_column +
                                              stay_size]
    final_pred = pred_label[:image.shape[0], :image.shape[1]]
    return final_pred
Exemplo n.º 8
0
def aug_validation(prob=1.0, img_size=224):
    return Compose([
        LongestMaxSize(max_size=img_size),
        PadIfNeeded(min_height=img_size,
                    min_width=img_size,
                    border_mode=cv2.BORDER_CONSTANT),
    ],
                   p=prob)
Exemplo n.º 9
0
def pre_transforms(image_size=224):
    return Compose(
        [
            LongestMaxSize(max_size=image_size),
            PadIfNeeded(
                image_size, image_size, border_mode=cv2.BORDER_CONSTANT
            ),
        ]
    )
Exemplo n.º 10
0
def pre_transforms(image_size: int = 256):
    """Transforms that always be applied before other transformations"""
    transforms = Compose([
        ImageToRGB(),
        LongestMaxSize(max_size=image_size),
        PadIfNeeded(
            image_size, image_size, border_mode=cv2.BORDER_CONSTANT
        ),
    ])
    return transforms
Exemplo n.º 11
0
 def __init__(self, width, height):
     self.aug = Compose([
         LongestMaxSize(max_size=width if width > height else height),
         PadIfNeeded(min_height=height,
                     min_width=width,
                     border_mode=cv2.BORDER_CONSTANT)
     ],
                        keypoint_params=KeypointParams(
                            format='xy',
                            label_fields=['pose_id', "join_id"],
                            remove_invisible=True))
Exemplo n.º 12
0
 def load(self, path='/model'):
     self.transform = Compose([
         LongestMaxSize(max_size=224),
         PadIfNeeded(224, 224, border_mode=cv2.BORDER_CONSTANT),
         Normalize(),
         ToTensor(),
     ])
     self.model = torch.jit.load(os.path.join(path, 'model.pth'))
     with open(os.path.join(path, 'tag2class.json')) as fin:
         self.tag2class = json.load(fin)
         self.class2tag = {v: k for k, v in self.tag2class.items()}
         logging.debug(f'class2tag: {self.class2tag}')
Exemplo n.º 13
0
 def load(self, path="/model"):
     self.transform = Compose([
         LongestMaxSize(max_size=32),
         PadIfNeeded(32, 32, border_mode=cv2.BORDER_CONSTANT),
         Normalize(),
         ToTensor(),
     ])
     self.model = torch.jit.load(os.path.join(path, "model.pth"))
     with open(os.path.join(path, "tag2class.json")) as fin:
         self.tag2class = json.load(fin)
         self.class2tag = {v: k for k, v in self.tag2class.items()}
         logging.debug(f"class2tag: {self.class2tag}")
 def __init__(self, data_root='data', split_file='', size=(256, 256), fold=0, resize=False):
     self.data_root = data_root
     pkl_data = pickle.load(open(split_file, 'rb'))
     self.path_list = pkl_data[fold]['val']
     self.len = len(self.path_list)
     if resize:
         self.transforms = Resize(height=size[0], width=size[1], interpolation=cv2.INTER_NEAREST)
     else:
         self.transforms = Compose([LongestMaxSize(max_size=max(size)),
                                    PadIfNeeded(min_height=size[0], min_width=size[1], value=0,
                                                border_mode=cv2.BORDER_CONSTANT)
                                    ])
Exemplo n.º 15
0
def train_transform(image_size=224):
    transforms = [
        LongestMaxSize(max_size=image_size),
        PadIfNeeded(image_size, image_size, border_mode=cv2.BORDER_CONSTANT),
        ShiftScaleRotate(shift_limit=0.1,
                         scale_limit=0.1,
                         rotate_limit=45,
                         border_mode=cv2.BORDER_REFLECT,
                         p=0.5),
        RandomRotate90(),
        JpegCompression(quality_lower=50),
        post_transform()
    ]
    transforms = Compose(transforms)
    return transforms
Exemplo n.º 16
0
 def load(self, path="/model"):
     image_size = 512
     self.transform = Compose([
         LongestMaxSize(max_size=image_size),
         PadIfNeeded(image_size,
                     image_size,
                     border_mode=cv2.BORDER_CONSTANT),
         Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225),
                   always_apply=True),
         ToTensor(),
     ])
     self.model = torch.jit.load(os.path.join(path, "model.pth"))
     with open(os.path.join(path, "tag2class.json")) as fin:
         self.tag2class = json.load(fin)
         self.class2tag = {v: k for k, v in self.tag2class.items()}
         logging.debug(f"class2tag: {self.class2tag}")
Exemplo n.º 17
0
def train_transform(
    from_dicom: bool,
    longest_max_size: int,
    additional_transforms: List[BasicTransform] = None
) -> Compose:
    additional_transforms = additional_transforms if additional_transforms is not None else []
    initial_transforms = [
        LongestMaxSize(longest_max_size),
    ]
    if from_dicom:
        initial_transforms.insert(0, Lambda(image=stack_channels_for_rgb))
    final_transforms = [
        ToFloat(),
        ToTensorV2(),
    ]
    transforms = initial_transforms + additional_transforms + final_transforms
    return Compose(transforms, bbox_params=BboxParams(format='pascal_voc', label_fields=['labels']))
Exemplo n.º 18
0
 def __init__(self, width, height):
     self.aug = Compose([
         ShiftScaleRotate(p=0.5,
                          rotate_limit=5,
                          scale_limit=0.05,
                          border_mode=cv2.BORDER_CONSTANT),
         ImageCompression(quality_lower=95, quality_upper=100, p=1),
         IAAAffine(shear=0.2, always_apply=False, p=0.3),
         LongestMaxSize(max_size=width if width > height else height),
         PadIfNeeded(min_height=height,
                     min_width=width,
                     border_mode=cv2.BORDER_CONSTANT)
     ],
                        keypoint_params=KeypointParams(
                            format='xy',
                            label_fields=['pose_id', "join_id"],
                            remove_invisible=True))
Exemplo n.º 19
0
def prepare(img,
            boxes,
            max_dim=None,
            xflip=False,
            gt_boxes=None,
            gt_labels=None):
    aug = get_aug([
        LongestMaxSize(max_size=max_dim),
        HorizontalFlip(p=float(xflip)),
        ToTensor(normalize=dict(mean=[0.485, 0.456, 0.406],
                                std=[0.229, 0.224, 0.225])),
    ])
    augmented = aug(image=img,
                    bboxes=boxes,
                    gt_labels=np.full(len(boxes), fill_value=1))
    augmented_gt = aug(image=img, bboxes=gt_boxes, gt_labels=gt_labels)

    img = augmented["image"].numpy().astype(np.float32)
    boxes = np.asarray(augmented["bboxes"]).astype(np.float32)
    gt_boxes = np.asarray(augmented_gt["bboxes"]).astype(np.float32)

    return img, boxes, gt_boxes
Exemplo n.º 20
0
    def __init__(self, to_tensor=True):
        augs = [
            LongestMaxSize(640),
            PadIfNeeded(640, 640, cv.BORDER_CONSTANT, value=0),
            #RandomSizedBBoxSafeCrop(height = 300,width = 300),
            RandomBrightness(p=0.5),
            RandomContrast(p=0.5),

            #RandomSunFlare(p=0.5, flare_roi=(0, 0, 1, 0.5), angle_lower=0.5,src_radius= 150),
            RandomShadow(p=0.5,
                         num_shadows_lower=1,
                         num_shadows_upper=1,
                         shadow_dimension=5,
                         shadow_roi=(0, 0.5, 1, 1)),
            HorizontalFlip(p=0.5),
            GaussianBlur(p=0.5),
        ]
        self.transform = Compose(augs,
                                 bbox_params={
                                     "format": "albumentations",
                                     "min_area": 0,
                                     "min_visibility": 0.2,
                                     'label_fields': ['category_id']
                                 })
    def __init__(self,
                 root_dirpath,
                 images_dirpath,
                 annotations_path,
                 L_classes_idx_filter,
                 support_set_size=10,
                 image_id_key="image_id",
                 category_id_key="category_id",
                 F_image_id_to_relative_path=None,
                 augmentation=True,
                 desired_size=280,
                 batch_size=12,
                 num_batch=1):

        self.root_dirpath = root_dirpath
        self.images_dirpath = images_dirpath
        self.annotations_path = annotations_path
        self.image_id_key = image_id_key
        self.category_id_key = category_id_key
        self.L_classes_idx_filter = L_classes_idx_filter
        self.support_set_size = support_set_size
        self.F_image_id_to_relative_path = F_image_id_to_relative_path
        self.augmentation = augmentation
        self.desired_size = desired_size
        self.batch_size = batch_size
        self.num_batch = num_batch

        with open(self.annotations_path) as f:
            self.D_instances = json.load(f)
            self.L_annotations = self.D_instances['annotations']
            self.L_categories = self.D_instances['categories']

        # list of real class indices in COCO dataset
        self.L_real_class_idx = [x['id'] for x in self.L_categories]
        # zero-based class indices of COCO dataset (0-79)
        self.L_class_idx = range(len(self.L_real_class_idx))
        # dict to map real class indices to zero-based indices
        self.D_real_class_idx_to_class_idx = {
            real_class_idx: class_idx
            for class_idx, real_class_idx in enumerate(self.L_real_class_idx)
        }

        # dict to map zero based indices to the relative annotations
        self.D_class_to_annotations = self.Create_class_to_annotations_dict()

        # filter zero-based class indices which have no annotations
        self.L_not_empty_class_idx = [
            class_idx for class_idx in self.L_class_idx
            if len(self.D_class_to_annotations[class_idx]) > 0
        ]
        # filter only specified class indices
        self.L_filtered_class_idx = [
            class_idx for class_idx in self.L_not_empty_class_idx
            if class_idx in self.L_classes_idx_filter
        ]

        bbox_params = BboxParams(format='coco',
                                 label_fields=[self.category_id_key])
        augmentation_transform = Compose([
            VerticalFlip(p=0.5),
            HorizontalFlip(p=0.5),
            LongestMaxSize(max_size=600),
            PadIfNeeded(min_height=710,
                        min_width=710,
                        border_mode=cv2.BORDER_CONSTANT),
            Rotate(p=1.0,
                   limit=12,
                   interpolation=cv2.INTER_LINEAR,
                   border_mode=cv2.BORDER_CONSTANT),
            Resize(height=desired_size, width=desired_size),
            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            ToTensor()
        ],
                                         bbox_params=bbox_params)

        loading_transform = Compose([
            LongestMaxSize(max_size=desired_size),
            PadIfNeeded(min_height=desired_size,
                        min_width=desired_size,
                        border_mode=cv2.BORDER_CONSTANT),
            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            ToTensor()
        ],
                                    bbox_params=bbox_params)

        self.transform = augmentation_transform if self.augmentation else loading_transform
Exemplo n.º 22
0
def aug_resize(prob=1.0, img_size=224, interpolation=0):
	return Compose([
		LongestMaxSize(max_size=img_size, interpolation=interpolation)
	], p=prob)
def transform(image, mask, image_name, mask_name):

    x, y = image, mask

    rand = random.uniform(0, 1)
    if (rand > 0.5):

        images_name = [f"{image_name}"]
        masks_name = [f"{mask_name}"]
        images_aug = [x]
        masks_aug = [y]

        it = iter(images_name)
        it2 = iter(images_aug)
        imagedict = dict(zip(it, it2))

        it = iter(masks_name)
        it2 = iter(masks_aug)
        masksdict = dict(zip(it, it2))

        return imagedict, masksdict

    mask_density = np.count_nonzero(y)

    ## Augmenting only images with Gloms
    if (mask_density > 0):
        try:
            h, w, c = x.shape
        except Exception as e:
            image = image[:-1]
            x, y = image, mask
            h, w, c = x.shape

        aug = Blur(p=1, blur_limit=3)
        augmented = aug(image=x, mask=y)
        x0 = augmented['image']
        y0 = augmented['mask']

        #    aug = CenterCrop(p=1, height=32, width=32)
        #    augmented = aug(image=x, mask=y)
        #    x1 = augmented['image']
        #    y1 = augmented['mask']

        ## Horizontal Flip
        aug = HorizontalFlip(p=1)
        augmented = aug(image=x, mask=y)
        x2 = augmented['image']
        y2 = augmented['mask']

        aug = VerticalFlip(p=1)
        augmented = aug(image=x, mask=y)
        x3 = augmented['image']
        y3 = augmented['mask']

        #      aug = Normalize(p=1)
        #      augmented = aug(image=x, mask=y)
        #      x4 = augmented['image']
        #      y4 = augmented['mask']

        aug = Transpose(p=1)
        augmented = aug(image=x, mask=y)
        x5 = augmented['image']
        y5 = augmented['mask']

        aug = RandomGamma(p=1)
        augmented = aug(image=x, mask=y)
        x6 = augmented['image']
        y6 = augmented['mask']

        ## Optical Distortion
        aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5)
        augmented = aug(image=x, mask=y)
        x7 = augmented['image']
        y7 = augmented['mask']

        ## Grid Distortion
        aug = GridDistortion(p=1)
        augmented = aug(image=x, mask=y)
        x8 = augmented['image']
        y8 = augmented['mask']

        aug = RandomGridShuffle(p=1)
        augmented = aug(image=x, mask=y)
        x9 = augmented['image']
        y9 = augmented['mask']

        aug = HueSaturationValue(p=1)
        augmented = aug(image=x, mask=y)
        x10 = augmented['image']
        y10 = augmented['mask']

        #        aug = PadIfNeeded(p=1)
        #        augmented = aug(image=x, mask=y)
        #        x11 = augmented['image']
        #        y11 = augmented['mask']

        aug = RGBShift(p=1)
        augmented = aug(image=x, mask=y)
        x12 = augmented['image']
        y12 = augmented['mask']

        ## Random Brightness
        aug = RandomBrightness(p=1)
        augmented = aug(image=x, mask=y)
        x13 = augmented['image']
        y13 = augmented['mask']

        ## Random  Contrast
        aug = RandomContrast(p=1)
        augmented = aug(image=x, mask=y)
        x14 = augmented['image']
        y14 = augmented['mask']

        #aug = MotionBlur(p=1)
        #augmented = aug(image=x, mask=y)
        #   x15 = augmented['image']
        #  y15 = augmented['mask']

        aug = MedianBlur(p=1, blur_limit=5)
        augmented = aug(image=x, mask=y)
        x16 = augmented['image']
        y16 = augmented['mask']

        aug = GaussianBlur(p=1, blur_limit=3)
        augmented = aug(image=x, mask=y)
        x17 = augmented['image']
        y17 = augmented['mask']

        aug = GaussNoise(p=1)
        augmented = aug(image=x, mask=y)
        x18 = augmented['image']
        y18 = augmented['mask']

        aug = GlassBlur(p=1)
        augmented = aug(image=x, mask=y)
        x19 = augmented['image']
        y19 = augmented['mask']

        aug = CLAHE(clip_limit=1.0,
                    tile_grid_size=(8, 8),
                    always_apply=False,
                    p=1)
        augmented = aug(image=x, mask=y)
        x20 = augmented['image']
        y20 = augmented['mask']

        aug = ChannelShuffle(p=1)
        augmented = aug(image=x, mask=y)
        x21 = augmented['image']
        y21 = augmented['mask']

        aug = ToGray(p=1)
        augmented = aug(image=x, mask=y)
        x22 = augmented['image']
        y22 = augmented['mask']

        aug = ToSepia(p=1)
        augmented = aug(image=x, mask=y)
        x23 = augmented['image']
        y23 = augmented['mask']

        aug = JpegCompression(p=1)
        augmented = aug(image=x, mask=y)
        x24 = augmented['image']
        y24 = augmented['mask']

        aug = ImageCompression(p=1)
        augmented = aug(image=x, mask=y)
        x25 = augmented['image']
        y25 = augmented['mask']

        aug = Cutout(p=1)
        augmented = aug(image=x, mask=y)
        x26 = augmented['image']
        y26 = augmented['mask']

        #       aug = CoarseDropout(p=1, max_holes=8, max_height=32, max_width=32)
        #       augmented = aug(image=x, mask=y)
        #       x27 = augmented['image']
        #       y27 = augmented['mask']

        #       aug = ToFloat(p=1)
        #       augmented = aug(image=x, mask=y)
        #       x28 = augmented['image']
        #       y28 = augmented['mask']

        aug = FromFloat(p=1)
        augmented = aug(image=x, mask=y)
        x29 = augmented['image']
        y29 = augmented['mask']

        ## Random Brightness and Contrast
        aug = RandomBrightnessContrast(p=1)
        augmented = aug(image=x, mask=y)
        x30 = augmented['image']
        y30 = augmented['mask']

        aug = RandomSnow(p=1)
        augmented = aug(image=x, mask=y)
        x31 = augmented['image']
        y31 = augmented['mask']

        aug = RandomRain(p=1)
        augmented = aug(image=x, mask=y)
        x32 = augmented['image']
        y32 = augmented['mask']

        aug = RandomFog(p=1)
        augmented = aug(image=x, mask=y)
        x33 = augmented['image']
        y33 = augmented['mask']

        aug = RandomSunFlare(p=1)
        augmented = aug(image=x, mask=y)
        x34 = augmented['image']
        y34 = augmented['mask']

        aug = RandomShadow(p=1)
        augmented = aug(image=x, mask=y)
        x35 = augmented['image']
        y35 = augmented['mask']

        aug = Lambda(p=1)
        augmented = aug(image=x, mask=y)
        x36 = augmented['image']
        y36 = augmented['mask']

        aug = ChannelDropout(p=1)
        augmented = aug(image=x, mask=y)
        x37 = augmented['image']
        y37 = augmented['mask']

        aug = ISONoise(p=1)
        augmented = aug(image=x, mask=y)
        x38 = augmented['image']
        y38 = augmented['mask']

        aug = Solarize(p=1)
        augmented = aug(image=x, mask=y)
        x39 = augmented['image']
        y39 = augmented['mask']

        aug = Equalize(p=1)
        augmented = aug(image=x, mask=y)
        x40 = augmented['image']
        y40 = augmented['mask']

        aug = Posterize(p=1)
        augmented = aug(image=x, mask=y)
        x41 = augmented['image']
        y41 = augmented['mask']

        aug = Downscale(p=1)
        augmented = aug(image=x, mask=y)
        x42 = augmented['image']
        y42 = augmented['mask']

        aug = MultiplicativeNoise(p=1)
        augmented = aug(image=x, mask=y)
        x43 = augmented['image']
        y43 = augmented['mask']

        aug = FancyPCA(p=1)
        augmented = aug(image=x, mask=y)
        x44 = augmented['image']
        y44 = augmented['mask']

        #       aug = MaskDropout(p=1)
        #       augmented = aug(image=x, mask=y)
        #       x45 = augmented['image']
        #       y45 = augmented['mask']

        aug = GridDropout(p=1)
        augmented = aug(image=x, mask=y)
        x46 = augmented['image']
        y46 = augmented['mask']

        aug = ColorJitter(p=1)
        augmented = aug(image=x, mask=y)
        x47 = augmented['image']
        y47 = augmented['mask']

        ## ElasticTransform
        aug = ElasticTransform(p=1,
                               alpha=120,
                               sigma=512 * 0.05,
                               alpha_affine=512 * 0.03)
        augmented = aug(image=x, mask=y)
        x50 = augmented['image']
        y50 = augmented['mask']

        aug = CropNonEmptyMaskIfExists(p=1, height=22, width=32)
        augmented = aug(image=x, mask=y)
        x51 = augmented['image']
        y51 = augmented['mask']

        aug = IAAAffine(p=1)
        augmented = aug(image=x, mask=y)
        x52 = augmented['image']
        y52 = augmented['mask']

        #        aug = IAACropAndPad(p=1)
        #        augmented = aug(image=x, mask=y)
        #        x53 = augmented['image']
        #        y53 = augmented['mask']

        aug = IAAFliplr(p=1)
        augmented = aug(image=x, mask=y)
        x54 = augmented['image']
        y54 = augmented['mask']

        aug = IAAFlipud(p=1)
        augmented = aug(image=x, mask=y)
        x55 = augmented['image']
        y55 = augmented['mask']

        aug = IAAPerspective(p=1)
        augmented = aug(image=x, mask=y)
        x56 = augmented['image']
        y56 = augmented['mask']

        aug = IAAPiecewiseAffine(p=1)
        augmented = aug(image=x, mask=y)
        x57 = augmented['image']
        y57 = augmented['mask']

        aug = LongestMaxSize(p=1)
        augmented = aug(image=x, mask=y)
        x58 = augmented['image']
        y58 = augmented['mask']

        aug = NoOp(p=1)
        augmented = aug(image=x, mask=y)
        x59 = augmented['image']
        y59 = augmented['mask']

        #       aug = RandomCrop(p=1, height=22, width=22)
        #       augmented = aug(image=x, mask=y)
        #       x61 = augmented['image']
        #       y61 = augmented['mask']

        #      aug = RandomResizedCrop(p=1, height=22, width=20)
        #      augmented = aug(image=x, mask=y)
        #      x63 = augmented['image']
        #      y63 = augmented['mask']

        aug = RandomScale(p=1)
        augmented = aug(image=x, mask=y)
        x64 = augmented['image']
        y64 = augmented['mask']

        #      aug = RandomSizedCrop(p=1, height=22, width=20, min_max_height = [32,32])
        #      augmented = aug(image=x, mask=y)
        #      x66 = augmented['image']
        #      y66 = augmented['mask']

        #      aug = Resize(p=1, height=22, width=20)
        #      augmented = aug(image=x, mask=y)
        #      x67 = augmented['image']
        #      y67 = augmented['mask']

        aug = Rotate(p=1)
        augmented = aug(image=x, mask=y)
        x68 = augmented['image']
        y68 = augmented['mask']

        aug = ShiftScaleRotate(p=1)
        augmented = aug(image=x, mask=y)
        x69 = augmented['image']
        y69 = augmented['mask']

        aug = SmallestMaxSize(p=1)
        augmented = aug(image=x, mask=y)
        x70 = augmented['image']
        y70 = augmented['mask']

        images_aug.extend([
            x, x0, x2, x3, x5, x6, x7, x8, x9, x10, x12, x13, x14, x16, x17,
            x18, x19, x20, x21, x22, x23, x24, x25, x26, x29, x30, x31, x32,
            x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44, x46,
            x47, x50, x51, x52, x54, x55, x56, x57, x58, x59, x64, x68, x69,
            x70
        ])

        masks_aug.extend([
            y, y0, y2, y3, y5, y6, y7, y8, y9, y10, y12, y13, y14, y16, y17,
            y18, y19, y20, y21, y22, y23, y24, y25, y26, y29, y30, y31, y32,
            y33, y34, y35, y36, y37, y38, y39, y40, y41, y42, y43, y44, y46,
            y47, y50, y51, y52, y54, y55, y56, y57, y58, y59, y64, y68, y69,
            y70
        ])

        idx = -1
        images_name = []
        masks_name = []
        for i, m in zip(images_aug, masks_aug):
            if idx == -1:
                tmp_image_name = f"{image_name}"
                tmp_mask_name = f"{mask_name}"
            else:
                tmp_image_name = f"{image_name}_{smalllist[idx]}"
                tmp_mask_name = f"{mask_name}_{smalllist[idx]}"
            images_name.extend(tmp_image_name)
            masks_name.extend(tmp_mask_name)
            idx += 1

        it = iter(images_name)
        it2 = iter(images_aug)
        imagedict = dict(zip(it, it2))

        it = iter(masks_name)
        it2 = iter(masks_aug)
        masksdict = dict(zip(it, it2))

    return imagedict, masksdict
Exemplo n.º 24
0
def albumentations_transforms(
    crop_size,
    shorter_side,
    low_scale,
    high_scale,
    img_mean,
    img_std,
    img_scale,
    ignore_label,
    num_stages,
    dataset_type,
):
    from albumentations import (
        Normalize,
        HorizontalFlip,
        RandomRotate90,  # my addition
        RandomBrightnessContrast,  # my addition
        CLAHE,  # my addition
        RandomGamma,  # my addition
        ElasticTransform,  # my addition
        GridDistortion,  # my addition
        MotionBlur,  # my addition
        RandomCrop,
        PadIfNeeded,
        RandomScale,
        LongestMaxSize,
        SmallestMaxSize,
        OneOf,
    )
    from albumentations.pytorch import ToTensorV2 as ToTensor
    from densetorch.data import albumentations2densetorch

    if dataset_type == "densetorch":
        wrapper = albumentations2densetorch
    elif dataset_type == "torchvision":
        wrapper = albumentations2torchvision
    else:
        raise ValueError(f"Unknown dataset type: {dataset_type}")

    common_transformations = [
        Normalize(max_pixel_value=1.0 / img_scale, mean=img_mean, std=img_std),
        ToTensor(),
    ]
    train_transforms = []
    for stage in range(num_stages):
        train_transforms.append(
            wrapper([
                ChangeBackground("../backgrounds", p=0.5),  # my addition
                MotionBlur(p=0.5),
                OneOf([
                    RandomScale(scale_limit=(low_scale[stage],
                                             high_scale[stage])),
                    LongestMaxSize(max_size=shorter_side[stage]),
                    SmallestMaxSize(max_size=shorter_side[stage]),
                ]),
                PadIfNeeded(
                    min_height=crop_size[stage],
                    min_width=crop_size[stage],
                    border_mode=cv2.BORDER_CONSTANT,
                    value=np.array(img_mean) / img_scale,
                    mask_value=ignore_label,
                ),
                HorizontalFlip(p=0.5, ),
                RandomRotate90(p=0.5),
                RandomBrightnessContrast(
                    p=.8),  # only applies to images, not masks
                RandomGamma(p=0.8),  # only applies to images
                OneOf(
                    [
                        ElasticTransform(p=0.5,
                                         alpha=120,
                                         sigma=500 * 0.05,
                                         alpha_affine=500 * 0.03),
                        GridDistortion(p=0.5),
                        #         A.OpticalDistortion(distort_limit=1, shift_limit=0.5, p=1),
                    ],
                    p=.5),
                RandomCrop(
                    height=crop_size[stage],
                    width=crop_size[stage],
                ),
            ] + common_transformations))
    val_transforms = wrapper(common_transformations)
    return train_transforms, val_transforms
Exemplo n.º 25
0
    ## Rotate

    elif augmentation == 'rotate':
        transform = Rotate(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'random_rotate90':
        transform = RandomRotate90(always_apply=True)
        transformed_image = transform(image=image)['image']
   
    elif augmentation == 'transpose':
        transform = Transpose(always_apply=True)
        transformed_image = transform(image=image)['image']

    ## Size

    elif augmentation == 'resize':
        transform = Resize(always_apply=True, height=100, width=100)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'longest_max_size':
        transform = LongestMaxSize(always_apply=True)
        transformed_image = transform(image=image)['image']
          
    elif augmentation == 'smallest_max_size':
        transform = SmallestMaxSize(always_apply=True)
        transformed_image = transform(image=image)['image']
   
    name, ext = image_name.split('.')
    new_path = name + '_' + augmentation + '.' + ext
    cv2.imwrite(new_path, transformed_image)
Exemplo n.º 26
0
def albumentations_transforms(
    crop_size,
    shorter_side,
    low_scale,
    high_scale,
    img_mean,
    img_std,
    img_scale,
    ignore_label,
    num_stages,
    dataset_type,
):
    from albumentations import (
        Normalize,
        HorizontalFlip,
        RandomCrop,
        PadIfNeeded,
        RandomScale,
        LongestMaxSize,
        SmallestMaxSize,
        OneOf,
    )
    from albumentations.pytorch import ToTensorV2 as ToTensor
    from densetorch.data import albumentations2densetorch

    if dataset_type == "densetorch":
        wrapper = albumentations2densetorch
    elif dataset_type == "torchvision":
        wrapper = albumentations2torchvision
    else:
        raise ValueError(f"Unknown dataset type: {dataset_type}")

    common_transformations = [
        Normalize(max_pixel_value=1.0 / img_scale, mean=img_mean, std=img_std),
        ToTensor(),
    ]
    train_transforms = []
    for stage in range(num_stages):
        train_transforms.append(
            wrapper(
                [
                    OneOf(
                        [
                            RandomScale(
                                scale_limit=(low_scale[stage], high_scale[stage])
                            ),
                            LongestMaxSize(max_size=shorter_side[stage]),
                            SmallestMaxSize(max_size=shorter_side[stage]),
                        ]
                    ),
                    PadIfNeeded(
                        min_height=crop_size[stage],
                        min_width=crop_size[stage],
                        border_mode=cv2.BORDER_CONSTANT,
                        value=np.array(img_mean) / img_scale,
                        mask_value=ignore_label,
                    ),
                    HorizontalFlip(p=0.5,),
                    RandomCrop(height=crop_size[stage], width=crop_size[stage],),
                ]
                + common_transformations
            )
        )
    val_transforms = wrapper(common_transformations)
    return train_transforms, val_transforms
Exemplo n.º 27
0
 local_inner = io.imread(os.path.join(
     data_root, dirname, 'pupil_edge_mask',
     os.path.splitext(img_name)[0] + '.png'),
                         as_gray=True)
 local_outer = io.imread(os.path.join(
     data_root, dirname, 'iris_edge_mask',
     os.path.splitext(img_name)[0] + '.png'),
                         as_gray=True)
 _, local_inner = cv2.threshold(local_inner, 127, 1, 0)
 _, local_outer = cv2.threshold(local_outer, 127, 1, 0)
 original_shape = img.shape
 if resize:
     img = cv2.resize(img, (input_size[1], input_size[0]),
                      interpolation=cv2.INTER_NEAREST)
 else:
     transforms = LongestMaxSize(max_size=max(input_size))
     img = transforms(image=img)['image']
     transformed_shape = img.shape
     img, pads = padding_2D(img)
 iris_prediction = np.zeros(img.shape)
 tta = 1
 if args.TTA:
     tta = 4
 for t in range(tta):
     if t == 0:
         img_tta = img
     elif t == 1:
         img_tta = np.flip(img, axis=1)
     elif t == 2:
         img_tta = exposure.adjust_gamma(img, 1.2)
     elif t == 3:
Exemplo n.º 28
0
    def __call__(self, data):
        data = to_numpy(data)
        img, label = data['image'], data['label']

        is_3d = True if img.shape == 4 else False

        max_size = max(self._output_size[0], self._output_size[1])

        if self._type == 'train':
            task = [
                HorizontalFlip(p=0.5),
                RandomBrightnessContrast(p=0.5),
                RandomGamma(p=0.5),
                GridDistortion(border_mode=cv2.BORDER_CONSTANT, p=0.5),
                LongestMaxSize(max_size, p=1),
                PadIfNeeded(self._output_size[0],
                            self._output_size[1],
                            cv2.BORDER_CONSTANT,
                            value=0,
                            p=1),
                ShiftScaleRotate(shift_limit=0.2,
                                 scale_limit=0.5,
                                 rotate_limit=30,
                                 border_mode=cv2.BORDER_CONSTANT,
                                 value=0,
                                 p=0.5)
            ]
        else:
            task = [
                LongestMaxSize(max_size, p=1),
                PadIfNeeded(self._output_size[0],
                            self._output_size[1],
                            cv2.BORDER_CONSTANT,
                            value=0,
                            p=1)
            ]

        if self.use_roi:
            assert 'roi' in data.keys() and len(data['roi']) is not 0
            roi = data['roi']
            min_y = 0
            max_y = img.shape[0]
            min_x = 0
            max_x = img.shape[1]
            min_x = max(min_x, roi['min_x'] - self._roi_error_range)
            max_x = min(max_x, roi['max_x'] + self._roi_error_range)
            min_y = max(min_y, roi['min_y'] - self._roi_error_range)
            max_y = min(max_y, roi['max_y'] + self._roi_error_range)

            crop = [Crop(min_x, min_y, max_x, max_y, p=1)]
            task = crop + task

        aug = Compose_albu(task)
        if not is_3d:
            aug_data = aug(image=img, mask=label)
            data['image'], data['label'] = aug_data['image'], aug_data['mask']

        else:
            keys = {}
            targets = {}
            for i in range(1, img.shape[2]):
                keys.update({f'image{i}': 'image'})
                keys.update({f'mask{i}': 'mask'})
                targets.update({f'image{i}': img[:, :, i]})
                targets.update({f'mask{i}': label[:, :, i]})
            aug.add_targets(keys)

            targets.update({'image': img[:, :, 0]})
            targets.update({'mask': label[:, :, 0]})

            aug_data = aug(**targets)
            imgs = [aug_data['image']]
            labels = [aug_data['mask']]

            for i in range(1, img.shape[2]):
                imgs.append(aug_data[f'image{i}'])
                labels.append(aug_data[f'mask{i}'])

            img = np.stack(imgs, axis=-1)
            label = np.stack(labels, axis=-1)
            data['image'] = img
            data['label'] = label

        return data
Exemplo n.º 29
0
def resize_longest(x):
    return Compose([
        LongestMaxSize(
            x, always_apply=True, interpolation=cv2.INTER_CUBIC, p=1)
    ],
                   p=1)
Exemplo n.º 30
0
import pandas as pd
import numpy as np
import os.path as osp
import cv2

from tqdm import tqdm

from albumentations import Compose, LongestMaxSize

IMAGE_DIR = '../../../data/cumc/images/cropped/'
coords = pd.read_csv('../../../data/cumc/train_with_splits.csv')

resize = Compose([LongestMaxSize(max_size=256)], p=1)

# Load in 1-by-1
pixel_mean = np.zeros(1)
pixel_std = np.zeros(1)

k = 1

for rownum in tqdm(range(len(coords)), total=len(coords)):
    img = cv2.imread(osp.join(IMAGE_DIR, coords['imgfile'].iloc[rownum]), 0)
    #x1, y1, x2, y2 = coords[['x1','y1','x2','y2']].iloc[rownum]
    #img = img[y1:y2,x1:x2]
    img = resize(image=img)['image']
    img = img / 255.
    pixels = img.reshape((-1, 1))  # img.shape[2]
    for pixel in pixels:
        diff = pixel - pixel_mean
        pixel_mean = pixel_mean + diff / k
        pixel_std = pixel_std + diff * (pixel - pixel_mean)