def main(): parser = argparse.ArgumentParser() parser.add_argument('--batchsize', '-b', type=int, default=128) parser.add_argument('--epoch', '-e', type=int, default=20) parser.add_argument('--out', '-o', default='result') args = parser.parse_args() model = L.Classifier(MLP(128, 10)) optimizer = optimizers.Adam() optimizer.setup(model) train, test = datasets.get_mnist() train_iter = iterators.SerialIterator(train, args.batchsize) test_iter = iterators.SerialIterator( test, args.batchsize, repeat=False, shuffle=False) updater = StandardUpdater(train_iter, optimizer, device=-1) trainer = Trainer(updater, (args.epoch, 'epoch'), out=args.out) trainer.extend(extensions.Evaluator(test_iter, model, device=-1)) trainer.extend(JsonlReport()) trainer.extend(extensions.PrintReport( ['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', 'elapsed_time'], log_report='JsonlReport', )) trainer.extend(extensions.ProgressBar()) trainer.run()
def main(): args = get_args() generator = Generator(hidden=args.hidden) discriminator = Discriminator() chainer.cuda.get_device_from_id(args.gpu).use() generator.to_gpu() discriminator.to_gpu() gen_optimizer = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5) gen_optimizer.setup(generator) gen_optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') dis_optimizer = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5) dis_optimizer.setup(discriminator) dis_optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') train, _ = chainer.datasets.get_mnist(withlabel=False, ndim=3, scale=255.) train_iter = chainer.iterators.SerialIterator(train, args.batch_size) updater = Updater(models=(generator, discriminator), iterator=train_iter, optimizer={'generator':gen_optimizer, 'discriminator':dis_optimizer}, device=args.gpu) trainer = Trainer(updater, (args.epoch, 'epoch'), out="result") trainer.extend(extensions.LogReport(trigger=(1, 'epoch'))) trainer.extend(extensions.PrintReport( ['epoch', 'generator/loss', 'discriminator/loss',]), trigger=(1, 'epoch')) trainer.extend(extensions.ProgressBar(update_interval=(1, 'epoch'))) trainer.extend(extensions.snapshot( filename='snapshot_epoch_{.updater.epoch}.npz'), trigger=(1, 'epoch')) trainer.extend(extensions.snapshot_object( generator, 'gen_epoch_{.updater.epoch}.npz'), trigger=(1, 'epoch')) trainer.extend(extensions.snapshot_object( discriminator, 'dis_epoch_{.updater.epoch}.npz'), trigger=(1, 'epoch')) trainer.run()
def _prepare_multinode_snapshot(n, result): n_units = 100 batchsize = 10 comm = create_communicator('naive') model = L.Classifier(MLP(n_units, 10)) optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.Adam(), comm) optimizer.setup(model) if comm.rank == 0: train, _ = chainer.datasets.get_mnist() else: train, _ = None, None train = chainermn.scatter_dataset(train, comm, shuffle=True) train_iter = chainer.iterators.SerialIterator(train, batchsize) updater = StandardUpdater(train_iter, optimizer) trainer = Trainer(updater, out=result) snapshot = extensions.snapshot(target=updater, autoload=True) replica_sets = [] mn_snapshot = multi_node_snapshot(comm, snapshot, replica_sets) mn_snapshot.initialize(trainer) for _ in range(n): updater.update() return updater, mn_snapshot, trainer
def train(idx): print('train by fold: {}'.format(idx)) df = pd.read_feather('./data/features.normalized.feather') fold = pd.read_feather('./data/fold.{}.feather'.format(idx)) df['valid'] = df['SK_ID_CURR'].isin(fold['SK_ID_CURR']) model = TemporalEnsembling(df) train_dset = create_dataset(df, 'TARGET', 'valid') train_iter = SerialIterator(train_dset, 512) optimizer = Adam() optimizer.setup(model) updater = StandardUpdater(train_iter, optimizer, device=0) trainer = Trainer(updater, (20, 'epoch'), out='tempem_result') trainer.extend( make_extension((1, 'epoch'))(UpdateExtention( train_dset, './data/nn.fold.{}.feather'.format(idx)))) trainer.extend(extensions.LogReport()) filename = 'fold_%d_snapshot_epoch-{.updater.epoch}' % (idx) trainer.extend(extensions.snapshot(filename=filename)) trainer.extend( extensions.PrintReport( ['epoch', 'main/loss', 'main/val/auc', 'elapsed_time'])) trainer.run()
def main(): train_x, train_y, val_x, val_y = load_pascal_voc_dataset(DATASET_ROOT) train_dataset = YoloDataset(train_x, train_y, target_size=model_class.img_size, n_grid=model_class.n_grid, augment=True) test_dataset = YoloDataset(val_x, val_y, target_size=model_class.img_size, n_grid=model_class.n_grid, augment=False) class_weights = [1.0 for i in range(train_dataset.n_classes)] class_weights[0] = 0.2 model = model_class(n_classes=train_dataset.n_classes, n_base_units=6, class_weights=class_weights) if os.path.exists(RESULT_DIR + '/model_last.npz'): print('continue from previous result') chainer.serializers.load_npz(RESULT_DIR + '/model_last.npz', model) optimizer = Adam() optimizer.setup(model) train_iter = SerialIterator(train_dataset, batch_size=BATCH_SIZE) test_iter = SerialIterator(test_dataset, batch_size=BATCH_SIZE, shuffle=False, repeat=False) updater = StandardUpdater(train_iter, optimizer, device=DEVICE) trainer = Trainer(updater, (N_EPOCHS, 'epoch'), out=RESULT_DIR) trainer.extend(extensions.dump_graph('main/loss')) trainer.extend(extensions.LogReport()) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(extensions.Evaluator(test_iter, model, device=DEVICE)) trainer.extend( extensions.PrintReport([ 'main/loss', 'validation/main/loss', 'main/cl_loss', 'validation/main/cl_loss', 'main/cl_acc', 'validation/main/cl_acc', 'main/pos_loss', 'validation/main/pos_loss', ])) trainer.extend(extensions.snapshot_object(model, 'best_loss.npz'), trigger=triggers.MinValueTrigger('validation/main/loss')) trainer.extend(extensions.snapshot_object(model, 'best_classification.npz'), trigger=triggers.MaxValueTrigger('validation/main/cl_acc')) trainer.extend( extensions.snapshot_object(model, 'best_position.npz'), trigger=triggers.MinValueTrigger('validation/main/pos_loss')) trainer.extend(extensions.snapshot_object(model, 'model_last.npz'), trigger=(1, 'epoch')) trainer.run()
def run(): config = get_config() print(yaml_dump(config)) s = "" while not (s == "y" or s == "n"): s = input("ok? (y/n): ") if s == "n": destroy_config(config) device = config["device"][0] if isinstance(config["device"], list) else config["device"] Model = getattr(import_module(config["model"]["module"]), config["model"]["class"]) model = Model(**config["model"]["params"]) Dataset = getattr(import_module(config["dataset"]["module"]), config["dataset"]["class"]) train_dataset = Dataset(**config["dataset"]["train"]["params"]) valid_dataset = Dataset(**config["dataset"]["valid"]["params"]) train_iterator = Iterator(train_dataset, config["batch"]["train"], True, True) valid_iterator = Iterator(valid_dataset, config["batch"]["valid"], False, False) Optimizer = getattr(import_module(config["optimizer"]["module"]), config["optimizer"]["class"]) optimizer = Optimizer(**config["optimizer"]["params"]) optimizer.setup(model) for hook_config in config["optimizer"]["hook"]: Hook = getattr(import_module(hook_config["module"]), hook_config["class"]) hook = Hook(**hook_config["params"]) optimizer.add_hook(hook) updater = Updater(train_iterator, optimizer, device=device) trainer = Trainer(updater, **config["trainer"]["params"]) trainer.extend(snapshot_object(model, "model_iter_{.updater.iteration}"), trigger=config["trainer"]["model_interval"]) trainer.extend(observe_lr(), trigger=config["trainer"]["log_interval"]) trainer.extend( LogReport([ "epoch", "iteration", "main/loss", "validation/main/loss", "lr", "elapsed_time" ], trigger=config["trainer"]["log_interval"])) trainer.extend(PrintReport([ "epoch", "iteration", "main/loss", "validation/main/loss", "lr", "elapsed_time" ]), trigger=config["trainer"]["log_interval"]) trainer.extend(Evaluator(valid_iterator, model, device=device), trigger=config["trainer"]["eval_interval"]) trainer.extend(ProgressBar(update_interval=10)) print("start training") trainer.run()
def training(modeldir, F, sp, model, batch_size, num_frame, lr, stop_trigger, ext_trigger, device): B = { 'trn': dataiter(datatf(F['trn'], dataprocess(sp, num_frame)), batch_size), 'val': dataiter(datatf(F['val'], dataprocess(sp, num_frame)), batch_size, False, False) } optimizer = optimizers.Adam(lr) optimizer.setup(model) updater = updaters.StandardUpdater(B['trn'], optimizer, converter=dataconverter(), loss_func=model.loss, device=device) trainer = Trainer(updater, stop_trigger, out=modeldir) trainer.extend(extensions.Evaluator(B['val'], model, eval_func=model.loss, device=device), trigger=ext_trigger) trainer.extend(extensions.snapshot(), trigger=ext_trigger) trainer.extend(extensions.LogReport(trigger=ext_trigger)) trainer.extend( extensions.PrintReport([ 'epoch', 'iteration', 'main/loss', 'validation/main/loss', 'main/lossA', 'validation/main/lossA', 'main/lossP', 'validation/main/lossP' ])) trainer.extend( extensions.PlotReport(['main/loss', 'validation/main/loss'], 'iteration', file_name='loss.png', trigger=ext_trigger)) trainer.extend( extensions.PlotReport(['main/lossA', 'validation/main/lossA'], 'iteration', file_name='lossA.png', trigger=ext_trigger)) trainer.extend( extensions.PlotReport(['main/lossP', 'validation/main/lossP'], 'iteration', file_name='lossP.png', trigger=ext_trigger)) ss = sorted([ int(str(p).split('_')[-1]) for p in pathlib.Path(modeldir).glob('snapshot_iter_*') ]) if ss: print('Trainer load : snapshot_iter_' + str(ss[-1])) serializers.load_npz(modeldir + '/snapshot_iter_' + str(ss[-1]), trainer) trainer.run() return model
def main(epochs=257*8, lr=0.38, seq_len=120, pred_len=39, out="result", device=0): # CHOOSE ONE: # get the training dataset but keep a slice for validation dataset = get_dataset(182, -39 -39) # get the entire dataset #dataset = get_dataset(182, -39) iter = ParallelSequentialIterator(dataset, pred_len=1, repeat=True) model = Model(pred_len=pred_len, dropout=0.1) if device >= 0: model.to_gpu() # Try some different optimizers #optimizer = optimizers.Adam(alpha=lr) #optimizer = optimizers.MomentumSGD(lr=lr, momentum=0.9) optimizer = optimizers.RMSpropGraves(lr=lr, alpha=0.95, momentum=0.2) #optimizer = optimizers.RMSprop(lr=lr, alpha=0.5) optimizer.setup(model) #optimizer.add_hook(chainer.optimizer.GradientClipping(5))#grad_clip)) #optimizer.add_hook(chainer.optimizer.WeightDecay(1.E-7)) updater = BPTTUpdater(iter, optimizer, seq_len=seq_len, pred_len=pred_len, data_len=len(dataset), device=device) trainer = Trainer(updater, (epochs, 'epoch'), out=out) interval = 10 # Try some learning-rate decay #trainer.extend(extensions.ExponentialShift('lr', 0.995)) #0.1, lr, lr * 0.1), trigger=(10, 'epoch')) trainer.extend(extensions.observe_lr(), trigger=(interval, "iteration")) trainer.extend(extensions.LogReport(trigger=(interval, 'iteration'))) trainer.extend(extensions.PrintReport(['epoch', 'iteration', 'loss', 'lr']), trigger=(interval, 'iteration')) trainer.extend(extensions.ProgressBar(update_interval=interval)) # export snapshots to resume training trainer.extend(extensions.snapshot(filename='snapshot_epoch_{.updater.epoch}'), trigger=(257*6, "epoch")) trainer.extend(extensions.snapshot_object(model, "model_epoch_{.updater.epoch}"), trigger=(257*2, "epoch")) # change to True to resume from file if False: chainer.serializers.load_npz('result/snapshot_epoch_1030', trainer) trainer.run() # save model from chainer import serializers serializers.save_npz('restaurant.model', model)
def chainer_model_pipe(self, nn, train, valid, params): epoch = params['epoch'] batch_size = params['batch_size'] use_gpu = params['use_gpu'] if 'fixed_base_w' in params.keys(): fixed_base_w = params['fixed_base_w'] else: fixed_base_w = False # Model Instance model = L.Classifier(nn) if use_gpu: device = 0 model.to_gpu(device) else: device = -1 # ミニバッチのインスタンスを作成 train_iter = SerialIterator(train, batch_size) valid_iter = SerialIterator(valid, batch_size, repeat=False, shuffle=False) # Set Lerning optimizer = Adam() optimizer.setup(model) if fixed_base_w: model.predictor.base.disable_update() updater = StandardUpdater(train_iter, optimizer, device=device) trainer = Trainer(updater, (epoch, 'epoch'), out='result/cat_dog') trainer.extend(Evaluator(valid_iter, model, device=device)) trainer.extend(LogReport(trigger=(1, 'epoch'))) trainer.extend(PrintReport([ 'epoch', 'main/accuracy', 'validation/main/accuracy', 'main/loss', 'validation/main/loss', 'elapsed_time' ]), trigger=(1, 'epoch')) trainer.run() if use_gpu: model.to_cpu() return model
def main(): # input_size: 299 #model = InceptionV4(dim_out=17) #model = InceptionV4(dim_out=17, base_filter_num=6, ablocks=2, bblocks=1, cblocks=1) #model = InceptionResNetV2(dim_out=17) #model = InceptionResNetV2(dim_out=17, base_filter_num=8, ablocks=1, bblocks=2, cblocks=1) # input_size: 224 #model = VGGNetBN(17) # VGGNet original size #model = VGGNetBN(17, 16) # VGGNet 1/4 of filter num #model = GoogLeNetBN(17) # GoogLeNet original size #model = GoogLeNetBN(17, 16) # GoogleNet 1/2 filter num #model = GoogLeNetBN(17, 8) # GoogleNet 1/4 filter num #model = ResNet50(17) # ResNet50 original size #model = ResNet50(17, 32) # ResNet50 1/2 size #model = ResNet50(17, 16) # ResNet50 1/4 size #model = SqueezeNet(17) #SqueezeNet original size #model = SqueezeNet(17, 8) #SqueezeNet 1/2 filter num #model = MobileNet(17) # MobileNet original size #model = MobileNet(17, 16) # MobileNet 1/2 filter num #model = MobileNet(17, 8) # MobileNet 1/4 filter num # input_size: 100 #model = FaceClassifier100x100V2(n_classes=17) model = FaceClassifier100x100V(n_classes=17) optimizer = Adam() optimizer.setup(model) train_dataset = load_dataset('train.tsv', True) test_dataset = load_dataset('test.tsv') train_iter = SerialIterator(train_dataset, batch_size=BATCH_SIZE) test_iter = SerialIterator(test_dataset, batch_size=BATCH_SIZE, shuffle=False, repeat=False) updater = StandardUpdater(train_iter, optimizer, device=DEVICE) trainer = Trainer(updater, (N_EPOCHS, 'epoch'), out='result') trainer.extend(extensions.dump_graph('main/loss')) trainer.extend(extensions.LogReport()) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(extensions.Evaluator(test_iter, model, device=DEVICE)) trainer.extend(extensions.PrintReport(['main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy'])) #trainer.extend(extensions.snapshot_object(model, 'snapshot_{.updater.epoch}.model')) trainer.run() chainer.serializers.save_npz('result/model.npz', model.to_cpu())
def main(args): chainer.config.user_gpu = args.g if args.g >= 0: chainer.backends.cuda.get_device_from_id(args.g).use() print("GPU mode") mnist_train = chainer.datasets.get_mnist()[0] # MNISTデータ取得 mnist_train = chainer.dataset.concat_examples(mnist_train)[ 0] # 画像だけ(ラベルは不要) mnist_train = mnist_train.reshape((-1, 1, 28, 28)) # 画像形式(N,C,H,W)に整形 mnist_iter = iterators.SerialIterator(mnist_train, args.batchsize, shuffle=True, repeat=True) # iteratorを作成 generator = Generator(Z_DIM) critic = Critic() if args.g >= 0: generator.to_gpu() critic.to_gpu() opt_g = optimizers.Adam(args.alpha, args.beta1, args.beta2) opt_g.setup(generator) opt_c = optimizers.Adam(args.alpha, args.beta1, args.beta2) opt_c.setup(critic) updater = WGANUpdater(mnist_iter, opt_g, opt_c, args.n_cri, args.gp_lam) trainer = Trainer(updater, (args.epoch, "epoch"), out=args.result_dir) trainer.extend(extensions.LogReport()) trainer.extend( extensions.PrintReport(["epoch", "generator/loss", "critic/loss"])) trainer.extend(extensions.ProgressBar()) trainer.extend( extensions.PlotReport(("generator/loss", "main/wdist"), "epoch", file_name="loss_plot.eps")) trainer.extend(extensions.snapshot_object( generator, "model_epoch_{.updater.epoch}.model"), trigger=(10, "epoch")) trainer.extend(ext_save_img(generator, args.result_dir + "/out_images")) trainer.run()
def main(args): chainer.config.user_gpu = args.g if args.g >= 0: chainer.backends.cuda.get_device_from_id(args.g).use() print("GPU mode") mnist_3 = get_mnist_num(args.neg_numbers) mnist_8 = get_mnist_num(args.pos_numbers) # iteratorを作成 kwds = {"batch_size": args.batchsize, "shuffle": True, "repeat": True} mnist_3_iter = iterators.SerialIterator(mnist_3, **kwds) mnist_8_iter = iterators.SerialIterator(mnist_8, **kwds) generator = Generator() critic = Critic() if args.g >= 0: generator.to_gpu() critic.to_gpu() adam_args = args.alpha, args.beta1, args.beta2 opt_g = optimizers.Adam(*adam_args) opt_g.setup(generator) opt_c = optimizers.Adam(*adam_args) opt_c.setup(critic) updater = WGANUpdater(mnist_3_iter, mnist_8_iter, opt_g, opt_c, args.n_cri1, args.n_cri2, args.gp_lam, args.l1_lam) trainer = Trainer(updater, (args.epoch, "epoch"), out=args.result_dir) trainer.extend(extensions.LogReport()) trainer.extend( extensions.PrintReport(["epoch", "generator/loss", "critic/loss"])) trainer.extend(extensions.ProgressBar()) trainer.extend( extensions.PlotReport(("generator/loss", "critic/loss"), "epoch", file_name="loss_plot.eps")) trainer.extend( ext_save_img(generator, mnist_8, args.result_dir + "/out_images")) trainer.run()
def train(): # train_txt = "/media/common-ns/New Volume/reseach/Dataset/OU-ISIR_by_Setoguchi/CV01.txt" train_dir = "/media/common-ns/New Volume/reseach/Dataset/OU-ISIR_by_Setoguchi/Gallery/CV01(Gallery)/*" train = load_OULP(path_dir=train_dir) # print(train[0]) # 教師データ # train = train[0:1000] train = [i[0] for i in train] # dataのパスとラベルのうち、dataだけ抜き出す train = datasets.TupleDataset(train, train) # 同じパス画像のペアから、dataに変換してタプルにする batch_size = 195 train_iter = chainer.iterators.SerialIterator(train, batch_size=batch_size) #model = L.Classifier(Autoencoder(), lossfun=F.mean_squared_error) model = L.Classifier(CAE(), lossfun=sce_loss) model.compute_accuracy = False optimizer = optimizers.Adam() optimizer.setup(model) updater = StandardUpdater(train_iter, optimizer, device=0) trainer = Trainer( updater, (1000, 'epoch'), out="result", ) trainer.extend(extensions.LogReport()) trainer.extend(extensions.PrintReport(['epoch', 'main/loss'])) trainer.extend(extensions.snapshot(), trigger=(200, 'epoch')) trainer.extend(extensions.snapshot_object( target=model, filename='model_snapshot_{.updater.iteration}'), trigger=(250, 'epoch')) trainer.extend(extensions.ProgressBar()) trainer.run() serializers.save_npz( "/home/common-ns/setoguchi/chainer_files/Convolutional_Auto_Encoder/CAE_FC_model", model)
def run_linear_network(loss_fn, alpha=0.3, batch_size=2): # Get data np.random.seed(42) dataset = get_dataset() iterator = SerialIterator(dataset, batch_size, repeat=True, shuffle=True) # Set up network and loss predictor = L.Linear(None, 1) ranker = Ranker(predictor) loss = Loss(ranker, loss_fn) # Optimizer optimizer = Adam(alpha=alpha) optimizer.setup(loss) updater = StandardUpdater(iterator, optimizer, converter=zeropad_concat) trainer = Trainer(updater, (100, 'epoch')) log_report = extensions.LogReport(log_name=None) trainer.extend(log_report) np.random.seed(42) trainer.run() last_ndcg = log_report.log[-1]['ndcg'] return last_ndcg
def main(args): s_train, s_test = dataset.load_svhn() t_train, t_test = dataset.load_mnist() s_train_iter = SerialIterator( s_train, args.batchsize, shuffle=True, repeat=True) t_train_iter = SerialIterator( t_test, args.batchsize, shuffle=True, repeat=True) s_test_iter = SerialIterator( s_test, args.batchsize, shuffle=False, repeat=False) t_test_iter = SerialIterator( t_test, args.batchsize, shuffle=False, repeat=False) model = drcn.DRCN() target_model = LossAndAccuracy(model) loss_list = ['loss_cla_s', 'loss_cla_t', 'loss_rec'] optimizer = chainer.optimizers.RMSprop(args.lr) optimizer.setup(model) optimizers = { 'model': optimizer } updater = Updater(s_train_iter, t_train_iter, optimizers, args) out_dir = utils.prepare_dir(args) trainer = Trainer(updater, (args.max_iter, 'iteration'), out=out_dir) trainer.extend(extensions.LogReport(trigger=(args.interval, args.unit))) trainer.extend( extensions.snapshot_object(model, filename='model'), trigger=MaxValueTrigger('acc_t', (args.interval, args.unit))) trainer.extend(extensions.Evaluator(t_test_iter, target_model, device=args.device), trigger=(args.interval, args.unit)) trainer.extend(extensions.PrintReport([args.unit, *loss_list, 'acc_s', 'acc_t', 'elapsed_time'])) trainer.extend(extensions.PlotReport([*loss_list], x_key=args.unit, file_name='loss.png', trigger=(args.interval, args.unit))) trainer.extend(extensions.PlotReport(['acc_s', 'acc_t'], x_key=args.unit, file_name='accuracy.png', trigger=(args.interval, args.unit))) trainer.extend(extensions.ProgressBar(update_interval=1)) trainer.run()
def main(): args = create_args('train') result_dir = create_result_dir(args.model_name) # Prepare devices devices = get_gpu_dict(args.gpus) # Instantiate a model model = RegNet(epsilon=args.epsilon) # Instantiate a optimizer optimizer = get_optimizer(model, **vars(args)) # Setting up datasets prep = TransformDataset(KITTI(args.kitti_path, 'train'), CalibPrepare(args.init_pose)) train, valid = split_dataset( prep, round(len(prep) * (1 - args.valid_proportion))) print("========== Model Parameters ==========") print("location loss weight (epsilon):", args.epsilon) print('train samples: {}, valid samples: {}'.format( len(train), len(valid))) # Iterator if DEBUG: Iterator = SerialIterator else: Iterator = MultiprocessIterator train_iter = Iterator(train, args.batchsize) valid_iter = Iterator(valid, args.valid_batchsize, repeat=False, shuffle=False) # Updater if DEBUG: Updater = StandardUpdater(train_iter, optimizer, device=devices['main']) else: Updater = ParallelUpdater(train_iter, optimizer, devices=devices) trainer = Trainer(Updater, (args.epoch, 'epoch'), out=result_dir) # Extentions trainer.extend(extensions.Evaluator(valid_iter, model, device=devices['main']), trigger=(args.valid_freq, 'epoch')) trainer.extend(extensions.snapshot(), trigger=(args.snapshot_iter, 'iteration')) trainer.extend(extensions.LogReport(), trigger=(args.show_log_iter, 'iteration')) trainer.extend(extensions.ProgressBar(update_interval=20)) trainer.extend( extensions.PrintReport([ 'epoch', 'iteration', 'main/loss', 'validation/main/loss', 'elapsed_time' ])) # Resume from snapshot if args.resume_from: chainer.serializers.load_npz(args.resume_from, trainer) # Train and save print("========== Training ==========") hook = CupyMemoryProfileHook() with hook: trainer.run() print("========== Saving ==========") chainer.serializers.save_hdf5(create_result_file(args.model_name), model) print("Done.") print("========== Memory Profiling ==========") hook.print_report()
np.array(cropimg2, dtype=np.float32).transpose( (2, 0, 1)) / 255)) cur_y += 20 cur_x += 20 iterator = SerialIterator(image_pairs, batch, shuffle=True) model = RgbNet() model = model.to_gpu() optimizer = optimizers.Adam().setup(model) updater = CustomUpdater(iterator, optimizer, device) trainer = Trainer(updater, (epoch, "epoch"), out="result") # 学習の進展を表示するようにする trainer.extend(extensions.ProgressBar()) trainer.extend(extensions.LogReport()) # 中間結果を保存する n_save = 0 @chainer.training.make_extension(trigger=(10, 'epoch')) def save_model(trainer): # NNのデータを保存 global n_save n_save = n_save + 1 chainer.serializers.save_hdf5('a-' + str(n_save) + '.hdf5', model)
from chainercv.links.model.resnet import ResNet50 from models.arcface import ArcFace from paired_image_dataset import PairedImageSet chainer.config.cv_resize_backend = 'cv2' if __name__ == "__main__": photo_path = sorted(Path('photos').glob('*')) sketch_path = sorted(Path('sketches').glob('*')) pair_list = [[str(i), str(j)] for i, j in zip(photo_path, sketch_path)] img_size = (200, 250) dataset = PairedImageSet(pair_list, '', img_size, False, np.float32) iter_train = MultiprocessIterator(dataset, 5, n_processes=2) adam = Adam(alpha=0.002, beta1=0.0, beta2=0.9) resnet = ResNet50(pretrained_model='imagenet') fc_dim = 500 resnet.fc6 = L.Linear(None, fc_dim) # change the number of fc layer to 500 temp = 30 margin = 0.5 arcface = ArcFace(temp, margin, resnet) adam.setup(arcface) updater = StandardUpdater(iter_train, adam) trainer = Trainer(updater, (1000, 'iteration')) trainer.run()
def __call__(self, x): return F.sum(x) def to_gpu(self, device=None): super(Model, self).to_gpu(device) class Dataset(DatasetMixin): def __init__(self): super(Dataset, self).__init__() def __len__(self): return 1024 def get_example(self, i): return np.array([[1, 2], [3, 4]], dtype=np.float32) dataset = Dataset() iterator = SerialIterator(dataset, 2, False, False) model_training = Model() model_training.to_gpu() optimizer = Adam() optimizer.setup(model_training) updater = StandardUpdater(iterator, optimizer, device=0) trainer = Trainer(updater, stop_trigger=[1, "iteration"]) trainer.extend(snapshot_object(model_training, "model_iter_{.updater.iteration}"), trigger=[1, "iteration"]) trainer.run() model_test = Model() load_npz("result/model_iter_1", model_test) model_test.to_gpu()
def main(arguments, neg_labels, pos_labels): output_dir_path = Path(arguments.output_dir) if not output_dir_path.exists(): output_dir_path.mkdir() # settings adam_setting = { "alpha": arguments.adam_alpha, "beta1": arguments.adam_beta1, "beta2": arguments.adam_beta2 } updater_setting = { "n_dis": arguments.n_dis, "l2_lam": arguments.l2_lam, "noise_std": arguments.noise_std } chainer.config.user_gpu_mode = (arguments.gpu_id >= 0) if chainer.config.user_gpu_mode: chainer.backends.cuda.get_device_from_id(arguments.gpu_id).use() # 訓練用正常データ mnist_neg = get_mnist_num(neg_labels) # iteratorを作成 iterator_setting = { "batch_size": arguments.batch_size, "shuffle": True, "repeat": True } neg_iter = iterators.SerialIterator(mnist_neg, **iterator_setting) generator = Generator() discriminator = Discriminator() if chainer.config.user_gpu_mode: generator.to_gpu() discriminator.to_gpu() opt_g = optimizers.Adam(**adam_setting) opt_g.setup(generator) opt_d = optimizers.Adam(**adam_setting) opt_d.setup(discriminator) if arguments.weight_decay > 0.0: opt_g.add_hook(chainer.optimizer.WeightDecay(arguments.weight_decay)) opt_d.add_hook(chainer.optimizer.WeightDecay(arguments.weight_decay)) updater = GANUpdater(neg_iter, opt_g, opt_d, **updater_setting) trainer = Trainer(updater, (arguments.iteration, "iteration"), out=str(output_dir_path)) # テストデータを取得 test_neg = get_mnist_num(neg_labels, train=False) test_pos = get_mnist_num(pos_labels, train=False) # 正常にラベル0,異常にラベル1を付与 test_neg = chainer.datasets.TupleDataset( test_neg, np.zeros(len(test_neg), dtype=np.int32)) test_pos = chainer.datasets.TupleDataset( test_pos, np.ones(len(test_pos), dtype=np.int32)) test_ds = chainer.datasets.ConcatenatedDataset(test_neg, test_pos) test_iter = iterators.SerialIterator(test_ds, repeat=False, shuffle=True, batch_size=500) ev_target = EvalModel(generator, discriminator, arguments.noise_std) ev_target = ExtendedClassifier(ev_target) if chainer.config.user_gpu_mode: ev_target.to_gpu() evaluator = extensions.Evaluator( test_iter, ev_target, device=arguments.gpu_id if chainer.config.user_gpu_mode else None) trainer.extend(evaluator) # 訓練経過の表示などの設定 trigger = (5000, "iteration") trainer.extend(extensions.LogReport(trigger=trigger)) trainer.extend(extensions.PrintReport( ["iteration", "generator/loss", "generator/l2", "discriminator/loss"]), trigger=trigger) trainer.extend(extensions.ProgressBar()) trainer.extend( extensions.PlotReport(("generator/loss", "discriminator/loss"), "iteration", file_name="loss_plot.eps", trigger=trigger)) trainer.extend( extensions.PlotReport(["generator/l2"], "iteration", file_name="gen_l2_plot.eps", trigger=trigger)) trainer.extend( extensions.PlotReport( ("validation/main/F", "validation/main/accuracy"), "iteration", file_name="acc_plot.eps", trigger=trigger)) trainer.extend(ext_save_img(generator, test_pos, test_neg, output_dir_path / "out_images", arguments.noise_std), trigger=trigger) trainer.extend(extensions.snapshot_object( generator, "gen_iter_{.updater.iteration:06d}.model"), trigger=trigger) trainer.extend(extensions.snapshot_object( discriminator, "dis_iter_{.updater.iteration:06d}.model"), trigger=trigger) # 訓練開始 trainer.run()
def train(opt): if opt.use_cpu: device = -1 print('[Msg] use CPU') else: device = 0 print('[Msg] use GPU0') train, valid = get_dataset(opt.dataset_dir, augment=True) print('[Msg] loaded normal dataset') if opt.use_rough: rough_train, _ = get_dataset(opt.rough_dataset_dir, augment=False, is_valid=False) print('[Msg] loaded rough dataset') train_iter = SerialIterator(train, opt.batch_size, shuffle=True, repeat=True) valid_iter = SerialIterator(valid, opt.batch_size, shuffle=True, repeat=True) if opt.use_rough: rough_iter = SerialIterator(rough_train, opt.batch_size, shuffle=True, repeat=True) else: rough_iter = train_iter print('[Msg] convert dataset to iterator') gen = SPADEGenerator(opt) if device != -1: gen.to_gpu(device) #use GPU if opt.dis_snapshot: load_npz(opt.gen_snapshot, gen, strict=False) print('[Msg] loaded gen npz (' + opt.gen_snapshot + ')') g_optim = Adam(alpha=opt.g_lr, beta1=opt.g_beta1, beta2=opt.g_beta2) g_optim.setup(gen) print('[Msg] completed generator setup') dis = PatchDiscriminator(opt) if device != -1: dis.to_gpu(device) #use GPU if opt.dis_snapshot: load_npz(opt.dis_snapshot, dis, strict=False) print('[Msg] loaded dis npz (' + opt.dis_snapshot + ')') d_optim = Adam(alpha=opt.d_lr, beta1=opt.d_beta1, beta2=opt.d_beta2) d_optim.setup(dis) print('[Msg] completed discriminator setup') updater = pix2pix_Updater(opt, iterator={'main': train_iter, 'valid': valid_iter, 'rough': rough_iter}, optimizer={'gen': g_optim, 'dis': d_optim}, device=device) trainer = Trainer(updater, (opt.max_iteration, 'iteration'), out=opt.out_dir) print('[Msg] created updater and trainer') #chainer training extensions trainer.extend(ex.LogReport(log_name=None, trigger=(1, 'iteration'))) trainer.extend(ex.ProgressBar((opt.max_iteration, 'iteration'), update_interval=1)) #plot #adv loss trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss'], x_key='iteration', filename='adv-loss.png', trigger=(25, 'iteration'))) trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss'], x_key='epoch', filename='adv-loss(details).png', trigger=(2, 'epoch'))) #adv loss with gp trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss_with_gp'], x_key='iteration', filename='adv-loss-with-gp.png', trigger=(25, 'iteration'))) trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss_with_gp'], x_key='epoch', filename='adv-loss-with-gp(details).png', trigger=(2, 'epoch'))) trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss_with_gp', 'gen/valid_adv_loss', 'dis/valid_adv_loss_with_gp'], x_key='epoch', filename='adv-loss-with-gp-and-valid.png', trigger=(2, 'epoch'))) #all loss trainer.extend(ex.PlotReport(['gen/loss', 'dis/loss'], x_key='iteration', filename='loss.png', trigger=(25, 'iteration'))) trainer.extend(ex.PlotReport(['gen/loss', 'dis/loss', 'gen/valid_loss', 'dis/valid_loss'], x_key='epoch', filename='loss-with-valid.png', trigger=(2, 'epoch'))) #other trainer.extend(ex.PlotReport(['dis/gp'], x_key='iteration', filename='gp.png', trigger=(25, 'iteration'))) trainer.extend(ex.PlotReport(['dis/gp'], x_key='epoch', filename='gp(details).png', trigger=(2, 'epoch'))) trainer.extend(ex.PlotReport(['gen/perceptual_loss'], x_key='iteration', filename='perceptual_loss.png', trigger=(25, 'iteration'))) trainer.extend(ex.PlotReport(['gen/perceptual_loss'], x_key='epoch', filename='perceptual_loss(details).png', trigger=(2, 'epoch'))) trainer.extend(ex.PlotReport(['gen/fm_loss'], x_key='iteration', filename='fm_loss.png', trigger=(25, 'iteration'))) trainer.extend(ex.PlotReport(['gen/fm_loss'], x_key='epoch', filename='fm_loss(details).png', trigger=(2, 'epoch'))) #snap trainer.extend(ex.snapshot_object(gen, 'gen_snapshot_epoch-{.updater.epoch}.npz'), trigger=(opt.snap_interval_epoch, 'epoch')) trainer.extend(ex.snapshot_object(dis, 'dis_snapshot_epoch-{.updater.epoch}.npz'), trigger=(opt.snap_interval_epoch, 'epoch')) trainer.extend(lambda *args: updater.save_img(), trigger=(opt.img_interval_iteration, 'iteration'), priority=PRIORITY_READER) trainer.extend(lambda *args: adam_lr_poly(opt, trainer), trigger=(100, 'iteration')) print('[Msg] applied extention') print('[Msg] start training...') trainer.run() #start learning
def train(opt): if opt.use_cpu: device = -1 print('[Message] use CPU') else: device = 0 print('[Message] use GPU0') annotated = get_dataset(opt) unlabeled = get_unlabel_dataset(opt) print('[Message] loaded options') train_iter = SerialIterator(annotated, opt.batch_size, shuffle=True) print('[Message] converted to iterator (train)') semi_iter = SerialIterator(unlabeled, opt.batch_size, shuffle=True) print('[Message] converted to iterator (semi)') gen = ResNetDeepLab(opt) #gen = DilatedFCN(opt) #gen = UNet(opt) if device != -1: gen.to_gpu(device) #use GPU g_optim = Adam(alpha=opt.g_lr, beta1=opt.g_beta1, beta2=opt.g_beta2) g_optim.setup(gen) if opt.g_weight_decay > 0: g_optim.add_hook(WeightDecay(opt.g_weight_decay)) print('[Message] setuped Generator') dis = FCN(opt) if device != -1: dis.to_gpu(device) #use GPU d_optim = Adam(alpha=opt.d_lr, beta1=opt.d_beta1, beta2=opt.d_beta2) d_optim.setup(dis) print('[Message] setuped Discriminator') updater = AdvSemiSeg_Updater(opt, iterator={ 'main': train_iter, 'semi': semi_iter }, optimizer={ 'gen': g_optim, 'dis': d_optim }, device=device) print('[Message] initialized Updater') trainer = Trainer(updater, (opt.max_epoch, 'epoch'), out=opt.out_dir) print('[Message] initialized Trainer') #chainer training extensions trainer.extend(ex.LogReport(log_name=None, trigger=(1, 'iteration'))) trainer.extend(ex.ProgressBar((opt.max_epoch, 'epoch'), update_interval=1)) trainer.extend( ex.PlotReport(['gen/adv_loss', 'dis/adv_loss', 'gen/semi_adv_loss'], x_key='iteration', file_name='adversarial_loss.png', trigger=(100, 'iteration'))) #test trainer.extend( ex.PlotReport(['gen/adv_loss'], x_key='iteration', file_name='adv_gen_loss.png', trigger=(100, 'iteration'))) trainer.extend( ex.PlotReport(['gen/ce_loss'], x_key='iteration', file_name='cross_entropy_loss.png', trigger=(100, 'iteration'))) trainer.extend( ex.PlotReport(['gen/semi_st_loss'], x_key='iteration', file_name='self_teach_loss.png', trigger=(100, 'iteration'))) trainer.extend( ex.PlotReport(['gen/loss', 'dis/loss', 'gen/semi_loss'], x_key='iteration', file_name='loss.png', trigger=(100, 'iteration'))) trainer.extend( ex.PlotReport(['gen/loss', 'dis/loss', 'gen/semi_loss'], x_key='epoch', file_name='loss_details.png', trigger=(5, 'epoch'))) trainer.extend( ex.PlotReport(['gen/semi_loss'], x_key='epoch', file_name='semi_loss.png', trigger=(1, 'epoch'))) #snap trainer.extend(ex.snapshot_object( gen, 'gen_snapshot_epoch-{.updater.epoch}.npz'), trigger=(opt.snap_interval_epoch, 'epoch')) trainer.extend(ex.snapshot_object( dis, 'dis_snapshot_epoch-{.updater.epoch}.npz'), trigger=(opt.snap_interval_epoch, 'epoch')) trainer.extend(lambda *args: updater.save_img(), trigger=(opt.img_interval_iteration, 'iteration'), priority=PRIORITY_READER) trainer.extend(lambda *args: updater.ignition_semi_learning(), trigger=(opt.semi_ignit_iteration, 'iteration'), priority=PRIORITY_READER) trainer.extend(lambda *args: adam_lr_poly(opt, trainer), trigger=(100, 'iteration')) print('[Message] initialized extension') print('[Message] start training ...') trainer.run() #start learning
def main(): args = parse_args() with open(args.config_path) as f: config = json.load(f) with open(args.app_config) as f: app_config = json.load(f) app_train_config = app_config.get('train', {}) command_config = {'gpu': args.gpu} if args.output_dir is not None: command_config['output_dir'] = args.output_dir config.update(command_config) device_id = config['gpu'] batch_size = config['batch_size'] network_params = config['network'] nets = {k: util.create_network(v) for k, v in network_params.items()} optimizers = { k: util.create_optimizer(v['optimizer'], nets[k]) for k, v in network_params.items() } if len(optimizers) == 1: key, target_optimizer = list(optimizers.items())[0] target = nets[key] else: target = nets target_optimizer = optimizers if device_id >= 0: chainer.cuda.get_device_from_id(device_id).use() for net in nets.values(): net.to_gpu() datasets = dataset.get_dataset() iterators = {} if isinstance(datasets, dict): for name, data in datasets.items(): if name == 'train': train_iterator = chainer.iterators.SerialIterator( data, batch_size) else: iterators[name] = chainer.iterators.SerialIterator( data, batch_size, repeat=False, shuffle=False) else: train_iterator = chainer.iterators.SerialIterator(datasets, batch_size) updater = TrainingStep(train_iterator, target_optimizer, model.calculate_metrics, device=device_id) trainer = Trainer(updater, (config['epoch'], 'epoch'), out=config['output_dir']) if hasattr(model, 'make_eval_func'): for name, iterator in iterators.items(): evaluator = extensions.Evaluator( iterator, target, eval_func=model.make_eval_func(target), device=device_id) trainer.extend(evaluator, name=name) dump_graph_node = app_train_config.get('dump_graph', None) if dump_graph_node is not None: trainer.extend(extensions.dump_graph(dump_graph_node)) trainer.extend(extensions.snapshot(filename='snapshot.state'), trigger=(1, 'epoch')) for k, net in nets.items(): file_name = 'latest.{}.model'.format(k) trainer.extend(extensions.snapshot_object(net, filename=file_name), trigger=(1, 'epoch')) max_value_trigger_key = app_train_config.get('max_value_trigger', None) min_value_trigger_key = app_train_config.get('min_value_trigger', None) if max_value_trigger_key is not None: trigger = triggers.MaxValueTrigger(max_value_trigger_key) for key, net in nets.items(): file_name = 'best.{}.model'.format(key) trainer.extend(extensions.snapshot_object(net, filename=file_name), trigger=trigger) elif min_value_trigger_key is not None: trigger = triggers.MinValueTrigger(min_value_trigger_key) for key, net in nets.items(): file_name = 'best.{}.model'.format(key) trainer.extend(extensions.snapshot_object(net, file_name), trigger=trigger) trainer.extend(extensions.LogReport()) if len(optimizers) == 1: for name, opt in optimizers.items(): if not hasattr(opt, 'lr'): continue trainer.extend(extensions.observe_lr(name)) else: for name, opt in optimizers.items(): if not hasattr(opt, 'lr'): continue key = '{}/lr'.format(name) trainer.extend(extensions.observe_lr(name, key)) if extensions.PlotReport.available(): plot_targets = app_train_config.get('plot_report', {}) for name, targets in plot_targets.items(): file_name = '{}.png'.format(name) trainer.extend( extensions.PlotReport(targets, 'epoch', file_name=file_name)) if not args.silent: print_targets = app_train_config.get('print_report', []) if print_targets is not None and print_targets != []: trainer.extend(extensions.PrintReport(print_targets)) trainer.extend(extensions.ProgressBar()) trainer.extend(generate_image(nets['gen'], 10, 10, config['output_image_dir']), trigger=(1, 'epoch')) if args.resume: chainer.serializers.load_npz(args.resume, trainer) trainer.run()
def training(configuration, i, verbose=False): #setting parameters batch_size = configuration["n_batchsize"] epochs = configuration["n_epochs"] n_z = configuration["n_z"] n_categorical = configuration["n_categorical"] n_continuous = configuration["n_continuous"] experiment = configuration["experiment"] n_neurons_gen = configuration["n_neurons_gen"] n_neurons_dis = configuration["n_neurons_dis"] n_neurons_cri = configuration["n_neurons_cri"] gpu = configuration["gpu"] output_name = configuration["output_name"] #import the training data if experiment == "random_left_right": x_dim = 12 #problem xi_dim = 6 #trajectory else: x_dim = 14 xi_dim = 7 if verbose: print("Loading data") train = import_dataset.import_data(configuration, x_dim, xi_dim) train_iter = iterators.SerialIterator(train, batch_size) z_iter = iterators.RandomNoiseIterator( UniformNoiseGenerator(-1, 1, n_z + n_continuous), batch_size) # Creating the Neural Networks models gen = Generator(n_z + n_continuous, x_dim, xi_dim, n_neurons_gen) dis = Discriminator(x_dim, xi_dim, n_neurons_dis, cinfogan=configuration["cinfogan"], n_continuous=n_continuous) critic = Critic(x_dim, xi_dim, n_neurons_cri) if (configuration["cinfogan"]): saving_directory = "results/models/cinfogan_models_" + str(i) else: saving_directory = "results/models/cgan_models_" + str(i) if configuration["wasserstein"]: if verbose: print("Using Wasserstein") optimizer_generator = optimizers.RMSprop(lr=0.00005) optimizer_critic = optimizers.RMSprop(lr=0.00005) a = xp.zeros((1, xi_dim)).astype(xp.float32) b = xp.zeros((1, x_dim)).astype(xp.float32) critic(Variable(a), Variable(b)) optimizer_generator.setup(gen) optimizer_critic.setup(critic) updater = WassersteinGANUpdater( iterator=train_iter, noise_iterator=z_iter, noise_dim=n_z, x_dim=x_dim, xi_dim=xi_dim, experiment=configuration["experiment"], optimizer_generator=optimizer_generator, optimizer_critic=optimizer_critic, device=gpu, ) else: if verbose: print("Not using Wasserstein") optimizer_generator = optimizers.Adam() optimizer_discriminator = optimizers.SGD() optimizer_generator.setup(gen) optimizer_discriminator.setup(dis) if configuration["cinfogan"]: updater = GANUpdater( iterator=train_iter, noise_iterator=z_iter, noise_dim=n_z, continuous_dim=n_continuous, x_dim=x_dim, xi_dim=xi_dim, experiment=configuration["experiment"], optimizer_generator=optimizer_generator, optimizer_discriminator=optimizer_discriminator, collision_measure=configuration["collision_measure"], saving_directory=saving_directory, device=gpu) else: updater = Cgan_GANUpdater( iterator=train_iter, noise_iterator=z_iter, noise_dim=n_z, x_dim=x_dim, xi_dim=xi_dim, experiment=configuration["experiment"], optimizer_generator=optimizer_generator, optimizer_discriminator=optimizer_discriminator, collision_measure=configuration["collision_measure"], saving_directory=saving_directory, device=gpu) if verbose: print("setup trainer...") trainer = Trainer(updater, stop_trigger=(epochs, 'epoch')) # changing the name because we do multiple experiments if configuration["cinfogan"]: trainer.out = "results/models/cinfogan_models_" + str(i) else: trainer.out = "results/models/cgan_models_" + str(i) trainer.extend(extensions.LogReport()) if configuration["wasserstein"]: print_report_args = [ 'epoch', 'gen/loss', 'cri/loss', 'lin_ratio', 'cgan_ratio', 'diff_ratio' ] else: print_report_args = [ 'epoch', 'gen/loss', 'dis/loss', 'lin_ratio', 'cgan_ratio', 'diff_ratio' ] if verbose: trainer.extend(extensions.PrintReport(print_report_args)) trainer.extend(extensions.ProgressBar()) if configuration["collision_measure"] != 0: trainer.extend(extensions.GeneratorSample(configuration, x_dim, xi_dim, n_continuous, n_z, train), trigger=(1, 'epoch')) # We delete the f_metric.dat file to be sure we do not mixed multiple experiment data. cmd = "touch results/f_metric.dat && rm results/f_metric.dat" os.system(cmd) if verbose: print("START TRAINING!!") trainer.run() if configuration["output_name"] != "": output_name = configuration["output_name"] else: output_name = str(configuration["experiment"])
def main(args): result_dir_path = Path(args.result_dir) try: result_dir_path.mkdir(parents=True) except FileExistsError: pass with Path(args.setting).open("r") as f: setting = json.load(f) pprint.pprint(setting) chainer.config.user_gpu_mode = (args.g >= 0) if chainer.config.user_gpu_mode: chainer.backends.cuda.get_device_from_id(args.g).use() # 訓練用正常データ mnist_neg = get_mnist_num(setting["label"]["neg"]) # iteratorを作成 kwds = { "batch_size": setting["iterator"]["batch_size"], "shuffle": True, "repeat": True } neg_iter = iterators.SerialIterator(mnist_neg, **kwds) generator = model.Generator() discriminator = model.Discriminator() if chainer.config.user_gpu_mode: generator.to_gpu() discriminator.to_gpu() opt_g = optimizers.Adam(**setting["optimizer"]) opt_g.setup(generator) opt_d = optimizers.Adam(**setting["optimizer"]) opt_d.setup(discriminator) if setting["regularization"]["weight_decay"] > 0.0: opt_g.add_hook( chainer.optimizer.WeightDecay( setting["regularization"]["weight_decay"])) opt_d.add_hook( chainer.optimizer.WeightDecay( setting["regularization"]["weight_decay"])) updater = GANUpdater(neg_iter, opt_g, opt_d, **setting["updater"]) trainer = Trainer(updater, (setting["iteration"], "iteration"), out=args.result_dir) # テストデータを取得 test_neg = get_mnist_num(setting["label"]["neg"], train=False) test_pos = get_mnist_num(setting["label"]["pos"], train=False) # 正常にラベル0,異常にラベル1を付与 test_neg = chainer.datasets.TupleDataset( test_neg, np.zeros(len(test_neg), dtype=np.int32)) test_pos = chainer.datasets.TupleDataset( test_pos, np.ones(len(test_pos), dtype=np.int32)) test_ds = chainer.datasets.ConcatenatedDataset(test_neg, test_pos) test_iter = iterators.SerialIterator(test_ds, repeat=False, shuffle=True, batch_size=500) ev_target = model.EvalModel(generator, discriminator, setting["updater"]["noise_std"]) ev_target = model.ExtendedClassifier(ev_target) if chainer.config.user_gpu_mode: ev_target.to_gpu() evaluator = extensions.Evaluator( test_iter, ev_target, device=args.g if chainer.config.user_gpu_mode else None) trainer.extend(evaluator) # 訓練経過の表示などの設定 trigger = (5000, "iteration") trainer.extend(extensions.LogReport(trigger=trigger)) trainer.extend(extensions.PrintReport( ["iteration", "generator/loss", "generator/l2", "discriminator/loss"]), trigger=trigger) trainer.extend(extensions.ProgressBar()) trainer.extend( extensions.PlotReport(("generator/loss", "discriminator/loss"), "iteration", file_name="loss_plot.eps", trigger=trigger)) trainer.extend( extensions.PlotReport(["generator/l2"], "iteration", file_name="gen_l2_plot.eps", trigger=trigger)) trainer.extend( extensions.PlotReport( ("validation/main/F", "validation/main/accuracy"), "iteration", file_name="acc_plot.eps", trigger=trigger)) trainer.extend(ext_save_img(generator, test_pos, test_neg, result_dir_path / "out_images", setting["updater"]["noise_std"]), trigger=trigger) trainer.extend(extensions.snapshot_object( generator, "gen_iter_{.updater.iteration:06d}.model"), trigger=trigger) trainer.extend(extensions.snapshot_object( discriminator, "dis_iter_{.updater.iteration:06d}.model"), trigger=trigger) # 訓練開始 trainer.run()
n_threads=batch) v_iter = MultithreadIterator(validation, batch_size=batch, repeat=True, shuffle=True, n_threads=batch) model = Network(channels, blocks, ksize) if device >= 0: model.to_gpu() optimizer = optimizers.Adam().setup(model) updater = CustomUpdater({ "main": t_iter, "test": v_iter }, optimizer, (patch, patch)) trainer = Trainer(updater, (epoch, "epoch"), out=out) log = extensions.LogReport() trainer.extend(log) trainer.extend( extensions.PrintReport(["epoch", "iteration", "loss", "test"], log)) trainer.extend(extensions.ProgressBar(update_interval=1)) trainer.extend(lambda trainer: save_hdf5( f"{out}/m{trainer.updater.iteration}.hdf5", model), trigger=(5, "epoch")) trainer.extend(lambda trainer: save_hdf5( f"{out}/o{trainer.updater.iteration}.hdf5", optimizer), trigger=(5, "epoch")) trainer.run() save_hdf5(f"{out}/model.hdf5", model) save_hdf5(f"{out}/optimizer.hdf5", optimizer)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpus', type=str, default="-1") parser.add_argument('--batchsize', type=int, default=2) parser.add_argument('--epoch', type=int, default=10) parser.add_argument('--mini', action="store_true") args = parser.parse_args() gpus = list(filter(lambda x: x >= 0, map(int, args.gpus.split(",")))) num_class = len(voc_bbox_label_names) data_augmentation_transform = DataAugmentationTransform(512) center_detection_transform = CenterDetectionTransform(512, num_class, 4) train = TransformDataset( ConcatenatedDataset( VOCBboxDataset(year='2007', split='trainval'), VOCBboxDataset(year='2012', split='trainval') ), data_augmentation_transform ) train = TransformDataset(train, center_detection_transform) if args.mini: train = datasets.SubDataset(train, 0, 100) train_iter = chainer.iterators.MultiprocessIterator(train, args.batchsize) test = VOCBboxDataset( year='2007', split='test', use_difficult=True, return_difficult=True) if args.mini: test = datasets.SubDataset(test, 0, 20) test_iter = chainer.iterators.SerialIterator( test, args.batchsize // len(gpus), repeat=False, shuffle=False) detector = CenterDetector(HourglassNet, 512, num_class) train_chain = CenterDetectorTrain(detector, 1, 0.1, 1) gpus.sort() first_gpu = gpus[0] remain_gpu = gpus[1:] train_chain.to_gpu(first_gpu) optimizer = Adam(amsgrad=True) optimizer.setup(train_chain) devices = { "main": first_gpu } for i, gpu in enumerate(remain_gpu): devices[f"{i + 2}"] = gpu updater = training.updaters.ParallelUpdater( train_iter, optimizer, devices=devices, ) log_interval = 1, 'epoch' trainer = Trainer(updater, (args.epoch, 'epoch')) trainer.extend(extensions.LogReport(trigger=log_interval)) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend(extensions.PrintReport( [ 'epoch', 'iteration', 'lr', 'main/loss', 'main/hm_loss', 'main/wh_loss', 'main/offset_loss', 'validation/main/map', ]), trigger=log_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend( DetectionVOCEvaluator( test_iter, detector, use_07_metric=True, label_names=voc_bbox_label_names), trigger=(1, 'epoch')) trainer.extend( extensions.snapshot_object(detector, 'detector{.updater.epoch:03}.npz'), trigger=(1, 'epoch') ) trainer.run()
def main(): comm = mn.create_communicator("pure_nccl") device = comm.intra_rank config = get_config() print("pid {}: mask loading...".format(comm.rank)) load_mask_module = import_module( config["additional information"]["mask"]["loader"]["module"], config["additional information"]["mask"]["loader"]["package"]) load_mask = getattr( load_mask_module, config["additional information"]["mask"]["loader"]["function"]) mask = load_mask( **config["additional information"]["mask"]["loader"]["params"]) print("pid {}: done.".format(comm.rank)) if comm.rank == 0: print("mask.shape: {}".format(mask.shape)) model_module = import_module(config["model"]["module"], config["model"]["package"]) Model = getattr(model_module, config["model"]["class"]) model = Model(comm=comm, mask=mask, **config["model"]["params"]) optimizer_module = import_module(config["optimizer"]["module"], config["optimizer"]["package"]) Optimizer = getattr(optimizer_module, config["optimizer"]["class"]) optimizer = mn.create_multi_node_optimizer( Optimizer(**config["optimizer"]["params"]), comm) optimizer.setup(model) if device >= 0: chainer.backends.cuda.get_device_from_id(device).use() model.to_gpu() print("pid {}: GPU {} enabled".format(comm.rank, device)) if comm.rank == 0: dataset_module = import_module(config["dataset"]["module"], config["dataset"]["package"]) Dataset = getattr(dataset_module, config["dataset"]["class"]) train_dataset = Dataset(**config["dataset"]["train"]["params"]) valid_dataset = Dataset(**config["dataset"]["valid"]["params"]) else: train_dataset = None valid_dataset = None train_dataset = mn.datasets.scatter_dataset(train_dataset, comm, shuffle=True) valid_dataset = mn.datasets.scatter_dataset(valid_dataset, comm, shuffle=True) train_iterator = Iterator(train_dataset, config["batch"]["train"]) valid_iterator = Iterator(valid_dataset, config["batch"]["valid"], False, False) updater = Updater(train_iterator, optimizer, device=device) trainer = Trainer(updater, **config["trainer"]["params"]) checkpointer = mn.create_multi_node_checkpointer(config["general"]["name"], comm) checkpointer.maybe_load(trainer, optimizer) trainer.extend(checkpointer, trigger=tuple(config["trainer"]["snapshot_interval"])) evaluator = Evaluator(valid_iterator, model, device=device) evaluator = mn.create_multi_node_evaluator(evaluator, comm) trainer.extend(evaluator) trainer.extend(observe_lr(), trigger=config["trainer"]["log_interval"]) if comm.rank == 0: trainer.extend(LogReport(trigger=config["trainer"]["log_interval"])) trainer.extend(PrintReport( ["epoch", "iteration", "main/loss", "validation/main/loss"]), trigger=config["trainer"]["log_interval"]) trainer.extend(ProgressBar(update_interval=1)) trainer.run()
def main(): parser = argparse.ArgumentParser(description='training mnist') parser.add_argument('--gpu', '-g', default=-1, type=int, help='GPU ID (negative value indicates CPU)') parser.add_argument('--epoch', '-e', type=int, default=100, help='Number of sweeps over the dataset to train') parser.add_argument('--batchsize', '-b', type=int, default=8, help='Number of images in each mini-batch') parser.add_argument('--seed', '-s', type=int, default=0, help='Random seed') parser.add_argument('--report_trigger', '-rt', type=str, default='1e', help='Interval for reporting(Ex.100i, default:1e)') parser.add_argument('--save_trigger', '-st', type=str, default='1e', help='Interval for saving the model(Ex.100i, default:1e)') parser.add_argument('--load_model', '-lm', type=str, default=None, help='Path of the model object to load') parser.add_argument('--load_optimizer', '-lo', type=str, default=None, help='Path of the optimizer object to load') args = parser.parse_args() start_time = datetime.now() save_dir = Path('output/{}'.format(start_time.strftime('%Y%m%d_%H%M'))) random.seed(args.seed) np.random.seed(args.seed) cupy.random.seed(args.seed) backbone = 'mobilenet' model = ModifiedClassifier(DeepLab(n_class=13, task='semantic', backbone=backbone), lossfun=F.softmax_cross_entropy) if args.load_model is not None: serializers.load_npz(args.load_model, model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() optimizer = optimizers.Adam(alpha=1e-3) optimizer.setup(model) if args.load_optimizer is not None: serializers.load_npz(args.load_optimizer, optimizer) dir_path = './dataset/2D-3D-S/' augmentations = {'mirror': 0.5, 'flip': 0.5} train_data = Stanford2D3DS(dir_path, 'semantic', area='1 2 3 4', train=True) train_data.set_augmentations(crop=513, augmentations=augmentations) valid_data = Stanford2D3DS(dir_path, 'semantic', area='6', train=False, n_data=100) valid_data.set_augmentations(crop=513) train_iter = iterators.MultiprocessIterator(train_data, args.batchsize, n_processes=1) valid_iter = iterators.MultiprocessIterator(valid_data, args.batchsize, repeat=False, shuffle=False, n_processes=1) updater = StandardUpdater(train_iter, optimizer, device=args.gpu) trainer = Trainer(updater, (args.epoch, 'epoch'), out=save_dir) label_list = list(valid_data.label_dict.keys())[1:] report_trigger = (int(args.report_trigger[:-1]), 'iteration' if args.report_trigger[-1] == 'i' else 'epoch') trainer.extend(extensions.LogReport(trigger=report_trigger)) trainer.extend(ModifiedEvaluator(valid_iter, model, label_names=label_list, device=args.gpu), name='val', trigger=report_trigger) trainer.extend(extensions.PrintReport(['epoch', 'iteration', 'main/loss', 'main/acc', 'val/main/loss', 'val/main/acc', 'val/main/mean_class_acc', 'val/main/miou', 'elapsed_time']), trigger=report_trigger) trainer.extend(extensions.PlotReport(['main/loss', 'val/main/loss'], x_key=report_trigger[1], marker='.', file_name='loss.png', trigger=report_trigger)) trainer.extend(extensions.PlotReport(['main/acc', 'val/main/acc'], x_key=report_trigger[1], marker='.', file_name='accuracy.png', trigger=report_trigger)) class_accuracy_report = ['val/main/mean_class_acc'] class_accuracy_report.extend(['val/main/class_acc/{}'.format(label) for label in label_list]) class_iou_report = ['val/main/miou'] class_iou_report.extend(['val/main/iou/{}'.format(label) for label in label_list]) trainer.extend(extensions.PlotReport(class_accuracy_report, x_key=report_trigger[1], marker='.', file_name='class_accuracy.png', trigger=report_trigger)) trainer.extend(extensions.PlotReport(class_iou_report, x_key=report_trigger[1], marker='.', file_name='class_iou.png', trigger=report_trigger)) save_trigger = (int(args.save_trigger[:-1]), 'iteration' if args.save_trigger[-1] == 'i' else 'epoch') trainer.extend(extensions.snapshot_object(model, filename='model_{0}-{{.updater.{0}}}.npz' .format(save_trigger[1])), trigger=save_trigger) trainer.extend(extensions.snapshot_object(optimizer, filename='optimizer_{0}-{{.updater.{0}}}.npz' .format(save_trigger[1])), trigger=save_trigger) if save_dir.exists(): shutil.rmtree(save_dir) save_dir.mkdir() (save_dir / 'training_details').mkdir() # Write parameters text with open(save_dir / 'training_details/train_params.txt', 'w') as f: f.write('model: {}(backbone: {})\n'.format(model.predictor.__class__.__name__, backbone)) f.write('n_epoch: {}\n'.format(args.epoch)) f.write('batch_size: {}\n'.format(args.batchsize)) f.write('n_data_train: {}\n'.format(len(train_data))) f.write('n_data_val: {}\n'.format(len(valid_data))) f.write('seed: {}\n'.format(args.seed)) if len(augmentations) > 0: f.write('[augmentation]\n') for process in augmentations: f.write(' {}: {}\n'.format(process, augmentations[process])) trainer.run()
def main(): # Parse arguments. with open('params.yml') as stream: args = yaml.load(stream) # Prepare training data. train, val = chainer.datasets.get_mnist(ndim=3) if args['memory'] == 'gpu' and 0 <= args['gpu']: train = [(cp.array(x), cp.array(y)) for x, y in train] val = [(cp.array(x), cp.array(y)) for x, y in val] # Prepare model. class Classifier(chainer.Chain): def __init__(self, predictor): super(Classifier, self).__init__() with self.init_scope(): self.predictor = predictor def forward(self, batch, labels): embeddings = self.predictor(batch) loss = functions.batch_all_triplet_loss( embeddings, labels, margin=args['margin'], dist_type=args['dist_type']) chainer.reporter.report( { 'loss': loss, 'VAL': functions.validation_rate(embeddings, labels, threshold=args['threshold'], dist_type=args['dist_type']), 'FAR': functions.false_accept_rate(embeddings, labels, threshold=args['threshold'], dist_type=args['dist_type']) }, self) return loss predictor = FaceNet() model = Classifier(predictor) if 0 <= args['gpu']: chainer.backends.cuda.get_device_from_id(args['gpu']).use() model.to_gpu() # Prepare optimizer. optimizer = chainer.optimizers.AdaDelta() optimizer.setup(model) # Make output directory. timestamp = f'{datetime.datetime.now():%Y%m%d%H%M%S}' directory = f'./temp/{timestamp}/' os.makedirs(directory, exist_ok=True) shutil.copy('params.yml', f'{directory}params.yml') # Prepare extensions. if args['memory'] == 'cpu' and 0 <= args['gpu']: def converter(batch, device=None, padding=None): return concat_examples([(cp.array(x), cp.array(y)) for x, y in batch], device=device, padding=padding) else: converter = concat_examples class DumpEmbeddings(chainer.training.extension.Extension): def __init__(self, iterator, model, converter, filename): self.iterator = iterator self.model = model self.converter = converter self.filename = filename self.xp = cp if 0 <= args['gpu'] else np def __call__(self, trainer): if hasattr(self.iterator, 'reset'): self.iterator.reset() it = self.iterator else: it = copy.copy(self.iterator) def forward(batch): x, _ = self.converter(batch) y = self.model.predictor(x) embeddings = y.data if 0 <= args['gpu']: embeddings = chainer.backends.cuda.to_cpu(embeddings) return embeddings embeddings = np.vstack([forward(batch) for batch in it]) np.save(os.path.join(trainer.out, self.filename.format(trainer)), embeddings) train_iter = SerialIterator(train, args['batch_size']) test_iter = SerialIterator(val, args['batch_size'], repeat=False, shuffle=False) updater = StandardUpdater(train_iter, optimizer, converter=converter) trainer = Trainer(updater, stop_trigger=(args['epochs'], 'epoch'), out=directory) trainer.extend(dump_graph('main/loss', out_name='model.dot')) trainer.extend(Evaluator(test_iter, model, converter=converter)) trainer.extend(snapshot_object(target=model, filename='model-{.updater.epoch:04d}.npz'), trigger=(args['checkpoint_interval'], 'epoch')) trainer.extend(LogReport(log_name='log')) trainer.extend( PlotReport(y_keys=['main/loss', 'validation/main/loss'], x_key='epoch', file_name='loss.png')) trainer.extend( PlotReport(y_keys=['main/VAL', 'validation/main/VAL'], x_key='epoch', file_name='VAL.png')) trainer.extend( PlotReport(y_keys=['main/FAR', 'validation/main/FAR'], x_key='epoch', file_name='FAR.png')) trainer.extend( PrintReport([ 'epoch', 'main/loss', 'validation/main/loss', 'main/VAL', 'validation/main/VAL', 'main/FAR', 'validation/main/FAR', 'elapsed_time' ])) trainer.extend(DumpEmbeddings(test_iter, model, converter=converter, filename='embeddings-{.updater.epoch}.npy'), trigger=(args['checkpoint_interval'], 'epoch')) trainer.extend(ProgressBar(update_interval=1)) # Execute training. trainer.run()