def test_map_semantic_img_fast_pytorch_uint16_stress_test(): """ Test fast method on large example. Map 60,000 classes to a different 60,000 classes with a fast array conversion. np.int16 cannot hold anything over 32767 / 32768. Should take less than 1 millisecond. """ semantic_img = np.array(range(60000)).reshape(3000, 20).astype(np.int64) semantic_img = torch.from_numpy(semantic_img).type(torch.LongTensor) label_mapping = {} for i in range(60000): label_mapping[i] = i + 1 label_mapping_copy = copy.deepcopy(label_mapping) label_mapping_arr = form_label_mapping_array_pytorch(label_mapping) label_mapping_arr_copy = label_mapping_arr.clone() assert label_mapping == label_mapping_copy start = time.time() mapped_img = map_semantic_img_fast_pytorch(semantic_img, label_mapping_arr) end = time.time() assert torch.allclose(label_mapping_arr_copy, label_mapping_arr) assert mapped_img.shape == torch.Size([3000, 20]) assert mapped_img.dtype == torch.int64 print(f'Took {end-start} sec.') gt_mapped_img = np.array(range(60000)).reshape(3000, 20).astype( np.uint16) + 1 assert np.allclose(gt_mapped_img, mapped_img.numpy())
def test_map_semantic_img_fast_pytorch(): """ Test fast method on simple conversion from 2x3 grayscale -> 2x3 grayscale. """ semantic_img = np.array([[254, 0, 1], [7, 8, 9]], dtype=np.uint8) semantic_img = torch.from_numpy(semantic_img).type(torch.LongTensor) label_mapping = {254: 253, 0: 255, 1: 0, 7: 6, 8: 7, 9: 8} label_mapping_arr = form_label_mapping_array_pytorch(label_mapping) mapped_img = map_semantic_img_fast_pytorch(semantic_img, label_mapping_arr) gt_mapped_img = np.array([[253, 255, 0], [6, 7, 8]], dtype=np.uint8) assert np.allclose(gt_mapped_img, mapped_img.numpy()) assert mapped_img.dtype == torch.int64
def transform_label(self, label: torch.Tensor, dataset: str) -> Mapping[int, torch.Tensor]: """ Function to be called externally for training. Perform fast grayscale->grayscale mapping for training data transformation. Args: - label: Pytorch tensor on the cpu with dtype Torch.LongTensor, representing a semantic image, according to PNG/dataloader format. - dataset: string representing dataset's name Returns: - labels: vector label for each depth in the tree, compatible with softmax """ from mseg.utils.mask_utils import map_semantic_img_fast_pytorch label = map_semantic_img_fast_pytorch( label, self.label_mapping_arr_dict[dataset]) return label
def transform_label(self, label: torch.Tensor, dataset: str) -> Mapping[int, torch.Tensor]: """Perform fast grayscale->grayscale mapping for training data transformation. Note: Function to be called externally for training. Args: label: Pytorch tensor on the cpu of shape (H,W) with dtype Torch.LongTensor, representing a semantic image, according to PNG/dataloader format. dataset: string representing dataset's name Returns: label: tensor also of shape (H,W), representing semantic classes in new taxonomy at each pixel """ from mseg.utils.mask_utils import map_semantic_img_fast_pytorch label = map_semantic_img_fast_pytorch( label, self.label_mapping_arr_dict[dataset]) return label