Esempio n. 1
0
def gen_img_and_overlay(source_file, segmentation, out_dir, color_changes, i):
    """
    Generate the segmentation image and the overlay.

    Parameters
    ----------
    source_file : str
        Name of the file of which the segmentation was done.
    segmentation : numpy array
    out_dir : str
        Directory in which the segmentation image and the overlay should be
        put.
    color_changes : dict
        Encode which classes (key) of 'segmentation' should get which color
        (value). The key and value have to be in (0, 0, 0)
    i : int
        Counter
    """
    # Paths
    basename = os.path.splitext(os.path.basename(source_file))[0]
    seg_name = "%i-%s-segmentation.png" % (i, basename)
    seg_path = os.path.join(out_dir, seg_name)
    overlay_name = "%i-%s-overlay.png" % (i, basename)
    overlay_path = os.path.join(out_dir, overlay_name)

    # Logic
    scipy.misc.imsave(seg_path, segmentation)  # Store segmentation
    input_image = scipy.misc.imread(source_file, mode='RGB')  # Load original
    overlayed = overlay_segmentation(input_image, segmentation, color_changes)
    scipy.misc.imsave(overlay_path, overlayed)
    logging.info("Created output for '%s'", source_file)
Esempio n. 2
0
def create_test_output(hypes, sess, image_pl, softmax):
    data_dir = hypes['dirs']['data_dir']
    data_file = os.path.join(data_dir, test_file)
    image_dir = os.path.dirname(data_file)

    logdir = "test_images/"

    logging.info("Images will be written to {}".format(logdir))

    logdir = os.path.join(hypes['dirs']['output_dir'], logdir)

    color2class_dict = utils.get_class2color(hypes)

    if not os.path.exists(logdir):
        os.mkdir(logdir)

    with open(data_file) as file:
        for i, image_file in enumerate(file):
            image_file = image_file.rstrip()
            image_file = os.path.join(image_dir, image_file)
            image = scp.misc.imread(image_file)
            shape = image.shape

            feed_dict = {image_pl: image}
            output = sess.run([softmax], feed_dict=feed_dict)

            output_im = output[0]['logits'].reshape(shape[0], shape[1], -1)
            output_im_seg = np.argmax(output_im, axis=2)
            ov_image = utils.overlay_segmentation(image, output_im_seg,
                                                  color2class_dict)

            name = os.path.basename(image_file)
            save_file = os.path.join(logdir, name)
            logging.info("Writing file: %s", save_file)
            scp.misc.imsave(save_file, ov_image)
def create_test_output(hypes, sess, image_pl, softmax, data_file):
    # data_dir = hypes['dirs']['data_dir']
    # data_file = os.path.join(data_dir, test_file)

    image_dir = os.path.dirname(data_file)

    logdir_green = "test_images_green/"

    logging.info(
        "Images will be written to {}/test_images_{{green, rg}}".format(
            logdir_green))

    logdir_green = os.path.join(hypes['dirs']['output_dir'], logdir_green)

    if not os.path.exists(logdir_green):
        os.mkdir(logdir_green)

    image_list = []
    color_dict = cutils.get_output_color_dict(hypes)

    with open(data_file) as file:
        for i, image_file in enumerate(file):

            t = time.time()

            image_file = image_file.rstrip()
            image_file = os.path.join(image_dir, image_file)
            image = scp.misc.imread(image_file)
            shape = image.shape

            feed_dict = {image_pl: image}

            output = sess.run([softmax['softmax']], feed_dict=feed_dict)
            output_im = output[0].argmax(axis=1).reshape(shape[0], shape[1])
            output_prob = output[0].max(axis=1).reshape(shape[0], shape[1])

            # Saving RB Plot
            ov_image = seg.make_overlay(image, output_prob)
            name = os.path.basename(image_file)
            image_list.append((name, ov_image))

            new_name = name.split('.')[0] + '_green.png'

            green_image = utils.overlay_segmentation(image, output_im,
                                                     color_dict)

            save_file = os.path.join(logdir_green, new_name)
            scp.misc.imsave(save_file, green_image)

            elapsed = time.time() - t
            print("elapsed time: " + str(elapsed))
Esempio n. 4
0
def test_overlay_segmentation():
    """Test overlay_segmentation."""
    from tensorvision.utils import overlay_segmentation
    import scipy.ndimage
    import numpy as np
    seg_image = 'tensorvision/tests/Crocodylus-johnsoni-3-seg.png'
    original_image = 'tensorvision/tests/Crocodylus-johnsoni-3.jpg'
    target_path = 'tensorvision/tests/croco-overlay-new.png'
    correct_path = 'tensorvision/tests/croco-overlay.png'
    input_image = scipy.misc.imread(original_image)
    segmentation = scipy.misc.imread(seg_image)
    color_changes = {0: (0, 255, 0, 127), 'default': (0, 0, 0, 0)}
    res = overlay_segmentation(input_image, segmentation, color_changes)
    scipy.misc.imsave(target_path, res)
    img1 = scipy.ndimage.imread(correct_path)
    img2 = scipy.ndimage.imread(target_path)
    assert np.array_equal(img1, img2)
Esempio n. 5
0
def test_overlay_segmentation():
    """Test overlay_segmentation."""
    from tensorvision.utils import overlay_segmentation
    import scipy.ndimage
    import numpy as np
    seg_image = 'tensorvision/tests/Crocodylus-johnsoni-3-seg.png'
    original_image = 'tensorvision/tests/Crocodylus-johnsoni-3.jpg'
    target_path = 'tensorvision/tests/croco-overlay-new.png'
    correct_path = 'tensorvision/tests/croco-overlay.png'
    input_image = scipy.misc.imread(original_image)
    segmentation = scipy.misc.imread(seg_image)
    color_changes = {0: (0, 255, 0, 127),
                     'default': (0, 0, 0, 0)}
    res = overlay_segmentation(input_image, segmentation, color_changes)
    scipy.misc.imsave(target_path, res)
    img1 = scipy.ndimage.imread(correct_path)
    img2 = scipy.ndimage.imread(target_path)
    assert np.array_equal(img1, img2)
Esempio n. 6
0
def _save_plot(image, output_im, color_dict, image_file, image_list):
    # image shape [H W n_chan], output_im (softmax) shape [HxW n_cl]

    # keep values with probabilities > 0.5
    hard = output_im > 0.5
    hard_mask = np.sum(hard, axis=1) > 0
    segm_mask = np.argmax(output_im, axis=1) + 1
    # '-1' for unknown class (probability value < 0.5)
    segm_mask = segm_mask * hard_mask - 1
    segm_mask = np.reshape(segm_mask, (image.shape[0], image.shape[1]))

    # Saving overlay image
    ov_image = utils.overlay_segmentation(image, segm_mask, color_dict)
    name = os.path.basename(image_file)
    filename, file_extension = os.path.splitext(name)
    ov_name = filename + '_overlay' + file_extension
    image_list.append((ov_name, ov_image))

    # Saving segmentation image
    seg_image = utils.segmentation_rgb(segm_mask, color_dict)
    seg_name = filename + '_segm' + file_extension
    image_list.append((seg_name, seg_image))
def create_test_output(hypes, sess, image_pl, softmax, data_file):
    # data_dir = hypes['dirs']['data_dir']
    # data_file = os.path.join(data_dir, test_file)

    image_dir = os.path.dirname(data_file)

    logdir_prediction = "test_images_prediction/"

    logging.info(
        "Images will be written to {}/test_images_{{prediction, rg}}".format(
            logdir_prediction))

    logdir_prediction = os.path.join(hypes['dirs']['output_dir'],
                                     logdir_prediction)

    if not os.path.exists(logdir_prediction):
        os.mkdir(logdir_prediction)

    num_classes = cutils.get_num_classes(hypes)
    color_dict = cutils.get_output_color_dict(hypes)
    total_confusion_matrix = np.zeros([num_classes, num_classes], int)
    name_classes = cutils.get_name_classes(hypes)

    with open(data_file) as file:
        for i, datum in enumerate(file):

            t = time.time()

            image_file = datum.rstrip()
            if len(image_file.split(" ")) > 1:
                image_file, gt_file = image_file.split(" ")
                gt_file = os.path.join(image_dir, gt_file)
                gt_image = scp.misc.imread(gt_file, mode='RGB')
            image_file = os.path.join(image_dir, image_file)
            image = scp.misc.imread(image_file)
            shape = image.shape

            feed_dict = {image_pl: image}

            output = sess.run([softmax['softmax']], feed_dict=feed_dict)
            output_im = output[0].argmax(axis=1).reshape(shape[0], shape[1])

            # Saving RB Plot
            name = os.path.basename(image_file)

            new_name = name.split('.')[0] + '_prediction.png'

            prediction_image = utils.overlay_segmentation(
                image, output_im, color_dict)

            save_file = os.path.join(logdir_prediction, new_name)
            scp.misc.imsave(save_file, prediction_image)

            elapsed = time.time() - t
            print("elapsed time: " + str(elapsed))
            if 'gt_image' in locals():
                confusion_matrix = kitti_eval.eval_image(
                    hypes, cutils.get_gt_image_index(gt_image, hypes),
                    output_im)
                total_confusion_matrix += confusion_matrix
                for j in range(num_classes):
                    gray_scale_file_name = name.split(
                        '.')[0] + '_' + name_classes[j] + '_grayscale.png'
                    save_file = os.path.join(logdir_prediction,
                                             gray_scale_file_name)
                    output_prob_class = np.around(
                        output[0][:, j].reshape(shape[0], shape[1]) * 255)
                    scp.misc.imsave(save_file, output_prob_class)

        if 'gt_image' in locals():
            normalized_total_confusion_matrix = total_confusion_matrix.astype(
                'float') / total_confusion_matrix.sum(axis=1)[:, np.newaxis]
            normalized_total_confusion_matrix[np.isnan(
                normalized_total_confusion_matrix)] = 0
            classes_result = {
                "confusion_matrix": total_confusion_matrix,
                "normalized_confusion_matrix":
                normalized_total_confusion_matrix
            }
            for i in range(num_classes):
                classes_result[
                    name_classes[i]] = kitti_eval.obtain_class_result(
                        total_confusion_matrix, i)
            eval_result = {"classes_result": classes_result}
            print(eval_result)
def evaluate(hypes, sess, image_pl, inf_out):

    softmax = inf_out['softmax']
    data_dir = hypes['dirs']['data_dir']

    color_dict = cutils.get_output_color_dict(hypes)

    num_classes = cutils.get_num_classes(hypes)

    eval_dict = {}
    for phase in ['train', 'val']:
        total_confusion_matrix = np.zeros([num_classes, num_classes], int)
        data_file = hypes['data']['{}_file'.format(phase)]
        data_file = os.path.join(data_dir, data_file)
        image_dir = os.path.dirname(data_file)

        image_list = []

        with open(data_file) as file:
            for i, datum in enumerate(file):
                datum = datum.rstrip()
                image_file, gt_file = datum.split(" ")
                image_file = os.path.join(image_dir, image_file)
                gt_file = os.path.join(image_dir, gt_file)

                image = scp.misc.imread(image_file, mode='RGB')
                gt_image = scp.misc.imread(gt_file, mode='RGB')

                if hypes['jitter']['fix_shape']:
                    shape = image.shape
                    image_height = hypes['jitter']['image_height']
                    image_width = hypes['jitter']['image_width']
                    assert(image_height >= shape[0])
                    assert(image_width >= shape[1])

                    offset_x = (image_height - shape[0])//2
                    offset_y = (image_width - shape[1])//2
                    new_image = np.zeros([image_height, image_width, 3])
                    new_image[offset_x:offset_x+shape[0],
                              offset_y:offset_y+shape[1]] = image
                    input_image = new_image
                elif hypes['jitter']['reseize_image']:
                    image_height = hypes['jitter']['image_height']
                    image_width = hypes['jitter']['image_width']
                    image, gt_image = resize_label_image(image, gt_image,
                                                         image_height,
                                                         image_width)
                    input_image = image
                else:
                    input_image = image

                shape = input_image.shape

                feed_dict = {image_pl: input_image}

                output = sess.run([softmax], feed_dict=feed_dict)
                output_im = output[0].argmax(axis=1).reshape(shape[0], shape[1])
                output_prob = output[0].max(axis=1).reshape(shape[0], shape[1])

                if hypes['jitter']['fix_shape']:
                    gt_shape = gt_image.shape
                    output_im = output_im[offset_x:offset_x+gt_shape[0],
                                          offset_y:offset_y+gt_shape[1]]
                    output_prob = output_prob[offset_x:offset_x + gt_shape[0],
                                              offset_y:offset_y + gt_shape[1]]

                if phase == 'val':
                    # Saving RB Plot
                    ov_image = seg.make_overlay(image, output_prob)
                    name = os.path.basename(image_file)
                    image_list.append((name, ov_image))

                    name2 = name.split('.')[0] + '_prediction.png'

                    prediction_image = utils.overlay_segmentation(image, output_im, color_dict)
                    image_list.append((name2, prediction_image))

                confusion_matrix = eval_image(hypes, cutils.get_gt_image_index(gt_image, hypes), output_im)

                total_confusion_matrix += confusion_matrix

        classes_result = {"confusion_matrix": total_confusion_matrix,
                          "normalized_confusion_matrix": normalize_confusion_matrix(total_confusion_matrix)}
        name_classes = cutils.get_name_classes(hypes)
        for i in range(num_classes):
            classes_result[name_classes[i]] = obtain_class_result(total_confusion_matrix, i)

        eval_dict[phase] = {"classes_result": classes_result}

        if phase == 'val':
            start_time = time.time()
            for i in xrange(10):
                sess.run([softmax], feed_dict=feed_dict)
            dt = (time.time() - start_time)/10

    eval_list = []

    for phase in ['train', 'val']:
        print(phase)
        print(eval_dict[phase]["classes_result"]["confusion_matrix"])
        print(eval_dict[phase]["classes_result"]["normalized_confusion_matrix"])
        for class_name in name_classes:
            eval_list.append(('[{} {}] Recall'.format(phase, class_name), 100 * eval_dict[phase]["classes_result"][class_name]["recall"]))
            eval_list.append(('[{} {}] Precision'.format(phase, class_name), 100 * eval_dict[phase]["classes_result"][class_name]["precision"]))
            eval_list.append(('[{} {}] TNR'.format(phase, class_name), 100 * eval_dict[phase]["classes_result"][class_name]["TNR"]))
            eval_list.append(('[{} {}] Accuracy'.format(phase, class_name), 100 * eval_dict[phase]["classes_result"][class_name]["accuracy"]))
            eval_list.append(('[{} {}] F1'.format(phase, class_name), 100 * eval_dict[phase]["classes_result"][class_name]["F1"]))

    eval_list.append(('Speed (msec)', 1000*dt))
    eval_list.append(('Speed (fps)', 1/dt))
    return eval_list, image_list
Esempio n. 9
0
def evaluate(hypes, sess, image_pl, inf_out):

    softmax = inf_out['softmax']
    data_dir = hypes['dirs']['data_dir']
    eval_dict = {}
    num_classes = np.int32(hypes['arch']['num_classes'])
    color2class_dict = utils.get_class2color(hypes)

    for phase in ['train', 'val']:
        data_file = hypes['data']['{}_file'.format(phase)]
        data_file = os.path.join(data_dir, data_file)
        image_dir = os.path.dirname(data_file)

        # thresh = np.array(range(0, 256))/255.0
        total_intersection_class = np.zeros(num_classes)
        total_pixel_class = np.zeros(num_classes)
        total_unionsection_class = np.zeros(num_classes)

        image_list = []

        with open(data_file) as file:
            for i, datum in enumerate(file):
                    datum = datum.rstrip()
                    image_file, gt_file = datum.split(" ")
                    image_file = os.path.join(image_dir, image_file)
                    gt_file = os.path.join(image_dir, gt_file)

                    image = scp.misc.imread(image_file, mode='RGB')
                    gt_image = scp.misc.imread(gt_file, mode='RGB')

                    if hypes['jitter']['fix_shape']:
                        shape = image.shape
                        image_height = hypes['jitter']['image_height']
                        image_width = hypes['jitter']['image_width']
                        assert(image_height >= shape[0])
                        assert(image_width >= shape[1])

                        offset_x = (image_height - shape[0])//2
                        offset_y = (image_width - shape[1])//2
                        new_image = np.zeros([image_height, image_width, 3])
                        new_image[offset_x:offset_x+shape[0],
                                  offset_y:offset_y+shape[1]] = image
                        input_image = new_image
                    elif hypes['jitter']['reseize_image']:
                        image_height = hypes['jitter']['image_height']
                        image_width = hypes['jitter']['image_width']
                        gt_image_old = gt_image
                        image, gt_image = resize_label_image(image, gt_image,
                                                             image_height,
                                                             image_width)
                        input_image = image
                    else:
                        input_image = image

                    shape = input_image.shape

                    feed_dict = {image_pl: input_image}

                    output = sess.run([softmax], feed_dict=feed_dict)
                    output_im = output[0].reshape(shape[0], shape[1], num_classes)

                    if hypes['jitter']['fix_shape']:
                        gt_shape = gt_image.shape
                        output_im = output_im[offset_x:offset_x+gt_shape[0],
                                              offset_y:offset_y+gt_shape[1]]

                    if phase == 'val':
                        # Saving RB Plot
                        output_im_seg = np.argmax(output_im, axis=2)
                        ov_image = utils.overlay_segmentation(input_image, output_im_seg, color2class_dict)

                        name = os.path.basename(image_file)
                        image_list.append((name, ov_image))

                        # name2 = name.split('.')[0] + '_green.png'
                        # hard = output_im > 0.5
                        # green_image = utils.fast_overlay(image, hard)
                        # image_list.append((name2, green_image))

                    intersection_class, pixel_class, unionsection_class = eval_image(hypes, gt_image, output_im)

                    total_intersection_class += intersection_class
                    total_pixel_class += pixel_class
                    total_unionsection_class += unionsection_class

        eval_dict[phase] = seg.pxEval(total_intersection_class, total_pixel_class, total_unionsection_class)

        if phase == 'val':
            start_time = time.time()
            for i in xrange(10):
                sess.run([softmax], feed_dict=feed_dict)
            dt = (time.time() - start_time)/10

    eval_list = []

    for phase in ['train', 'val']:
        eval_list.append(('[{}] PixelAcc'.format(phase),
                          100*eval_dict[phase]['PixelAcc']))
        eval_list.append(('[{}] MeanPixelAcc'.format(phase),
                          100*eval_dict[phase]['MeanPixelAcc']))
        eval_list.append(('[{}] MeanIoU'.format(phase),
                          100*eval_dict[phase]['MeanIoU']))
        eval_list.append(('[{}] FwMeanIoU'.format(phase),
                          100 * eval_dict[phase]['FwMeanIoU']))

    eval_list.append(('Speed (msec)', 1000*dt))
    eval_list.append(('Speed (fps)', 1/dt))

    return eval_list, image_list