示例#1
0
 def transform(self, image, mask):
     H, W = image.shape[0], image.shape[1]
     new_h, new_w = self.input_shape[0], self.input_shape[1]
     if H > self.input_shape[0] and W > self.input_shape[1]:
         y_min, x_min = random.randint(0, H -
                                       self.input_shape[0]), random.randint(
                                           0, W - self.input_shape[1])
         compose = Compose([
             transforms.Crop(x_min=x_min,
                             y_min=y_min,
                             x_max=x_min + self.input_shape[1],
                             y_max=y_min + self.input_shape[0]),
             ToTensorV2()
         ])
         new_image = compose(image=image)['image']
         new_mask = compose(image=mask)['image']
     else:
         resize_ratio = int(
             math.ceil(max([new_h / float(H), new_w / float(W)])))
         h, w = H * resize_ratio, W * resize_ratio
         y_min, x_min = random.randint(0, h -
                                       self.input_shape[0]), random.randint(
                                           0, w - self.input_shape[1])
         compose = Compose([
             transforms.Resize(h, w),
             transforms.Crop(x_min=x_min,
                             y_min=y_min,
                             x_max=x_min + self.input_shape[1],
                             y_max=y_min + self.input_shape[0]),
             ToTensorV2()
         ])
         new_image = compose(image=image)['image']
         new_mask = compose(image=mask)['image']
     assert new_image.shape[0] == 3 and new_mask.shape[0] == 2
     return new_image, new_mask
示例#2
0
    def get_data_transforms(self) -> Tuple[BasicTransform, BasicTransform]:
        """Get albumentations transform objects for data augmentation.

        Returns:
           1st tuple arg: a transform that doesn't do any data augmentation
           2nd tuple arg: a transform with data augmentation
        """
        cfg = self.cfg
        bbox_params = self.get_bbox_params()
        transform = Compose([Resize(cfg.data.img_sz, cfg.data.img_sz)],
                            bbox_params=bbox_params)

        augmentors_dict = {
            'Blur': Blur(),
            'RandomRotate90': RandomRotate90(),
            'HorizontalFlip': HorizontalFlip(),
            'VerticalFlip': VerticalFlip(),
            'GaussianBlur': GaussianBlur(),
            'GaussNoise': GaussNoise(),
            'RGBShift': RGBShift(),
            'ToGray': ToGray()
        }
        aug_transforms = []
        for augmentor in cfg.data.augmentors:
            try:
                aug_transforms.append(augmentors_dict[augmentor])
            except KeyError as e:
                log.warning(
                    '{0} is an unknown augmentor. Continuing without {0}. \
                    Known augmentors are: {1}'.format(
                        e, list(augmentors_dict.keys())))
        aug_transforms.append(Resize(cfg.data.img_sz, cfg.data.img_sz))
        aug_transform = Compose(aug_transforms, bbox_params=bbox_params)

        return transform, aug_transform
示例#3
0
    def get_data_transforms(self):
        cfg = self.cfg
        bbox_params = self.get_bbox_params()
        transform = Compose([Resize(cfg.data.img_sz, cfg.data.img_sz)],
                            bbox_params=bbox_params)

        augmentors_dict = {
            'Blur': Blur(),
            'RandomRotate90': RandomRotate90(),
            'HorizontalFlip': HorizontalFlip(),
            'VerticalFlip': VerticalFlip(),
            'GaussianBlur': GaussianBlur(),
            'GaussNoise': GaussNoise(),
            'RGBShift': RGBShift(),
            'ToGray': ToGray()
        }
        aug_transforms = []
        for augmentor in cfg.data.augmentors:
            try:
                aug_transforms.append(augmentors_dict[augmentor])
            except KeyError as e:
                log.warning(
                    '{0} is an unknown augmentor. Continuing without {0}. \
                    Known augmentors are: {1}'.format(
                        e, list(augmentors_dict.keys())))
        aug_transforms.append(Resize(cfg.data.img_sz, cfg.data.img_sz))
        aug_transform = Compose(aug_transforms, bbox_params=bbox_params)

        return transform, aug_transform
 def aug(self, image):
     '''
     center crop, resize, affine, hue, saturation,
     '''
     if self.phase == 'train':
         imgaug = Compose([
             transforms.Resize(height=self.input_shape[0], width=self.input_shape[1]),
             RandomRotate90(),
             Flip(),
             Transpose(),
             OneOf([
                 IAAAdditiveGaussianNoise(),
                 GaussNoise(),
             ], p=0.2),
             OneOf([
                 MotionBlur(p=.2),
                 MedianBlur(blur_limit=3, p=.1),
                 Blur(blur_limit=3, p=.1),
             ], p=0.2),
             ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=.2),
             ToTensorV2(),
         ], p=1)
     else:
         imgaug = Compose([transforms.Resize(height=self.input_shape[0], width=self.input_shape[1]),
                           ToTensorV2()])
     image = imgaug(image=image)['image']
     return image
示例#5
0
def get_training_dataset(cfg: DictConfig = None) -> Dict[str, Dataset]:
    """
    Get training and validation datasets.

    Parameters
    ----------
    cfg : DictConfig, optional
        Project configuration, by default None

    Returns
    -------
    Dict[str, Dataset]
        {"train": train_dataset, "valid": valid_dataset}
    """
    images_dir = to_absolute_path(cfg.data.images_folder_path)

    data = pd.read_csv(to_absolute_path(cfg.data.dataset_path))
    data["x1"] = data["x"] + data["w"]
    data["y1"] = data["y"] + data["h"]
    data["area"] = data["w"] * data["h"]

    train_ids, valid_ids = train_test_split(
        data["image_id"].unique(),
        test_size=cfg.data.validation_split,
        random_state=cfg.training.seed,
    )

    # for fast training
    if cfg.training.debug:
        train_ids = train_ids[:10]
        valid_ids = valid_ids[:10]

    train_df = data.loc[data["image_id"].isin(train_ids)]
    valid_df = data.loc[data["image_id"].isin(valid_ids)]

    train_augs_list = [
        load_obj(i["class_name"])(**i["params"])
        for i in cfg["augmentation"]["train"]["augs"]
    ]
    train_bbox_params = OmegaConf.to_container(
        (cfg["augmentation"]["train"]["bbox_params"])
    )
    train_augs = Compose(train_augs_list, bbox_params=train_bbox_params)

    valid_augs_list = [
        load_obj(i["class_name"])(**i["params"])
        for i in cfg["augmentation"]["valid"]["augs"]
    ]
    valid_bbox_params = OmegaConf.to_container(
        (cfg["augmentation"]["valid"]["bbox_params"])
    )
    valid_augs = Compose(valid_augs_list, bbox_params=valid_bbox_params)

    train_dataset = XrayDataset(train_df, "train", images_dir, cfg, train_augs)
    valid_dataset = XrayDataset(valid_df, "valid", images_dir, cfg, valid_augs)

    return {"train": train_dataset, "valid": valid_dataset}
示例#6
0
def test_compose_with_bbox_noop(bboxes, bbox_format, labels):
    image = np.ones((100, 100, 3))
    if labels is not None:
        aug = Compose([NoOp(p=1.)], bbox_params={'format': bbox_format, 'label_fields': ['labels']})
        transformed = aug(image=image, bboxes=bboxes, labels=labels)
    else:
        aug = Compose([NoOp(p=1.)], bbox_params={'format': bbox_format})
        transformed = aug(image=image, bboxes=bboxes)
    assert np.array_equal(transformed['image'], image)
    assert transformed['bboxes'] == bboxes
示例#7
0
def test_compose_with_keypoint_noop(keypoints, keypoint_format, labels):
    image = np.ones((100, 100, 3))
    if labels is not None:
        aug = Compose([NoOp(p=1.)], keypoint_params={'format': keypoint_format, 'label_fields': ['labels']})
        transformed = aug(image=image, keypoints=keypoints, labels=labels)
    else:
        aug = Compose([NoOp(p=1.)], keypoint_params={'format': keypoint_format})
        transformed = aug(image=image, keypoints=keypoints)
    assert np.array_equal(transformed['image'], image)
    assert transformed['keypoints'] == keypoints
示例#8
0
def test_compose_with_keypoint_noop(keypoints, keypoint_format, labels):
    image = np.ones((100, 100, 3))
    if labels is not None:
        aug = Compose([NoOp(p=1.0)], keypoint_params={"format": keypoint_format, "label_fields": ["labels"]})
        transformed = aug(image=image, keypoints=keypoints, labels=labels)
    else:
        aug = Compose([NoOp(p=1.0)], keypoint_params={"format": keypoint_format})
        transformed = aug(image=image, keypoints=keypoints)
    assert np.array_equal(transformed["image"], image)
    assert transformed["keypoints"] == keypoints
示例#9
0
def test_compose_with_bbox_noop(bboxes, bbox_format, labels):
    image = np.ones((100, 100, 3))
    if labels is not None:
        aug = Compose([NoOp(p=1.0)], bbox_params={"format": bbox_format, "label_fields": ["labels"]})
        transformed = aug(image=image, bboxes=bboxes, labels=labels)
    else:
        aug = Compose([NoOp(p=1.0)], bbox_params={"format": bbox_format})
        transformed = aug(image=image, bboxes=bboxes)
    assert np.array_equal(transformed["image"], image)
    assert np.all(np.isclose(transformed["bboxes"], bboxes))
示例#10
0
def test_compose_with_bbox_noop_different_name(bboxes, bbox_format, labels):
    image = np.ones((100, 100, 3))
    if labels is not None:
        aug = Compose([NoOp(p=1.)], bbox_params={'format': bbox_format, 'label_fields': ['labels']},
                      additional_targets={'bboxes0': 'bboxes', 'image_left': 'image'})
        transformed = aug(image_left=image, bboxes0=bboxes, labels=labels)
    else:
        aug = Compose([NoOp(p=1.)], bbox_params={'format': bbox_format},
                      additional_targets={'bboxes0': 'bboxes', 'image_left': 'image'})
        transformed = aug(image_left=image, bboxes0=bboxes)
    assert np.array_equal(transformed['image_left'], image)
    assert transformed['bboxes0'] == bboxes
示例#11
0
def test_keypoint_transform_format_xyas(aug, keypoints, expected):
    transform = Compose([aug(p=1)], keypoint_params={'format': 'xyas',
                                                     'angle_in_degrees': True, 'label_fields': ['labels']})

    image = np.ones((100, 100, 3))
    transformed = transform(image=image, keypoints=keypoints, labels=np.ones(len(keypoints)))
    assert np.allclose(expected, transformed['keypoints'])
示例#12
0
def process_aug_dict(pipeline_dict, meta_augs_list=['oneof', 'oneorother']):
    """Create a Compose object from an augmentation config dict.

    Notes
    -----
    See the documentation for instructions on formatting the config .yaml to
    enable utilization by get_augs.

    Arguments
    ---------
    aug_dict : dict
        The ``'training_augmentation'`` or ``'validation_augmentation'``
        sub-dict from the ``config`` object.
    meta_augs_list : dict, optional
        The list of augmentation names that correspond to "meta-augmentations"
        in all lowercase (e.g. ``oneof``, ``oneorother``). This will be used to
        find augmentation dictionary items that need further parsing.

    Returns
    -------
    ``Compose`` instance
        The composed augmentation pipeline.
    """
    if pipeline_dict is None:
        return None
    p = pipeline_dict.get('p', 1.0)  # probability of applying augs in pipeline
    xforms = pipeline_dict['augmentations']
    composer_list = get_augs(xforms, meta_augs_list)
    return Compose(composer_list, p=p)
示例#13
0
文件: coco_utils.py 项目: dodler/kgl
def get_coco(root, image_set, transforms, mode='instances'):
    anno_file_template = "{}_{}2017.json"
    PATHS = {
        "train": ("train2017",
                  os.path.join("annotations",
                               anno_file_template.format(mode, "train"))),
        "val": ("val2017",
                os.path.join("annotations",
                             anno_file_template.format(mode, "val"))),
        # "train": ("val2017", os.path.join("annotations", anno_file_template.format(mode, "val")))
    }

    t = [ConvertCocoPolysToMask()]

    if transforms is not None:
        t.append(transforms)
    transforms = Compose(t)

    img_folder, ann_file = PATHS[image_set]
    img_folder = os.path.join(root, img_folder)
    ann_file = os.path.join(root, ann_file)

    dataset = CocoDetection(img_folder, ann_file, transforms=transforms)

    if image_set == "train":
        dataset = _coco_remove_images_without_annotations(dataset)

    # dataset = torch.utils.data.Subset(dataset, [i for i in range(500)])

    return dataset
    def __init__(self,
                 df,
                 df_controls,
                 stats_experiments,
                 img_dir,
                 mode,
                 verbose=True,
                 channels=[1, 2, 3, 4, 5, 6]):

        self.records = deepcopy(df).to_records(index=False)

        df_conts = deepcopy(df_controls)
        mask = (df_conts['well_type'] == 'negative_control') & \
               (df_conts['well'] == 'B02')
        df_neg_conts = df_conts[mask]
        self.records_neg_conts = df_neg_conts.to_records(index=False)
        mask = (df_conts['well_type'] == 'positive_control')
        df_pos_conts = df_conts[mask]
        self.records_pos_conts = df_pos_conts.to_records(index=False)

        self.stats_exps = stats_experiments
        self.mode = mode
        self.channels = channels
        self.img_dir = img_dir
        self.len = df.shape[0]
        self.transform_train = Compose([
            VerticalFlip(p=0.5),
            HorizontalFlip(p=0.5),
            ShiftScaleRotate(
                shift_limit=0, scale_limit=0, rotate_limit=180, p=1.0),
            RandomCrop(height=364, width=364, p=1.0)
        ],
                                       p=1.0)
        self.transform_val = Compose(
            [CenterCrop(height=364, width=364, p=1.0)], p=1.0)

        if verbose:
            print()
        self.imgs = self._load_imgs(self.records,
                                    desc='Images',
                                    verbose=verbose)
        self.imgs_neg_conts = self._load_imgs(self.records_neg_conts,
                                              desc='Negative controls',
                                              verbose=verbose)
        self.imgs_pos_conts = self._load_imgs(self.records_pos_conts,
                                              desc='Positive controls',
                                              verbose=verbose)
示例#15
0
def test_compose_to_tensor():
    first = MagicMock()
    second = MagicMock()
    to_tensor = MagicMock()
    augmentation = Compose([first, second], to_tensor=to_tensor, p=0)
    image = np.ones((8, 8))
    augmentation(image=image)
    assert to_tensor.called
示例#16
0
def test_keypoint_transform_format_xyas(aug, keypoints, expected):
    transform = Compose(
        [aug(p=1)], keypoint_params={"format": "xyas", "angle_in_degrees": True, "label_fields": ["labels"]}
    )

    image = np.ones((100, 100, 3))
    transformed = transform(image=image, keypoints=keypoints, labels=np.ones(len(keypoints)))
    assert np.allclose(expected, transformed["keypoints"])
示例#17
0
def test_compose_with_additional_targets():
    image = np.ones((100, 100, 3))
    keypoints = [(10, 10), (50, 50)]
    kp1 = [(15, 15), (55, 55)]
    aug = Compose([CenterCrop(50, 50)], keypoint_params={"format": "xy"}, additional_targets={"kp1": "keypoints"})
    transformed = aug(image=image, keypoints=keypoints, kp1=kp1)
    assert transformed["keypoints"] == [(25, 25)]
    assert transformed["kp1"] == [(30, 30)]
示例#18
0
def test_compose_with_keypoint_noop_label_outside(keypoints, keypoint_format, labels):
    image = np.ones((100, 100, 3))
    aug = Compose([NoOp(p=1.)], keypoint_params={'format': keypoint_format, 'label_fields': list(labels.keys())})
    transformed = aug(image=image, keypoints=keypoints, **labels)
    assert np.array_equal(transformed['image'], image)
    assert transformed['keypoints'] == keypoints
    for k, v in labels.items():
        assert transformed[k] == v
def test_always_apply():
    first = MagicMock(always_apply=True)
    second = MagicMock(always_apply=False)
    augmentation = Compose([first, second], p=0)
    image = np.ones((8, 8))
    augmentation(image=image)
    assert first.called
    assert not second.called
def oneof_always_apply_crash():
    aug = Compose(
        [HorizontalFlip(),
         Rotate(),
         OneOf([Blur(), MedianBlur()], p=1)], p=1)
    image = np.ones((8, 8))
    data = aug(image=image)
    assert data
示例#21
0
def test_compose_with_additional_targets():
    image = np.ones((100, 100, 3))
    keypoints = [[10, 10], [50, 50]]
    kp1 = [[15, 15], [55, 55]]
    aug = Compose([CenterCrop(50, 50)], keypoint_params={'format': 'xy'}, additional_targets={'kp1': 'keypoints'})
    transformed = aug(image=image, keypoints=keypoints, kp1=kp1)
    assert transformed['keypoints'] == [[25, 25]]
    assert transformed['kp1'] == [[30, 30]]
示例#22
0
def test_compose_with_bbox_noop_label_outside(bboxes, bbox_format, labels):
    image = np.ones((100, 100, 3))
    aug = Compose([NoOp(p=1.)], bbox_params={'format': bbox_format, 'label_fields': list(labels.keys())})
    transformed = aug(image=image, bboxes=bboxes, **labels)
    assert np.array_equal(transformed['image'], image)
    assert transformed['bboxes'] == bboxes
    for k, v in labels.items():
        assert transformed[k] == v
def test_compose():
    first = MagicMock()
    second = MagicMock()
    augmentation = Compose([first, second], p=1)
    image = np.ones((8, 8))
    augmentation(image=image)
    assert first.called
    assert second.called
示例#24
0
def test_keypoint_flips_transform_3x3(aug, keypoints, expected):
    transform = Compose([aug(p=1)], keypoint_params={"format": "xy"})

    image = np.ones((3, 3, 3))
    transformed = transform(image=image,
                            keypoints=keypoints,
                            labels=np.ones(len(keypoints)))
    assert np.allclose(expected, transformed["keypoints"])
示例#25
0
def test_check_each_transform(targets, bbox_params, keypoint_params, expected):
    image = np.empty([100, 100], dtype=np.uint8)
    augs = Compose(
        [Crop(0, 0, 50, 50), PadIfNeeded(100, 100)], bbox_params=bbox_params, keypoint_params=keypoint_params
    )
    res = augs(image=image, **targets)

    for key, item in expected.items():
        assert np.all(np.array(item) == np.array(res[key]))
示例#26
0
def test_compose_with_keypoint_noop_error_label_fields(keypoints,
                                                       keypoint_format):
    image = np.ones((100, 100, 3))
    aug = Compose([NoOp(p=1.0)],
                  keypoint_params={
                      "format": keypoint_format,
                      "label_fields": "class_id"
                  })
    with pytest.raises(Exception):
        aug(image=image, keypoints=keypoints, cls_id=[0])
示例#27
0
 def __init__(self, aug: DictConfig):
     color_tfms = [aug.tfms.brightness_contrast, aug.tfms.hue_sat]
     color_transform = OneOf([instantiate(tfm) for tfm in color_tfms],
                             p=0.9)
     tfm_list = [color_transform] + [
         instantiate(tfm)
         for tfm in aug.tfms.values() if tfm not in color_tfms
     ]
     self.transforms = Compose(tfm_list,
                               bbox_params=instantiate(aug.bbox_params))
示例#28
0
def test_iaa_transforms_emit_warning(aug, keypoints, expected):
    with pytest.warns(
            UserWarning,
            match=
            "IAAFliplr transformation supports only 'xy' keypoints augmentation"
    ):
        Compose([aug(p=1)],
                keypoint_params={
                    "format": "xyas",
                    "label_fields": ["labels"]
                })
示例#29
0
def aug_preprocess(resize):
    return Compose([
        RandomResizedCrop(height=resize,
                          width=resize,
                          scale=(0.8, 1.0),
                          ratio=(4 / 5, 5 / 4)),
        RandomBrightnessContrast(0.1, 0.1),
        HorizontalFlip(p=0.5),
        Lambda(image=preprocess_input)
    ],
                   p=1)  # always apply augmentation
示例#30
0
def test_iaa_transforms_emit_warning(aug, keypoints, expected):
    with pytest.warns(
            UserWarning,
            match=
            'IAAFliplr transformation supports only \'xy\' keypoints augmentation'
    ):
        Compose([aug(p=1)],
                keypoint_params={
                    'format': 'xyas',
                    'label_fields': ['labels']
                })