Example #1
0
def get_C100():
    """CIFAR-100 dataset without augmentation"""
    train, test = cifar.get_cifar100()
    train_dataset = CIFARDataset(train, C100_MEAN, C100_STD, False)
    test_dataset = CIFARDataset(test, C100_MEAN, C100_STD, False)

    return train_dataset, test_dataset
 def __init__(self,
              root=os.path.join("~", ".chainer", "datasets", "cifar100"),
              mode="train",
              transform=None):
     assert (root is not None)
     self.transform = transform
     train_ds, test_ds = get_cifar100()
     self.base = train_ds if mode == "train" else test_ds
def get_val_data_iterator(dataset_name, batch_size, num_workers):

    if dataset_name == "CIFAR10":
        _, test_ds = cifar.get_cifar10()
    elif dataset_name == "CIFAR100":
        _, test_ds = cifar.get_cifar100()
    elif dataset_name == "SVHN":
        _, test_ds = svhn.get_svhn()
    else:
        raise Exception('Unrecognized dataset: {}'.format(dataset_name))

    val_dataset = test_ds
    val_dataset_len = len(val_dataset)

    val_iterator = iterators.MultiprocessIterator(dataset=val_dataset,
                                                  batch_size=batch_size,
                                                  repeat=False,
                                                  shuffle=False,
                                                  n_processes=num_workers,
                                                  shared_mem=300000000)

    return val_iterator, val_dataset_len
Example #4
0
def main(args):
    assert((args.depth - args.block - 1) % args.block == 0)
    n_layer = int((args.depth - args.block - 1) / args.block)
    if args.dataset == 'cifar10':
        train, test = cifar.get_cifar10()
        n_class = 10
    elif args.dataset == 'cifar100':
        train, test = cifar.get_cifar100()
        n_class = 100
    elif args.dataset == 'SVHN':
        raise NotImplementedError()

    images = convert.concat_examples(train)[0]
    mean = images.mean(axis=(0, 2, 3))
    std = images.std(axis=(0, 2, 3))

    train = PreprocessedDataset(train, mean, std, random=args.augment)
    test = PreprocessedDataset(test, mean, std)

    train_iter = chainer.iterators.SerialIterator(
        train, args.batchsize / args.split_size)
    test_iter = chainer.iterators.SerialIterator(
        test, args.batchsize / args.split_size, repeat=False, shuffle=False)

    model = chainer.links.Classifier(DenseNet(
        n_layer, args.growth_rate, n_class, args.drop_ratio, 16, args.block))
    if args.init_model:
        serializers.load_npz(args.init_model, model)
    chainer.cuda.get_device(args.gpu).use()
    model.to_gpu()

    optimizer = chainer.optimizers.NesterovAG(lr=args.lr, momentum=0.9)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(args.weight_decay))

    updater = StandardUpdater(
        train_iter, optimizer, (args.split_size, 'mean'), device=args.gpu)
    trainer = training.Trainer(updater, (300, 'epoch'), out=args.dir)

    val_interval = (1, 'epoch')
    log_interval = (1, 'epoch')

    def lr_shift():  # DenseNet specific!
        if updater.epoch == 150 or updater.epoch == 225:
            optimizer.lr *= 0.1
        return optimizer.lr

    trainer.extend(Evaluator(
        test_iter, model, device=args.gpu), trigger=val_interval)
    trainer.extend(extensions.observe_value(
        'lr', lambda _: lr_shift()), trigger=(1, 'epoch'))
    trainer.extend(extensions.dump_graph('main/loss'))
    trainer.extend(extensions.snapshot_object(
        model, 'epoch_{.updater.epoch}.model'), trigger=val_interval)
    trainer.extend(extensions.snapshot_object(
        optimizer, 'epoch_{.updater.epoch}.state'), trigger=val_interval)
    trainer.extend(extensions.LogReport(trigger=log_interval))
    start_time = time.time()
    trainer.extend(extensions.observe_value(
        'time', lambda _: time.time() - start_time), trigger=log_interval)
    trainer.extend(extensions.PrintReport([
        'time', 'epoch', 'iteration', 'main/loss', 'validation/main/loss',
        'main/accuracy', 'validation/main/accuracy', 'lr',
    ]), trigger=log_interval)
    trainer.extend(extensions.observe_value(
        'graph', lambda _: create_fig(args.dir)),
        trigger=(1, 'epoch'), priority=50)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    trainer.run()
Example #5
0
    np.random.seed(args.seed)
    if len(args.gpus) > 1 or args.gpus[0] >= 0:
        chainer.cuda.cupy.random.seed(args.seed)
        gpus = {'main': args.gpus[0]}
        if len(args.gpus) > 1:
            gpus.update({'gpu{}'.format(i): i for i in args.gpus[1:]})
        args.gpus = gpus

    # print gpus
    with cp.cuda.Device(gpus['main']):

        if args.dataset == 'cifar10':
            train, valid = cifar.get_cifar10(scale=255.)
            n_class = 10
        elif args.dataset == 'cifar100':
            train, valid = cifar.get_cifar100(scale=255.)
            n_class = 100

        # Enable autotuner of cuDNN
        chainer.config.autotune = True
        # Load model
        ext = os.path.splitext(args.model_file)[1]
        mod_path = '.'.join(os.path.split(args.model_file.replace(ext, '')))
        mod = import_module(mod_path)
        net = getattr(mod, args.model_name)(n_class=n_class)

        # create result dir
        result_dir = create_result_dir(args.model_name)
        shutil.copy(
            args.model_file,
            os.path.join(result_dir, os.path.basename(args.model_file)))
Example #6
0
def main(args):

    assert ((args.depth - args.block - 1) % args.block == 0)
    n_layer = (args.depth - args.block - 1) / args.block
    if args.dataset == 'cifar10':
        train, test = cifar.get_cifar10()
        n_class = 10
    elif args.dataset == 'cifar100':
        train, test = cifar.get_cifar100()
        n_class = 100
    elif args.dataset == 'SVHN':
        raise NotImplementedError()

    mean = numpy.zeros((3, 32, 32), dtype=numpy.float32)
    for image, _ in train:
        mean += image / len(train)

    train = PreprocessedDataset(train, mean, random=True)
    test = PreprocessedDataset(test, mean)

    train_iter = chainer.iterators.MultiprocessIterator(train, args.batchsize)
    test_iter = chainer.iterators.MultiprocessIterator(test,
                                                       args.batchsize,
                                                       repeat=False,
                                                       shuffle=False)

    model = chainer.links.Classifier(
        DenseNet(n_layer, args.growth_rate, n_class, args.drop_ratio, 16,
                 args.block))
    if args.init_model:
        serializers.load_npz(args.init_model, model)

    optimizer = chainer.optimizers.MomentumSGD(lr=args.lr / len(args.gpus),
                                               momentum=0.9)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(args.weight_decay))

    devices = {'main': args.gpus[0]}
    if len(args.gpus) > 2:
        for gid in args.gpus[1:]:
            devices['gpu%d' % gid] = gid
    updater = training.ParallelUpdater(train_iter, optimizer, devices=devices)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.dir)

    val_interval = (1, 'epoch')
    log_interval = (1, 'epoch')

    eval_model = model.copy()
    eval_model.train = False

    trainer.extend(extensions.Evaluator(test_iter,
                                        eval_model,
                                        device=args.gpus[0]),
                   trigger=val_interval)
    trainer.extend(extensions.ExponentialShift('lr', args.lr_decay_ratio),
                   trigger=(args.lr_decay_freq, 'epoch'))
    trainer.extend(extensions.dump_graph('main/loss'))
    trainer.extend(extensions.snapshot_object(model,
                                              'epoch_{.updater.epoch}.model'),
                   trigger=val_interval)
    trainer.extend(extensions.snapshot_object(optimizer,
                                              'epoch_{.updater.epoch}.state'),
                   trigger=val_interval)
    trainer.extend(extensions.LogReport(trigger=log_interval))
    trainer.extend(extensions.observe_lr(), trigger=log_interval)
    start_time = time.time()
    trainer.extend(extensions.observe_value(
        'time', lambda _: time.time() - start_time),
                   trigger=log_interval)
    trainer.extend(extensions.PrintReport([
        'time',
        'epoch',
        'iteration',
        'main/loss',
        'validation/main/loss',
        'main/accuracy',
        'validation/main/accuracy',
        'lr',
    ]),
                   trigger=log_interval)
    trainer.extend(extensions.observe_value('graph',
                                            lambda _: create_fig(args.dir)),
                   trigger=(2, 'epoch'))
    trainer.extend(extensions.ProgressBar(update_interval=10))

    trainer.run()
Example #7
0
def get_cifar100_dataset(scale):
    train, valid = cifar.get_cifar100(scale=scale)
    train = PreprocessedCifar100Dataset(train)
    valid = PreprocessedCifar100Dataset(valid)

    return train, valid
Example #8
0
import pickle
import os
import numpy as np
import chainer.links as L
import Mynet
from chainer import serializers
import chainer
import VGG_chainer
import cv2 as cv

# In[]
os.getcwd()
os.chdir('C:/Users/mikiu/Desktop/det_rec_mov_obj')
os.getcwd()
# In[]
train, test = cifar.get_cifar100(withlabel=True)
test
# In[]
x,t=test[10]
t
# In[]
z,w = train[10]
w
# In[]
plt.imshow(x.transpose(1,2,0))
plt.imshow(z.transpose(1,2,0))
# In[]
def unpickle(file):
    # file.decode('utf-8')
    fo = open(file, 'rb')
    # fo.decode('utf-8')
Example #9
0
def train(model_object, train_dataset=None, test_dataset=None):
    # define options
    parser = argparse.ArgumentParser(
        description='Training script of Tiny Model on CIFAR-10 dataset')
    parser.add_argument('--epoch', '-e', type=int, default=20,
                        help='Number of epochs to train')
    parser.add_argument('--gpu', '-g', type=int, default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--initmodel',
                        help='Initialize the model from given file')
    parser.add_argument('--resume', '-r', default='',
                        help='Initialize the trainer from given file')
    parser.add_argument('--out', '-o', default='result',
                        help='Output directory')
    parser.add_argument('--batchsize', '-b', type=int, default=64,
                        help='Validation minibatch size')
    parser.add_argument('--numlayers', '-L', type=int, default=40,
                        help='Number of layers')
    parser.add_argument('--growth', '-G', type=int, default=12,
                        help='Growth rate parameter')
    parser.add_argument('--dropout', '-D', type=float, default=0.2,
                        help='Dropout ratio')
    parser.add_argument('--dataset', type=str, default='cifar10',
                        choices=('cifar10', 'cifar100'),
                        help='Dataset used for training (Default is cifar10)')
    args = parser.parse_args()

    # 1. Dataset
    if args.dataset == 'cifar10':
        train, test = cifar.get_cifar10()
    elif args.dataset == 'cifar10':
        train, test = cifar.get_cifar100()

    # 2. Iterator
    train_iter = iterators.SerialIterator(train, args.batchsize)
    test_iter = iterators.SerialIterator(test, args.batchsize, False, False)

    # 3. Model
    model = L.Classifier(model_object)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    # 4. Optimizer
    optimizer = optimizers.Adam()
    optimizer.setup(model)

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

    # 6. Trainer
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    # 7. Evaluator

    class TestModeEvaluator(extensions.Evaluator):

        def evaluate(self):
            model = self.get_target('main')
            model.train = False
            ret = super(TestModeEvaluator, self).evaluate()
            model.train = True
            return ret

    trainer.extend(extensions.LogReport())
    trainer.extend(TestModeEvaluator(test_iter, model, device=args.gpu))
    trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'main/accuracy', 'validation/main/loss', 'validation/main/accuracy', 'elapsed_time']))
    trainer.extend(extensions.PlotReport(['main/loss', 'validation/main/loss'], x_key='epoch', file_name='loss.png'))
    trainer.extend(extensions.PlotReport(['main/accuracy', 'validation/main/accuracy'], x_key='epoch', file_name='accuracy.png'))
    trainer.extend(extensions.snapshot(), trigger=(10, 'epoch'))
    trainer.extend(extensions.snapshot_object(
                   model, 'model_{.updater.epoch}.npz'))
    trainer.run()
    del trainer

    return model
Example #10
0
import chainer.functions as F
import chainer.links as L
from chainer import Chain
import chainer.optimizers as optimizers

from chainer.datasets.cifar import get_cifar100
from chainer import optimizers, training
from chainer.training import extensions


# In[3]:


# データセットがダウンロード済みでなければ、ダウンロードも行う
train, test = get_cifar100()
train, validation = chainer.datasets.split_dataset_random(train, 40000, seed=0)


# In[ ]:


class MyConvNet(Chain):
    def __init__(self):
        super(MyConvNet, self).__init__()
        with self.init_scope():
            # 畳み込み層の定義
            # in_channels:Noneを指定しても動的にメモリ確保するので問題なく動作する
            # out_channels:出力する配列のチャンネル数
            # ksize:フィルタのサイズ(平行移動するフィルターの長さを指定)
            # stride:入力データに対してstride分フィルターを適用していくパラメータを指定
Example #11
0
def main():
    parser = argparse.ArgumentParser(
        description='Shake-shake resularization CIFAR10 w/ Chainer')
    parser.add_argument('--dataset',
                        '-d',
                        default='cifar10',
                        help='The dataset to use: cifar10 or cifar100')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=128,
                        help='Number of images in each mini-batch')
    parser.add_argument('--lr',
                        '-l',
                        type=float,
                        default=0.1,
                        help='Learning rate for SGD')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=1800,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--base_width',
                        '-w',
                        type=int,
                        default=64,
                        help='Base width parameter for Shake-Shake model')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=0,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out',
                        '-o',
                        default='run_0',
                        help='Directory to output the result')
    parser.add_argument('--resume',
                        '-r',
                        default='',
                        help='Resume the training from snapshot')
    parser.add_argument('--nobar',
                        dest='bar',
                        action='store_false',
                        help='Disable ProgressBar extension')
    args = parser.parse_args()

    print('GPU: {}'.format(args.gpu))
    print('# Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('')

    log_dir = os.path.join("results", args.out)
    writer = SummaryWriter(log_dir=log_dir)

    # Set up a neural network to train.
    # Classifier reports softmax cross entropy loss and accuracy at every
    # iteration, which will be used by the PrintReport extension below.
    if args.dataset == 'cifar10':
        print('Using CIFAR10 dataset.')
        class_labels = 10
        train, test = cifar.get_cifar10(scale=255.)
    elif args.dataset == 'cifar100':
        print('Using CIFAR100 dataset.')
        class_labels = 100
        train, test = cifar.get_cifar100(scale=255.)
    else:
        raise RuntimeError('Invalid dataset choice.')

    # Data preprocess
    mean = np.mean([x for x, _ in train], axis=(0, 2, 3))
    std = np.std([x for x, _ in train], axis=(0, 2, 3))

    train_transfrom = partial(transform, mean=mean, std=std, train=True)
    test_transfrom = partial(transform, mean=mean, std=std, train=False)

    train = TransformDataset(train, train_transfrom)
    test = TransformDataset(test, test_transfrom)

    print('Finised data preparation. Preparing for model training...')

    # Model and optimizer configuration
    model = L.Classifier(ShakeResNet(class_labels, base_width=args.base_width))

    if args.gpu >= 0:
        # Make a specified GPU current
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()  # Copy the model to the GPU

    optimizer = chainer.optimizers.MomentumSGD(args.lr, momentum=0.9)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(1e-4))

    # Set up a trainer
    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test,
                                                 args.batchsize,
                                                 repeat=False,
                                                 shuffle=False)

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

    # Evaluate the model with the test dataset for each epoch
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))

    # Decrease learning rate with cosine annealing
    trainer.extend(LrSceduler_CosineAnneal(args.lr, args.epoch))

    # Dump a computational graph from 'loss' variable at the first iteration
    # The "main" refers to the target link of the "main" optimizer.
    trainer.extend(extensions.dump_graph('main/loss'))

    # Take a snapshot at each epoch
    trainer.extend(extensions.snapshot(filename='training_chekpoint'))

    # Take a snapshot of the current best model
    trigger_save_model = triggers.MaxValueTrigger('validation/main/accuracy')
    trainer.extend(extensions.snapshot_object(model, filename='best_model'),
                   trigger=trigger_save_model)

    # Write a log of evaluation statistics for each epoch
    trainer.extend(extensions.LogReport())

    # Monitor learning rate at every iteration
    trainer.extend(extensions.observe_lr(), trigger=(1, 'iteration'))

    # Save two plot images to the result dir
    if extensions.PlotReport.available():
        trainer.extend(
            extensions.PlotReport(['main/loss', 'validation/main/loss'],
                                  'epoch',
                                  file_name='loss.png'))

        trainer.extend(
            extensions.PlotReport(
                ['main/accuracy', 'validation/main/accuracy'],
                'epoch',
                file_name='accuracy.png'))

        trainer.extend(
            extensions.PlotReport(['lr'], 'epoch', file_name='lr.png'))

    # Print selected entries of the log to stdout
    # Here "main" refers to the target link of the "main" optimizer again, and
    # "validation" refers to the default name of the Evaluator extension.
    # Entries other than 'epoch' are reported by the Classifier link, called by
    # either the updater or the evaluator.
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy',
            'validation/main/accuracy', 'lr', 'elapsed_time'
        ]))

    if args.bar:
        # Print a progress bar to stdout
        trainer.extend(extensions.ProgressBar())

    # Write training log to TensorBoard log file
    trainer.extend(
        TensorboardLogger(writer, [
            'main/loss', 'validation/main/loss', 'main/accuracy',
            'validation/main/accuracy', 'lr'
        ]))

    if args.resume:
        # Resume from a snapshot
        chainer.serializers.load_npz(args.resume, trainer)

    print("Finished preparation. Starting model training...")
    print()

    # Run the training
    trainer.run()