Example #1
0
 def test_lossfun(self):
     self.assertEqual(GET.lossfun('mse').__name__, 'mean_squared_error')
     self.assertEqual(GET.lossfun('mae').__name__, 'mean_absolute_error')
     self.assertEqual(GET.lossfun('ber').__name__, 'bernoulli_nll')
     self.assertEqual(
         GET.lossfun('gauss_kl').__name__, 'gaussian_kl_divergence')
     self.assertEqual(GET.lossfun('test').__name__, 'mean_squared_error')
     self.assertEqual(GET.lossfun('').__name__, 'mean_squared_error')
Example #2
0
def main(args):
    # 各種データをユニークな名前で保存するために時刻情報を取得する
    exec_time = GET.datetimeSHA()

    # 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.

    # 活性化関数を取得する
    actfun1 = GET.actfun(args.actfun1)
    actfun2 = GET.actfun(args.actfun2)
    # モデルを決定する
    if args.network == 0:
        from Lib.network import JC_DDUU as JC
    else:
        from Lib.network2 import JC_UDUD as JC

    model = L.Classifier(
        JC(n_unit=args.unit, layer=args.layer_num, rate=args.shuffle_rate,
           actfun1=actfun1, actfun2=actfun2, dropout=args.dropout,
           view=args.only_check),
        lossfun=GET.lossfun(args.lossfun)
    )
    # Accuracyは今回使用しないのでFalseにする
    # もしも使用したいのであれば、自分でAccuracyを評価する関数を作成する必要あり?
    model.compute_accuracy = False

    # Setup an optimizer
    optimizer = GET.optimizer(args.optimizer).setup(model)

    # Load dataset
    train, test, _ = GET.imgData(args.in_path)
    train = ResizeImgDataset(train, args.shuffle_rate)
    test = ResizeImgDataset(test, args.shuffle_rate)
    # predict.pyでモデルを決定する際に必要なので記憶しておく
    model_param = F.args2dict(args)
    model_param['shape'] = train[0][0].shape

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

    # Set up a trainer
    updater = training.StandardUpdater(
        train_iter, optimizer, device=args.gpu_id
    )
    trainer = training.Trainer(
        updater, (args.epoch, 'epoch'), out=args.out_path
    )

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

    # 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', out_name=exec_time + '_graph.dot')
    )

    # Take a snapshot for each specified epoch
    frequency = args.epoch if args.frequency == -1 else max(1, args.frequency)
    trainer.extend(
        extensions.snapshot(filename=exec_time + '_{.updater.epoch}.snapshot'),
        trigger=(frequency, 'epoch')
    )

    # Write a log of evaluation statistics for each epoch
    trainer.extend(extensions.LogReport(log_name=exec_time + '.log'))
    # trainer.extend(extensions.observe_lr())

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

        # trainer.extend(
        #     PlotReportLog(['lr'],
        #                   'epoch', file_name='lr.png', val_pos=(-80, -60))
        # )

    # 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',
        # 'lr',
        'elapsed_time'
    ]))

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

    # Resume from a snapshot
    if args.resume:
        chainer.serializers.load_npz(args.resume, trainer)
        # Set pruning
        # http://tosaka2.hatenablog.com/entry/2017/11/17/194051
        masks = pruning.create_model_mask(model, args.pruning, args.gpu_id)
        trainer.extend(pruning.pruned(model, masks))

    # Make a specified GPU current
    if args.gpu_id >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu_id).use()
        # Copy the model to the GPU
        model.to_gpu()
        chainer.global_config.autotune = True
    else:
        model.to_intel64()

    # predict.pyでモデルのパラメータを読み込むjson形式で保存する
    if args.only_check is False:
        F.dict2json(args.out_path, exec_time + '_train', model_param)

    # Run the training
    trainer.run()

    # 最後にモデルを保存する
    # スナップショットを使ってもいいが、
    # スナップショットはファイルサイズが大きい
    chainer.serializers.save_npz(
        F.getFilePath(args.out_path, exec_time, '.model'),
        model
    )