Пример #1
0
    def _check(self):
        if self.filename is None:
            extension = DetectionVisReport(self.iterator, self.link)
        else:
            extension = DetectionVisReport(self.iterator,
                                           self.link,
                                           filename=self.filename)

        if not _available:
            return

        for iter_ in range(3):
            self.trainer.updater.iteration = iter_
            extension(self.trainer)

            for idx in six.moves.range(len(self.dataset)):
                out_file = os.path.join(self.trainer.out,
                                        self.filename_func(iter_, idx))
                self.assertTrue(os.path.exists(out_file))
    def setUp(self):
        self.trainer = mock.MagicMock()
        self.out_dir = tempfile.mkdtemp()
        self.trainer.out = self.out_dir
        self.trainer.updater.iteration = 0

        model = ConstantReturnModel(np.random.uniform(size=(1, ) + self.shape))
        dataset = DummyDataset(shapes=[(3, 10, 10), self.shape],
                               dtypes=[np.float32, np.float32])

        self.extension = DetectionVisReport(self.indices,
                                            dataset,
                                            model,
                                            filename_base='detection')
Пример #3
0
class TestDetectionVisReport(unittest.TestCase):
    def setUp(self):
        self.trainer = mock.MagicMock()
        self.trainer.out = tempfile.mkdtemp()

        self.link = _RandomDetectionStubLink()
        self.dataset = TupleDataset(np.random.uniform(size=(10, 3, 32, 48)),
                                    np.random.uniform(size=(10, 5, 4)),
                                    np.random.randint(0, 19, size=(10, 5)))
        self.iterator = SerialIterator(self.dataset,
                                       10,
                                       repeat=False,
                                       shuffle=False)

    def test_available(self):
        self.extension = DetectionVisReport(self.dataset, self.link)
        self.assertEqual(self.extension.available(), _available)

    def _check(self):
        if self.filename is None:
            extension = DetectionVisReport(self.iterator, self.link)
        else:
            extension = DetectionVisReport(self.iterator,
                                           self.link,
                                           filename=self.filename)

        if not _available:
            return

        for iter_ in range(3):
            self.trainer.updater.iteration = iter_
            extension(self.trainer)

            for idx in six.moves.range(len(self.dataset)):
                out_file = os.path.join(self.trainer.out,
                                        self.filename_func(iter_, idx))
                self.assertTrue(os.path.exists(out_file))

    def test_cpu(self):
        self._check()

    @attr.gpu
    def test_gpu(self):
        self.link.to_gpu()
        self._check()
Пример #4
0
class TestDetectionVisReport(unittest.TestCase):

    def setUp(self):
        self.trainer = mock.MagicMock()
        self.trainer.out = tempfile.mkdtemp()

        self.link = _RandomDetectionStubLink()
        self.dataset = TupleDataset(
            np.random.uniform(size=(10, 3, 32, 48)),
            np.random.uniform(size=(10, 5, 4)),
            np.random.randint(0, 19, size=(10, 5)))
        self.iterator = SerialIterator(
            self.dataset, 10, repeat=False, shuffle=False)

    def test_available(self):
        self.extension = DetectionVisReport(self.dataset, self.link)
        self.assertEqual(self.extension.available(), optional_modules)

    def _check(self):
        if self.filename is None:
            extension = DetectionVisReport(self.iterator, self.link)
        else:
            extension = DetectionVisReport(
                self.iterator, self.link, filename=self.filename)

        if not optional_modules:
            return

        for iter_ in range(3):
            self.trainer.updater.iteration = iter_
            extension(self.trainer)

            for idx in six.moves.range(len(self.dataset)):
                out_file = os.path.join(
                    self.trainer.out, self.filename_func(iter_, idx))
                self.assertTrue(os.path.exists(out_file))

    def test_cpu(self):
        self._check()

    @attr.gpu
    def test_gpu(self):
        self.link.to_gpu()
        self._check()
Пример #5
0
 def test_available(self):
     self.extension = DetectionVisReport(self.dataset, self.link)
     self.assertEqual(self.extension.available(), _available)
Пример #6
0
from faster_rcnn import FasterRCNN

if __name__ == '__main__':
    test_data = VOCDetectionDataset(mode='train',
                                    use_cache=True,
                                    year='2007',
                                    bgr=True)
    wrappers = [
        lambda d: SubtractWrapper(d,
                                  value=np.array([103.939, 116.779, 123.68])),
        lambda d: ResizeWrapper(d,
                                preprocess_idx=0,
                                output_shape=output_shape_soft_min_hard_max(
                                    600, 1200),
                                hook=bbox_resize_hook(1)),
    ]
    for wrapper in wrappers:
        test_data = wrapper(test_data)

    model = FasterRCNN()
    chainer.serializers.load_npz('VGG16_faster_rcnn_final.model', model)
    trainer = mock.MagicMock()
    trainer.out = 'result'
    trainer.updater.iteration = 0

    extension = DetectionVisReport([3, 4, 5, 6, 7, 8],
                                   test_data,
                                   model,
                                   predict_func=model.predict_bboxes)
    extension(trainer)
Пример #7
0
 def test_available(self):
     self.extension = DetectionVisReport(self.dataset, self.link)
     self.assertEqual(self.extension.available(), optional_modules)
Пример #8
0
def main(gpu=-1, epoch=100, batch_size=1, lr=5e-4, out='result'):
    train_data = VOCDetectionDataset(mode='train', use_cache=True, year='2007')
    test_data = VOCDetectionDataset(mode='val', use_cache=True, year='2007')

    def transform(in_data):
        img, bbox = in_data
        img -= np.array([103.939, 116.779, 123.68])[:, None, None]

        # Resize bounding box to a shape
        # with the smaller edge at least at length 600
        input_shape = img.shape[1:]
        output_shape = _shape_soft_min_hard_max(input_shape, 600, 1200)
        img = transforms.resize(img, output_shape)
        bbox = transforms.bbox_resize(bbox, input_shape, output_shape)

        # horizontally flip
        img, flips = transforms.random_flip(img,
                                            horizontal_flip=True,
                                            return_flip=True)
        h_flip = flips['h']
        bbox = transforms.bbox_flip(bbox, output_shape, h_flip)
        return img, bbox

    transforms.extend(train_data, transform)
    transforms.extend(test_data, transform)

    model = FasterRCNN(gpu=gpu)
    if gpu != -1:
        model.to_gpu(gpu)
        chainer.cuda.get_device(gpu).use()
    # optimizer = chainer.optimizers.MomentumSGD(lr=lr)
    optimizer = chainer.optimizers.Adam(alpha=0.001,
                                        beta1=0.9,
                                        beta2=0.999,
                                        eps=1e-8)
    optimizer.setup(model)
    # optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005))

    train_iter = chainer.iterators.SerialIterator(test_data, batch_size=1)
    updater = ParallelUpdater(train_iter, optimizer, devices={'main': gpu})

    # updater = chainer.training.updater.StandardUpdater(train_iter, optimizer)
    trainer = training.Trainer(updater, (epoch, 'epoch'), out=out)

    log_interval = 20, 'iteration'
    val_interval = 3000, 'iteration'
    trainer.extend(extensions.LogReport(trigger=log_interval))
    trainer.extend(extensions.PrintReport([
        'iteration', 'main/time', 'main/rpn_loss_cls', 'main/rpn_loss_bbox',
        'main/loss_cls', 'main/loss_bbox'
    ]),
                   trigger=log_interval)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    # visualize training
    trainer.extend(extensions.PlotReport(['main/rpn_loss_cls'],
                                         file_name='rpn_loss_cls.png'),
                   trigger=log_interval)
    trainer.extend(extensions.PlotReport(['main/rpn_loss_bbox'],
                                         file_name='rpn_loss_bbox.png'),
                   trigger=log_interval)
    trainer.extend(extensions.PlotReport(['main/loss_cls'],
                                         file_name='loss_cls.png'),
                   trigger=log_interval)
    trainer.extend(extensions.PlotReport(['main/loss_bbox'],
                                         file_name='loss_bbox.png'),
                   trigger=log_interval)
    trainer.extend(
        DetectionVisReport(
            range(10),  # visualize outputs for the first 10 data of test_data
            train_data,
            model,
            filename_base='detection_train',
            predict_func=model.predict_bboxes),
        trigger=val_interval,
        invoke_before_training=True)
    trainer.extend(
        DetectionVisReport(
            range(10),  # visualize outputs for the first 10 data of test_data
            test_data,
            model,
            forward_func=model.predict_bboxes),
        trigger=val_interval,
        invoke_before_training=True)

    trainer.extend(extensions.dump_graph('main/loss'))

    trainer.run()