def test_cutout_blacks_out_image(img, expected, cut_size): dc = slc.DataContainer((img, ), "I") trf = slc.Stream([slt.CutOut(p=1, cutout_size=cut_size)]) dc_res = trf(dc, return_torch=False) assert np.array_equal(expected, dc_res.data[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
def test_crop_or_cutout_size_are_too_big(img_2x2, cutout_crop_size): dc = slc.DataContainer((img_2x2, ), "I") trf = slt.Crop(crop_to=cutout_crop_size) with pytest.raises(ValueError): trf(dc) trf = slt.CutOut(p=1, cutout_size=cutout_crop_size) with pytest.raises(ValueError): trf(dc)
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()
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}
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}
def test_cutout_1x1_blacks_corner_pixels_2x2_img(img_2x2, cut_size): dc = slc.DataContainer((img_2x2.copy(), ), "I") trf = slc.Stream([slt.CutOut(p=1, cutout_size=cut_size)]) dc_res = trf(dc, return_torch=False) equal = 0 for i in range(2): for j in range(2): tmp_opt = img_2x2.copy() tmp_opt[i, j] = 0 if np.array_equal(dc_res.data[0], tmp_opt): equal += 1 assert equal == 1
{ "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, "padding": None,
def test_wrong_crop_size_types(cutout_crop_size): with pytest.raises(TypeError): slt.Crop(crop_to=cutout_crop_size) with pytest.raises(TypeError): slt.CutOut(cutout_size=cutout_crop_size)