def main(): parser = argparse.ArgumentParser(description='gpat train ') parser.add_argument("out") parser.add_argument('--resume', default=None) parser.add_argument('--log_dir', default='runs_16') parser.add_argument('--gpus', '-g', type=int, nargs="*", default=[0, 1, 2, 3]) parser.add_argument('--iterations', default=10**5, type=int, help='number of iterations to learn') parser.add_argument('--interval', default=1000, type=int, help='number of iterations to evaluate') parser.add_argument('--batch_size', '-b', type=int, default=128, help='learning minibatch size') parser.add_argument('--lr', type=float, default=1e-4) parser.add_argument('--loaderjob', type=int, default=8) parser.add_argument('--hed', dest='hed', action='store_true', default=False) # parser.add_argument('--size', '-s', default=96, type=int, choices=[48, 64, 80, 96, 112, 128], # help='image size') parser.add_argument('--no-texture', dest='texture', action='store_false', default=True) parser.add_argument('--cbp', dest='cbp', action='store_true', default=False) parser.add_argument('--no-color_aug', dest='color_aug', action='store_false', default=True) parser.add_argument('--model_test', default='', type=str) parser.add_argument('--no-finetune', dest='finetune', action='store_false', default=True) parser.add_argument('--arch', default='googlenet', choices=[ 'texturecnn', 'resnet50', 'resnet101', 'googlenet', 'vgg', 'alex', 'trained', 'resume' ]) parser.add_argument('--opt', default='adam', choices=['adam', 'momentum']) parser.add_argument('--train_path', default='train_extracted_dataset.pkl') parser.add_argument('--test_path', default='test_extracted_dataset.pkl') parser.add_argument('--data_size', type=float, default=1.) parser.add_argument('--new', action='store_true', default=False) args = parser.parse_args() devices = tuple(args.gpus) # os.environ['PATH'] += ':/usr/local/cuda/bin' # log directory logger.init(args) # load data train_dataset = np.load(os.path.join(dataset_path, args.train_path)) test_dataset = np.load(os.path.join(dataset_path, args.test_path)) num_class = 2 image_size = 256 crop_size = 224 if 'extracted' in train_dataset: perm = np.random.permutation( len(train_dataset))[:int(len(train_dataset) * args.data_size)] train_dataset = [train_dataset[p] for p in perm] preprocess_type = args.arch if not args.hed else 'hed' if 'extracted' in args.train_path: train = CamelyonDatasetEx(train_dataset, original_size=image_size, crop_size=crop_size, aug=True, color_aug=args.color_aug, preprocess_type=preprocess_type) else: train = CamelyonDatasetFromTif(train_dataset, original_size=image_size, crop_size=crop_size, aug=True, color_aug=args.color_aug, preprocess_type=preprocess_type) if len(devices) > 1: train_iter = [ chainer.iterators.MultiprocessIterator(i, args.batch_size, n_processes=args.loaderjob) for i in chainer.datasets.split_dataset_n_random( train, len(devices)) ] else: train_iter = iterators.MultiprocessIterator(train, args.batch_size, n_processes=args.loaderjob) test = CamelyonDatasetEx(test_dataset, original_size=image_size, crop_size=crop_size, aug=False, color_aug=False, preprocess_type=preprocess_type) test_iter = iterators.MultiprocessIterator(test, args.batch_size, repeat=False, shuffle=False) # model construct if args.texture: model = BilinearCNN(base_cnn=args.arch, pretrained_model='auto', num_class=num_class, texture_layer=None, cbp=args.cbp, cbp_size=4096) else: model = TrainableCNN(base_cnn=args.arch, pretrained_model='auto', num_class=num_class) if args.model_test: # test # model_path = os.path.join('runs_16', args.model_test, 'models', # sorted(os.listdir(os.path.join('runs_16', args.model_test, 'models')))[-1]) # print(model_path) # chainer.serializers.load_npz(model_path, model) cuda.get_device_from_id(devices[0]).use() model.to_gpu() with chainer.using_config('train', False), chainer.no_backprop_mode(): evaluate_ex(model, test_iter, devices[0]) logger.flush() exit() if args.resume is not None: model_path = os.path.join( 'runs_16', args.resume, 'models', sorted(os.listdir(os.path.join('runs_16', args.resume, 'models')))[-1]) print(model_path) chainer.serializers.load_npz(model_path, model) # set optimizer optimizer = make_optimizer(model, args.opt, args.lr) if len(devices) > 1: updater = updaters.MultiprocessParallelUpdater(train_iter, optimizer, devices=devices) else: cuda.get_device_from_id(devices[0]).use() model.to_gpu() # updater updater = chainer.training.StandardUpdater(train_iter, optimizer, device=devices[0]) # start training start = time.time() train_loss = 0 train_accuracy = 0 while updater.iteration < args.iterations: # train updater.update() progress_report(updater.iteration, start, len(devices) * args.batch_size, len(train)) train_loss += model.loss.data train_accuracy += model.accuracy.data if updater.iteration % args.interval == 0: logger.plot('train_loss', cuda.to_cpu(train_loss) / args.interval) logger.plot('train_accuracy', cuda.to_cpu(train_accuracy) / args.interval) train_loss = 0 train_accuracy = 0 # test with chainer.using_config('train', False), chainer.no_backprop_mode(): evaluate_ex(model, test_iter, devices[0]) # logger logger.flush() # save serializers.save_npz(os.path.join(logger.out_dir, 'resume'), updater) if updater.iteration % 20000 == 0: if args.opt == 'adam': optimizer.alpha *= 0.1 else: optimizer.lr *= 0.1
from logger import log, flush x = 0 while (x < 20): log("Hello") x = x + 1 flush("log.txt")
def flush_log(fileName): flush(fileName)
def main(): parser = argparse.ArgumentParser(description='gpat train') parser.add_argument("out") parser.add_argument('--resume', default=None) parser.add_argument('--log_dir', default='runs_active') parser.add_argument('--gpu', '-g', type=int, default=0) parser.add_argument('--iterations', default=10**5, type=int, help='number of iterations to learn') parser.add_argument('--interval', default=100, type=int, help='number of iterations to evaluate') parser.add_argument('--batch_size', '-b', type=int, default=64, help='learning minibatch size') parser.add_argument('--lr', type=float, default=1e-4) parser.add_argument('--loaderjob', type=int, default=8) parser.add_argument('--hed', dest='hed', action='store_true', default=False) parser.add_argument('--from_tiff', dest='from_tiff', action='store_true', default=False) parser.add_argument('--no-texture', dest='texture', action='store_false', default=True) parser.add_argument('--cbp', dest='cbp', action='store_true', default=False) parser.add_argument('--no-color_aug', dest='color_aug', action='store_false', default=True) parser.add_argument('--model_test', default='', type=str) parser.add_argument('--arch', default='googlenet', choices=[ 'texturecnn', 'resnet', 'googlenet', 'vgg', 'alex', 'trained', 'resume' ]) parser.add_argument('--opt', default='adam', choices=['adam', 'momentum']) parser.add_argument('--train_path', default='train_extracted_dataset.pkl') parser.add_argument('--test_path', default='test_extracted_dataset.pkl') parser.add_argument('--epoch_interval', default=20, type=int) parser.add_argument('--active_sample_size', type=int, default=100) parser.add_argument('--no-every_init', dest='every_init', action='store_false', default=True) parser.add_argument('--random_sample', action='store_true', default=False) parser.add_argument('--fixed_ratio', action='store_true', default=False) parser.add_argument('--label_init', choices=['random', 'clustering'], default='clustering') parser.add_argument('--init_size', default=100, type=int) parser.add_argument('--uncertain', action='store_true', default=False) parser.add_argument('--uncertain_with_dropout', action='store_true', default=False) parser.add_argument('--uncertain_strategy', choices=['entropy', 'least_confident', 'margin'], default='margin') parser.add_argument('--clustering', action='store_true', default=False) parser.add_argument('--kmeans_cache', default='initial_clustering_result.pkl') parser.add_argument('--initial_label_cache', default='initial_label_cache.npy') parser.add_argument('--query_by_committee', action='store_true', default=False) parser.add_argument('--qbc_strategy', choices=['vote', 'average_kl'], default='average_kl') parser.add_argument('--committee_size', default=10, type=int) parser.add_argument('--aug_in_inference', action='store_true', default=False) args = parser.parse_args() device = args.gpu # log directory logger.init(args) # load data train_dataset = np.load(os.path.join(dataset_path, args.train_path)) test_dataset = np.load(os.path.join(dataset_path, args.test_path)) num_class = 2 image_size = 256 crop_size = 224 preprocess_type = args.arch if not args.hed else 'hed' perm = np.random.permutation(len(test_dataset))[:10000] test_dataset = [test_dataset[idx] for idx in perm] test = CamelyonDatasetEx(test_dataset, original_size=image_size, crop_size=crop_size, aug=False, color_aug=False, preprocess_type=preprocess_type) test_iter = iterators.MultiprocessIterator(test, args.batch_size, repeat=False, shuffle=False) cbp_feat = np.load('train_cbp512_feat.npy') labeled_data, unlabeled_data, feat = initialize_labeled_dataset( args, train_dataset, cbp_feat) print('now {} labeled samples, {} unlabeled'.format( len(labeled_data), len(unlabeled_data))) # start training reporter = ProgresssReporter(args) for iteration in range(100): # model construct if args.texture: model = BilinearCNN(base_cnn=args.arch, pretrained_model='auto', num_class=num_class, texture_layer=None, cbp=args.cbp, cbp_size=4096) else: model = TrainableCNN(base_cnn=args.arch, pretrained_model='auto', num_class=num_class) # set optimizer optimizer = make_optimizer(model, args.opt, args.lr) # use gpu cuda.get_device_from_id(device).use() model.to_gpu() labeled_dataset = CamelyonDatasetEx(labeled_data, original_size=image_size, crop_size=crop_size, aug=True, color_aug=True, preprocess_type=preprocess_type) labeled_iter = iterators.MultiprocessIterator(labeled_dataset, args.batch_size) # train phase count = 0 train_loss = 0 train_acc = 0 epoch_interval = args.epoch_interval if len( labeled_data[0]) < 10000 else args.epoch_interval * 2 anneal_epoch = int(epoch_interval * 0.8) while labeled_iter.epoch < epoch_interval: # train with labeled dataset batch = labeled_iter.next() x, t = chainer.dataset.concat_examples(batch, device=device) optimizer.update(model, x, t) reporter(labeled_iter.epoch) if labeled_iter.is_new_epoch and labeled_iter.epoch == anneal_epoch: optimizer.alpha *= 0.1 if labeled_iter.epoch > args.epoch_interval - 5: count += len(batch) train_loss += model.loss.data * len(batch) train_acc += model.accuracy.data * len(batch) # if labeled_iter.is_new_epoch: # train_loss_tmp = cuda.to_cpu(train_loss) / len(labeled_iter.dataset) # loss_history.append(train_loss_tmp - np.sum(loss_history)) reporter.reset() logger.plot('train_loss', cuda.to_cpu(train_loss) / count) logger.plot('train_accuracy', cuda.to_cpu(train_acc) / count) # test print('\ntest') with chainer.using_config('train', False), chainer.no_backprop_mode(): evaluate_ex(model, test_iter, device) # logger logger.flush() if len(labeled_data[0]) >= 10000: print('done') exit() tmp_indices = np.random.permutation(len(unlabeled_data))[:10000] tmp_unlabeled_data = [unlabeled_data[idx] for idx in tmp_indices] tmp_cbp_feat = cbp_feat[tmp_indices] unlabeled_dataset = CamelyonDatasetEx(tmp_unlabeled_data, original_size=image_size, crop_size=crop_size, aug=args.aug_in_inference, color_aug=args.aug_in_inference, preprocess_type=preprocess_type) unlabeled_iter = iterators.MultiprocessIterator(unlabeled_dataset, args.batch_size, repeat=False, shuffle=False) preds = np.zeros((args.committee_size, len(tmp_unlabeled_data), 2)) # feat = np.zeros((len(unlabeled_iter.dataset), 784)) if args.random_sample: tmp_query_indices = np.random.permutation( len(tmp_unlabeled_data))[:args.active_sample_size] else: loop_num = args.committee_size for loop in range(loop_num): count = 0 for batch in unlabeled_iter: x, t = chainer.dataset.concat_examples(batch, device=device) with chainer.no_backprop_mode(): y = F.softmax(model.forward(x)) preds[loop, count:count + len(batch)] = cuda.to_cpu(y.data) count += len(batch) # if loop == 0: # feat[i * batch_size: (i + 1) * batch_size] = cuda.to_cpu(x) unlabeled_iter.reset() tmp_query_indices = active_annotation(preds, tmp_cbp_feat, opt=args) # active sampling print('active sampling: ', end='') if iteration % 10 == 0: logger.save(model, [tmp_unlabeled_data[idx] for idx in tmp_query_indices]) query_indices = tmp_indices[tmp_query_indices] labeled_data, unlabeled_data, cbp_feat = query_dataset( labeled_data, unlabeled_data, cbp_feat, query_indices) print('now {} labeled samples, {} unlabeled'.format( len(labeled_data), len(unlabeled_data)))
def main(): parser = argparse.ArgumentParser(description='') parser.add_argument('out') parser.add_argument('--gpu', '-g', type=int, default=0, help='GPU device ID') parser.add_argument('--epoch', '-e', type=int, default=200, help='# of epoch') parser.add_argument('--batch_size', '-b', type=int, default=10) parser.add_argument('--memory_size', '-m', type=int, default=500) parser.add_argument('--real_label', type=float, default=0.9) parser.add_argument('--fake_label', type=float, default=0.0) parser.add_argument('--block_num', type=int, default=6) parser.add_argument('--g_nobn', dest='g_bn', action='store_false', default=True) parser.add_argument('--d_nobn', dest='d_bn', action='store_false', default=True) parser.add_argument('--variable_size', action='store_true', default=False) parser.add_argument('--lambda_dis_real', type=float, default=0) parser.add_argument('--size', type=int, default=128) parser.add_argument('--lambda_', type=float, default=10) # args = parser.parse_args() args, unknown = parser.parse_known_args() # log directory out = datetime.datetime.now().strftime('%m%d%H') out = out + '_' + args.out out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", out)) os.makedirs(os.path.join(out_dir, 'models'), exist_ok=True) os.makedirs(os.path.join(out_dir, 'visualize'), exist_ok=True) # hyper parameter with open(os.path.join(out_dir, 'setting.txt'), 'w') as f: for k, v in args._get_kwargs(): print('{} = {}'.format(k, v)) f.write('{} = {}\n'.format(k, v)) trainA = ImageDataset('horse2zebra/trainA', augmentation=True, image_size=256, final_size=args.size) trainB = ImageDataset('horse2zebra/trainB', augmentation=True, image_size=256, final_size=args.size) testA = ImageDataset('horse2zebra/testA', image_size=256, final_size=args.size) testB = ImageDataset('horse2zebra/testB', image_size=256, final_size=args.size) train_iterA = chainer.iterators.MultiprocessIterator(trainA, args.batch_size, n_processes=min( 8, args.batch_size)) train_iterB = chainer.iterators.MultiprocessIterator(trainB, args.batch_size, n_processes=min( 8, args.batch_size)) N = len(trainA) # genA convert B -> A, genB convert A -> B genA = Generator(block_num=args.block_num, bn=args.g_bn) genB = Generator(block_num=args.block_num, bn=args.g_bn) # disA discriminate realA and fakeA, disB discriminate realB and fakeB disA = Discriminator(bn=args.d_bn) disB = Discriminator(bn=args.d_bn) if args.gpu >= 0: cuda.get_device_from_id(args.gpu).use() genA.to_gpu() genB.to_gpu() disA.to_gpu() disB.to_gpu() optimizer_genA = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5, beta2=0.9) optimizer_genB = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5, beta2=0.9) optimizer_disA = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5, beta2=0.9) optimizer_disB = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5, beta2=0.9) optimizer_genA.setup(genA) optimizer_genB.setup(genB) optimizer_disA.setup(disA) optimizer_disB.setup(disB) # start training start = time.time() fake_poolA = np.zeros( (args.memory_size, 3, args.size, args.size)).astype('float32') fake_poolB = np.zeros( (args.memory_size, 3, args.size, args.size)).astype('float32') lambda_ = args.lambda_ const_realA = np.asarray([testA.get_example(i) for i in range(10)]) const_realB = np.asarray([testB.get_example(i) for i in range(10)]) iterations = 0 for epoch in range(args.epoch): if epoch > 100: decay_rate = 0.0002 / 100 optimizer_genA.alpha -= decay_rate optimizer_genB.alpha -= decay_rate optimizer_disA.alpha -= decay_rate optimizer_disB.alpha -= decay_rate # train iter_num = N // args.batch_size for i in range(iter_num): # load real batch imagesA = train_iterA.next() imagesB = train_iterB.next() if args.variable_size: crop_size = np.random.choice([160, 192, 224, 256]) resize_size = np.random.choice([160, 192, 224, 256]) imagesA = [ random_augmentation(image, crop_size, resize_size) for image in imagesA ] imagesB = [ random_augmentation(image, crop_size, resize_size) for image in imagesB ] realA = chainer.Variable(genA.xp.asarray(imagesA, 'float32')) realB = chainer.Variable(genB.xp.asarray(imagesB, 'float32')) # load fake batch if iterations < args.memory_size: fakeA = genA(realB) fakeB = genB(realA) fakeA.unchain_backward() fakeB.unchain_backward() else: fake_imagesA = fake_poolA[np.random.randint( args.memory_size, size=args.batch_size)] fake_imagesB = fake_poolB[np.random.randint( args.memory_size, size=args.batch_size)] if args.variable_size: fake_imagesA = [ random_augmentation(image, crop_size, resize_size) for image in fake_imagesA ] fake_imagesB = [ random_augmentation(image, crop_size, resize_size) for image in fake_imagesB ] fakeA = chainer.Variable(genA.xp.asarray(fake_imagesA)) fakeB = chainer.Variable(genA.xp.asarray(fake_imagesB)) ############################ # (1) Update D network ########################### # dis A y_realA = disA(realA) y_fakeA = disA(fakeA) loss_disA = (F.sum((y_realA - args.real_label) ** 2) + F.sum((y_fakeA - args.fake_label) ** 2)) \ / np.prod(y_fakeA.shape) # dis B y_realB = disB(realB) y_fakeB = disB(fakeB) loss_disB = (F.sum((y_realB - args.real_label) ** 2) + F.sum((y_fakeB - args.fake_label) ** 2)) \ / np.prod(y_fakeB.shape) # discriminate real A and real B not only realA and fakeA if args.lambda_dis_real > 0: y_realB = disA(realB) loss_disA += F.sum( (y_realB - args.fake_label)**2) / np.prod(y_realB.shape) y_realA = disB(realA) loss_disB += F.sum( (y_realA - args.fake_label)**2) / np.prod(y_realA.shape) # update dis disA.cleargrads() disB.cleargrads() loss_disA.backward() loss_disB.backward() optimizer_disA.update() optimizer_disB.update() ########################### # (2) Update G network ########################### # gan A fakeA = genA(realB) y_fakeA = disA(fakeA) loss_ganA = F.sum( (y_fakeA - args.real_label)**2) / np.prod(y_fakeA.shape) # gan B fakeB = genB(realA) y_fakeB = disB(fakeB) loss_ganB = F.sum( (y_fakeB - args.real_label)**2) / np.prod(y_fakeB.shape) # rec A recA = genA(fakeB) loss_recA = F.mean_absolute_error(recA, realA) # rec B recB = genB(fakeA) loss_recB = F.mean_absolute_error(recB, realB) # gen loss loss_gen = loss_ganA + loss_ganB + lambda_ * (loss_recA + loss_recB) # loss_genB = loss_ganB + lambda_ * (loss_recB + loss_recA) # update gen genA.cleargrads() genB.cleargrads() loss_gen.backward() # loss_genB.backward() optimizer_genA.update() optimizer_genB.update() # logging logger.plot('loss dis A', float(loss_disA.data)) logger.plot('loss dis B', float(loss_disB.data)) logger.plot('loss rec A', float(loss_recA.data)) logger.plot('loss rec B', float(loss_recB.data)) logger.plot('loss gen A', float(loss_gen.data)) # logger.plot('loss gen B', float(loss_genB.data)) logger.tick() # save to replay buffer fakeA = cuda.to_cpu(fakeA.data) fakeB = cuda.to_cpu(fakeB.data) for k in range(args.batch_size): fake_sampleA = fakeA[k] fake_sampleB = fakeB[k] if args.variable_size: fake_sampleA = cv2.resize( fake_sampleA.transpose(1, 2, 0), (256, 256), interpolation=cv2.INTER_AREA).transpose(2, 0, 1) fake_sampleB = cv2.resize( fake_sampleB.transpose(1, 2, 0), (256, 256), interpolation=cv2.INTER_AREA).transpose(2, 0, 1) fake_poolA[(iterations * args.batch_size) % args.memory_size + k] = fake_sampleA fake_poolB[(iterations * args.batch_size) % args.memory_size + k] = fake_sampleB iterations += 1 progress_report(iterations, start, args.batch_size) if epoch % 5 == 0: logger.flush(out_dir) visualize(genA, genB, const_realA, const_realB, epoch=epoch, savedir=os.path.join(out_dir, 'visualize')) serializers.save_hdf5( os.path.join(out_dir, "models", "{:03d}.disA.model".format(epoch)), disA) serializers.save_hdf5( os.path.join(out_dir, "models", "{:03d}.disB.model".format(epoch)), disB) serializers.save_hdf5( os.path.join(out_dir, "models", "{:03d}.genA.model".format(epoch)), genA) serializers.save_hdf5( os.path.join(out_dir, "models", "{:03d}.genB.model".format(epoch)), genB)
def signal_handler(sig, frame): logger.flush() logger.bye() sys.exit(0)
def finished(self): logger.flush()
def main(): parser = argparse.ArgumentParser( description='Adult contents save the world') parser.add_argument("out") parser.add_argument('--init', '-i', default=None, help='Initialize the model from given file') parser.add_argument('--gpu', '-g', type=int, default=-1) parser.add_argument('--iterations', default=10**5, type=int, help='number of iterations to learn') parser.add_argument('--train_path', '-tr', default='../datasets/train_dataset.npy', type=str) parser.add_argument('--test_path', '-ta', default='../datasets/test_dataset.npy', type=str) parser.add_argument('--interval', default=1000, type=int, help='number of iterations to evaluate') parser.add_argument('--batch_size', '-b', type=int, default=64, help='learning minibatch size') parser.add_argument('--lr', type=float, default=1e-4) parser.add_argument('--test', dest='test', action='store_true', default=False) parser.add_argument('--color_aug', dest='color_aug', action='store_true', default=False) parser.add_argument('--divide_type', type=str, default='normal') parser.add_argument('--loaderjob', type=int, default=8) parser.add_argument('--opt', default='adam', choices=['adam', 'momentum']) args = parser.parse_args() device = args.gpu # my logger logger.init(args) # load data train_data = np.load(args.train_path) test_data = np.load(args.test_path) # prepare dataset and iterator train = KomeDataset(train_data, random=True, color_augmentation=args.color_aug, divide_type=args.divide_type) train_iter = iterators.MultiprocessIterator(train, args.batch_size) test = KomeDataset(test_data, random=False, color_augmentation=False, divide_type=args.divide_type) test_iter = iterators.MultiprocessIterator(test, args.batch_size, repeat=False, shuffle=False) # model construct model = archs[args.arch](texture=args.texture, cbp=args.cbp, normalize=args.normalize) if args.finetune: model.load_pretrained(os.path.join(MODEL_PATH, init_path[args.arch]), num_class=2) else: model.convert_to_finetune_model(num_class=2) # optimizer if args.opt == 'adam': optimizer = chainer.optimizers.Adam(alpha=args.lr) elif args.opt == 'momentum': optimizer = chainer.optimizers.MomentumSGD(lr=args.lr) else: raise ValueError('invalid argument') optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(1e-5)) cuda.get_device_from_id(device).use() model.to_gpu() # test only mode if args.test: with chainer.using_config('train', False), chainer.no_backprop_mode(): evaluate(model, test_iter, device) logger.flush() exit() updater = chainer.training.StandardUpdater(train_iter, optimizer, device=device) # start training start = time.time() train_loss = 0 train_accuracy = 0 while updater.iteration < args.iterations: # train updater.update() progress_report(updater.iteration, start, args.batch_size, len(train)) train_loss += model.loss.data train_accuracy += model.accuracy.data # evaluation if updater.iteration % args.interval == 0: logger.plot('train loss', cuda.to_cpu(train_loss) / args.interval) logger.plot('train accuracy', cuda.to_cpu(train_accuracy) / args.interval) train_loss = 0 train_accuracy = 0 # test with chainer.using_config('train', False), chainer.no_backprop_mode(): it = copy.copy(test_iter) evaluate(model, it, device) # logger logger.flush() # save serializers.save_npz(os.path.join(logger.out_dir, 'resume'), updater) serializers.save_hdf5( os.path.join(logger.out_dir, "models", "cnn_{}.model".format(updater.iteration)), model) if updater.iteration % 10000 == 0: if args.opt == 'adam': optimizer.alpha *= 0.5 else: optimizer.lr *= 0.5