def main(args): timer = Timer() print('create images...') timer.reset() # 入力画像の生成 x = create(args.other_path, args.human_path, args.background_path, args.obj_size, args.img_size, args.obj_num, args.human_num, args.img_num) timer.view() print('save images...') timer.reset() # 保存するパスを取得 w_path = [ F.getFilePath(args.out_path, GET.datetimeSHA(GET.randomStr(10), str_len=12), '.jpg') for i in x ] # 画像を保存する [cv2.imwrite(w, i) for i, w in zip(x, w_path)] timer.view() print('save param...') timer.reset() # 画像の生成に使用したパラメータを保存する F.dict2json(args.out_path, 'dataset', F.args2dict(args)) timer.view()
def main(args): print('create images...') x, y = create(args.other_path, args.human_path, args.background_path, args.obj_size, args.img_size, args.obj_num, args.human_num, args.img_num) # 画像の並び順をシャッフルするための配列を作成する # compとrawの対応を崩さないようにシャッフルしなければならない # また、train_sizeで端数を切り捨てる # データをint8からfloat16に変えるとデータ数が大きくなるので注意 print('shuffle images...') dtype = np.float16 shuffle = np.random.permutation(range(len(x))) train_size = int(len(x) * args.train_per_all) train_x = IMG.imgs2arr(x[shuffle[:train_size]], dtype=dtype) train_y = IMG.imgs2arr(y[shuffle[:train_size]], dtype=dtype) test_x = IMG.imgs2arr(x[shuffle[train_size:]], dtype=dtype) test_y = IMG.imgs2arr(y[shuffle[train_size:]], dtype=dtype) print('train x/y:{0}/{1}'.format(train_x.shape, train_y.shape)) print('test x/y:{0}/{1}'.format(test_x.shape, test_y.shape)) print('save param...') F.dict2json(args.out_path, 'dataset', F.args2dict(args)) # 生成したデータをnpz形式でデータセットとして保存する # ここで作成したデータの中身を確認する場合はnpz2jpg.pyを使用するとよい print('save npz...') saveNPZ(train_x, train_y, 'train', args.out_path, args.img_size) saveNPZ(test_x, test_y, 'test', args.out_path, args.img_size)
def main(args): # フォント画像の読み込みと分割する print('read and split images...') fonts, _ = I.cnv.splitSQN(I.io.readN(args.font, 3), args.font_size) print(fonts.shape) param = F.args2dict(args) param['shape'] = fonts.shape # 正解画像の生成と保存 max_folder = 4000 proc = 7 print('create and save images...') for i in range(0, args.img_num, max_folder): num = np.min([max_folder, args.img_num - i]) [ cv2.imwrite(getPath(args.out_path, i // max_folder), j) for j in createN(fonts, args.img_size, args.font_num, num, processes=proc) ] print('save param...') F.dict2json(args.out_path, 'dataset', param)
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 )