示例#1
0
def visualize_segmentation_dataset_one(images_path,
                                       segs_path,
                                       n_classes,
                                       do_augment=False,
                                       no_show=False,
                                       ignore_non_matching=False):

    img_seg_pairs = get_pairs_from_paths(
        images_path, segs_path, ignore_non_matching=ignore_non_matching)

    colors = class_colors

    im_fn, seg_fn = random.choice(img_seg_pairs)

    img = cv2.imread(im_fn)
    seg = cv2.imread(seg_fn)
    print("Found the following classes in the segmentation image:",
          np.unique(seg))

    img, seg_img = _get_colored_segmentation_image(img,
                                                   seg,
                                                   colors,
                                                   n_classes,
                                                   do_augment=do_augment)

    if not no_show:
        cv2.imshow("img", img)
        cv2.imshow("seg_img", seg_img)
        cv2.waitKey()

    return img, seg_img
示例#2
0
def evaluate(model=None,
             inp_images=None,
             annotations=None,
             inp_images_dir=None,
             annotations_dir=None,
             checkpoints_path=None):

    if model is None:
        assert (
            checkpoints_path
            is not None), "Please provide the model or the checkpoints_path"
        model = model_from_checkpoint_path(checkpoints_path)

    if inp_images is None:
        assert (inp_images_dir
                is not None), "Please provide inp_images or inp_images_dir"
        assert (annotations_dir
                is not None), "Please provide inp_images or inp_images_dir"

        paths = get_pairs_from_paths(inp_images_dir, annotations_dir)
        paths = list(zip(*paths))
        inp_images = list(paths[0])
        annotations = list(paths[1])

    assert type(inp_images) is list
    assert type(annotations) is list

    tp = np.zeros(model.n_classes)
    fp = np.zeros(model.n_classes)
    fn = np.zeros(model.n_classes)
    n_pixels = np.zeros(model.n_classes)

    for inp, ann in tqdm(zip(inp_images, annotations)):
        pr = predict(model, inp)
        gt = get_segmentation_array(ann,
                                    model.n_classes,
                                    model.output_width,
                                    model.output_height,
                                    no_reshape=True)
        gt = gt.argmax(-1)
        #pr = pr.flatten()
        #gt = gt.flatten()

        for cl_i in range(model.n_classes):

            tp[cl_i] += np.sum((pr == cl_i) * (gt == cl_i))
            fp[cl_i] += np.sum((pr == cl_i) * ((gt != cl_i)))
            fn[cl_i] += np.sum((pr != cl_i) * ((gt == cl_i)))
            n_pixels[cl_i] += np.sum(gt == cl_i)

    cl_wise_score = tp / (tp + fp + fn + 0.000000000001)
    n_pixels_norm = n_pixels / np.sum(n_pixels)
    frequency_weighted_IU = np.sum(cl_wise_score * n_pixels_norm)
    mean_IU = np.mean(cl_wise_score)
    return {
        "frequency_weighted_IU": frequency_weighted_IU,
        "mean_IU": mean_IU,
        "class_wise_IU": cl_wise_score
    }
示例#3
0
def visualize_segmentation_dataset(images_path,
                                   segs_path,
                                   n_classes,
                                   do_augment=False,
                                   ignore_non_matching=False,
                                   no_show=False):
    try:
        # Get image-segmentation pairs
        img_seg_pairs = get_pairs_from_paths(
            images_path, segs_path, ignore_non_matching=ignore_non_matching)

        # Get the colors for the classes
        colors = class_colors

        print("Please press any key to display the next image")
        for im_fn, seg_fn in img_seg_pairs:
            img = cv2.imread(im_fn)[..., ::-1]
            seg = cv2.imread(seg_fn)
            print("Found the following classes in the segmentation image:",
                  np.unique(seg))
            img, seg_img = _get_colored_segmentation_image(
                img, seg, colors, n_classes, do_augment=do_augment)
            #print("Please press any key to display the next image")
            #cv2.imshow("img", img)
            #cv2.imshow("seg_img", seg_img)
            #cv2.waitKey()
            fig = plt.figure(figsize=(14, 7))
            ax1 = fig.add_subplot(1, 3, 1)
            ax1.imshow(img)
            ax2 = fig.add_subplot(1, 3, 2)
            ax2.imshow(seg)
            ax3 = fig.add_subplot(1, 3, 3)
            ax3.imshow(seg_img)
            plt.show(block=False)
            plt.pause(1)
            plt.close()
    except DataLoaderError as e:
        print("Found error during data loading\n{0}".format(str(e)))
        return False
示例#4
0
def visualize_segmentation_dataset(images_path,
                                   segs_path,
                                   num_imgs=None,
                                   img_size=None,
                                   augment=False,
                                   n_classes=255):
    import matplotlib.pyplot as plt
    import matplotlib
    from axelerate.networks.segnet.data_utils.data_loader import get_pairs_from_paths, DATA_LOADER_SEED, class_colors, DataLoaderError

    try:
        matplotlib.use('TkAgg')
    except:
        pass

    def _get_colored_segmentation_image(img,
                                        seg,
                                        colors,
                                        n_classes,
                                        img_size,
                                        do_augment=False):
        """ Return a colored segmented image """

        img, seg = process_image_segmentation(img, seg, img_size, img_size,
                                              img_size, img_size, do_augment)
        seg_img = np.zeros_like(seg)

        for c in range(n_classes):
            seg_img[:, :, 0] += ((seg[:, :, 0] == c) *
                                 (colors[c][0])).astype('uint8')
            seg_img[:, :, 1] += ((seg[:, :, 0] == c) *
                                 (colors[c][1])).astype('uint8')
            seg_img[:, :, 2] += ((seg[:, :, 0] == c) *
                                 (colors[c][2])).astype('uint8')

        return img, seg_img

    try:
        # Get image-segmentation pairs
        img_seg_pairs = get_pairs_from_paths(images_path,
                                             segs_path,
                                             ignore_non_matching=True)
        # Get the colors for the classes
        colors = class_colors

        print("Please press any key to display the next image")
        for im_fn, seg_fn in img_seg_pairs[:num_imgs]:
            img = cv2.imread(im_fn)[..., ::-1]
            seg = cv2.imread(seg_fn)
            print("Found the following classes in the segmentation image:",
                  np.unique(seg))
            img, seg_img = _get_colored_segmentation_image(img,
                                                           seg,
                                                           colors,
                                                           n_classes,
                                                           img_size,
                                                           do_augment=augment)
            fig = plt.figure(figsize=(14, 7))
            ax1 = fig.add_subplot(1, 2, 1)
            ax1.imshow(img)
            ax3 = fig.add_subplot(1, 2, 2)
            ax3.imshow(seg_img)
            plt.show(block=False)
            plt.pause(1)
            plt.close()
    except DataLoaderError as e:
        print("Found error during data loading\n{0}".format(str(e)))
        return False