def after_epoch(self, epoch):
        self.data = {"inputs": self.inputs, "outputs": self.outputs}
        out_path = os.path.join(self.root, "data.p")
        with open(out_path, "wb") as f:
            pickle.dump(self.data, f)
        print("Wrote retrieval matrix data in:")
        print(out_path)

        for k, o in self.outputs.items():
            i_out = 0
            for i, im_batch in enumerate(o):

                if len(im_batch.shape) == 3:
                    im_batch = np.expand_dims(im_batch, axis=-1)

                for im in np.split(im_batch, len(im_batch), 0):
                    out_path = os.path.join(self.root,
                                            "{}_{:07d}.png".format(k, i_out))
                    plot_batch(im, out_path)
                    i_out += 1

        for k, o in self.inputs.items():
            i_out = 0
            for i, im_batch in enumerate(o):

                if len(im_batch.shape) == 3:
                    im_batch = np.expand_dims(im_batch, axis=-1)

                for im in np.split(im_batch, len(im_batch), 0):
                    out_path = os.path.join(self.root,
                                            "{}_{:07d}.png".format(k, i_out))
                    plot_batch(im, out_path)
                    i_out += 1
Exemplo n.º 2
0
    def after_step(self, batch_index, last_results):
        if batch_index % self.interval == 0:
            step = last_results["global_step"]
            # TODO: fix hard-coded font type
            # TODO: add option to log overview to tensorboard
            batches = []
            try:
                fnt = ImageFont.truetype("LiberationMono-Regular.ttf", 20)
            except OSError:
                fnt = ImageFont.load_default()
            last_results = last_results["logging"]
            for name, im in sorted(last_results["images"].items()):
                canvas = batch_to_canvas(im)
                canvas = (canvas + 1.0) / 2.0
                canvas = np.clip(255 * canvas, 0, 255)
                canvas = np.array(canvas, dtype="uint8")
                im = Image.fromarray(canvas)
                im.thumbnail((512, 512), Image.ANTIALIAS)
                d = ImageDraw.Draw(im)
                d.text((10, 10), name, fill=(255, 0, 0), font=fnt)
                batches.append(im)

            im = Image.new("RGB", batches[0].size, color=(0, 0, 0))
            try:
                fnt = ImageFont.truetype("LiberationMono-Regular.ttf", 50)
            except OSError:
                fnt = ImageFont.load_default()
            d = ImageDraw.Draw(im)
            d.text((10, 10), "epoch\n{:07d}".format(step), fill=(255, 0, 0), font=fnt)
            batches.append(im)
            batch = np.stack(batches, axis=0) / 255.0 * 2 - 1.0
            out_path = os.path.join(self.root, "overview_{:07d}.png".format(step))
            plot_batch(batch, out_path)
Exemplo n.º 3
0
 def log_images(self, results, step, path):
     for name, image_batch in results.items():
         full_name = name + "_{:07}.png".format(step)
         save_path = os.path.join(self.root, path, full_name)
         try:
             plot_batch(image_batch, save_path)
         except FileNotFoundError:
             os.makedirs(os.path.split(save_path)[0])
             plot_batch(image_batch, save_path)
Exemplo n.º 4
0
    def after_step(self, batch_index, last_results):
        if batch_index % self.interval == 0:
            step = last_results["global_step"]
            last_results = last_results["logging"]
            if self._has_summary:
                summary = last_results["summaries"]
                self.writer.add_summary(summary, step)

            logs = last_results["logs"]
            for name in sorted(logs.keys()):
                self.logger.info("{}: {}".format(name, logs[name]))

            if not self.log_images_to_tensorboard:
                for name, image_batch in last_results["images"].items():
                    full_name = name + "_{:07}.png".format(step)
                    save_path = os.path.join(self.root, full_name)
                    plot_batch(image_batch, save_path)

            self.logger.info("project root: {}".format(self.root))
Exemplo n.º 5
0
    def after_step(self, batch_index, last_results):
        if batch_index % self.interval == 0:
            step = last_results["global_step"]

            for key in self.scalar_keys:
                value = retrieve(key, last_results)
                self.tb_logger.add_scalar(key, value, step)

            for key in self.histogram_keys:
                value = retrieve(key, last_results)
                self.tb_logger.add_histogram(key, value, step)

            for key in self.image_keys:
                value = retrieve(key, last_results)

                name = key.split("/")[-1]
                full_name = name + "_{:07}.png".format(step)
                save_path = os.path.join(self.root, full_name)
                plot_batch(value, save_path)

            for key in self.log_keys:
                value = retrieve(key, last_results)
                self.logger.info("{}: {}".format(key, value))
Exemplo n.º 6
0
    def after_step(self, batch_index, results):
        step = retrieve('global_step', results)

        for i, key in enumerate(self.keys):
            image_batch = retrieve(key, results)
            if self.names is not None:
                name = self.names[i]
            else:
                name = key.split('/')[-1]

            if self.time_axis is not None:
                n = image_batch.shape[self.time_axis]
                t_batches = np.split(image_batch, n, axis=self.time_axis)
                for ts, sub_batch in enumerate(t_batches):
                    sub_batch = sub_batch.squeeze(self.time_axis)
                    savename = name + '_{:0>7d}-{:0>4}-ts{:0>2}.png' \
                        .format(step, batch_index, ts)
                    savename = os.path.join(self.root, savename)
                    plot_batch(sub_batch, savename)
            else:
                savename = name + '_{:0>7d}-{:0>4}.png'.format(step,
                                                               batch_index)
                savename = os.path.join(self.root, savename)
                plot_batch(image_batch, savename)
Exemplo n.º 7
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,
        )
Exemplo n.º 8
0
 def log_images(self, results, step):
     for name, image_batch in results.items():
         full_name = name + "_{:07}.png".format(self._step)
         save_path = os.path.join(self.root, full_name)
         plot_batch(image_batch, save_path)
Exemplo n.º 9
0
from eddata.pennaction import PennAction, PennActionCropped
from edflow.iterators.batches import plot_batch
import numpy as np

if __name__ == "__main__":
    d = PennAction()
    e = d[0]
    print(e)
    for k, v in e.items():
        print(k, type(v))

    print(sorted(set(d.labels["action"])))

    d = PennActionCropped()
    batch = np.stack([d[10 * i]["image"] for i in range(16)])
    plot_batch(batch, "pennaction_examples.png")