def test_save_pred_vs_label_7tuple_short_side_60_img(): """ When the image is too low in resolution (e.g. short side 60), we cannot use cv2's text rendering code. Instead, we will also save the upsampled version. """ short_side = 60 # pixels data_dir = f'{TEST_DATA_ROOT}/Camvid_test_data' img_fpath = f'{data_dir}/images/0016E5_08159.png' label_fpath = f'{data_dir}/preds/0016E5_08159.png' img_rgb = imageio.imread(img_fpath) label_img = imageio.imread(label_fpath) img_rgb = resize_img_by_short_side(img_rgb, short_side, img_type='rgb') label_img = resize_img_by_short_side(label_img, short_side, img_type='label') img_h, img_w = label_img.shape pred_img = np.random.randint(0, 200, (img_h, img_w)).astype(np.uint16) id_to_class_name_map = get_dataloader_id_to_classname_map( 'pascal-context-460') save_fpath = f'{TEST_DATA_ROOT}/rand_549_temp_small.png' save_pred_vs_label_7tuple(img_rgb, pred_img, label_img, id_to_class_name_map, save_fpath) os.remove( f'{TEST_DATA_ROOT}/rand_549_temp_small_upsample_pred_labels_palette.png' ) os.remove(f'{TEST_DATA_ROOT}/rand_549_temp_small_pred_labels_palette.png')
def test_save_pred_vs_label_7tuple_uniform_random_preds(): """ """ data_dir = f'{TEST_DATA_ROOT}/Camvid_test_data' img_fpath = f'{data_dir}/images/0016E5_08159.png' label_fpath = f'{data_dir}/preds/0016E5_08159.png' img_rgb = imageio.imread(img_fpath) label_img = imageio.imread(label_fpath) img_h, img_w = label_img.shape pred_img = np.random.randint(0, 459, (img_h, img_w)).astype(np.uint16) id_to_class_name_map = get_dataloader_id_to_classname_map( 'pascal-context-460') save_fpath = f'{TEST_DATA_ROOT}/rand_549_temp.png' save_pred_vs_label_7tuple(img_rgb, pred_img, label_img, id_to_class_name_map, save_fpath) os.remove(f'{TEST_DATA_ROOT}/rand_549_temp_pred_labels_palette.png')
def test_save_pred_vs_label_7tuple_shifted(): """ """ data_dir = f'{TEST_DATA_ROOT}/Camvid_test_data' img_fpath = f'{data_dir}/images/0016E5_08159.png' label_fpath = f'{data_dir}/preds/0016E5_08159.png' img_rgb = imageio.imread(img_fpath) label_img = imageio.imread(label_fpath) pred_img = np.zeros_like(label_img) pred_img[:, 150:] = label_img[:, :-150] id_to_class_name_map = get_dataloader_id_to_classname_map( 'pascal-context-460') save_fpath = f'{TEST_DATA_ROOT}/shifted_temp.png' save_pred_vs_label_7tuple(img_rgb, pred_img, label_img, id_to_class_name_map, save_fpath) os.remove(f'{TEST_DATA_ROOT}/shifted_temp_pred_labels_palette.png')
def evaluate_predictions(self, save_vis: bool = True) -> None: """ Calculate accuracy. Args: - data_list: - pred_folder: Returns: - None """ pred_folder = self.gray_folder for i, (image_path, target_path) in enumerate(self.data_list): if self.args.img_name_unique: image_name = Path(image_path).stem else: image_name = get_unique_stem_from_last_k_strs(image_path) pred = cv2.imread(os.path.join(pred_folder, image_name + '.png'), cv2.IMREAD_GRAYSCALE) target_img = imageio.imread(target_path) target_img = target_img.astype(np.int64) target_img = self.convert_label_to_pred_taxonomy(target_img) self.sam.update_metrics_cpu(pred, target_img, self.num_eval_classes) if (i + 1) % self.args.vis_freq == 0: print_str = f'Evaluating {i + 1}/{len(self.data_list)} on image {image_name+".png"},' + \ f' accuracy {self.sam.accuracy:.4f}.' logger.info(print_str) if save_vis: if (i + 1) % self.args.vis_freq == 0: mask_save_dir = pred_folder.replace( 'gray', 'rgb_mask_predictions') grid_save_fpath = f'{mask_save_dir}/{image_name}.png' rgb_img = cv2_imread_rgb(image_path) save_pred_vs_label_7tuple(rgb_img, pred, target_img, self.id_to_class_name_map, grid_save_fpath)
def test_save_pred_vs_label_7tuple_100_strided_preds(): """ """ data_dir = f'{TEST_DATA_ROOT}/Camvid_test_data' img_fpath = f'{data_dir}/images/0016E5_08159.png' label_fpath = f'{data_dir}/preds/0016E5_08159.png' img_rgb = imageio.imread(img_fpath) label_img = imageio.imread(label_fpath) img_h, img_w = label_img.shape class_ids_to_sample = np.array([0, 100, 200, 300]) pred_img = np.random.choice(a=class_ids_to_sample, size=(img_h, img_w)).astype(np.uint16) id_to_class_name_map = get_dataloader_id_to_classname_map( 'pascal-context-460') save_fpath = f'{TEST_DATA_ROOT}/100_strided_temp.png' save_pred_vs_label_7tuple(img_rgb, pred_img, label_img, id_to_class_name_map, save_fpath) os.remove(f'{TEST_DATA_ROOT}/100_strided_temp_pred_labels_palette.png')