Esempio n. 1
0
def save_prediction_visualization(
        pred_folder: str, image_path: str, image_name: str, pred: np.ndarray,
        target_img: np.ndarray, id_to_class_name_map: Mapping[int,
                                                              str]) -> None:
    """
        Args:
        -   pred_folder
        -   image_path
        -   pred
        -   target_img
        -   id_to_class_name_map

        Returns:
        -   None
    """
    image_name = Path(image_name).stem
    mask_save_dir = pred_folder.replace('gray', 'rgb_mask_predictions')
    grid_save_fpath = f'{mask_save_dir}/{image_name}.jpg'
    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)
    write_six_img_grid_w_embedded_names(rgb_img, pred, target_img,
                                        id_to_class_name_map, grid_save_fpath)

    overlaid_save_fpath = f'{mask_save_dir}_overlaid/{image_name}.jpg'
    create_leading_fpath_dirs(overlaid_save_fpath)
    frame_visualizer = Visualizer(rgb_img, metadata=None)
    overlaid_img = frame_visualizer.overlay_instances(
        label_map=pred, id_to_class_name_map=id_to_class_name_map)
    imageio.imwrite(overlaid_save_fpath, overlaid_img)
Esempio n. 2
0
def read_resize_write_rgb(old_fpath: str, new_fpath: str, short_side_sz) -> None:
	"""
	Args:
	    old_fpath
	    new_fpath
	"""
	img_rgb = cv2_imread_rgb(old_fpath)
	img_rgb_resized = resize_img_by_short_side(img_rgb, short_side_len=short_side_sz, img_type='rgb')
	cv2.imwrite(new_fpath, img_rgb_resized[:,:,::-1])
Esempio n. 3
0
def relabel_pair(old_dataroot: str,
                 new_dataroot: str,
                 orig_pair: Tuple[str, str],
                 remapped_pair: Tuple[str, str],
                 dname: str,
                 tax_converter: TaxonomyConverter,
                 segm_to_class: Mapping[int, int],
                 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 not os.path.exists(old_label_fpath):
        print("Warning: File " + old_label_fpath + " not found!")
        return

    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)

    if not segm_to_class is None:
        label_img_id = label_img[:, :, 0] + (label_img[:, :, 1] * 256) + (
            label_img[:, :, 2] * 256**2)
        label_img = np.ones(label_img.shape[:2],
                            dtype=np.uint8) * 255  #initialize with unlabeled
        for src, dst in segm_to_class.items():
            label_img[label_img_id == src] = dst

    labels = torch.tensor(label_img, dtype=torch.int64)
    remapped_img = tax_converter.transform_label(labels, dname)

    new_label_fpath = f'{new_dataroot}/{remapped_rel_label_fpath}'
    create_leading_fpath_dirs(new_label_fpath)
    remapped_img = remapped_img.numpy().astype(dtype=np.uint8)
    imageio.imwrite(new_label_fpath, remapped_img)
Esempio n. 4
0
def dump_img_masks(highlight_classname: str, split: str, rgb_img_fpath: str,
                   folder_prefix: str, api: MapillaryMaskDataset):
    """
	"""
    # print(f'On image {i}/{len(rgb_img_fpaths)}')
    rgb_img = cv2_imread_rgb(rgb_img_fpath)
    fname_stem = Path(rgb_img_fpath).stem
    label_rgb_fpath = f'{api.dataroot}/{split}/labels/{fname_stem}.png'
    label_img_rgb = cv2_imread_rgb(label_rgb_fpath)
    label_img = api.labelrgb_to_label(label_img_rgb)

    present_classnames = [
        api.id_to_classname_map[id] for id in np.unique(label_img)
    ]
    if highlight_classname not in present_classnames:
        return

    instance_img_fpath = f'{api.dataroot}/{split}/instances/{fname_stem}.png'
    instance_img = imageio.imread(instance_img_fpath)
    instance_ids = np.unique(instance_img)

    for instance_id in instance_ids:
        instance_mask = (instance_img == instance_id).astype(np.uint8)

        is_single_class, sem_class_ids = mask_belongs_single_semantic_class(
            instance_mask, label_img)
        assert is_single_class
        instance_classid = int(sem_class_ids)

        instance_classname = api.id_to_classname_map[int(sem_class_ids)]
        if instance_classname != highlight_classname:  # not in required_class_names:
            continue

        save_fpath = f'temp_files/{folder_prefix}_{split}_2020_04_18/mapillary_{fname_stem}_{instance_id}.jpg'
        save_binary_mask_double(rgb_img,
                                instance_mask,
                                save_fpath,
                                save_to_disk=True)
Esempio n. 5
0
def read_resize_write_rgb(old_fpath, new_fpath, short_side_sz):
    """
		Args:
		-	old_fpath
		-	new_fpath

		Returns:
		-	None
	"""
    img_rgb = cv2_imread_rgb(old_fpath)
    img_rgb_resized = resize_img_by_short_side(img_rgb,
                                               short_side_len=short_side_sz,
                                               img_type='rgb')
    cv2.imwrite(new_fpath, img_rgb_resized[:, :, ::-1])
Esempio n. 6
0
def visual_sanitychecks():
    """
	Save every 1000th image of each dataset, with classnames embedded.
	"""
    save_dir = 'temp_files/MSeg_verify'

    for dname, d_info in infos.items():
        print(f'Writing visual sanity checks for {dname}...')

        if dname in [
                'coco-panoptic-inst-201', 'mapillary-public66',
                'ade20k-151-inst'
        ]:
            continue  # is in RGB format and not comparable

        id_to_classname_map = get_dataloader_id_to_classname_map(dname)
        splits = ['train', 'val']
        split_lists = [d_info.trainlist, d_info.vallist]

        for split, split_list in zip(splits, split_lists):
            pairs = generate_all_img_label_pair_fpaths(d_info.dataroot,
                                                       split_list)

            # Save 5 examples from each dataset split
            step_sz = math.floor(len(pairs) // 5)
            for i, (rgb_fpath, label_fpath) in enumerate(pairs[::step_sz]):
                print(f'On {i} of {dname}')

                rgb_img = cv2_imread_rgb(rgb_fpath)
                label_img = imageio.imread(label_fpath)

                fname_stem = Path(rgb_fpath).stem
                save_fpath = f'{save_dir}/{dname}_{fname_stem}.jpg'
                blend_save_fpath = f'{save_dir}/{dname}_{fname_stem}_blended.jpg'

                if rgb_img.ndim == 2:
                    # this image was grayscale
                    rgb_img = grayscale_to_color(rgb_img)

                form_mask_triple_embedded_classnames(rgb_img,
                                                     label_img,
                                                     id_to_classname_map,
                                                     save_fpath,
                                                     save_to_disk=True)
                frame_visualizer = Visualizer(rgb_img, metadata=None)
                output_img = frame_visualizer.overlay_instances(
                    label_map=label_img,
                    id_to_class_name_map=id_to_classname_map)
                imageio.imwrite(blend_save_fpath, output_img)
Esempio n. 7
0
    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)
Esempio n. 8
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)