Beispiel #1
0
def test_scale_range_from_number(scale, expected):
    if expected is not None:
        trf = slt.Scale(range_x=scale, range_y=scale)
        assert trf.range_x == expected
        assert trf.range_x == expected
    else:
        with pytest.raises(ValueError):
            slt.Scale(range_x=scale)
        with pytest.raises(ValueError):
            slt.Scale(range_x=None, range_y=scale)
Beispiel #2
0
def test_scale_xy_axis_even(img_6x6):
    stream = slt.Scale((0.5, 0.5), (2, 2), same=False, p=1, ignore_fast_mode=True)
    dc = slc.DataContainer((img_6x6,), "I")
    H, W = img_6x6.shape[0], img_6x6.shape[1]
    img_res = stream(dc)[0][0]
    assert H * 2 == img_res.shape[0]
    assert W // 2 == img_res.shape[1]
Beispiel #3
0
def test_scale_xy_axis_odd(img_5x5):
    stream = slt.Scale((0.5, 0.5), (3, 3), same=False, p=1, ignore_fast_mode=True)
    dc = slc.DataContainer((img_5x5,), "I")
    H, W = img_5x5.shape[0], img_5x5.shape[1]
    img_res = stream(dc)[0][0]
    assert H * 3 == img_res.shape[0]
    assert W // 2 == img_res.shape[1]
Beispiel #4
0
def test_zoom_x_axis_odd(img_5x5):
    stream = slt.Scale(range_x=(0.5, 0.5), range_y=(1, 1), same=False, p=1, ignore_fast_mode=True)
    dc = slc.DataContainer((img_5x5,), "I")
    H, W = img_5x5.shape[0], img_5x5.shape[1]
    img_res = stream(dc)[0][0]
    assert H == img_res.shape[0]
    assert W // 2 == img_res.shape[1]
Beispiel #5
0
def test_scale_img_mask(img_3x4, mask_3x4):
    img_mask_3x4 = img_3x4, mask_3x4
    stream = slt.Scale((0.5, 0.5), (2, 2), same=False, p=1, ignore_fast_mode=True)
    dc = slc.DataContainer(img_mask_3x4, "IM")
    H, W = img_mask_3x4[0].shape[0], img_mask_3x4[0].shape[1]
    img_res = stream(dc)[0][0]
    mask_res = stream(dc)[1][0]
    assert H * 2 == img_res.shape[0], W // 2 == img_res.shape[1]
    assert H * 2 == mask_res.shape[0], W // 2 == mask_res.shape[1]
Beispiel #6
0
def test_fusion_happens(img_5x5):
    ppl = slc.Stream([
        slt.Scale((0.5, 1.5), (0.5, 1.5), p=1),
        slt.Rotate((-50, 50), padding='z', p=1),
        slt.Shear((-0.5, 0.5), (-0.5, 0.5), padding='z', p=1),
    ])
    dc = slc.DataContainer(img_5x5, 'I')
    st = ppl.optimize_transforms_stack(ppl.transforms, dc)
    assert len(st) == 1
Beispiel #7
0
def test_scale_x_axis_even(img):
    stream = slt.Scale((0.5, 0.5), (1, 1),
                       same=False,
                       p=1,
                       ignore_fast_mode=True)
    dc = slc.DataContainer((img, ), "I")
    H, W = img.shape[:2]
    img_res = stream(dc)[0][0]
    assert H == img_res.shape[0]
    assert W // 2 == img_res.shape[1]
Beispiel #8
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}
Beispiel #9
0
def test_random_proj_and_selective_stream(img):
    dc = slc.DataContainer((img, ), "I")

    ppl = slt.Projection(
        slc.SelectiveStream(
            [
                slt.Rotate(angle_range=(90, 90), p=0),
                slt.Scale(range_y=(0, 0.1), same=True, p=0),
                slt.Shear(range_y=(-0.1, 0.1), p=0),
            ],
            n=3,
        ),
        v_range=(0, 0),
    )

    dc_res = ppl(dc)

    assert np.array_equal(dc.data, dc_res.data)
Beispiel #10
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()
Beispiel #11
0
                                    }
                                },
                            }
                        },
                    ]
                }
            },
            slc.Stream([
                slc.Stream([slt.Pad(34),
                            slt.Crop(32, "r"),
                            slt.CutOut(2)]),
                slt.Pad(34),
                slt.Crop(32, "c"),
                slt.CutOut(4),
                slt.Projection(slc.Stream([slt.Rotate(30),
                                           slt.Scale(2)]),
                               v_range=(0, 1e-3)),
            ]),
        ],
    ],
)
def test_deserialize_from_dict_nested(serialized: dict, stream: slc.Stream):
    serialized_trfs = json.dumps(stream.to_dict())
    serialized_from_deserialized = json.dumps(
        slu.from_dict(serialized).to_dict())

    assert serialized_trfs == serialized_from_deserialized


def test_stream_serializes_all_args_are_set():
    ppl = slc.Stream([
Beispiel #12
0
def test_scale_sampling_scale(same, scale_x, scale_y, expected, img_6x6_rgb):
    dc = slc.DataContainer(img_6x6_rgb, "I")
    trf = slt.Scale(range_x=scale_x, range_y=scale_y, same=same)
    trf.sample_transform(dc)
    assert expected == (trf.state_dict["scale_x"], trf.state_dict["scale_y"])
Beispiel #13
0
def test_scale_when_range_x_is_none(scale_x, scale_y, same, expected,
                                    img_6x6_rgb):
    dc = slc.DataContainer(img_6x6_rgb, "I")
    trf = slt.Scale(range_x=scale_x, range_y=scale_y, same=same, p=1)
    trf.sample_transform(dc)
    assert (trf.state_dict["scale_x"], trf.state_dict["scale_y"]) == expected