Exemplo n.º 1
0
def my_transforms():
    train_trf = solt.Stream([
        slt.Scale(range_x=(0.9, 1.1), same=False, p=0.5),
        slt.Shear(range_x=(-0.05, 0.05), p=0.5),
        slt.Rotate((-5, 5), p=0.5),
        slt.Pad(pad_to=(32, 32))
    ])

    test_trf = solt.Stream([slt.Pad(pad_to=(32, 32))])

    return {'train': train_trf, 'eval': test_trf}
Exemplo n.º 2
0
def test_padding_img_mask_3x4_5x5(img_3x4, mask_3x4):
    img, mask = img_3x4, mask_3x4
    dc = slc.DataContainer((img, mask), "IM")
    transf = slt.Pad((5, 5))
    res = transf(dc)
    assert (res[0][0].shape[0] == 5) and (res[0][0].shape[1] == 5)
    assert (res[1][0].shape[0] == 5) and (res[1][0].shape[1] == 5)
Exemplo n.º 3
0
def test_2x2_pad_to_20x20_center_crop_2x2(pad_size, crop_size, img_2x2,
                                          mask_2x2):
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 1], [1, 1], [1, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 2, 2)
    img, mask = img_2x2, mask_2x2

    dc = slc.DataContainer((
        img,
        mask,
        kpts,
    ), "IMP")

    stream = slc.Stream(
        [slt.Pad(pad_to=pad_size),
         slt.Crop(crop_to=crop_size)])
    res = stream(dc, return_torch=False)

    assert (res[0][0].shape[0] == 2) and (res[0][0].shape[1] == 2)
    assert (res[1][0].shape[0] == 2) and (res[1][0].shape[1] == 2)
    assert (res[2][0].height == 2) and (res[2][0].width == 2)

    assert np.array_equal(res[0][0], img)
    assert np.array_equal(res[1][0], mask)
    assert np.array_equal(res[2][0].data, kpts_data)
Exemplo n.º 4
0
def test_reflective_padding_cant_be_applied_to_kpts():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [2, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 3, 4)
    dc = slc.DataContainer((1, kpts), "LP")
    trf = slt.Pad(pad_to=(10, 10), padding="r")
    with pytest.raises(ValueError):
        trf(dc)
Exemplo n.º 5
0
def test_padding_img_mask_2x2_3x3(img_2x2, mask_2x2):
    img, mask = img_2x2, mask_2x2
    dc = slc.DataContainer((img, mask), "IM")
    transf = slt.Pad((3, 3))
    res = transf(dc)
    assert (res[0][0].shape[0] == 3) and (res[0][0].shape[1] == 3)
    assert (res[1][0].shape[0] == 3) and (res[1][0].shape[1] == 3)
Exemplo n.º 6
0
def test_pad_does_not_change_the_data_when_the_image_and_the_mask_are_big(pad_size, pad_type, img_3x3, mask_3x3):
    dc = slc.DataContainer((img_3x3, mask_3x3), "IM")
    trf = slt.Pad(pad_to=pad_size, padding=pad_type)
    dc_res = trf(dc)

    np.testing.assert_array_equal(dc_res.data[0], img_3x3)
    np.testing.assert_array_equal(dc_res.data[1], mask_3x3)
Exemplo n.º 7
0
def test_deserialize_from_dict(serialized):
    trfs = slc.Stream([slt.Pad(34), slt.Crop(32, "r"), slt.CutOut(2)])

    serialized_trfs = json.dumps(trfs.to_dict())
    serialized_from_deserialized = json.dumps(
        slu.from_dict(serialized).to_dict())

    assert serialized_trfs == serialized_from_deserialized
Exemplo n.º 8
0
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}
Exemplo n.º 9
0
def test_different_crop_modes(crop_mode, img_2x2, mask_2x2):
    if crop_mode == "d":
        with pytest.raises(ValueError):
            slt.Crop(crop_to=2, crop_mode=crop_mode)
    else:
        stream = slc.Stream([slt.Pad(pad_to=20), slt.Crop(crop_to=2, crop_mode=crop_mode),])
        img, mask = img_2x2, mask_2x2
        dc = slc.DataContainer((img, mask,), "IM")
        dc_res = stream(dc, return_torch=False)

        for el in dc_res.data:
            assert el.shape[0] == 2
            assert el.shape[1] == 2
Exemplo n.º 10
0
def test_matrix_transforms_state_reset(img_5x5, ignore_state, pipeline):
    n_iter = 50
    if pipeline:
        ppl = slc.Stream([
            slt.Rotate(angle_range=(-180, 180), p=1,
                       ignore_state=ignore_state),
            slt.Pad(pad_to=(10, 10)),
        ])
    else:
        ppl = slt.Rotate(angle_range=(-180, 180),
                         p=1,
                         ignore_state=ignore_state)

    img_test = img_5x5.copy()
    img_test[0, 0] = 1
    random.seed(42)

    trf_not_eq = 0
    imgs_not_eq = 0
    for i in range(n_iter):
        dc1 = slc.DataContainer((img_test.copy(), ), 'I')
        dc2 = slc.DataContainer((img_test.copy(), ), 'I')
        if pipeline:
            dc1_res = ppl(dc1, return_torch=False).data[0].squeeze()
        else:
            dc1_res = ppl(dc1).data[0].squeeze()
        if pipeline:
            trf_state1 = ppl.transforms[0].state_dict[
                'transform_matrix_corrected']
        else:
            trf_state1 = ppl.state_dict['transform_matrix_corrected']
        if pipeline:
            dc2_res = ppl(dc2, return_torch=False).data[0].squeeze()
        else:
            dc2_res = ppl(dc2).data[0].squeeze()
        if pipeline:
            trf_state2 = ppl.transforms[0].state_dict[
                'transform_matrix_corrected']
        else:
            trf_state2 = ppl.state_dict['transform_matrix_corrected']

        if not np.array_equal(trf_state1, trf_state2):
            trf_not_eq += 1

        if not np.array_equal(dc1_res, dc2_res):
            imgs_not_eq += 1

    random.seed(None)
    assert trf_not_eq > n_iter // 2
    assert imgs_not_eq > n_iter // 2
Exemplo n.º 11
0
def my_transforms():
    train_trf = solt.Stream([
        # slt.Scale(range_x=(0.9, 1.1), range_y=(0.9, 1.1), same=True, p=0.5),
        # slt.Translate(range_x=(-0.05, 0.05), range_y=(-0.05, 0.05), p=0.5),
        # slt.GammaCorrection(gamma_range=0.1, p=0.5),
        slt.Pad(pad_to=(36, 36)),
        slt.Rotate((-5, 5), p=0.5),
        slt.Crop((32, 32)),
        # slt.Noise(gain_range=0.1, p=0.8),
        slt.CutOut((8, 8))
    ])

    test_trf = solt.Stream([])

    custom_trf = solt.Stream([
        slt.Pad(pad_to=(36, 36)),
        slt.Rotate((-5, 5), p=0.5),
        slt.Crop((32, 32)),
        # slt.Noise(gain_range=0.1, p=0.8),
        slt.CutOut((8, 8))
    ])

    return {'train': train_trf, 'eval': test_trf, 'transforms': custom_trf}
Exemplo n.º 12
0
def test_3x3_pad_to_20x20_center_crop_3x3_shape_stayes_unchanged(img_3x3, mask_3x3):
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 3, 3)
    img, mask = img_3x3, mask_3x3

    dc = slc.DataContainer((img, mask, kpts,), "IMP")

    stream = slc.Stream([slt.Pad((20, 20)), slt.Crop((3, 3))])
    res = stream(dc, return_torch=False)

    assert (res[0][0].shape[0] == 3) and (res[0][0].shape[1] == 3)
    assert (res[1][0].shape[0] == 3) and (res[1][0].shape[1] == 3)
    assert (res[2][0].height == 3) and (res[2][0].width == 3)
Exemplo n.º 13
0
def test_6x6_pad_to_20x20_center_crop_6x6_kpts_img(img):
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 5], [1, 3], [2, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, frame=(6, 6))

    dc = slc.DataContainer((kpts, img), "PI")

    stream = slc.Stream([slt.Pad((20, 20)), slt.Crop((6, 6))])
    res = stream(dc, return_torch=False)

    assert (res[1][0].shape[0] == 6) and (res[1][0].shape[1] == 6)
    assert (res[0][0].frame[0] == 6) and (res[0][0].frame[1] == 6)

    assert np.array_equal(res[1][0], img)
    assert np.array_equal(res[0][0].data, kpts_data)
Exemplo n.º 14
0
def test_pad_to_20x20_img_mask_keypoints_3x3_kpts_first(img_3x3, mask_3x3):
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 3, 3)
    img, mask = img_3x3, mask_3x3

    dc = slc.DataContainer((kpts, img, mask), "PIM")
    transf = slt.Pad((20, 20))
    res = transf(dc)

    assert (res[2][0].shape[0] == 20) and (res[2][0].shape[1] == 20)
    assert (res[1][0].shape[0] == 20) and (res[1][0].shape[1] == 20)
    assert (res[0][0].height == 20) and (res[0][0].width == 20)

    assert np.array_equal(res[0][0].data, np.array([[8, 8], [8, 10], [10, 10], [10, 8]]).reshape((4, 2)))
Exemplo n.º 15
0
def test_matrix_transforms_use_cache_for_different_dc_items_raises_error(
        img_5x5, mask_3x4, pipeline):
    dc = slc.DataContainer((img_5x5, mask_3x4), 'IM')
    if pipeline:
        ppl = slc.Stream([
            slt.Rotate(angle_range=(-180, 180), p=1, ignore_state=False),
            slt.Pad(pad_to=(10, 10)),
        ])
    else:
        ppl = slt.Rotate(angle_range=(-180, 180), p=1, ignore_state=False)

    with pytest.raises(ValueError):
        if pipeline:
            ppl(dc, return_torch=False)
        else:
            ppl(dc)
Exemplo n.º 16
0
def test_pad_to_20x20_img_mask_keypoints_3x3(img, mask):
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, frame=(3, 3))

    dc = slc.DataContainer((
        img,
        mask,
        kpts,
    ), "IMP")
    transf = slt.Pad((20, 20))
    res = transf(dc)

    assert (res[0][0].shape[0] == 20) and (res[0][0].shape[1] == 20)
    assert (res[1][0].shape[0] == 20) and (res[1][0].shape[1] == 20)
    assert (res[2][0].frame[0] == 20) and (res[2][0].frame[1] == 20)

    assert np.array_equal(
        res[2][0].data,
        np.array([[8, 8], [8, 10], [10, 10], [10, 8]]).reshape((4, 2)))
Exemplo n.º 17
0
    def __init__(self, pad, img_size=256):
        super(Pad, self).__init__(img_size)

        self.pad = pad

        self.solt_pipeline = slt.Pad(pad)

        self.albumentations_pipeline = albu.Compose([
            albu.PadIfNeeded(min_height=pad,
                             min_width=pad,
                             border_mode=cv2.BORDER_CONSTANT,
                             value=0),
            ToTensor(normalize={
                "mean": [0.485, 0.456, 0.406],
                "std": [0.229, 0.224, 0.225]
            }),
        ])

        self.torchvision_pipeline = tv_transforms.Compose([
            tv_transforms.Pad(pad),
            tv_transforms.ToTensor(),
            tv_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]),
        ])
Exemplo n.º 18
0
def test_padding_for_item(inp, transform_settings, expected):
    dc = slc.DataContainer((inp, ), "I", transform_settings)
    dc_res = slt.Pad(pad_to=(7, 7))(dc)
    assert np.array_equal(expected, dc_res.data[0])
Exemplo n.º 19
0
                                 }
                             },
                             {
                                 "cutout": {
                                     "cutout_size": 2,
                                     "p": 0.5
                                 }
                             },
                         ],
                     },
                 },
             ]
         }
     },
     slc.Stream([
         slt.Pad(34),
         slt.Crop(32, "r"),
         slt.CutOut(2),
         slc.Stream([slt.Pad(34),
                     slt.Crop(32, "r"),
                     slt.CutOut(2)]),
     ]),
 ],
 [
     {
         "stream": {
             "transforms": [
                 {
                     "stream": {
                         "interpolation":
                         None,
Exemplo n.º 20
0
def test_padding_cant_be_float():
    with pytest.raises(TypeError):
        slt.Pad(pad_to=2.5)
Exemplo n.º 21
0
def test_wrong_transform_type_in_a_stream(img_2x2):
    dc = slc.DataContainer(img_2x2, 'I')
    with pytest.raises(TypeError):
        slc.Stream.exec_stream([slt.Pad(4), lambda x: x**2], dc, False)
Exemplo n.º 22
0
def test_padding_img_mask(img, mask, pad_to, shape_out):
    dc = slc.DataContainer((img, mask), "IM")
    transf = slt.Pad(pad_to)
    res = transf(dc)
    np.testing.assert_array_equal(res[0][0].shape[:-1], shape_out)
    np.testing.assert_array_equal(res[1][0].shape, shape_out)
Exemplo n.º 23
0
def init_mnist_transforms():
    return Stream([slt.Pad(pad_to=(32, 32))])
Exemplo n.º 24
0
def test_padding_img_2x2_2x2(img_2x2):
    img = img_2x2
    dc = slc.DataContainer((img, ), "I")
    transf = slt.Pad((2, 2))
    res = transf(dc)
    assert (res[0][0].shape[0] == 2) and (res[0][0].shape[1] == 2)
Exemplo n.º 25
0
 def __init__(self):
     self.solt_stream = slc.Stream(
         [slt.Pad(pad_to=(512, 512), padding="r")])
Exemplo n.º 26
0
def test_padding_img(img, pad_to):
    dc = slc.DataContainer((img, ), "I")
    transf = slt.Pad(pad_to)
    res = transf(dc)
    np.testing.assert_array_equal(res[0][0].shape[:-1], pad_to)