Exemple #1
0
def test_map_semantic_img_fast_uint16_stress_test():
    """
	Test fast method on large example. Map 60,000 classes to a 
	different 60,000 classes with a fast array conversion.

	Should take less than 1 millisecond.
	"""
    semantic_img = np.array(range(60000)).reshape(3000, 20).astype(np.uint16)
    # will expect uint16 back since 60000 < 65535, which is uint16 max
    gt_mapped_img1 = np.array(range(60000)).reshape(3000, 20).astype(
        np.uint16) + 1

    label_mapping = {i: i + 1 for i in range(60000)}

    label_mapping_copy = copy.deepcopy(label_mapping)
    label_mapping_arr = form_label_mapping_array(label_mapping)
    dict_is_equal(label_mapping, label_mapping_copy)
    assert label_mapping == label_mapping_copy
    start = time.time()
    mapped_img = map_semantic_img_fast(semantic_img, label_mapping_arr)
    end = time.time()
    print(f'Took {end-start} sec.')

    gt_mapped_img2 = map_semantic_img_slow(semantic_img, label_mapping)

    assert np.allclose(gt_mapped_img1, mapped_img)
    assert np.allclose(gt_mapped_img2, mapped_img)
    assert gt_mapped_img1.dtype == mapped_img.dtype
    assert gt_mapped_img2.dtype == mapped_img.dtype
Exemple #2
0
def test_map_semantic_img_fast_dontwidenvalue():
    """
	Test fast method on simple conversion from 2x3 grayscale -> 2x3 grayscale.
	"""
    semantic_img = np.array([[300, 301, 302], [300, 301, 302]],
                            dtype=np.uint16)

    label_mapping = {300: 0, 301: 1, 302: 2}
    label_mapping_arr = form_label_mapping_array(label_mapping)
    mapped_img = map_semantic_img_fast(semantic_img, label_mapping_arr)
    # Expect uint8 since max class index in values <= 255, so uint16 unnecessary
    gt_mapped_img1 = np.array([[0, 1, 2], [0, 1, 2]], dtype=np.uint8)

    assert np.allclose(gt_mapped_img1, mapped_img)
    assert mapped_img.dtype == np.uint8
Exemple #3
0
def test_map_semantic_img_fast_widenvalue():
    """
	Test fast method on simple conversion from 2x3 grayscale -> 2x3 grayscale.
	"""
    semantic_img = np.array([[255, 255, 255], [255, 255, 255]], dtype=np.uint8)

    label_mapping = {255: 256}
    label_mapping_arr = form_label_mapping_array(label_mapping)
    mapped_img = map_semantic_img_fast(semantic_img, label_mapping_arr)

    # Expect uint8 since max class index <= 255, so uint16 unnecessary
    gt_mapped_img1 = np.array([[256, 256, 256], [256, 256, 256]],
                              dtype=np.uint16)

    assert np.allclose(gt_mapped_img1, mapped_img)
    assert mapped_img.dtype == np.uint16
Exemple #4
0
def test_map_semantic_img_fast():
    """
	Test fast method on simple conversion from 2x3 grayscale -> 2x3 grayscale.
	"""
    semantic_img = np.array([[254, 0, 1], [7, 8, 9]], dtype=np.uint8)

    label_mapping = {254: 253, 0: 255, 1: 0, 7: 6, 8: 7, 9: 8}
    label_mapping_arr = form_label_mapping_array(label_mapping)
    mapped_img = map_semantic_img_fast(semantic_img, label_mapping_arr)

    # Expect uint8 since max class index <= 255, so uint16 unnecessary
    gt_mapped_img1 = np.array([[253, 255, 0], [6, 7, 8]], dtype=np.uint8)
    gt_mapped_img2 = map_semantic_img_slow(semantic_img, label_mapping)

    assert np.allclose(gt_mapped_img1, mapped_img)
    assert np.allclose(gt_mapped_img2, mapped_img)
    assert gt_mapped_img1.dtype == mapped_img.dtype
    assert gt_mapped_img2.dtype == mapped_img.dtype
Exemple #5
0
def relabel_pair(old_dataroot: str,
                 new_dataroot: str,
                 orig_pair: Tuple[str, str],
                 remapped_pair: Tuple[str, str],
                 label_mapping_arr: np.ndarray,
                 dataset_colors: Optional[np.ndarray] = None):
    """
	No need to copy the RGB files again. We just update the label file paths.

		Args:
		-	old_dataroot:
		-	new_dataroot: 
		-	orig_pair: Tuple containing relative path to RGB image and label image
		-	remapped_pair: Tuple containing relative path to RGB image and label image
		-	label_mapping_arr: 
		-	dataset_colors:

		Returns:
		-	None
	"""
    _, orig_rel_label_fpath = orig_pair
    _, remapped_rel_label_fpath = remapped_pair

    old_label_fpath = f'{old_dataroot}/{orig_rel_label_fpath}'

    if dataset_colors is None:
        label_img = imageio.imread(old_label_fpath)
    else:
        # remap from RGB encoded labels to 1-channel class indices
        label_img_rgb = cv2_imread_rgb(old_label_fpath)
        label_img = rgb_img_to_obj_cls_img(label_img_rgb, dataset_colors)
    remapped_img = map_semantic_img_fast(label_img, label_mapping_arr)

    new_label_fpath = f'{new_dataroot}/{remapped_rel_label_fpath}'
    create_leading_fpath_dirs(new_label_fpath)
    imageio.imwrite(new_label_fpath, remapped_img)