Ejemplo n.º 1
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_label(label, camvid_label_names, camvid_label_colors, ax=ax2)
    plot.show()
Ejemplo n.º 2
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()
Ejemplo n.º 3
0
def main():
    chainer.config.train = False

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

    if args.dataset == 'camvid':
        if args.pretrained_model is None:
            args.pretrained_model = 'camvid'
        label_names = camvid_label_names
        colors = camvid_label_colors

    model = SegNetBasic(n_class=len(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, label_names, colors, ax=ax2)
    plt.show()
Ejemplo n.º 4
0
class TestSegNetBasic(unittest.TestCase):
    def setUp(self):
        self.n_class = 10
        self.link = SegNetBasic(n_class=self.n_class)

    def check_call(self):
        xp = self.link.xp
        x = chainer.Variable(
            xp.random.uniform(low=-1, high=1,
                              size=(2, 3, 128, 160)).astype(np.float32))
        y = self.link(x)

        self.assertIsInstance(y, chainer.Variable)
        self.assertIsInstance(y.data, xp.ndarray)
        self.assertEqual(y.shape, (2, self.n_class, 128, 160))

    def test_call_cpu(self):
        self.check_call()

    @attr.gpu
    def test_call_gpu(self):
        self.link.to_gpu()
        self.check_call()

    def check_predict(self):
        hs = np.random.randint(128, 160, size=(2, ))
        ws = np.random.randint(128, 160, size=(2, ))
        imgs = [
            np.random.uniform(size=(3, hs[0], ws[0])).astype(np.float32),
            np.random.uniform(size=(3, hs[1], ws[1])).astype(np.float32),
        ]

        labels = self.link.predict(imgs)

        self.assertEqual(len(labels), 2)
        for i in range(2):
            self.assertIsInstance(labels[i], np.ndarray)
            self.assertEqual(labels[i].shape, (hs[i], ws[i]))
            self.assertEqual(labels[i].dtype, np.int64)

    def test_predict_cpu(self):
        self.check_predict()

    @attr.gpu
    def test_predict_gpu(self):
        self.link.to_gpu()
        self.check_predict()