Ejemplo n.º 1
0
 def test_reproduce_same_permutation(self):
     dataset = [1, 2, 3, 4, 5, 6]
     order_sampler1 = iterators.ShuffleOrderSampler(
         numpy.random.RandomState(self._seed))
     it1 = iterators.MultiprocessIterator(
         dataset, 6, order_sampler=order_sampler1)
     order_sampler2 = iterators.ShuffleOrderSampler(
         numpy.random.RandomState(self._seed))
     it2 = iterators.MultiprocessIterator(
         dataset, 6, order_sampler=order_sampler2)
     for _ in range(5):
         self.assertEqual(it1.next(), it2.next())
Ejemplo n.º 2
0
def main():
    import multiprocessing
    multiprocessing.set_start_method('forkserver')

    parser = argparse.ArgumentParser(description='Cats training.')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=0,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--normalization',
                        type=str,
                        choices=NORMALIZATIONS,
                        required=True,
                        help='Normalization method')
    args = parser.parse_args()

    gpu = args.gpu
    out_dir = args.out
    image_dir = 'images'

    batch_size = 32
    short_edge = 256
    crop_edge = 224

    seed = 3141592653
    n_processes = len(os.sched_getaffinity(0))

    normalization = get_normalization(args.normalization)

    initial_lr = 0.1
    epochs = 300
    lr_reduce_interval = (100, 'epoch')
    lr_reduce_rate = 0.1
    weight_decay = 5e-4

    numpy_random = numpy.random.RandomState(seed)
    random = Random.from_numpy_random(numpy_random)
    train_dataset, valid_dataset, _ = CatsDataset.train_valid(
        image_dir, short_edge, crop_edge, random)
    order_sampler = iterators.ShuffleOrderSampler(numpy_random)
    train_iter = iterators.MultiprocessIterator(train_dataset,
                                                batch_size,
                                                repeat=True,
                                                shuffle=None,
                                                n_processes=n_processes,
                                                n_prefetch=4,
                                                order_sampler=order_sampler)
    valid_iter = iterators.MultiprocessIterator(valid_dataset,
                                                batch_size,
                                                repeat=False,
                                                shuffle=False,
                                                n_processes=n_processes,
                                                n_prefetch=4)

    numpy.random.seed(seed)
    model = ResNet50(len(CatsDataset.classes), normalization)
    model = chainer.links.Classifier(model)
    if gpu >= 0:
        chainer.cuda.get_device_from_id(gpu).use()
        model.to_gpu()

    optimizer = optimizers.MomentumSGD(lr=initial_lr)
    optimizer.setup(model)
    optimizer.add_hook(optimizer_hooks.WeightDecay(weight_decay))

    updater = training.updaters.StandardUpdater(train_iter,
                                                optimizer,
                                                device=gpu)
    trainer = training.Trainer(updater, (epochs, 'epoch'), out=out_dir)

    trainer.extend(extensions.ExponentialShift('lr', lr_reduce_rate),
                   trigger=lr_reduce_interval)
    trainer.extend(extensions.Evaluator(valid_iter, model, device=gpu),
                   trigger=(1, 'epoch'))

    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy',
            'validation/main/accuracy', 'elapsed_time'
        ]))

    trainer.run()

    chainer.serializers.save_npz(os.path.join(out_dir, 'model.npz'), model)