Example #1
0
def test_resize_img_to_arbitrary_size(img, mask, resize_to):
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape(
        (4, 2))  # Top left corner
    kpts = slc.Keypoints(kpts_data.copy(), frame=(img.shape[:2]))

    dc = slc.DataContainer((
        img,
        mask,
        kpts,
    ), "IMP")
    transf = slt.Resize(resize_to=resize_to)
    res = transf(dc).data
    if isinstance(resize_to, int):
        resize_to = (resize_to, resize_to)

    scales = tuple(resize_to[i] / img.shape[i] for i in range(img.ndim - 1))

    assert transf.resize_to == resize_to
    np.testing.assert_array_equal(res[0].shape[:-1], resize_to)
    np.testing.assert_array_equal(res[1].shape, resize_to)
    np.testing.assert_array_equal(res[2].frame, resize_to)

    kpts_data = kpts_data.astype(float)
    kpts_data = (kpts_data * np.asarray(scales)[None, ])
    kpts_data = kpts_data.astype(int)
    assert np.array_equal(res[2].data, kpts_data)
Example #2
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)
Example #3
0
def test_rotate_90_img_mask_keypoints_destructive(img_3x3, mask_3x3, transform_settings, ignore_state):
    # 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
    H, W = mask.shape

    dc = slc.DataContainer((img, mask, kpts, 1), "IMPL", transform_settings=copy.deepcopy(transform_settings),)
    # Defining the 90 degrees transform (clockwise)
    stream = slt.Rotate(angle_range=(90, 90), p=1, ignore_state=ignore_state)
    dc_res = stream(dc)

    img_res, _, _ = dc_res[0]
    mask_res, _, _ = dc_res[1]
    kpts_res, _, _ = dc_res[2]
    label_res, _, _ = dc_res[3]
    M = cv2.getRotationMatrix2D((W // 2, H // 2), -90, 1)

    img_inter = ALLOWED_INTERPOLATIONS["bicubic"]
    img_pad = ALLOWED_PADDINGS["z"]
    if transform_settings is not None:
        img_inter = ALLOWED_INTERPOLATIONS[transform_settings[0]["interpolation"]]
        img_pad = ALLOWED_PADDINGS[transform_settings[0]["padding"]]

    expected_img_res = cv2.warpAffine(img, M, (W, H), flags=img_inter, borderMode=img_pad).reshape((H, W, 1))
    expected_mask_res = cv2.warpAffine(mask, M, (W, H))
    expected_kpts_res = np.array([[2, 0], [0, 0], [0, 2], [2, 2]]).reshape((4, 2))

    assert np.array_equal(expected_img_res, img_res)
    assert np.array_equal(expected_mask_res, mask_res)
    np.testing.assert_array_almost_equal(expected_kpts_res, kpts_res.data)
    assert label_res == 1
Example #4
0
def test_transform_returns_original_data_if_not_in_specified_indices(
        trf, img_3x3_rgb):
    img_3x3 = img_3x3_rgb * 128
    kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data.copy(), 3, 3)
    dc = slc.DataContainer((img_3x3.copy(), img_3x3.copy(), img_3x3.copy(),
                            img_3x3.copy(), 1, kpts, 2), "IIIILPL")

    kwargs = {"p": 1, "data_indices": (0, 1, 4)}
    if class_accepts(trf, "gain_range"):
        kwargs["gain_range"] = (0.7, 0.9)
    if class_accepts(trf, "brightness_range"):
        kwargs["brightness_range"] = (10, 20)
    if class_accepts(trf, "h_range"):
        kwargs["h_range"] = (50, 50)
        kwargs["s_range"] = (50, 50)
    if class_accepts(trf, "h_range"):
        kwargs["h_range"] = (50, 50)
        kwargs["s_range"] = (50, 50)

    trf = trf(**kwargs)
    res = trf(dc)

    assert np.linalg.norm(res.data[0] - img_3x3) > 0
    assert np.linalg.norm(res.data[1] - img_3x3) > 0
    np.testing.assert_array_equal(res.data[2], img_3x3)
    np.testing.assert_array_equal(res.data[3], img_3x3)
    assert res.data[-1] == 2
    np.testing.assert_array_equal(res.data[5].data, kpts_data)
Example #5
0
def test_resize_img_to_arbitrary_size(img, mask, resize_to):
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape(
        (4, 2))  # Top left corner
    kpts = slc.Keypoints(kpts_data.copy(), img.shape[0], img.shape[1])

    dc = slc.DataContainer((
        img,
        mask,
        kpts,
    ), "IMP")
    transf = slt.Resize(resize_to=resize_to)
    res = transf(dc).data

    if isinstance(resize_to, int):
        resize_to = (resize_to, resize_to)

    scale_x = resize_to[0] / img.shape[1]
    scale_y = resize_to[1] / img.shape[0]

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

    kpts_data = kpts_data.astype(float)
    kpts_data[:, 0] *= scale_x
    kpts_data[:, 1] *= scale_y
    kpts_data = kpts_data.astype(int)
    assert np.array_equal(res[2].data, kpts_data)
Example #6
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)
Example #7
0
def test_interpolation_or_padding_settings_for_labels_or_keypoints(setting):
    kpts = slc.Keypoints(pts=np.array([[0, 0], [0, 2], [2, 2],
                                       [2, 0]]).reshape((4, 2)),
                         frame=(3, 3))
    with pytest.raises(TypeError):
        slc.DataContainer(data=(kpts, ),
                          fmt="P",
                          transform_settings={0: setting})
Example #8
0
def test_create_4_keypoints_change_frame():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, frame=(3, 4))
    kpts.frame = (2, 2)

    assert kpts.frame[0] == 2
    assert kpts.frame[1] == 2
    assert np.array_equal(kpts_data, kpts.data)
Example #9
0
def test_create_4_keypoints_change_frame():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 3, 4)
    kpts.height = 2
    kpts.width = 2

    assert kpts.height == 2
    assert kpts.width == 2
    assert np.array_equal(kpts_data, kpts.data)
Example #10
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)))
Example #11
0
def test_keypoint_jitter_works_correctly(jitter_x, jitter_y, exp_x, exp_y):
    kpts_data = np.array([[1, 1],]).reshape((1, 2))
    kpts = slc.Keypoints(kpts_data.copy(), 2, 2)

    dc = slc.DataContainer((kpts,), "P")
    trf = slc.Stream([slt.KeypointsJitter(p=1, dx_range=(jitter_x, jitter_x), dy_range=(jitter_y, jitter_y))])
    dc_res = trf(dc, return_torch=False)

    assert np.array_equal(dc_res.data[0].data, np.array([exp_x, exp_y]).reshape((1, 2)))
Example #12
0
def test_pad_crop_resize_dont_change_data_when_parameters_are_not_set(img_3x3, mask_3x3, trf):
    # 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")
    res = trf()(dc, return_torch=False)
    assert dc == res
Example #13
0
def test_gaussian_noise_no_image_throws_value_error():
    trf = slt.Noise(p=1)
    # Setting up the data
    kpts_data = np.array([[0, 0], [0, 5], [1, 3], [2, 0]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 6, 6)
    dc = slc.DataContainer((kpts, ), "P")

    with pytest.raises(ValueError):
        trf(dc)
Example #14
0
def test_data_container_keypoints_rescale_to_torch():
    kpts_data = np.array([[100, 20], [1023, 80], [20, 20],
                          [100, 700]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 768, 1024)
    ppl = slc.Stream()
    k, label = ppl({'keypoints': kpts, 'label': 1}, as_dict=False)
    assert isinstance(k, torch.FloatTensor)
    np.testing.assert_almost_equal(k.max().item() * 1023, 1023)
    np.testing.assert_almost_equal(k.min().item() * 1023, 20)
    assert label == 1
Example #15
0
def test_keypoints_get_set():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 3, 4)

    assert np.array_equal(kpts[0], np.array([0, 0]))
    kpts[0] = np.array([2, 2])
    assert np.array_equal(kpts[0], np.array([2, 2]))

    with pytest.raises(TypeError):
        kpts[0] = [2, 2]
Example #16
0
def test_image_trfs_dont_change_mask_labels_kpts(trf_cls, trf_params, img_3x4, mask_3x4):
    trf = trf_cls(**trf_params)
    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((img_3x4, mask_3x4, kpts, 1), "IMPL")
    dc_res = trf(dc)

    assert np.all(dc.data[1] == dc_res.data[1])
    assert np.all(dc.data[2].data == dc_res.data[2].data)
    assert dc.data[3] == dc_res.data[3]
Example #17
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)))
Example #18
0
def test_keypoints_assert_reflective(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")
    # Defining the 90 degrees transform (clockwise)
    stream = slt.Rotate(angle_range=(20, 20), p=1, padding="r")
    with pytest.raises(ValueError):
        stream(dc)
Example #19
0
def test_create_4_keypoints_change_grid_and_frame():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, frame=(3, 4))

    kpts_data_new = np.array([[0, 0], [0, 1], [1, 0], [1, 1],
                              [0.5, 0.5]]).reshape((5, 2))
    kpts.frame = (2, 2)
    kpts.pts = kpts_data_new

    assert kpts.frame[0] == 2
    assert kpts.frame[1] == 2
    assert np.array_equal(kpts_data_new, kpts.pts)
Example #20
0
def test_create_4_keypoints_change_grid_and_frame():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 3, 4)

    kpts_data_new = np.array([[0, 0], [0, 1], [1, 0], [1, 1],
                              [0.5, 0.5]]).reshape((5, 2))
    kpts.height = 2
    kpts.width = 2
    kpts.pts = kpts_data_new

    assert kpts.height == 2
    assert kpts.width == 2
    assert np.array_equal(kpts_data_new, kpts.pts)
Example #21
0
def test_data_container_from_and_to_dict(img_3x4, mask_3x4, order, presence):
    img, mask = img_3x4, mask_3x4
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape(
        (4, 2)).astype(float)
    dc, dc_reordered = generate_data_container_based_on_presence(
        img, mask, kpts_data, order, presence)
    assert dc == dc_reordered

    # Now we will also test whether conversion to dict and back works well.
    tensor_dict = dc_reordered.to_torch(as_dict=True,
                                        normalize=False,
                                        scale_keypoints=False)
    for k in tensor_dict:
        if isinstance(tensor_dict[k], (list, tuple)):
            tmp = []
            for el in tensor_dict[k]:
                tmp.append(el.numpy() if isinstance(el, torch.Tensor) else el)
                if 'imag' in k:
                    tmp[-1] = (tmp[-1].transpose(
                        (1, 2, 0)) * 255).astype(np.uint8)
                if 'mask' in k:
                    tmp[-1] = tmp[-1].astype(np.uint8).squeeze()
                if 'keypoints' in k:
                    tmp[-1] = slc.Keypoints(tmp[-1], 3, 4)

            tensor_dict[k] = tmp
        else:
            el = tensor_dict[k]
            tensor_dict[k] = (el.numpy()).astype(np.uint8) if isinstance(
                el, torch.Tensor) else el
            if 'imag' in k:
                tensor_dict[k] = (tensor_dict[k].transpose(
                    (1, 2, 0)) * 255).astype(np.uint8)
            if 'mask' in k:
                tensor_dict[k] = tensor_dict[k].astype(np.uint8).squeeze()
            if 'keypoints' in k:
                tensor_dict[k] = slc.Keypoints(tensor_dict[k], 3, 4)

    assert dc == slc.DataContainer.from_dict(tensor_dict)
Example #22
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)
Example #23
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)
Example #24
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)))
Example #25
0
def test_selective_pipeline_selects_transforms_and_does_the_fusion():
    ppl = slc.SelectiveStream([
        slt.Rotate(angle_range=(90, 90), p=1),
        slt.Rotate(angle_range=(-90, -90), p=1),
    ],
                              n=2,
                              probs=[0.5, 0.5],
                              optimize_stack=True)

    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = slc.Keypoints(kpts_data, 3, 4)
    dc = slc.DataContainer(kpts, 'P')
    dc_res = ppl(dc, return_torch=False)

    assert np.array_equal(np.eye(3),
                          ppl.transforms[0].state_dict['transform_matrix'])
Example #26
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)))
Example #27
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)
Example #28
0
def test_keypoint_raises_with_pts_no_frame():
    with pytest.raises(ValueError):
        slc.Keypoints(pts=np.asarray([[0, 1], [1, 0]]))
Example #29
0
def test_create_empty_keypoints():
    kpts = slc.Keypoints()
    assert kpts.height is None
    assert kpts.width is None
    assert kpts.data is None
Example #30
0
def generate_data_container_based_on_presence(img, mask, kpts_data, order,
                                              presence):
    kpts = slc.Keypoints(kpts_data.copy(), 3, 4)

    n_obj1, n_obj2, n_obj3, n_obj4, n_obj5, n_obj6, n_obj7, n_obj8 = presence
    dc_content = []
    dc_format = ''
    order = list(order)
    d = {}

    if n_obj1:
        d['image'] = img
        dc_content.append(img)
        dc_format += 'I'
    else:
        del order[order.index('image')]
    if n_obj2:
        d['images'] = [img.copy() for _ in range(n_obj2)]
        dc_content.extend(d['images'])
        dc_format += 'I' * n_obj2
    else:
        del order[order.index('images')]
    if n_obj3:
        d['mask'] = mask
        dc_content.append(mask)
        dc_format += 'M'
    else:
        del order[order.index('mask')]
    if n_obj4:
        d['masks'] = [mask.copy() for i in range(n_obj4)]
        dc_content.extend(d['masks'])
        dc_format += 'M' * n_obj4
    else:
        del order[order.index('masks')]
    if n_obj5:
        d['keypoints'] = slc.Keypoints(kpts_data.copy(), 3, 4)
        dc_content.append(kpts)
        dc_format += 'P'
    else:
        del order[order.index('keypoints')]
    if n_obj6:
        d['keypoints_array'] = [
            slc.Keypoints(kpts_data.copy(), 3, 4) for _ in range(n_obj6)
        ]
        dc_content.extend(d['keypoints_array'])
        dc_format += 'P' * n_obj6
    else:
        del order[order.index('keypoints_array')]
    if n_obj7:
        d['label'] = 1
        dc_content.append(1)
        dc_format += 'L'
    else:
        del order[order.index('label')]
    if n_obj8:
        d['labels'] = [1 for _ in range(n_obj8)]
        dc_content.extend([1 for _ in range(n_obj8)])
        dc_format += 'L' * n_obj8
    else:
        del order[order.index('labels')]
    dc = slc.DataContainer(tuple(dc_content), dc_format)

    reordered_d = {k: d[k] for k in order}

    # This tests whether the creation from dict works as expected
    dc_reordered = slc.DataContainer.from_dict(reordered_d)

    return dc, dc_reordered