예제 #1
0
def test_img_mask_vertical_horizontal_flip(img_3x4, mask_3x4):
    img, mask = img_3x4, mask_3x4
    dc = slc.DataContainer((img, mask), "IM")

    stream = slc.Stream([slt.Flip(p=1, axis=0), slt.Flip(p=1, axis=1)])

    dc = stream(dc, return_torch=False)
    img_res, _, _ = dc[0]
    mask_res, _, _ = dc[1]

    h, w = mask.shape
    assert np.array_equal(cv2.flip(cv2.flip(img, 0), 1).reshape(h, w, 1), img_res)
    assert np.array_equal(cv2.flip(cv2.flip(mask, 0), 1), mask_res)
예제 #2
0
    def __init__(self, img_size=256):
        super(VHFlipRotateCrop, self).__init__(img_size)

        self.solt_pipeline = solt.Stream([
            slt.Flip(p=self.probablity, axis=0),
            slt.Flip(p=self.probablity, axis=1),
            slt.Rotate(angle_range=(0, 20)),
            slt.Crop(224, crop_mode="r"),
        ])

        self.albumentations_pipeline = albu.Compose([
            albu.VerticalFlip(p=self.probablity),
            albu.HorizontalFlip(p=self.probablity),
            albu.Rotate(limit=(0, 20),
                        p=self.probablity,
                        border_mode=cv2.BORDER_CONSTANT,
                        value=0),
            albu.RandomCrop(height=224, width=224),
            ToTensor(normalize={
                "mean": [0.485, 0.456, 0.406],
                "std": [0.229, 0.224, 0.225]
            }),
        ])

        self.torchvision_pipeline = tv_transforms.Compose([
            tv_transforms.RandomHorizontalFlip(p=self.probablity),
            tv_transforms.RandomRotation(degrees=(0, 20)),
            tv_transforms.RandomCrop(224),
            tv_transforms.ToTensor(),
            tv_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]),
        ])

        _augm_ppl = augmentor.Pipeline()
        _augm_ppl.flip_top_bottom(probability=self.probablity)
        _augm_ppl.flip_left_right(probability=self.probablity)
        _augm_ppl.rotate(probability=self.probablity,
                         max_left_rotation=0,
                         max_right_rotation=20)
        _augm_ppl.crop_random(probability=1,
                              percentage_area=224 / float(self.img_size))

        self.augmentor_pipeline = tv_transforms.Compose([
            _augm_ppl.torch_transform(),
            tv_transforms.transforms.ToTensor(),
            tv_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]),
        ])
예제 #3
0
def test_fusion_rotate_360_flip_rotate_360(img_5x5):
    img = img_5x5
    dc = slc.DataContainer((img, ), 'I')

    ppl = slc.Stream([
        slc.Stream([
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
        ],
                   optimize_stack=True),
        slt.Flip(p=1, axis=1),
        slc.Stream([
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
            slt.Rotate((45, 45), padding='z', p=1),
        ],
                   optimize_stack=True)
    ])

    img_res = ppl(dc, return_torch=False)[0][0]

    np.testing.assert_array_almost_equal(
        cv2.flip(img, 1).reshape(5, 5, 1), img_res)
예제 #4
0
def test_image_shape_equal_3_after_nested_flip(img):
    dc = slc.DataContainer((img, ), "I")

    stream = slc.Stream([
        slt.Flip(p=1, axis=0),
        slt.Flip(p=1, axis=1),
        slc.Stream([
            slt.Flip(p=1, axis=1),
            slt.Flip(p=1, axis=0),
        ])
    ])

    dc = stream(dc, return_torch=False)
    img_res, _, _ = dc[0]

    assert np.array_equal(len(img.shape), 3)
예제 #5
0
    def __init__(self, img_size=256):
        super(VerticalFlip, self).__init__(img_size)

        self.solt_pipeline = slt.Flip(p=self.probablity, axis=0)

        self.albumentations_pipeline = albu.Compose([
            albu.VerticalFlip(p=self.probablity),
            ToTensor(normalize={
                "mean": [0.485, 0.456, 0.406],
                "std": [0.229, 0.224, 0.225]
            }),
        ])

        self.torchvision_pipeline = tv_transforms.Compose([
            tv_transforms.RandomVerticalFlip(p=self.probablity),
            tv_transforms.ToTensor(),
            tv_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]),
        ])

        _augm_ppl = augmentor.Pipeline()
        _augm_ppl.flip_top_bottom(probability=self.probablity)
        self.augmentor_pipeline = tv_transforms.Compose([
            _augm_ppl.torch_transform(),
            tv_transforms.transforms.ToTensor(),
            tv_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]),
        ])
예제 #6
0
def test_keypoints_vertical_flip_within_stream():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 2, 2)
    stream = slc.Stream([slt.Flip(p=1, axis=0)])
    dc = slc.DataContainer((kpts,), "P")

    dc_res = stream(dc, return_torch=False)

    assert np.array_equal(dc_res[0][0].data, np.array([[0, 1], [0, 0], [1, 1], [1, 0]]).reshape((4, 2)))
예제 #7
0
def test_nested_stream(img, mask):
    dc = slc.DataContainer((img, mask), "IM")

    stream = slc.Stream([
        slt.Flip(p=1, axis=0),
        slt.Flip(p=1, axis=1),
        slc.Stream([
            slt.Flip(p=1, axis=1),
            slt.Flip(p=1, axis=0),
        ])
    ])

    dc = stream(dc, return_torch=False)
    img_res, t0, _ = dc[0]
    mask_res, t1, _ = dc[1]

    assert np.array_equal(img, img_res)
    assert np.array_equal(mask, mask_res)
예제 #8
0
def test_keypoints_vertical_flip():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, frame=(2, 2))
    stream = slt.Flip(p=1, axis=0)
    dc = slc.DataContainer((kpts, ), "P")

    dc_res = stream(dc)

    assert np.array_equal(
        dc_res[0][0].data,
        np.array([[0, 1], [0, 0], [1, 1], [1, 0]]).reshape((4, 2)))
예제 #9
0
파일: utils.py 프로젝트: MIPT-Oulu/Collagen
def my_transforms():
    train_trf = solt.Stream([
        slt.Pad(pad_to=(36, 36)),
        slt.Rotate(10),
        slt.Crop((32, 32)),
        slt.CutOut((8, 8)),
        slt.Flip(p=0.5)
    ])

    test_trf = solt.Stream([])

    return {'train': train_trf, 'eval': test_trf}
예제 #10
0
def test_img_mask_vertical_horizontal_flip_negative_axes(img_3x4, mask_3x4):
    img, mask = img_3x4, mask_3x4
    dc = slc.DataContainer((img, mask), "IM")

    stream = slt.Flip(p=1, axis=-1)

    dc = stream(dc)
    img_res, _, _ = dc[0]
    mask_res, _, _ = dc[1]

    h, w = mask.shape
    assert np.array_equal(cv2.flip(cv2.flip(img, 0), 1).reshape(h, w, 1), img_res)
    assert np.array_equal(cv2.flip(cv2.flip(mask, 0), 1), mask_res)
예제 #11
0
def create_train_transforms(size):
    return solt.Stream([
                        slt.JPEGCompression(p=0.5,quality_range=(60,100)),
                        slt.Noise(p=0.25),
                        slt.Brightness(),
                        slt.Contrast(),
                        slt.Flip(),
                        slt.Rotate90(),
                        solt.SelectiveStream([
                            slt.GammaCorrection(gamma_range=0.5, p=1),
                            slt.Noise(gain_range=0.1, p=1),
                            slt.SaltAndPepper(),
                            slt.Blur(),
                        ], n=3),
                        slt.Rotate(angle_range=(-10, 10), p=0.5),
                        slt.Resize((size,size)),
                    ])
예제 #12
0
def test_complex_transform_serialization():
    stream = slc.Stream([
        slt.Flip(axis=1, p=0.5),
        slc.SelectiveStream([
            slt.Rotate(angle_range=(-45, -45), p=1, padding="r"),
            slt.Rotate90(1, p=1),
            slt.Rotate(angle_range=(45, 45), p=1, padding="r"),
        ]),
        slt.Crop((350, 350)),
        slc.SelectiveStream([
            slt.GammaCorrection(gamma_range=0.5, p=1),
            slt.Noise(gain_range=0.1, p=1),
            slt.Blur()
        ],
                            n=3),
        slt.Projection(
            affine_transforms=slc.Stream([
                slt.Rotate(angle_range=(-45, 45), p=1),
                slt.Scale(range_x=(0.8, 1.5),
                          range_y=(0.8, 1.5),
                          p=1,
                          same=False),
            ]),
            v_range=(1e-4, 1e-3),
            p=1,
        ),
        slc.SelectiveStream(
            [
                slt.CutOut(40, p=1),
                slt.CutOut(30, p=1),
                slt.CutOut(20, p=1),
                slt.CutOut(40, p=1),
                slc.Stream(),
                slc.Stream(),
                slc.Stream(),
            ],
            n=3,
        ),
    ])

    assert slu.from_yaml(stream.to_yaml()).to_yaml() == slu.from_yaml(
        stream.to_yaml()).to_yaml()
예제 #13
0
def test_img_mask__kptsvertical_horizontal_flip_negative_axes(img, mask):
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data.copy(), frame=(3, 4))
    dc = slc.DataContainer((img, mask, kpts), "IMP")

    stream = slt.Flip(p=1, axis=-1)

    dc = stream(dc)
    img_res, _, _ = dc[0]
    mask_res, _, _ = dc[1]
    kpts_res, _, _ = dc[2]

    h, w = mask.shape
    assert np.array_equal(
        cv2.flip(cv2.flip(img, 0), 1).reshape(h, w, 1), img_res)
    assert np.array_equal(cv2.flip(cv2.flip(mask, 0), 1), mask_res)

    kpts_data[:, 0] = 4 - 1 - kpts_data[:, 0]
    kpts_data[:, 1] = 3 - 1 - kpts_data[:, 1]

    assert np.array_equal(kpts_data, kpts_res.data)
예제 #14
0
    def __init__(self, img_size=256):
        super(HFlipCrop, self).__init__(img_size)

        self.solt_pipeline = solt.Stream([
            slt.Flip(p=self.probablity, axis=1),
            slt.Crop(224, crop_mode="r")
        ])

        self.albumentations_pipeline = albu.Compose([
            albu.HorizontalFlip(p=self.probablity),
            albu.RandomCrop(height=224, width=224),
            ToTensor(normalize={
                "mean": [0.485, 0.456, 0.406],
                "std": [0.229, 0.224, 0.225]
            }),
        ])

        self.torchvision_pipeline = tv_transforms.Compose([
            tv_transforms.RandomHorizontalFlip(p=self.probablity),
            tv_transforms.RandomCrop(224),
            tv_transforms.ToTensor(),
            tv_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]),
        ])

        _augm_ppl = augmentor.Pipeline()
        _augm_ppl.flip_left_right(probability=self.probablity)
        _augm_ppl.crop_random(probability=1,
                              percentage_area=224 / float(self.img_size))

        self.augmentor_pipeline = tv_transforms.Compose([
            _augm_ppl.torch_transform(),
            tv_transforms.transforms.ToTensor(),
            tv_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]),
        ])
예제 #15
0
    "translate_x, translate_y, expected", [(None, (2, 2), (0, 2)), ((2, 2), None, (2, 0)), (None, None, (0, 0)),],
)
def test_translate_when_range_x_is_none(translate_x, translate_y, expected, img_6x6_rgb):
    dc = slc.DataContainer(img_6x6_rgb, "I")
    trf = slt.Translate(range_x=translate_x, range_y=translate_y, p=1)
    trf.sample_transform(dc)
    assert (trf.state_dict["translate_x"], trf.state_dict["translate_y"]) == expected


@pytest.mark.parametrize(
    "param_set",
    [
        {"affine_transforms": "123"},
        {"affine_transforms": 123},
        {"affine_transforms": []},
        {"affine_transforms": slc.Stream([slt.Flip(),])},
        {"affine_transforms": slc.Stream([slt.Flip(),])},
        {"v_range": "123"},
        {"v_range": 123},
        {"v_range": ("123", "456")},
        {"v_range": ((2,), (4,))},
    ],
)
def test_random_projection_raises_type_errors(param_set):
    with pytest.raises(TypeError):
        slt.Projection(**param_set)


@pytest.mark.parametrize(
    "value_range,to_catch",
    [
예제 #16
0
def test_transform_returns_original_data_when_not_used_and_applied(img_2x2):
    trf = slt.Flip(p=0)
    dc = slc.DataContainer(img_2x2, "I")
    dc_res = trf(dc)
    assert dc_res == dc
예제 #17
0
def test_flip_invalid_axis():
    with pytest.raises(ValueError):
        slt.Flip(p=1, axis=100)
예제 #18
0
 def __init__(self):
     self.imgaug_transform = iaa.Fliplr(p=1)
     self.augmentor_op = Operations.Flip(probability=1,
                                         top_bottom_left_right="LEFT_RIGHT")
     self.solt_stream = slc.Stream([slt.Flip(p=1, axis=1)])
예제 #19
0
 def __init__(self):
     self.imgaug_transform = iaa.Flipud(p=1)
     self.augmentor_op = Operations.Flip(probability=1,
                                         top_bottom_left_right="TOP_BOTTOM")
     self.solt_stream = slc.Stream([slt.Flip(p=1, axis=0)])
예제 #20
0
@pytest.mark.parametrize(
    "param_set",
    [
        {
            "affine_transforms": "123"
        },
        {
            "affine_transforms": 123
        },
        {
            "affine_transforms": []
        },
        {
            "affine_transforms": slc.Stream([
                slt.Flip(),
            ])
        },
        {
            "affine_transforms": slc.Stream([
                slt.Flip(),
            ])
        },
        {
            "v_range": "123"
        },
        {
            "v_range": 123
        },
        {
            "v_range": ("123", "456")