Example #1
0
    def _process_forward_result(self, y_argmax, logit, target, tag,
                                extraction_vals, main_folder, save_results):
        # hack for avoiding storing logits for frames, which are not evaluated
        if "DO_NOT_STORE_LOGITS" in tag:
            logit = None
            tag = tag.replace("_DO_NOT_STORE_LOGITS", "")

        if "__" in tag.split("/")[-2]:
            sp = tag.split("/")
            sp2 = sp[-2].split("__")
            assert len(sp2) == 2
            folder = main_folder + sp2[0] + "/" + sp2[1] + "/"
        else:
            folder = main_folder + tag.split("/")[-2] + "/"
        out_fn = folder + tag.split("/")[-1].replace(".jpg", ".png").replace(
            ".bin", ".png")
        tf.gfile.MakeDirs(folder)

        # TODO: generalize for multiple classes
        measures = compute_measures_for_binary_segmentation(y_argmax, target)
        if save_results:
            if self.adjustable_output_layer:
                save_with_pascal_colormap(out_fn, y_argmax)
            else:
                y_scaled = (y_argmax * 255).astype("uint8")
                imsave(out_fn, numpy.squeeze(y_scaled, axis=2))
            print(out_fn)
        if logit is not None:
            if self.save_as_png:
                out_fn_logits = out_fn.replace(".png", "_prob.png")
                import cv2
                cv2.imwrite(out_fn_logits,
                            (logit[..., 0] *
                             numpy.iinfo(numpy.uint16).max).astype("uint16"))
            else:
                out_fn_logits = out_fn.replace(".png", ".pickle")
                pickle.dump(logit, open(out_fn_logits, "w"),
                            pickle.HIGHEST_PROTOCOL)
        for e in extraction_vals:
            assert e.shape[0] == 1  # batch size should be 1 here for now
        for name, val in zip(self.extractions, extraction_vals):
            val = val[0]  # remove batch dimension
            sp = out_fn.replace(".png", ".bin").split("/")
            sp[-1] = name + "_" + sp[-1]
            out_fn_extract = "/".join(sp)
            print(out_fn_extract)
            val.tofile(out_fn_extract)
        return measures
Example #2
0
def do_seq(model, seq):
    pattern = BASE_DIR + model + "/valid/" + seq + "/*/"
    out_folder = BASE_DIR + model + "_merged/valid/" + seq + "/"
    mkdir_p(out_folder)
    object_folders = sorted(glob.glob(pattern))
    filenames = sorted(
        map(os.path.basename, glob.glob(object_folders[0] + "/*.pickle")))
    for fn in filenames:
        posteriors = [
            pickle.load(open(obj_folder + "/" + fn))
            for obj_folder in object_folders
        ]
        background = numpy.stack([x[..., 0] for x in posteriors],
                                 axis=2).min(axis=2, keepdims=True)
        rest = numpy.stack([x[..., 1] for x in posteriors], axis=2)
        combined = numpy.concatenate([background, rest], axis=2)
        result = combined.argmax(axis=2)
        out_fn = out_folder + fn.replace(".pickle", ".png")
        print(out_fn)
        save_with_pascal_colormap(out_fn, result)