def test_zoom_x_axis_odd(img_5x5): stream = slt.RandomScale(range_x=(0.5, 0.5), range_y=(1, 1), same=False, p=1) dc = sld.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]
def test_data_dep_trf_raises_value_error_when_imgs_are_of_different_size( img_1, img_2): trf = slt.ImageSaltAndPepper(gain_range=0., p=1) with pytest.raises(ValueError): trf( sld.DataContainer(( 1, img_1().astype(np.uint8), img_2().astype(np.uint8), ), 'LII'))
def parse_item_test(root, entry, transform, data_key, target_key): img = cv2.imread(str(entry.fname), 0) # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) dc = sld.DataContainer((img, ), 'I', transform_settings={0: {'interpolation': 'bilinear'}}) img = transform(dc)[0] img = torch.cat([img, img, img], 0) / 255. img = img.permute(2, 0, 1) / 255. return {data_key: img}
def __getitem__(self, i): entry = self.meta.iloc[i] label = entry.Fracture side = side_index[entry.Side] file_name = os.path.join(self.root, side, f'{entry.ID}_{side}.png') img = cv2.imread(file_name) dc = sld.DataContainer((img, label), 'IL') img, target = self.transform(dc) # img, label = self.transform((img, label)) # return {'img': img, 'label': label, 'fname': entry.ID} return img, label
def test_keypoints_horizontal_flip_within_stream(): kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2)) kpts = sld.KeyPoints(kpts_data, 2, 2) stream = slc.Stream([slt.RandomFlip(p=1, axis=1)]) dc = sld.DataContainer((kpts, ), 'P') dc_res = stream(dc) assert np.array_equal( dc_res[0][0].data, np.array([[1, 0], [1, 1], [0, 0], [0, 1]]).reshape((4, 2)))
def wrap2solt(inp_data): img, entry = inp_data if entry.SIDE == 1: # Left img = cv2.flip(img, 1) data_c_content = (img, entry.XRKL, entry.XROSTL, entry.XROSFL, entry.XRJSL, entry.XROSTM, entry.XROSFM, entry.XRJSM) dc = sld.DataContainer(data_c_content, 'ILLLLLLL') return dc
def wrap_img_target_solt(img, target): if not isinstance(img, np.ndarray): raise TypeError if len(img.shape) == 2: img = img.reshape((img.shape[0], img.shape[1], 1)) if len(img.shape) != 3: raise ValueError return sld.DataContainer((img, target), fmt='IL')
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 = sld.KeyPoints(kpts_data, 3, 4) dc = sld.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]
def wrap_solt(entry): return sld.DataContainer(entry, 'II', transform_settings={ 0: { 'interpolation': 'bilinear' }, 1: { 'interpolation': 'bilinear' } })
def wrap_slt(img, annotator_type='lc'): if annotator_type == 'lc': img = np.dstack((img, img, img)) row, col, _ = img.shape # right, left encoding img = (img[:, :col // 2 + col % 2], img[:, col // 2:]) else: img_right = np.dstack((img[0], img[0], img[0])) img_left = np.dstack((img[1], img[1], img[1])) img = (img_right, img_left) return sld.DataContainer((img[0], img[1]), 'II')
def test_selective_stream_low_prob_transform_should_not_change_the_data( img_5x5): img = img_5x5 dc = sld.DataContainer((img, ), 'I') ppl = slc.SelectiveStream([ slt.RandomRotate(rotation_range=(90, 90), p=0), slt.RandomRotate(rotation_range=(-90, -90), p=0) ]) dc_res = ppl(dc) np.array_equal(dc.data, dc_res.data)
def test_transform_returns_original_data_if_not_in_specified_indices(img_2x2, img_3x3, img_3x4, img_5x5): kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape((4, 2)) kpts = sld.KeyPoints(kpts_data, 3, 3) dc = sld.DataContainer((img_2x2, img_3x3, img_3x4, img_5x5, 1, kpts, 2), 'IIIILPL') trf = slt.RandomFlip(p=1, data_indices=(0, 1, 4)) res = trf(dc) assert np.linalg.norm(res.data[0]-img_2x2) > 0 assert np.linalg.norm(res.data[1]-img_3x3) > 0 np.testing.assert_array_equal(res.data[2], img_3x4) np.testing.assert_array_equal(res.data[3], img_5x5) assert res.data[-1] == 2 np.testing.assert_array_equal(res.data[5].data, kpts_data)
def test_img_mask_horizontal_flip(img_3x4, mask_3x4): img, mask = img_3x4, mask_3x4 dc = sld.DataContainer((img, mask), 'IM') stream = slc.Stream([slt.RandomFlip(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(img, 1).reshape(h, w, 1), img_res) assert np.array_equal(cv2.flip(mask, 1), mask_res)
def test_img_mask_vertical_horizontal_flip_negative_axes(img_3x4, mask_3x4): img, mask = img_3x4, mask_3x4 dc = sld.DataContainer((img, mask), 'IM') stream = slt.RandomFlip(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)
def test_cutout_1x1_blacks_corner_pixels_2x2_img(img_2x2): dc = sld.DataContainer((img_2x2.copy(), ), 'I') trf = slc.Stream([slt.ImageCutOut(p=1, cutout_size=1)]) dc_res = trf(dc) 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
def wrap_img_landmarks_solt(img, landmarks): if not isinstance(img, np.ndarray): raise TypeError if not isinstance(landmarks, np.ndarray): raise TypeError if len(img.shape) == 2: img = img.reshape((img.shape[0], img.shape[1], 1)) if len(img.shape) != 3 or landmarks.shape[1] != 2: raise ValueError return sld.DataContainer((img, sld.KeyPoints(landmarks, img.shape[0], img.shape[1])), fmt='IP')
def test_random_proj_and_selective_stream(img_5x5): img = img_5x5 dc = sld.DataContainer((img, ), 'I') ppl = slt.RandomProjection(slc.SelectiveStream([ slt.RandomRotate(rotation_range=(90, 90), p=0), slt.RandomScale(range_y=(0, 0.1), same=True, p=0), slt.RandomShear(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)
def test_hsv_doesnt_change_an_image(h_range, s_range, v_range, img_6x6): trf = slt.ImageRandomHSV(p=1, h_range=h_range, s_range=s_range, v_range=v_range) img_rgb = np.dstack((img_6x6, img_6x6, img_6x6)).astype(np.uint8) * 255 dc = sld.DataContainer(img_rgb, 'I') dc_res = trf(dc) assert 0 == trf.state_dict['h_mod'] assert 0 == trf.state_dict['s_mod'] assert 0 == trf.state_dict['v_mod'] np.testing.assert_array_equal(img_rgb, dc_res.data[0])
def test_selective_pipeline_selects_transforms_and_does_the_fusion(): ppl = slc.SelectiveStream([ slt.RandomRotate(rotation_range=(90, 90), p=1), slt.RandomRotate(rotation_range=(-90, -90), p=1), ], n=2, probs=[0.5, 0.5]) kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2)) kpts = sld.KeyPoints(kpts_data, 3, 4) dc = sld.DataContainer(kpts, 'P') dc_res = ppl(dc) assert np.array_equal(np.eye(3), ppl.transforms[0].state_dict['transform_matrix'])
def test_brightness_returns_correct_number_of_channels(img_3x4, img_6x6_rgb): trf = slt.ImageRandomBrightness(p=1, brightness_range=(10, 10)) dc = sld.DataContainer((img_3x4, img_3x4, img_6x6_rgb), 'III') dc_res = trf(dc) img1, img2, img3 = dc_res.data assert len(img1.shape) == 3 assert img1.shape[-1] == 1 assert len(img2.shape) == 3 assert img2.shape[-1] == 1 assert len(img3.shape) == 3 assert img3.shape[-1] == 3
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 = sld.KeyPoints(kpts_data, 3, 3) img, mask = img_3x3, mask_3x3 dc = sld.DataContainer(( img, mask, kpts, ), 'IMP') # Defining the 90 degrees transform (clockwise) stream = slt.RandomRotate(rotation_range=(20, 20), p=1, padding='r') with pytest.raises(ValueError): stream(dc)
def test_different_interpolations_per_item_per_transform( img_6x6, transform_settings): dc = sld.DataContainer((img_6x6, ), 'I', transform_settings=transform_settings) dc_res = slt.ResizeTransform(resize_to=(10, 15), interpolation='bilinear')(dc) interp = allowed_interpolations['bilinear'] if transform_settings is not None: interp = allowed_interpolations[transform_settings[0]['interpolation'] [0]] assert np.array_equal( cv2.resize(img_6x6, (10, 15), interpolation=interp).reshape(15, 10, 1), dc_res.data[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 = sld.KeyPoints(kpts_data.copy(), 2, 2) dc = sld.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) assert np.array_equal(dc_res.data[0].data, np.array([exp_x, exp_y]).reshape((1, 2)))
def wrap_solt(entry): """ Converts the image into Solt format :param entry: Input image :return: Image in Solt format """ return sld.DataContainer(entry, 'IM', transform_settings={ 0: { 'interpolation': 'bilinear' }, 1: { 'interpolation': 'nearest' } })
def test_matrix_transforms_use_cache_for_different_dc_items_raises_error( img_5x5, mask_3x4, pipeline): dc = sld.DataContainer((img_5x5, mask_3x4), 'IM') if pipeline: ppl = slc.Stream([ slt.RandomRotate(rotation_range=(-180, 180), p=1, ignore_state=False), slt.PadTransform(pad_to=(10, 10)), ]) else: ppl = slt.RandomRotate(rotation_range=(-180, 180), p=1, ignore_state=False) with pytest.raises(ValueError): ppl(dc)
def test_image_shape_equal_3_after_nested_flip(img_3x4): img = img_3x4 dc = sld.DataContainer((img, ), 'I') stream = slc.Stream([ slt.RandomFlip(p=1, axis=0), slt.RandomFlip(p=1, axis=1), slc.Stream([ slt.RandomFlip(p=1, axis=1), slt.RandomFlip(p=1, axis=0), ]) ]) dc = stream(dc) img_res, _, _ = dc[0] assert np.array_equal(len(img.shape), 3)
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 = sld.KeyPoints(kpts_data, 3, 3) img, mask = img_3x3, mask_3x3 dc = sld.DataContainer((kpts, img, mask), 'PIM') transf = slt.PadTransform((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].H == 20) and (res[0][0].W == 20) assert np.array_equal( res[0][0].data, np.array([[8, 8], [8, 10], [10, 10], [10, 8]]).reshape((4, 2)))
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 = sld.KeyPoints(kpts_data, 3, 3) img, mask = img_3x3, mask_3x3 H, W = mask.shape dc = sld.DataContainer( (img, mask, kpts, 1), 'IMPL', transform_settings=copy.deepcopy(transform_settings)) # Defining the 90 degrees transform (clockwise) stream = slt.RandomRotate(rotation_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
def test_6x6_pad_to_20x20_center_crop_6x6_kpts_img(img_6x6): # Setting up the data kpts_data = np.array([[0, 0], [0, 5], [1, 3], [2, 0]]).reshape((4, 2)) kpts = sld.KeyPoints(kpts_data, 6, 6) img = img_6x6 dc = sld.DataContainer((kpts, img), 'PI') stream = slc.Stream( [slt.PadTransform((20, 20)), slt.CropTransform((6, 6))]) res = stream(dc) assert (res[1][0].shape[0] == 6) and (res[1][0].shape[1] == 6) assert (res[0][0].H == 6) and (res[0][0].W == 6) assert np.array_equal(res[1][0], img) assert np.array_equal(res[0][0].data, kpts_data)
def test_rotate_90_img_mask_nondestructive(k, img_3x3, mask_3x3): # Setting up the data img, mask = img_3x3, mask_3x3 H, W = mask.shape dc = sld.DataContainer((img, mask), 'IM') # Defining the 90 degrees transform (counterclockwise) stream = slt.RandomRotate90(k=k, p=1) dc_res = stream(dc) img_res, _, _ = dc_res[0] mask_res, _, _ = dc_res[1] expected_img_res = np.rot90(img, -k).reshape((H, W, 1)) expected_mask_res = np.rot90(mask, -k) assert np.array_equal(expected_img_res, img_res) assert np.array_equal(expected_mask_res, mask_res)