Exemple #1
0
def get_best_segmentation(groundtruth_segmentation, inferred_segmentation):
    """Remap inferred segmentation onto ground truth segmentation so that it
    matches the groundtruth_segmentation as good as possible

    Parameters
    ----------
    groundtruth_segmentation : np.ndarray
        Ground truth segmentation label map[N, H, W]

    inferred_segmentation : np.ndarray
        Inferred segmentation label map [N, H, W]
    Returns
    -------
    np.ndarray
        remapped inferred segmentation
    """

    best_remapping = denseposelib.compute_best_iou_remapping(
        inferred_segmentation, groundtruth_segmentation)
    remapped_inferred = denseposelib.remap_parts(inferred_segmentation,
                                                 best_remapping)
    return remapped_inferred
Exemple #2
0
def make_figure_1(data: dict, root: str, config: dict, global_step: int):
    figure01_options = config.get("figure01_options")
    dp_semantic_remap_dict = config.get("dp_semantic_remap_dict")
    dp_new_part_list = sorted(list(dp_semantic_remap_dict.keys()))
    dp_remap_dict = denseposelib.semantic_remap_dict2remap_dict(
        dp_semantic_remap_dict, dp_new_part_list)

    inferred_segmentation = (
        data["outputs"][figure01_options["inferred_segmentation_key"]] + 1
    )  # +1 because the visualizer code uses + 1
    sampled_segmentation = data["outputs"][
        figure01_options["sampled_mask_key"]]
    images = data["inputs"][figure01_options["input_view_key"]]
    generated = data["outputs"][figure01_options["generated_image_key"]]
    groundtruth_segmentation = data["batches"][
        figure01_options["gt_segmentation_key"]]
    groundtruth_segmentation = denseposelib.resize_labels(
        groundtruth_segmentation, (128, 128))
    remapped_gt_segmentation = denseposelib.remap_parts(
        groundtruth_segmentation, dp_remap_dict)
    best_remapping = denseposelib.compute_best_iou_remapping(
        inferred_segmentation, remapped_gt_segmentation)
    remapped_inferred = denseposelib.remap_parts(inferred_segmentation,
                                                 best_remapping)

    ncols = 7
    n_inferred_parts = config.get("n_inferred_parts", 10)
    colors = make_mask_colors(len(set(dp_new_part_list)), background_id=1)

    df = pd.DataFrame(columns=["global_step", "batch_idx"] + dp_new_part_list)

    for i in range(
            len(inferred_segmentation)
    ):  # TODO: maybe replace this amount of plots by parameters in the config file
        image_container = []

        # remap inferred segmentation
        old_inferred = inferred_segmentation[i]
        current_sampled_segmentation = np.argmax(sampled_segmentation[i], -1)
        old_inferred_colors = make_mask_colors(n_inferred_parts,
                                               background_id=1)

        image_container.append(old_inferred_colors[old_inferred - 1])
        image_container.append(
            old_inferred_colors[current_sampled_segmentation])

        new_inferred = remapped_inferred[i]
        current_gt_segmentation = remapped_gt_segmentation[i]

        # remap GT segmentation
        iou, iou_labels = denseposelib.compute_iou(new_inferred,
                                                   current_gt_segmentation)

        # filter out background
        iou_filter = np.ones_like(iou) == 1.0
        iou_filter[iou_labels == dp_new_part_list.index("background")] = False

        df_update = {p: -1.0 for p in dp_new_part_list}
        df_update.update({
            p: float(np.squeeze(iou[pi == iou_labels]))
            for pi, p in enumerate(dp_new_part_list) if pi in iou_labels
        })
        df_update.update({"batch_idx": i, "global_step": global_step})

        df = df.append(df_update, ignore_index=True)

        filtered_iou = iou[iou_filter]
        mean_iou = np.mean(filtered_iou)

        image_container.append(colors[new_inferred])
        image_container.append(colors[current_gt_segmentation])

        legend_labels = []
        for pi, p in enumerate(dp_new_part_list):
            if pi in iou_labels:
                p_iou = np.squeeze(iou[np.argwhere(iou_labels == pi)])
            else:
                p_iou = 0.0
            legend_labels.append(p + " - IOU : {:.03f}".format(p_iou))
        legend_labels.append("mIOU (no BG) : {:.03f}".format(mean_iou))
        colors = np.concatenate([colors, np.reshape([0, 0, 0], (1, 3))],
                                axis=0)
        text_colors = [1, 1, 1] * len(colors)
        legend_image = utils.make_legend_image(legend_labels, colors,
                                               text_colors, (128, 128), 1)
        image_container.append(legend_image)

        current_image = images[i]
        current_generated = generated[i]

        image_container.append(
            imageutils.convert_range(current_image, [-1, 1], [0, 1]))
        image_container.append(
            imageutils.convert_range(current_generated, [-1, 1], [0, 1]))

        # write files
        out_path = os.path.join(root, "figure_01")
        os.makedirs(out_path, exist_ok=True)
        out_image = np.stack(image_container)
        out_image = imageutils.convert_range(out_image, [0, 1], [-1, 1])
        plot_batch(out_image,
                   os.path.join(out_path, "{:06d}.png".format(i)),
                   cols=ncols)

    df.to_csv(os.path.join(root, "part_ious.csv"), index=False, header=True)

    df_mean = df[df != -1].mean().to_frame().transpose()
    with open(os.path.join(root, "mean_part_ios.csv"), "w") as f:
        print(
            tabulate(df_mean,
                     headers="keys",
                     tablefmt="psql",
                     showindex="never"),
            file=f,
        )