Пример #1
0
 def test_vis_semantic_segmentation_exceed_value(self):
     label = np.random.randint(10, 20, size=(48, 64)).astype(np.int32)
     with self.assertRaises(ValueError):
         vis_semantic_segmentation(None,
                                   label,
                                   label_names=('class0', 'class1',
                                                'class2'))
Пример #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained-model', default='cityscapes')
    parser.add_argument('--min-input-size', type=int, default=None)
    parser.add_argument('image')
    args = parser.parse_args()

    model = DeepLabV3plusXception65(
        pretrained_model=args.pretrained_model,
        min_input_size=args.min_input_size)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    labels = model.predict([img])
    label = labels[0]

    fig = plt.figure()
    ax1 = fig.add_subplot(1, 2, 1)
    vis_image(img, ax=ax1)
    ax2 = fig.add_subplot(1, 2, 2)
    # Do not overlay the label image on the color image
    vis_semantic_segmentation(
        None, label, voc_semantic_segmentation_label_names,
        voc_semantic_segmentation_label_colors, ax=ax2)
    plt.show()
Пример #3
0
def main():
    chainer.config.train = False

    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained_model', default='camvid')
    parser.add_argument('image')
    args = parser.parse_args()

    model = SegNetBasic(
        n_class=len(camvid_label_names),
        pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    labels = model.predict([img])
    label = labels[0]

    fig = plot.figure()
    ax1 = fig.add_subplot(1, 2, 1)
    vis_image(img, ax=ax1)
    ax2 = fig.add_subplot(1, 2, 2)
    vis_semantic_segmentation(
        label, camvid_label_names, camvid_label_colors, ax=ax2)
    plot.show()
Пример #4
0
def main():
    chainer.config.train = False

    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained-model', default='camvid')
    parser.add_argument('image')
    args = parser.parse_args()

    model = SegNetBasic(n_class=len(camvid_label_names),
                        pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    labels = model.predict([img])
    label = labels[0]

    fig = plt.figure()
    ax1 = fig.add_subplot(1, 2, 1)
    vis_image(img, ax=ax1)
    ax2 = fig.add_subplot(1, 2, 2)
    # Do not overlay the label image on the color image
    vis_semantic_segmentation(None,
                              label,
                              camvid_label_names,
                              camvid_label_colors,
                              ax=ax2)
    plt.show()
Пример #5
0
 def test_vis_semantic_segmentation_mismatch_names_and_colors(self):
     label = np.random.randint(-1, 2, size=(48, 64)).astype(np.int32)
     with self.assertRaises(ValueError):
         vis_semantic_segmentation(None,
                                   label,
                                   label_names=('class0', 'class1',
                                                'class2'),
                                   label_colors=((255, 0, 0), (0, 255, 0)))
    def test_vis_semantic_segmentation_exceed_value(self):
        label = np.random.randint(10, 20, size=(48, 64)).astype(np.int32)

        if optional_modules:
            with self.assertRaises(ValueError):
                vis_semantic_segmentation(
                    label,
                    label_names=('class0', 'class1', 'class2'))
Пример #7
0
def demo_enet():
    """Demo ENet."""
    chainer.config.train = False
    chainer.config.enable_backprop = False

    config, img_path = parse_args()
    test_data = load_dataset_test(config["dataset"])
    test_iter = create_iterator_test(test_data, config['iterator'])
    model = get_model(config["model"])
    devices = parse_devices(config['gpus'])

    if devices:
        model.to_gpu(devices['main'])

    img = read_image(img_path)
    img = img.transpose(1, 2, 0)
    img = cv2.resize(img, (1024, 512)).transpose(2, 0, 1)

    for i in range(2):
        s = time.time()
        pred = model.predict(img)[0]
        print("time: {}".format(time.time() - s))
    # Save the result image
    ax = vis_image(img)
    _, legend_handles = vis_semantic_segmentation(
        pred,
        label_colors=cityscapes_label_colors,
        label_names=cityscapes_label_names,
        alpha=1.0,
        ax=ax)
    ax.legend(handles=legend_handles,
              bbox_to_anchor=(1.05, 1),
              loc=2,
              borderaxespad=0.)
    plot.axis('off')
    plot.show()

    fig, (ax1, ax2) = plot.subplots(ncols=2, figsize=(10, 3))
    img = read_image(
        img_path.replace("leftImg8bit",
                         "gtFine").replace("gtFine.png", "gtFine_color.png"))
    img = img.transpose(1, 2, 0)
    ax1.set_title("GroundTruth")
    img = cv2.resize(img, (1024, 512)).transpose(2, 0, 1)
    ax1 = vis_image(img, ax1)
    ax2 = vis_image(img, ax2)
    ax2, legend_handles = vis_semantic_segmentation(
        pred,
        label_colors=cityscapes_label_colors,
        label_names=cityscapes_label_names,
        alpha=1.0,
        ax=ax2)
    ax2.set_title("Prediction")
    ax1.axis("off")
    ax2.axis("off")
    plot.tight_layout()
    plot.savefig("./compare.png", dpi=400, bbox_inches='tight')
    plot.show()
    def test_vis_semantic_segmentation_mismatch_names_and_colors(self):
        label = np.random.randint(-1, 2, size=(48, 64)).astype(np.int32)

        if optional_modules:
            with self.assertRaises(ValueError):
                vis_semantic_segmentation(
                    label,
                    label_names=('class0', 'class1', 'class2'),
                    label_colors=((255, 0, 0), (0, 255, 0)))
Пример #9
0
def demo_linknet():
    """Demo LinkNet."""
    chainer.config.train = False
    chainer.config.enable_backprop = False

    config, img_path = parse_args()
    test_data = load_dataset_test(config["dataset"])
    test_iter = create_iterator_test(test_data, config['iterator'])
    model = get_model(config["model"])
    devices = parse_devices(config['gpus'])

    if devices:
        model.to_gpu(devices['main'])

    img = read_image(img_path)
    # img = img.transpose(1, 2, 0)
    # img = cv2.resize(img, (512, 256)).transpose(2, 0, 1)

    for i in range(2):
        s = time.time()
        pred = model.predict(img)[0]
        print("time: {}".format(time.time() - s))
    # Save the result image
    ax = vis_image(img)
    _, legend_handles = vis_semantic_segmentation(
        pred,
        label_colors=cityscapes_label_colors,
        label_names=cityscapes_label_names,
        alpha=1.0,
        ax=ax)
    ax.legend(handles=legend_handles,
              bbox_to_anchor=(1.05, 1),
              loc=2,
              borderaxespad=0.)
    plot.show()
Пример #10
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--pretrained-model')
    parser.add_argument('--input-size', type=int, default=713)
    parser.add_argument('image')
    args = parser.parse_args()

    label_names = cityscapes_semantic_segmentation_label_names
    colors = cityscapes_semantic_segmentation_label_colors
    n_class = len(label_names)

    input_size = (args.input_size, args.input_size)
    model = PSPNetResNet101(n_class, args.pretrained_model, input_size)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu(args.gpu)

    img = read_image(args.image)
    labels = model.predict([img])
    label = labels[0]

    fig = plt.figure()
    ax1 = fig.add_subplot(1, 2, 1)
    vis_image(img, ax=ax1)
    ax2 = fig.add_subplot(1, 2, 2)
    ax2, legend_handles = vis_semantic_segmentation(
        img, label, label_names, colors, ax=ax2)
    ax2.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2)

    plt.show()
    def test_vis_semantic_segmentation(self):
        if optional_modules:
            ax, legend_handles = vis_semantic_segmentation(
                self.label,
                label_names=self.label_names, label_colors=self.label_colors,
                all_label_names_in_legend=self.all_label_names_in_legend)

            self.assertIsInstance(ax, matplotlib.axes.Axes)
            for handle in legend_handles:
                self.assertIsInstance(handle, matplotlib.patches.Patch)
Пример #12
0
    def test_vis_semantic_segmentation(self):
        if optional_modules:
            ax, legend_handles = vis_semantic_segmentation(
                self.label,
                label_names=self.label_names,
                label_colors=self.label_colors,
                all_label_names_in_legend=self.all_label_names_in_legend)

            self.assertIsInstance(ax, matplotlib.axes.Axes)
            for handle in legend_handles:
                self.assertIsInstance(handle, matplotlib.patches.Patch)
Пример #13
0
 def visualize(self, i):
     img = self._get_image(i)
     depth = self._get_depth(i)
     lbl_img = self._get_lbl_img(i)
     f, axes = plt.subplots(1, 3, sharey=True)
     vis_image(img, ax=axes[0])
     axes[1].imshow(depth)
     _, legend_handles = vis_semantic_segmentation(
         img,
         lbl_img + 1,
         label_names=['background'] + self.label_names,
         ax=axes[2])
     axes[2].legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2)
     plt.show()
Пример #14
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--pretrained-model')
    parser.add_argument('--input-size', type=int, default=448)
    args = parser.parse_args()

    label_names = voc_semantic_segmentation_label_names
    colors = voc_semantic_segmentation_label_colors
    n_class = len(label_names)

    input_size = (args.input_size, args.input_size)
    model = get_pspnet_resnet50(n_class)
    chainer.serializers.load_npz(args.pretrained_model, model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu(args.gpu)

    dataset = get_sbd_augmented_voc()
    for i in range(1, 100):
        img = dataset[i][0]

        # img = read_image(args.image)
        labels = model.predict([img])
        label = labels[0]

        from chainercv.utils import write_image
        write_image(label[None], '{}.png'.format(i))

        fig = plt.figure()
        ax1 = fig.add_subplot(1, 2, 1)
        vis_image(img, ax=ax1)
        ax2 = fig.add_subplot(1, 2, 2)
        ax2, legend_handles = vis_semantic_segmentation(img,
                                                        label,
                                                        label_names,
                                                        colors,
                                                        ax=ax2)
        ax2.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2)

        plt.show()
Пример #15
0
    def visualize_cb(self, event):
        if (not self.visualize or self.img is None or self.header is None
                or self.label is None):
            return

        with self.lock:
            img = self.img.copy()
            header = copy.deepcopy(self.header)
            label = self.label.copy()

        fig = plt.figure(tight_layout={'pad': 0})
        ax = plt.subplot(1, 1, 1)
        ax.axis('off')
        ax, legend_handles = vis_semantic_segmentation(
            img.transpose((2, 0, 1)),
            label,
            label_names=self.label_names,
            alpha=0.7,
            all_label_names_in_legend=True,
            ax=ax)
        ax.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2)
        fig.canvas.draw()
        w, h = fig.canvas.get_width_height()
        vis_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
        vis_img.shape = (h, w, 3)
        fig.clf()
        plt.close()
        if self.pub_image.get_num_connections() > 0:
            vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8')
            # BUG: https://answers.ros.org/question/316362/sensor_msgsimage-generates-float-instead-of-int-with-python3/  # NOQA
            vis_msg.step = int(vis_msg.step)
            vis_msg.header = header
            self.pub_image.publish(vis_msg)
        if self.pub_image_compressed.get_num_connections() > 0:
            # publish compressed http://wiki.ros.org/rospy_tutorials/Tutorials/WritingImagePublisherSubscriber  # NOQA
            vis_compressed_msg = CompressedImage()
            vis_compressed_msg.header = header
            vis_compressed_msg.format = "jpeg"
            vis_img_rgb = cv2.cvtColor(vis_img, cv2.COLOR_BGR2RGB)
            vis_compressed_msg.data = np.array(
                cv2.imencode('.jpg', vis_img_rgb)[1]).tostring()
            self.pub_image_compressed.publish(vis_compressed_msg)
Пример #16
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--pretrained-model')
    parser.add_argument('--input-size', type=int, default=448)
    args = parser.parse_args()

    label_names = voc_semantic_segmentation_label_names
    colors = voc_semantic_segmentation_label_colors
    n_class = len(label_names)

    input_size = (args.input_size, args.input_size)
    model = get_pspnet_resnet50(n_class)
    chainer.serializers.load_npz(args.pretrained_model, model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu(args.gpu)

    dataset = get_sbd_augmented_voc()
    for i in range(1, 100):
        img = dataset[i][0]

        # img = read_image(args.image)
        labels = model.predict([img])
        label = labels[0]

        from chainercv.utils import write_image
        write_image(
            label[None], '{}.png'.format(i))

        fig = plt.figure()
        ax1 = fig.add_subplot(1, 2, 1)
        vis_image(img, ax=ax1)
        ax2 = fig.add_subplot(1, 2, 2)
        ax2, legend_handles = vis_semantic_segmentation(
            img, label, label_names, colors, ax=ax2)
        ax2.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2)

        plt.show()
    def image_cb(self, msg):
        img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='rgb8')
        H, W = img.shape[:2]
        input_H, input_W = self.input_shape
        input_tensor = cv2.resize(img, (input_W, input_H))
        input_tensor = input_tensor.flatten()

        _, label = self.engine.run_inference(input_tensor)
        label = label.reshape(self.input_shape)
        label = cv2.resize(label, (W, H), interpolation=cv2.INTER_NEAREST)
        label = label.astype(np.int32)

        label_msg = self.bridge.cv2_to_imgmsg(label, '32SC1')
        label_msg.header = msg.header
        self.pub_label.publish(label_msg)

        if self.visualize:
            fig = plt.figure(tight_layout={'pad': 0})
            ax = plt.subplot(1, 1, 1)
            ax.axis('off')
            ax, legend_handles = vis_semantic_segmentation(
                img.transpose((2, 0, 1)),
                label,
                label_names=self.label_names,
                alpha=0.7,
                all_label_names_in_legend=True,
                ax=ax)
            ax.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2)
            fig.canvas.draw()
            w, h = fig.canvas.get_width_height()
            vis_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
            vis_img.shape = (h, w, 3)
            fig.clf()
            plt.close()
            vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8')
            # BUG: https://answers.ros.org/question/316362/sensor_msgsimage-generates-float-instead-of-int-with-python3/  # NOQA
            vis_msg.step = int(vis_msg.step)
            vis_msg.header = msg.header
            self.pub_image.publish(vis_msg)
Пример #18
0
def save_semantic_image(image,
                        label,
                        label_names=label_name,
                        label_colors=label_color,
                        filename=None):
    ax, legend_handles = vis_semantic_segmentation(
        image,
        label,
        label_names=label_names,
        label_colors=label_colors,
        alpha=0.6,
        all_label_names_in_legend=False)

    # ax.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2)

    plt.tick_params(labelbottom=False,
                    labelleft=False,
                    labelright=False,
                    labeltop=False)

    plt.tick_params(bottom=False, left=False, right=False, top=False)
    plt.savefig(filename, bbox_inches='tight', pad_inches=0)
    plt.close()
Пример #19
0
    from chainercv.visualizations import vis_bbox
    from chainercv.visualizations import vis_semantic_segmentation
    from chainercv.chainer_experimental.datasets.sliceable import TransformDataset
    import matplotlib.pyplot as plt
    import numpy as np

    voc_segm = VOCSemanticSegmentationWithBboxDataset(split='aug')
    dataset = VOCSemanticSegmentationWithBboxDataset(
        split='aug').slice[:, ['img', 'bbox', 'label']]
    # transformed = TransformDataset(
    #     dataset, ('img', 'label_map'), grabcut_transform)
    transformed = TransformDataset(dataset, ('img', 'label_map'),
                                   SimpleDoesItTransform())

    indices = np.random.choice(np.arange(len(voc_segm)), size=(10, ))
    for index in indices:
        img, label_map, bbox, label = voc_segm[index]

        vis_bbox(img, bbox, label)
        plt.show()
        # see doc for better visualization
        vis_semantic_segmentation(img, label_map)
        plt.show()

        img, label_map = transformed[index]
        vis_semantic_segmentation(img,
                                  label_map,
                                  alpha=0.6,
                                  ignore_label_color=(255, 0, 0))
        plt.show()
Пример #20
0
if __name__ == '__main__':
    from chainercv.visualizations import vis_bbox
    from chainercv.visualizations import vis_semantic_segmentation
    from chainercv.chainer_experimental.datasets.sliceable import TransformDataset 
    import matplotlib.pyplot as plt
    import numpy as np

    voc_segm = VOCSemanticSegmentationWithBboxDataset(
        split='aug')
    dataset = VOCSemanticSegmentationWithBboxDataset(
        split='aug').slice[:, ['img', 'bbox', 'label']]
    # transformed = TransformDataset(
    #     dataset, ('img', 'label_map'), grabcut_transform)
    transformed = TransformDataset(
        dataset, ('img', 'label_map'), SimpleDoesItTransform())

    indices = np.random.choice(np.arange(len(voc_segm)), size=(10,))
    for index in indices:
        img, label_map, bbox, label = voc_segm[index]

        vis_bbox(img, bbox, label)
        plt.show()
        # see doc for better visualization
        vis_semantic_segmentation(img, label_map)
        plt.show()

        img, label_map = transformed[index]
        vis_semantic_segmentation(img, label_map, alpha=0.6, ignore_label_color=(255, 0, 0))
        plt.show()
Пример #21
0
    elif args.model == 'cityscapes':
        model = PSPNet(pretrained_model='cityscapes')
        labels = cityscapes_label_names
        colors = cityscapes_label_colors
    elif args.model == 'ade20k':
        model = PSPNet(pretrained_model='ade20k')
        labels = ade20k_label_names
        colors = ade20k_label_colors

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu(args.gpu)

    img = read_image(args.img_fn)
    pred = model.predict([img])[0]

    # Save the result image
    ax = vis_image(img)
    _, legend_handles = vis_semantic_segmentation(pred,
                                                  labels,
                                                  colors,
                                                  alpha=1.0,
                                                  ax=ax)
    ax.legend(handles=legend_handles,
              bbox_to_anchor=(1.05, 1),
              loc=2,
              borderaxespad=0.)
    base = os.path.splitext(os.path.basename(args.img_fn))[0]
    out_fn = os.path.join(args.out_dir, 'predict_{}.png'.format(base))
    plot.savefig(out_fn, bbox_inches='tight', dpi=400)