Beispiel #1
0
class TestPixelwiseSoftmaxClassifier(unittest.TestCase):
    def setUp(self):
        model = DummySemanticSegmentationModel(self.n_class)
        if self.class_weight:
            self.class_weight = [0.1 * i for i in range(self.n_class)]
        self.link = PixelwiseSoftmaxClassifier(model, self.ignore_label,
                                               self.class_weight)
        self.x = np.random.rand(2, 3, 16, 16).astype(np.float32)
        self.t = np.random.randint(self.n_class,
                                   size=(2, 16, 16)).astype(np.int32)

    def _check_call(self):
        xp = self.link.xp
        loss = self.link(chainer.Variable(xp.asarray(self.x)),
                         chainer.Variable(xp.asarray(self.t)))
        self.assertIsInstance(loss, chainer.Variable)
        self.assertIsInstance(loss.data, self.link.xp.ndarray)
        self.assertEqual(loss.shape, ())

        self.assertTrue(hasattr(self.link, 'y'))
        self.assertIsNotNone(self.link.y)

        self.assertTrue(hasattr(self.link, 'loss'))
        xp.testing.assert_allclose(self.link.loss.data, loss.data)

    def test_call_cpu(self):
        self._check_call()

    @attr.gpu
    def test_call_gpu(self):
        self.link.to_gpu()
        self._check_call()
class TestPixelwiseSoftmaxClassifier(unittest.TestCase):

    def setUp(self):
        model = DummySemanticSegmentationModel(self.n_class)
        if self.class_weight:
            self.class_weight = [0.1 * i for i in range(self.n_class)]
        self.link = PixelwiseSoftmaxClassifier(
            model, self.ignore_label, self.class_weight)
        self.x = np.random.rand(2, 3, 16, 16).astype(np.float32)
        self.t = np.random.randint(
            self.n_class, size=(2, 16, 16)).astype(np.int32)

    def _check_call(self):
        xp = self.link.xp
        loss = self.link(chainer.Variable(xp.asarray(self.x)),
                         chainer.Variable(xp.asarray(self.t)))
        self.assertIsInstance(loss, chainer.Variable)
        self.assertIsInstance(loss.array, self.link.xp.ndarray)
        self.assertEqual(loss.shape, ())

        self.assertTrue(hasattr(self.link, 'y'))
        self.assertIsNotNone(self.link.y)

        self.assertTrue(hasattr(self.link, 'loss'))
        xp.testing.assert_allclose(self.link.loss.array, loss.array)

    def test_call_cpu(self):
        self._check_call()

    @attr.gpu
    def test_call_gpu(self):
        self.link.to_gpu()
        self._check_call()
Beispiel #3
0
def train_one_epoch(model, train_data, lr, gpu, batchsize, out):
    train_model = PixelwiseSoftmaxClassifier(model)
    if gpu >= 0:
        # Make a specified GPU current
        chainer.cuda.get_device_from_id(gpu).use()
        train_model.to_gpu()  # Copy the model to the GPU
    log_trigger = (0.1, 'epoch')
    validation_trigger = (1, 'epoch')
    end_trigger = (1, 'epoch')

    train_data = TransformDataset(train_data, ('img', 'label_map'),
                                  SimpleDoesItTransform(model.mean))
    val = VOCSemanticSegmentationWithBboxDataset(
        split='val').slice[:, ['img', 'label_map']]

    # Iterator
    train_iter = iterators.MultiprocessIterator(train_data, batchsize)
    val_iter = iterators.MultiprocessIterator(val,
                                              1,
                                              shuffle=False,
                                              repeat=False,
                                              shared_mem=100000000)

    # Optimizer
    optimizer = optimizers.MomentumSGD(lr=lr, momentum=0.9)
    optimizer.setup(train_model)
    optimizer.add_hook(chainer.optimizer_hooks.WeightDecay(rate=0.0001))

    # Updater
    updater = training.updaters.StandardUpdater(train_iter,
                                                optimizer,
                                                device=gpu)

    # Trainer
    trainer = training.Trainer(updater, end_trigger, out=out)

    trainer.extend(extensions.LogReport(trigger=log_trigger))
    trainer.extend(extensions.observe_lr(), trigger=log_trigger)
    trainer.extend(extensions.dump_graph('main/loss'))

    if extensions.PlotReport.available():
        trainer.extend(
            extensions.PlotReport(['main/loss'],
                                  x_key='iteration',
                                  file_name='loss.png'))
        trainer.extend(
            extensions.PlotReport(['validation/main/miou'],
                                  x_key='iteration',
                                  file_name='miou.png'))

    trainer.extend(extensions.snapshot_object(model, filename='snapshot.npy'),
                   trigger=end_trigger)
    trainer.extend(extensions.PrintReport([
        'epoch', 'iteration', 'elapsed_time', 'lr', 'main/loss',
        'validation/main/miou', 'validation/main/mean_class_accuracy',
        'validation/main/pixel_accuracy'
    ]),
                   trigger=log_trigger)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    trainer.extend(SemanticSegmentationEvaluator(
        val_iter, model, voc_semantic_segmentation_label_names),
                   trigger=validation_trigger)
    trainer.run()