def test_optimizer(self): self.assertEqual(GET.optimizer('adam').__class__.__name__, 'Adam') self.assertEqual(GET.optimizer('ada_d').__class__.__name__, 'AdaDelta') self.assertEqual(GET.optimizer('ada_g').__class__.__name__, 'AdaGrad') self.assertEqual( GET.optimizer('m_sgd').__class__.__name__, 'MomentumSGD') self.assertEqual( GET.optimizer('n_ag').__class__.__name__, 'NesterovAG') self.assertEqual(GET.optimizer('rmsp').__class__.__name__, 'RMSprop') self.assertEqual( GET.optimizer('rmsp_g').__class__.__name__, 'RMSpropGraves') self.assertEqual(GET.optimizer('sgd').__class__.__name__, 'SGD') self.assertEqual(GET.optimizer('smorms').__class__.__name__, 'SMORMS3') self.assertEqual(GET.optimizer('test').__class__.__name__, 'Adam') self.assertEqual(GET.optimizer('').__class__.__name__, 'Adam')
def main(args): # 各種データをユニークな名前で保存するために時刻情報を取得する exec_time = GET.datetimeSHA() # Load dataset train, test, n_out = getDataset(args.in_path) # モデルを決定する actfun = GET.actfun(args.actfun) model = L.Classifier(CNT(n_out, args.n_unit, actfun, args.dropout)) if args.gpu_id >= 0: # Make a specified GPU current chainer.backends.cuda.get_device_from_id(args.gpu_id).use() model.to_gpu() # Copy the model to the GPU chainer.global_config.autotune = True # else: # model.to_intel64() # Setup an optimizer optimizer = GET.optimizer(args.optimizer).setup(model) for func_name in model.predictor.base._children: for param in model.predictor.base[func_name].params(): param.update_rule.hyperparam.alpha *= args.alpha # Setup iterator train_iter = MultiprocessIterator(train, args.batchsize) test_iter = MultiprocessIterator(test, args.batchsize, repeat=False, shuffle=False) # 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( extensions.PlotReport( ['main/accuracy', 'validation/main/accuracy'], 'epoch', file_name='acc.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', 'main/accuracy', 'validation/main/accuracy', # 'lr', 'elapsed_time' ])) # Print a progress bar to stdout trainer.extend(extensions.ProgressBar()) if args.resume: # Resume from a snapshot 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)) # predict.pyでモデルを決定する際に必要なので記憶しておく model_param = F.args2dict(args) model_param['shape'] = train[0][0].shape model_param['n_out'] = n_out if args.only_check is False: # predict.pyでモデルのパラメータを読み込むjson形式で保存する with open(F.getFilePath(args.out_path, exec_time, '.json'), 'w') as f: json.dump(model_param, f, indent=4, sort_keys=True) # Run the training trainer.run() # 最後にモデルを保存する # スナップショットを使ってもいいが、 # スナップショットはファイルサイズが大きいので chainer.serializers.save_npz( F.getFilePath(args.out_path, exec_time, '.model'), model)
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 )