Beispiel #1
0
def visualize_algo_result(scene, algo_dir, tgt_dir, add_pfms_to_result):
    algo_result = misc.get_algo_result_from_dir(algo_dir, scene)

    # visualize
    fig = init_figure()
    cm = plt.imshow(algo_result, **settings.disp_map_args(scene))
    add_colorbar(cm, bins=8)

    # save fig
    relative_fname_thumb = get_relative_path(scene, "dispmap")
    fpath = op.normpath(op.join(tgt_dir, relative_fname_thumb))
    plotting.save_tight_figure(fig, fpath, hide_frames=True, pad_inches=0.01)

    # path info
    height, width = np.shape(algo_result)[:2]
    disp_map_data = {
        "thumb": relative_fname_thumb,
        "channels": 3,
        "height": height,
        "width": width
    }

    # save raw disparity map
    if add_pfms_to_result and not scene.is_test():
        relative_fname_raw = get_relative_path(scene,
                                               "dispmap",
                                               file_type="pfm")
        fpath_tgt = op.normpath(op.join(tgt_dir, relative_fname_raw))
        fpath_src = misc.get_fname_algo_result(algo_dir, scene)
        log.info("Copying disp map file from %s to %s" %
                 (fpath_src, fpath_tgt))
        shutil.copyfile(fpath_src, fpath_tgt)
        disp_map_data["raw"] = relative_fname_raw

    return disp_map_data
def _plot_normals_entry(scene, disp_map, gt, mask_planes, mask_contin,
                        algo_name, metric_mae_contin, metric_mae_planes, idx,
                        grid, entries_per_row, n_vis_types, cols,
                        colorbar_args, fs):

    add_ylabel = not idx % entries_per_row  # is first column
    add_colorbar = not (idx + 1) % entries_per_row  # is last column
    idx_row = (idx / entries_per_row) * n_vis_types
    idx_col = idx % entries_per_row

    idx = idx_row * cols + idx_col

    # plot disparity map
    plt.subplot(grid[idx])
    cb = plt.imshow(disp_map, **settings.disp_map_args(scene))
    plt.title(algo_name, fontsize=fs)

    if add_ylabel:
        plt.ylabel("DispMap", fontsize=fs)
    if add_colorbar:
        plotting.add_colorbar(grid[idx + 1], cb, **colorbar_args)

    # plot normal map
    plt.subplot(grid[idx + cols])
    plt.imshow(scene.get_normal_vis_from_disp_map(disp_map))

    if add_ylabel:
        plt.ylabel("Normals", fontsize=fs)

    # plot angular errors
    plt.subplot(grid[idx + 2 * cols])

    # compute angular errors
    try:
        score_contin = "%0.2f" % metric_mae_contin.get_score(
            disp_map, gt, scene)
    except IOError:
        score_contin = "-"

    try:
        score_planes = "%0.2f" % metric_mae_planes.get_score(
            disp_map, gt, scene)
    except IOError:
        score_planes = "-"

    plt.title("%s / %s" % (score_contin, score_planes), fontsize=fs)

    if add_ylabel:
        plt.ylabel("Angular Error", fontsize=fs)

    # get combined error visualization (if applicable)
    mask = mask_contin + mask_planes
    if np.sum(mask) > 0:
        score, vis_normals = metric_mae_contin.get_score_from_mask(
            disp_map, gt, scene, mask, with_visualization=True)
        cb = plt.imshow(vis_normals, **settings.metric_args(metric_mae_contin))

        if add_colorbar:
            plotting.add_colorbar(grid[idx + 2 * cols + 1], cb,
                                  **colorbar_args)
Beispiel #3
0
    def plot_algo_vis_for_metric(self, metric, algo_result, gt, mask, hide_gt=False, fontsize=10):
        score, vis = metric.get_score(algo_result, gt, self, with_visualization=True)

        if hide_gt and metric.pixelize_results():
            vis = plotting.pixelize(vis)

        # plot algorithm disparity map as background
        plt.imshow(algo_result, **settings.disp_map_args(self, cmap="gray"))

        # plot masked metric visualization on top
        cm = plt.imshow(np.ma.masked_array(vis, mask=~mask), **settings.metric_args(metric))
        plt.title(metric.format_score(score), fontsize=fontsize)

        return cm
Beispiel #4
0
def plot_benchmark_scene_overview(benchmark_scenes, subdir="overview", fs=16):
    # prepare grid figure
    rows, cols = 2, 12
    fig = plt.figure(figsize=(21.6, 4))
    grids = plotting.get_grid(rows, cols)

    # plot center view and ground truth for each scene
    for idx_s, scene in enumerate(benchmark_scenes):

        center_view = scene.get_center_view()
        plt.subplot(grids[idx_s])
        plt.imshow(center_view)
        plt.title("\n\n" + scene.get_display_name(), fontsize=fs)

        try:
            gt = scene.get_gt()
            plt.subplot(grids[cols + idx_s])
            if scene.hidden_gt():
                gt = plotting.pixelize(gt, noise_factor=0.5)
            plt.imshow(gt, **settings.disp_map_args(scene))
        except IOError as e:
            # skip potentially missing ground truth of test scenes
            log.warning(e)
            continue

    # add text
    height = 785
    plt.gca().annotate("(a) Stratified Scenes", (400, 420), (500, height),
                       fontsize=fs,
                       xycoords='figure pixels')
    plt.gca().annotate("(b) Training Scenes", (400, 420), (1910, height),
                       fontsize=fs,
                       xycoords='figure pixels')
    plt.gca().annotate("(c) Test Scenes (Hidden Ground Truth)", (400, 420),
                       (3070, height),
                       fontsize=fs,
                       xycoords='figure pixels')

    # save figure
    fig_path = plotting.get_path_to_figure("benchmark_scenes", subdir=subdir)
    plotting.save_tight_figure(fig,
                               fig_path,
                               hide_frames=True,
                               hspace=0.02,
                               wspace=0.02,
                               dpi=200)
Beispiel #5
0
def save_visualization(algo_result, metric_vis, metric, scene, tgt_dir):
    fig = init_figure()

    # algorithm result as background
    plt.imshow(algo_result, **settings.disp_map_args(scene, cmap="gray"))

    # metric visualization on top
    if scene.hidden_gt() and metric.pixelize_results() and settings.PIXELIZE:
        metric_vis = plotting.pixelize(metric_vis, noise_factor=0.05)
    cm = plt.imshow(metric_vis, **settings.metric_args(metric))
    add_colorbar(cm, metric.colorbar_bins)

    # save fig
    relative_fname = get_relative_path(scene, metric.get_id())
    fpath = op.normpath(op.join(tgt_dir, relative_fname))
    plotting.save_tight_figure(fig, fpath, hide_frames=True, pad_inches=0.01)

    return relative_fname
Beispiel #6
0
    def plot_metric_rows(self, grids, algorithms, metrics, offset, fontsize):
        gt = self.get_gt()
        center_view = self.get_center_view()

        for idx_a, algorithm in enumerate(algorithms):
            log.info("Algorithm: %s" % algorithm)
            algo_result = misc.get_algo_result(algorithm, self)

            # add algorithm disparity map
            plt.sca(grids[0][idx_a + 1])
            cm = plt.imshow(algo_result, **settings.disp_map_args(self))
            plt.title(algorithm.get_display_name(), fontsize=fontsize)

            # add colorbar to last disparity map in row
            if idx_a == (len(algorithms) - 1):
                plotting.create_colorbar(cm, cax=grids[0].cbar_axes[0],
                                         colorbar_bins=7, fontsize=fontsize)

            # add algorithm metric visualizations
            for idx_m, metric in enumerate(metrics):
                log.info(metric.get_display_name())
                mask = metric.get_evaluation_mask(self)

                plt.sca(grids[idx_m + offset + 1][idx_a + 1])
                cm = self.plot_algo_vis_for_metric(metric, algo_result, gt, mask,
                                                   self.hidden_gt(), fontsize)

                # add colorbar to last metric visualization in row
                if idx_a == len(algorithms) - 1:
                    plotting.create_colorbar(cm, cax=grids[idx_m + offset + 1].cbar_axes[0],
                                             colorbar_bins=metric.colorbar_bins, fontsize=fontsize)

                # add mask visualizations as 1st column
                if idx_a == 0:
                    plt.sca(grids[idx_m + offset + 1][0])
                    plotting.plot_img_with_transparent_mask(center_view, mask,
                                                            alpha=0.7, color=settings.MASK_COLOR)
                    plt.ylabel(metric.get_short_name(), fontsize=fontsize)
                    plt.title("Region Mask", fontsize=fontsize)
Beispiel #7
0
def plot_discont_overview(algorithms,
                          scene,
                          n_rows=2,
                          fs=15,
                          subdir="overview",
                          xmin=150,
                          ymin=230,
                          ww=250):

    # prepare figure grid
    n_vis_types = 2
    n_entries_per_row = int(np.ceil((len(algorithms) + 1) / float(n_rows)))
    rows, cols = (n_vis_types * n_rows), n_entries_per_row + 1

    fig = plt.figure(figsize=(cols * 1.7, 1.45 * rows * 1.5))
    grid, cb_height, cb_width = plotting.get_grid_with_colorbar(
        rows, cols, scene)
    colorbar_args = {
        "height": cb_height,
        "width": cb_width,
        "colorbar_bins": 7,
        "fontsize": fs
    }

    # prepare data
    median_algo = PerPixMedianDiff()
    gt = scene.get_gt()
    median_result = misc.get_algo_result(median_algo, scene)
    center_view = scene.get_center_view()

    # center view
    plt.subplot(grid[0])
    plt.imshow(center_view[ymin:ymin + ww, xmin:xmin + ww])
    plt.title("Center View", fontsize=fs)
    plt.ylabel("DispMap", fontsize=fs)
    plt.subplot(grid[cols])
    plt.ylabel("MedianDiff", fontsize=fs)

    for idx_a, algorithm in enumerate(algorithms):
        algo_result = misc.get_algo_result(algorithm, scene)
        idx = idx_a + 1

        add_ylabel = not idx % n_entries_per_row  # is first column
        add_colorbar = not (idx + 1) % n_entries_per_row  # is last column
        idx_row = (idx / n_entries_per_row) * n_vis_types
        idx_col = idx % n_entries_per_row

        idx = idx_row * cols + idx_col

        # top row with algorithm disparity map
        plt.subplot(grid[idx])
        algo_result_crop = algo_result[ymin:ymin + ww, xmin:xmin + ww]
        cb_depth = plt.imshow(algo_result_crop,
                              **settings.disp_map_args(scene))
        plt.title(algorithm.get_display_name(), fontsize=fs)

        if add_ylabel:
            plt.ylabel("DispMap", fontsize=fs)
        if add_colorbar:
            plotting.add_colorbar(grid[idx + 1], cb_depth, **colorbar_args)

        # second row with median diff
        plt.subplot(grid[idx + cols])
        diff = (np.abs(median_result - gt) -
                np.abs(algo_result - gt))[ymin:ymin + ww, xmin:xmin + ww]
        cb_error = plt.imshow(diff,
                              interpolation="none",
                              cmap=cm.RdYlGn,
                              vmin=-.05,
                              vmax=.05)

        if add_ylabel:
            plt.ylabel("MedianDiff", fontsize=fs)
        if add_colorbar:
            plotting.add_colorbar(grid[idx + cols + 1], cb_error,
                                  **colorbar_args)

    fig_path = plotting.get_path_to_figure("discont_%s" % scene.get_name(),
                                           subdir=subdir)
    plotting.save_tight_figure(fig,
                               fig_path,
                               hide_frames=True,
                               hspace=0.03,
                               wspace=0.03,
                               dpi=100)
Beispiel #8
0
def plot(algorithms,
         scenes,
         meta_algo,
         subdir="meta_algo_comparisons",
         fig_name=None,
         with_gt_row=False,
         fs=12):

    # prepare figure
    rows, cols = len(algorithms) + int(with_gt_row), len(scenes) * 3 + 1
    fig = plt.figure(figsize=(cols * 1.3, rows * 1.5))
    grid, cb_height, cb_width = plotting.get_grid_with_colorbar(
        rows, cols, scenes[0])
    colorbar_args = {
        "height": cb_height * 0.8,
        "width": cb_width,
        "colorbar_bins": 4,
        "fontsize": fs
    }

    for idx_s, scene in enumerate(scenes):
        gt = scene.get_gt()
        meta_algo_result = misc.get_algo_result(meta_algo, scene)
        add_label = idx_s == 0  # is first column
        add_colorbar = idx_s == len(scenes) - 1  # is last column

        # plot one row per algorithm
        for idx_a, algorithm in enumerate(algorithms):
            algo_result = misc.get_algo_result(algorithm, scene)
            add_title = idx_a == 0  # is top row

            idx = idx_a * cols + 3 * idx_s

            # disparity map
            plt.subplot(grid[idx])
            plt.imshow(algo_result, **settings.disp_map_args(scene))
            if add_title:
                plt.title("DispMap", fontsize=fs)
            if add_label:
                plt.ylabel(algorithm.get_display_name(), fontsize=fs)

            # error map: gt - algo
            plt.subplot(grid[idx + 1])
            cb1 = plt.imshow(gt - algo_result,
                             **settings.diff_map_args(vmin=-.1, vmax=.1))
            if add_title:
                plt.title("GT-Algo", fontsize=fs)

            # error map: |meta-gt| - |algo-gt|
            plt.subplot(grid[idx + 2])
            median_diff = np.abs(meta_algo_result - gt) - np.abs(algo_result -
                                                                 gt)
            cb2 = plt.imshow(median_diff,
                             interpolation="none",
                             cmap=cm.RdYlGn,
                             vmin=-.05,
                             vmax=.05)
            if add_title:
                plt.title(meta_algo.get_display_name().replace("PerPix", ""),
                          fontsize=fs)

            if add_colorbar:
                if idx_a % 2 == 0:
                    plotting.add_colorbar(grid[idx + 3], cb1, **colorbar_args)
                else:
                    plotting.add_colorbar(grid[idx + 3], cb2, **colorbar_args)

        if with_gt_row:
            idx = len(algorithms) * cols + 3 * idx_s

            plt.subplot(grid[idx])
            plt.imshow(gt, **settings.disp_map_args(scene))
            plt.xlabel("GT", fontsize=fs)

            if add_label:
                plt.ylabel("Reference")

            plt.subplot(grid[idx + 1])
            cb1 = plt.imshow(np.abs(gt - meta_algo_result),
                             **settings.abs_diff_map_args())
            plt.xlabel("|GT-%s|" % meta_algo.get_display_name(),
                       fontsize=fs - 2)

            if add_colorbar:
                plotting.add_colorbar(grid[idx + 3], cb1, **colorbar_args)

    if fig_name is None:
        scene_names = "_".join(s.get_name() for s in scenes)
        fig_name = "%s_comparison_%s" % (meta_algo.get_name(), scene_names)
    fig_path = plotting.get_path_to_figure(fig_name, subdir=subdir)
    plotting.save_tight_figure(fig,
                               fig_path,
                               hide_frames=True,
                               hspace=0.02,
                               wspace=0.0)
    def plot_algo_overview(self,
                           algorithms,
                           with_metric_vis=True,
                           subdir="algo_overview",
                           fs=14):
        self.set_scale_for_algo_overview()
        metrics = self.get_scene_specific_metrics()
        n_metrics = len(metrics)

        if not with_metric_vis:
            rows, cols = 2 + n_metrics, len(algorithms) + 2
            fig = plt.figure(figsize=(2.6 * len(algorithms), 4.9))
            offset = 0
        else:
            rows, cols = 2 + 2 * n_metrics, len(algorithms) + 2
            fig = plt.figure(figsize=(2.6 * len(algorithms), rows + 3))
            offset = n_metrics

        labelpad = -15
        hscale, wscale = 7, 5
        width_ratios = [wscale] * (len(algorithms) + 1) + [1]
        height_ratios = [hscale] * (rows - n_metrics) + [1] * n_metrics
        gs = gridspec.GridSpec(rows,
                               cols,
                               height_ratios=height_ratios,
                               width_ratios=width_ratios)

        gt = self.get_gt()
        dummy = np.ones((self.get_height() / hscale, self.get_width()))
        cb_height, w = np.shape(gt)
        cb_width = w / float(wscale)

        # first column (gt, center view, ...)
        plt.subplot(gs[0])
        plt.imshow(gt, **settings.disp_map_args(self))
        plt.title("Ground Truth", fontsize=fs)
        plt.ylabel("Disparity Map", fontsize=fs)

        plt.subplot(gs[cols])
        plt.imshow(self.get_center_view())
        plt.ylabel("diff: GT - Algo", fontsize=fs)

        for idx_m, metric in enumerate(metrics):
            plt.subplot(gs[(2 + idx_m + offset) * cols])
            plt.xlabel(metric.get_short_name(), labelpad=labelpad, fontsize=fs)
            plt.imshow(dummy, cmap="gray_r")

        # algorithm columns
        for idx_a, algorithm in enumerate(algorithms):
            log.info("Processing algorithm: %s" % algorithm)
            algo_result = misc.get_algo_result(algorithm, self)

            # algorithm disparities
            plt.subplot(gs[idx_a + 1])
            plt.title(algorithm.get_display_name(), fontsize=fs)
            cm1 = plt.imshow(algo_result, **settings.disp_map_args(self))

            # algorithm diff map
            plt.subplot(gs[cols + idx_a + 1])
            cm2 = plt.imshow(gt - algo_result, **settings.diff_map_args())

            # add colorbar if last column
            if idx_a == (len(algorithms) - 1):
                plotting.add_colorbar(gs[idx_a + 2],
                                      cm1,
                                      cb_height,
                                      cb_width,
                                      colorbar_bins=5,
                                      fontsize=fs - 4)
                plotting.add_colorbar(gs[cols + idx_a + 2],
                                      cm2,
                                      cb_height,
                                      cb_width,
                                      colorbar_bins=5,
                                      fontsize=fs - 4)

            # score and background color for metrics
            for idx_m, metric in enumerate(metrics):

                if with_metric_vis:
                    plt.subplot(gs[(2 + idx_m) * cols + idx_a + 1])
                    score, vis = metric.get_score(algo_result,
                                                  gt,
                                                  self,
                                                  with_visualization=True)
                    cm3 = plt.imshow(vis, **settings.metric_args(metric))

                    if idx_a == 0:
                        plt.ylabel(metric.get_short_name(), fontsize=fs)
                    elif idx_a == (len(algorithms) - 1):
                        plotting.add_colorbar(
                            gs[(2 + idx_m) * cols + idx_a + 2],
                            cm3,
                            cb_height,
                            cb_width,
                            colorbar_bins=metric.colorbar_bins,
                            fontsize=fs - 4)

                else:
                    score = metric.get_score(algo_result, gt, self)

                plt.subplot(gs[(2 + idx_m + offset) * cols + idx_a + 1])
                plt.imshow(
                    dummy * score,
                    **settings.score_color_args(vmin=metric.vmin,
                                                vmax=metric.vmax))
                plt.xlabel(metric.format_score(score),
                           labelpad=labelpad,
                           fontsize=fs)

        fig_name = "algo_overview_" + self.get_name(
        ) + with_metric_vis * "_vis"
        fig_path = plotting.get_path_to_figure(fig_name, subdir=subdir)
        plotting.save_tight_figure(fig,
                                   fig_path,
                                   wspace=0.04,
                                   hide_frames=True)