def check_collective_communication(param, use_gpu): communicator = create_communicator(param, use_gpu) mpi_comm.barrier() model = ExampleModel(param.model_dtype) if use_gpu: model.to_gpu() check_bcast_data(communicator, model) check_allreduce_grad(communicator, model) check_allreduce_grad_empty(communicator, model) # Check allreduce debug mode model = ExampleModel() if use_gpu: model.to_gpu() # The example model includes some nan parameters so the debug mode # must detect it. chainer.set_debug(True) with pytest.raises(ValueError, match=r'.* diverged .*'): check_allreduce_grad(communicator, model) chainer.set_debug(False) # barrier() requires before destructor of PureNcclCommunicator # because communication may not be finished. mpi_comm.barrier() destroy_communicator(communicator)
def main(args): chainer.set_debug(True) # Initialize the model to train model = models.archs[args.arch]() if args.finetune and hasattr(model, 'finetuned_model_path'): utils.finetuning.load_param(model.finetuned_model_path, model) if args.initmodel: print('Load model from', args.initmodel) chainer.serializers.load_npz(args.initmodel, model) outputdir = os.path.join('.', args.out, args.arch) if args.initmodel: outputdir = os.path.dirname(args.initmodel) if not os.path.exists(outputdir): os.makedirs(outputdir) for layer in args.layer: outputpath = os.path.join( outputdir, "{0}_distance_{1}.npy".format(layer, args.dis)) similar_path = os.path.join( outputdir, "{0}_similar_{1}.tsv".format(layer, args.dis)) best_path = os.path.join( outputdir, "{0}_best{1}_{2}.tsv".format(layer, args.best, args.dis)) W, b = model[layer].W.data, model[layer].b.data if args.withbias: params = np.hstack((W.reshape( (W.shape[0], -1)), b.reshape((b.shape[0], -1)))) else: params = W.reshape((W.shape[0], -1)) print("{0}\tW: {1}\tb: {2}".format(layer, W.shape, b.shape)) N = params.shape[0] #d = np.zeros(N**2).reshape((N, N)) '''for i, p in enumerate(tqdm(itertools.product(params, repeat=2))): w1, w2 = p if args.dis == 'cos': d[i] = dis.cosine(w1, w2) elif args.dis == 'euclidean': d[i] = dis.euclidean(w1, w2)''' if args.dis == 'cos': d = cos_distance_matrix(params) elif args.dis == 'euclidean': d = euclidean_distance_matrix(params) #d = d.reshape((N, N)) np.savetxt(outputpath, d, delimiter=",") d_list = [(int(x[0]), int(x[1]), d[x[0], x[1]]) for x in itertools.combinations(six.moves.range(N), 2)] if args.threshold: similar_list = filter((lambda x: x[2] < args.threshold), d_list) save_list(similar_path, similar_list) if args.best: order_by_disance_ascending = sorted(d_list, key=lambda x: x[2]) save_list(best_path, order_by_disance_ascending[0:args.best])
def setUp(self): self.original_debug = chainer.is_debug() chainer.set_debug(True) self.one = numpy.array(1, numpy.float32) self.f = chainer.FunctionNode() self.return_value = tuple(None if x is None else chainer.Variable(x) for x in self.return_data)
def setUp(self): chainer.set_debug(True) np.random.seed(0) dataset = VOC('train') img, im_info, bbox = dataset[1] self.x = Variable(img[None, ...]) self.im_info = Variable(im_info[None, ...]) self.gt_boxes = Variable(bbox[None, ...])
def main(args): chainer.set_debug(True) # Initialize the model to train model = models.archs[args.arch]() if args.finetune and hasattr(model, 'finetuned_model_path'): utils.finetuning.load_param(model.finetuned_model_path, model) if args.initmodel: print('Load model from', args.initmodel) chainer.serializers.load_npz(args.initmodel, model) outputdir = os.path.join('.', args.out, args.arch) if args.initmodel: outputdir = os.path.dirname(args.initmodel) if not os.path.exists(outputdir): os.makedirs(outputdir) for layer in args.layer: outputpath = os.path.join(outputdir, "{0}_distance_{1}.npy".format(layer, args.dis)) similar_path = os.path.join(outputdir, "{0}_similar_{1}.tsv".format(layer, args.dis)) best_path = os.path.join(outputdir, "{0}_best{1}_{2}.tsv".format(layer, args.best, args.dis)) W, b = model[layer].W.data, model[layer].b.data if args.withbias: params = np.hstack((W.reshape((W.shape[0], -1)), b.reshape((b.shape[0], -1)))) else: params = W.reshape((W.shape[0], -1)) print("{0}\tW: {1}\tb: {2}".format(layer, W.shape, b.shape)) N = params.shape[0] #d = np.zeros(N**2).reshape((N, N)) '''for i, p in enumerate(tqdm(itertools.product(params, repeat=2))): w1, w2 = p if args.dis == 'cos': d[i] = dis.cosine(w1, w2) elif args.dis == 'euclidean': d[i] = dis.euclidean(w1, w2)''' if args.dis == 'cos': d = cos_distance_matrix(params) elif args.dis == 'euclidean': d = euclidean_distance_matrix(params) #d = d.reshape((N, N)) np.savetxt(outputpath, d, delimiter=",") d_list = [(int(x[0]), int(x[1]), d[x[0],x[1]]) for x in itertools.combinations(six.moves.range(N), 2)] if args.threshold: similar_list = filter((lambda x: x[2] < args.threshold), d_list) save_list(similar_path, similar_list) if args.best: order_by_disance_ascending = sorted(d_list, key=lambda x: x[2]) save_list(best_path, order_by_disance_ascending[0:args.best])
def setUp(self): chainer.set_debug(True) np.random.seed(0) x = np.random.randint(0, 255, size=(224, 224, 3)).astype(np.float) x -= np.array([[[102.9801, 115.9465, 122.7717]]]) self.x = np.expand_dims(x, 0).transpose(0, 3, 1, 2).astype(np.float32) self.im_info = np.array([[224, 224, 1.6]]) self.gt_boxes = np.array([[10, 10, 60, 200, 0], [50, 100, 210, 210, 1], [160, 40, 200, 70, 2]])
def main(args): chainer.set_debug(True) # Initialize the model to train model = models.archs[args.arch]() if hasattr(model, 'finetuned_model_path'): finetuning.load_param(model.finetuned_model_path, model) if args.initmodel: print('Load model from', args.initmodel) chainer.serializers.load_npz(args.initmodel, model) layer = model[args.layer] outputdir = os.path.join('.', args.out, args.arch) outputpath = os.path.join(outputdir, args.layer + '_distance.npy') similar_path = os.path.join(outputdir, args.layer + '_similar.tsv') best_path = os.path.join(outputdir, args.layer + '_best.tsv') if not os.path.exists(outputdir): os.makedirs(outputdir) W = layer.W.data b = layer.b.data if args.withbias: params = np.hstack((W.reshape( (W.shape[0], -1)), b.reshape((b.shape[0], -1)))) else: params = W.reshape((W.shape[0], -1)) print('W', W.shape) print('b', b.shape) N = params.shape[0] d = np.zeros(N**2) for i, p in enumerate(itertools.product(params, repeat=2)): w1, w2 = p if args.dis == 'cos': d[i] = dis.cosine(w1, w2) elif args.dis == 'euclidean': d[i] = dis.euclidean(w1, w2) d = d.reshape((N, N)) np.savetxt(outputpath, d, delimiter=",") d_list = [(int(x[0]), int(x[1]), d[x[0], x[1]]) for x in itertools.combinations_with_replacement( six.moves.range(N), 2)] d_list = filter((lambda x: x[0] != x[1]), d_list) if args.threshold: similar_list = filter((lambda x: x[2] < args.threshold), d_list) save_list(similar_path, similar_list) if args.best: order_by_disance_ascending = sorted(d_list, key=lambda x: x[2]) save_list(best_path, order_by_disance_ascending[0:args.best]) return d
def main(): parser = argparse.ArgumentParser(description='Dynamic SGM Net') parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') parser.add_argument('--out', '-o', default='output', help='Directory to output the result') parser.add_argument('--vol', '-v', type=str2bool, default=False, help='Save cost volume data') args = parser.parse_args() outdir = args.out print('cuda:' + str(chainer.cuda.available)) print('cudnn:' + str(chainer.cuda.cudnn_enabled)) print('GPU: {}'.format(args.gpu)) print('outdir: ', outdir) print('') chainer.config.train = False chainer.set_debug(False) chainer.using_config('use_cudnn', 'auto') # Load MC-CNN pre-trained models from # kitti_fast, kitti_slow, kitti2015_fast, kitti2015_slow, mb_fast, mb_slow model_kitti = mcnet.MCCNN_pretrained('mccnn/kitti_fast') model_mb = mcnet.MCCNN_pretrained('mccnn/mb_slow') if args.gpu >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(args.gpu).use() model_kitti.to_gpu() # Copy the model to the GPU model_mb.to_gpu() # Copy the model to the GPU samples = [] sattellite_flag = 9999 #samples.append((model_mb, 'mb2014', 145)) #samples.append((model_kitti, 'kitti', 70)) samples.append((model_mb, 'satellite', sattellite_flag)) for sample in samples: model, target, ndisp = sample print('Processing ' + target) im0 = load_image(os.path.join('input', target, 'im0.png')).astype(np.float32) im1 = load_image(os.path.join('input', target, 'im1.png')).astype(np.float32) inputs = (im0, im1, np.array([ndisp])) batch = chainer.dataset.concat_examples([inputs], args.gpu) with chainer.no_backprop_mode(): vol = model(*batch)[0].array if target == 'satellite': dmin, dmax = load_ndisp(os.path.join('input', target, 'ndisp.txt')) ndisp = dmax-dmin disp = vol.argmin(0).astype(np.float32) * (255 / ndisp) os.makedirs(os.path.join(args.out, target), exist_ok=True) cv2.imwrite(os.path.join(args.out, target, 'disp0.png'), chainer.cuda.to_cpu(disp)) if args.vol: vol.tofile(os.path.join(args.out, target, 'im0.bin'))
def setUp(self): chainer.set_debug(True) np.random.seed(0) x = np.random.randint(0, 255, size=(224, 224, 3)).astype(np.float) x -= np.array([[[102.9801, 115.9465, 122.7717]]]) self.x = np.expand_dims(x, 0).transpose(0, 3, 1, 2).astype(np.float32) self.im_info = np.array([[224, 224, 1.6]]) self.gt_boxes = np.array([ [10, 10, 60, 200, 0], [50, 100, 210, 210, 1], [160, 40, 200, 70, 2] ])
def main(): chainer.set_debug(True) parser = get_parser() args = parser.parse_args() reset_seed(args.seed) #load vocabulary source0_ids = load_vocabulary(args.SOURCE_VOCAB0) source1_ids = load_vocabulary(args.SOURCE_VOCAB1) target_ids = load_vocabulary(args.TARGET_VOCAB) corpus = make_data_tuple(source0=(source0_ids, args.SOURCE0), source1=(source1_ids, args.SOURCE1), target=(target_ids, args.TARGET)) source0_words = {i: w for w, i in source0_ids.items()} source1_words = {i: w for w, i in source1_ids.items()} target_words = {i: w for w, i in target_ids.items()} # Setup model model = Seq2seq(args.layer, len(source0_ids), len(source1_ids), len(target_ids), args.unit) if args.resume: # Resume from a snapshot print("Load Model") chainer.serializers.load_npz(args.resume, model) if args.gpu >= 0: chainer.backends.cuda.get_device(args.gpu).use() model.to_gpu(args.gpu) for i in range(len(corpus)): source0, source1, target = corpus[i] result = model.translate([model.xp.array(source0)], [model.xp.array(source1)])[0] source0_sentence = ' '.join([source0_words[x] for x in source0]) source1_sentence = ' '.join([source1_words[x] for x in source1]) target_sentence = ' '.join([target_words[y] for y in target]) result_sentence = ' '.join([target_words[y] for y in result]) print('# source0 : ' + source0_sentence) print('# source1 : ' + source1_sentence) print('# result : ' + result_sentence) print('# expect : ' + target_sentence) print("")
def check_collective_communication(param, use_gpu, use_chx): communicator = create_communicator(param, use_gpu, use_chx) mpi_comm.barrier() model = ExampleModel(param.model_dtype) if use_gpu: device = chainermn.testing.get_device(communicator.intra_rank, use_chx) else: device = chainermn.testing.get_device(use_chainerx=use_chx) model.to_device(device) check_bcast_data(communicator, model) model = ExampleModel(param.model_dtype) model.to_device(device) check_multi_node_mean_grad(communicator, model) model = ExampleModel(param.model_dtype) model.to_device(device) check_multi_node_mean_grad_empty(communicator, model) model = ExampleModel(param.model_dtype) model.to_device(device) check_multi_node_mean_grad_empty_half(communicator, model) # Check allreduce debug mode model = ExampleModel() model.to_device(device) # The example model includes some nan parameters so the debug mode # must detect it. chainer.set_debug(True) with pytest.raises(ValueError, match=r'.* diverged .*'): check_multi_node_mean_grad(communicator, model) chainer.set_debug(False) # barrier() requires before destructor of PureNcclCommunicator # because communication may not be finished. mpi_comm.barrier() communicator.finalize()
def setUp(self): self.original_debug = chainer.is_debug() chainer.set_debug(True) self.one = numpy.array([1], numpy.float32) self.f = chainer.Function()
def setUp(self): self.default_debug = chainer.is_debug() chainer.set_debug(True) self.a_data = numpy.random.uniform(-1, 1, (4, 3, 2)) self.b_data = numpy.random.uniform(-1, 1, (2, 2))
def tearDown(self): chainer.set_debug(False)
def main(args): chainer.set_debug(True) # Initialize the model to train model = models.archs[args.arch]() if args.finetune and hasattr(model, 'finetuned_model_path'): utils.finetuning.load_param(model.finetuned_model_path, model, args.ignore) if args.initmodel: print('Load model from', args.initmodel) chainer.serializers.load_npz(args.initmodel, model) if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() # Make the GPU current #cuda.cudnn_enabled = False model.to_gpu() nowt = datetime.datetime.today() outputdir = os.path.join(args.out, args.arch, 'extract') if args.initmodel is not None: outputdir = os.path.dirname(args.initmodel) # Load the datasets and mean file mean = None if hasattr(model, 'mean_value'): mean = makeMeanImage(model.mean_value) else: mean = np.load(args.mean) assert mean is not None val = ppds.PreprocessedDataset(args.val, args.root, mean, model.insize, False) val_iter = chainer.iterators.SerialIterator( val, args.val_batchsize, repeat=False, shuffle=False) # Set up an optimizer optimizer = chainer.optimizers.MomentumSGD() optimizer.setup(model) # Set up a trainer updater = training.StandardUpdater(val_iter, optimizer, device=args.gpu) trainer = training.Trainer(updater, (1, 'epoch'), outputdir) #val_interval = (10 if args.test else int(len(train) / args.batchsize)), 'iteration' val_interval = (1, 'iteration') #snapshot_interval = (10, 'iteration') if args.test else (2, 'epoch') #log_interval = (10, 'iteration') # Copy the chain with shared parameters to flip 'train' flag only in test eval_model = model.copy() eval_model.train = False val_extractor = utils.Extractor(val_iter, eval_model, device=args.gpu) val_extractor.layer_rank = eval_model.layer_rank[args.layer] val_extractor.layer_name = args.layer val_extractor.operation = args.operation val_extractor.save_features = args.savefeatures val_extractor.top = args.top if 'googlenet' in args.arch: val_extractor.lastname = 'validation/main/loss3' trainer.extend(val_extractor, trigger=val_interval) #trainer.extend(extensions.PrintReport([ # 'epoch', 'iteration', 'main/loss', 'validation/main/loss', # 'main/accuracy', 'validation/main/accuracy', #]), trigger=log_interval) #trainer.extend(extensions.ProgressBar(update_interval=10)) if args.resume: chainer.serializers.load_npz(args.resume, trainer) results = val_extractor(trainer) results['outputdir'] = outputdir return results
def main(args): chainer.set_debug(True) # Initialize the model to train model = models.archs[args.arch]() if args.finetune and hasattr(model, 'finetuned_model_path'): utils.finetuning.load_param(model.finetuned_model_path, model, args.ignore) if args.initmodel: print('Load model from', args.initmodel) chainer.serializers.load_npz(args.initmodel, model) if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() # Make the GPU current #if args.test: #cuda.cudnn_enabled = False model.to_gpu() nowt = datetime.datetime.today() outputdir = os.path.join(args.out, args.arch, 'extract') if args.initmodel is not None: outputdir = os.path.dirname(args.initmodel) #if args.indices is None: # args.indices = os.path.join(outputdir, 'features', 'top_' + args.layer + '.txt') # Load the datasets and mean file mean = None if hasattr(model, 'mean_value'): mean = makeMeanImage(model.mean_value) else: mean = np.load(args.mean) assert mean is not None #if args.indices is None: # args.indices = os.path.join(args.out, args.arch, 'extract', 'top_' + args.layer + '.txt') #top_path = os.path.join(args.out, args.arch, 'extract', 'top_' + args.layer + '.txt') #train = ppds.PreprocessedDataset(args.train, args.root, mean, model.insize) val = ppds.PreprocessedDataset(args.val, args.root, mean, model.insize, False) # These iterators load the images with subprocesses running in parallel to # the training/validation. #train_iter = chainer.iterators.MultiprocessIterator( # train, args.batchsize, shuffle=False, n_processes=args.loaderjob) #val_iter = chainer.iterators.MultiprocessIterator( # val, args.val_batchsize, repeat=False, shuffle=False, n_processes=args.loaderjob) val_iter = chainer.iterators.SerialIterator( val, args.val_batchsize, repeat=False, shuffle=False) # Set up an optimizer optimizer = chainer.optimizers.MomentumSGD() optimizer.setup(model) # Set up a trainer updater = training.StandardUpdater(val_iter, optimizer, device=args.gpu) trainer = training.Trainer(updater, (1, 'epoch'), outputdir) #val_interval = (10 if args.test else int(len(train) / args.batchsize)), 'iteration' val_interval = (1, 'iteration') #snapshot_interval = (10, 'iteration') if args.test else (2, 'epoch') log_interval = (10, 'iteration') # Copy the chain with shared parameters to flip 'train' flag only in test eval_model = model.copy() eval_model.train = False val_acquirer = utils.SaliencyMapAcquirer(val_iter, eval_model, device=args.gpu) val_acquirer.mean = mean val_acquirer.layer_rank = eval_model.layer_rank[args.layer] val_acquirer.layer_name = args.layer val_acquirer.operation = args.operation val_acquirer.fixed_RMS = args.rms #val_acquirer.top = args.top val_acquirer.target = args.target val_acquirer.n_features = model.labelsize if 'googlenet' in args.arch: val_acquirer.lastname = 'validation/main/loss3' trainer.extend(val_acquirer, trigger=val_interval) #trainer.extend(extensions.dump_graph('main/loss')) #trainer.extend(extensions.snapshot(), trigger=snapshot_interval) #trainer.extend(extensions.snapshot_object( # model, 'model_iter_{.updater.iteration}'), trigger=snapshot_interval) # Be careful to pass the interval directly to LogReport # (it determines when to emit log rather than when to read observations) #trainer.extend(extensions.LogReport(trigger=log_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', ]), trigger=log_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) #trainer.extend(extensions.ExponentialShift('lr', args.gamma), # trigger=(1, 'epoch')) if args.resume: chainer.serializers.load_npz(args.resume, trainer) #if not args.test: # trainer.run() # chainer.serializers.save_npz(outputdir + '/model', model) results = val_acquirer(trainer) results['outputdir'] = outputdir #if eval_model.layer_rank[args.layer] == 1: # save_first_conv_filter(os.path.join(outputdir, args.layer), # model[args.layer].W.data, cols = args.cols, pad = args.pad, # scale = args.scale, gamma = args.gamma) #if args.test: #print(val_acquirer.confmat) #categories = utils.io.load_categories(args.categories) #confmat_csv_name = args.initmodel + '.csv' #confmat_fig_name = args.initmodel + '.eps' #utils.io.save_confmat_csv(confmat_csv_name, val_acquirer.confmat, categories) #utils.io.save_confmat_fig(confmat_fig_name, val_acquirer.confmat, categories, # mode="rate", saveFormat="eps") return results
def main(args): chainer.set_debug(True) # Initialize the model to train model = models.archs[args.arch]() if args.finetune and hasattr(model, 'finetuned_model_path'): finetuning.load_param(model.finetuned_model_path, model, args.ignore) if args.initmodel: print('Load model from', args.initmodel) chainer.serializers.load_npz(args.initmodel, model) if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() # Make the GPU current #if args.test: #cuda.cudnn_enabled = False model.to_gpu() nowt = datetime.datetime.today() outputdir = args.out + '/' + args.arch + '/' + nowt.strftime( "%Y%m%d-%H%M") + '_bs' + str(args.batchsize) if args.test and args.initmodel is not None: outputdir = os.path.dirname(args.initmodel) # Load the datasets and mean file mean = None if hasattr(model, 'mean_value'): mean = makeMeanImage(model.mean_value) else: mean = np.load(args.mean) assert mean is not None train = ppds.PreprocessedDataset(args.train, args.root, mean, model.insize) val = ppds.PreprocessedDataset(args.val, args.root, mean, model.insize, False) # These iterators load the images with subprocesses running in parallel to # the training/validation. train_iter = chainer.iterators.MultiprocessIterator( train, args.batchsize, shuffle=False, n_processes=args.loaderjob) #val_iter = chainer.iterators.MultiprocessIterator( # val, args.val_batchsize, repeat=False, shuffle=False, n_processes=args.loaderjob) val_iter = chainer.iterators.SerialIterator(val, args.val_batchsize, repeat=False, shuffle=False) # Set up an optimizer optimizer = chainer.optimizers.MomentumSGD(lr=args.baselr, momentum=0.9) optimizer.setup(model) # Set up a trainer updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), outputdir) #val_interval = (10 if args.test else int(len(train) / args.batchsize)), 'iteration' val_interval = (10, 'iteration') if args.test else (1, 'epoch') snapshot_interval = (10, 'iteration') if args.test else (2, 'epoch') log_interval = (10 if args.test else 200), 'iteration' # Copy the chain with shared parameters to flip 'train' flag only in test eval_model = model.copy() eval_model.train = False if not args.test: val_evaluator = extensions.Evaluator(val_iter, eval_model, device=args.gpu) else: val_evaluator = evaluator_plus.EvaluatorPlus(val_iter, eval_model, device=args.gpu) if 'googlenet' in args.arch: val_evaluator.lastname = 'validation/main/loss3' trainer.extend(val_evaluator, trigger=val_interval) trainer.extend(extensions.dump_graph('main/loss')) trainer.extend(extensions.snapshot(), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( model, 'model_iter_{.updater.iteration}'), trigger=snapshot_interval) # Be careful to pass the interval directly to LogReport # (it determines when to emit log rather than when to read observations) trainer.extend(extensions.LogReport(trigger=log_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', ]), trigger=log_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(extensions.ExponentialShift('lr', args.gamma), trigger=(1, 'epoch')) if args.resume: chainer.serializers.load_npz(args.resume, trainer) if not args.test: trainer.run() chainer.serializers.save_npz(outputdir + '/model', model) results = val_evaluator(trainer) results['outputdir'] = outputdir if args.test: print(val_evaluator.confmat) categories = dataio.load_categories(args.categories) confmat_csv_name = args.initmodel + '.csv' confmat_fig_name = args.initmodel + '.eps' dataio.save_confmat_csv(confmat_csv_name, val_evaluator.confmat, categories) dataio.save_confmat_fig(confmat_fig_name, val_evaluator.confmat, categories, mode="rate", saveFormat="eps") return results
def setUp(self): self.default_debug = chainer.is_debug() chainer.set_debug(True) self.x_data = numpy.random.uniform(-1, 1, (4, 3, 2))
def setUp(self): self.x = numpy.arange(10).reshape((2, 5)).astype('f') self.ind = numpy.array(self.indices, 'i') self.debug = chainer.is_debug() chainer.set_debug(True)
from multi_task.multi_task_300 import Multi_task_300 from multi_task.multi_task_512 import Multi_task_512 from datasets.multi_task_dataset_voc import Multi_task_VOC from config.datasets import voc_experiments from datasets.transforms import Transform import chainer.functions as F from multi_task.evaluator.multi_task_evaluator import MultitaskEvaluator import numpy as np from multi_task.segmentation.loss.losses import MultiLoss chainer.set_debug(False) class MultiboxTrainChain(chainer.Chain): def __init__(self, model, gpu=False, alpha=1, k=3, use_multi_task_loss=False, use_dynamic_loss=False, loss_split=0.5): super(MultiboxTrainChain, self).__init__() self.gpu = gpu with self.init_scope():
def setUp(self): self.default_debug = chainer.is_debug() chainer.set_debug(True)
# training parameter parser.add_argument('--batchsize', '-b', type=int, default=128) parser.add_argument('--epoch', '-e', type=int, default=10, help='The number of training epoch') # model parameter parser.add_argument('--unit-num', '-u', type=int, default=512, help='The unit size of each layer in MLP') args = parser.parse_args() chainer.set_debug(True) train, val = kaggle.get_kaggle() train_iter = I.SerialIterator(train, args.batchsize) val_iter = I.SerialIterator(val, args.batchsize, repeat=False, shuffle=False) C = len(kaggle.task_names) predictor = mlp.MLP(args.unit_num, C) classifier = classifier.Classifier(predictor=predictor) if args.gpu >= 0: cuda.get_device(args.gpu).use() classifier.to_gpu() optimizer = O.SGD() optimizer.setup(classifier)
def main(): chainer.set_debug(True) parser = get_parser() args = parser.parse_args() reset_seed(args.seed) #load vocabulary source0_ids = load_vocabulary(args.SOURCE_VOCAB0) source1_ids = load_vocabulary(args.SOURCE_VOCAB1) target_ids = load_vocabulary(args.TARGET_VOCAB) print('Source vocabulary size: %d' % len(source0_ids)) print('Source vocabulary size: %d' % len(source1_ids)) print('Target vocabulary size: %d' % len(target_ids)) train_data = make_data_tuple(source0=(source0_ids, args.SOURCE0), source1=(source1_ids, args.SOURCE1), target=(target_ids, args.TARGET)) source0_words = {i: w for w, i in source0_ids.items()} source1_words = {i: w for w, i in source1_ids.items()} target_words = {i: w for w, i in target_ids.items()} # Setup model model = Seq2seq(args.layer, len(source0_ids), len(source1_ids), len(target_ids), args.unit) if args.gpu >= 0: chainer.backends.cuda.get_device(args.gpu).use() model.to_gpu(args.gpu) # Setup optimizer optimizer = chainer.optimizers.Adam() optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(args.l2)) # Setup iterator train_iter = chainer.iterators.SerialIterator(train_data, args.batchsize) # Setup updater and trainer updater = training.updaters.StandardUpdater(train_iter, optimizer, converter=convert, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) trainer.extend( extensions.LogReport(trigger=(args.log_interval, 'iteration'))) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'main/loss', 'validation/main/loss', 'main/perp', 'validation/main/perp', 'validation/main/bleu', 'test/main/bleu', 'elapsed_time' ]), trigger=(args.log_interval, 'iteration')) if args.validation_source0 and args.validation_source1 and args.validation_target: valid_data = make_data_tuple( source0=(source0_ids, args.validation_source0), source1=(source1_ids, args.validation_source1), target=(target_ids, args.validation_target)) @chainer.training.make_extension() def translate(trainer): source0, source1, target = valid_data[numpy.random.choice( len(valid_data))] result = model.translate([model.xp.array(source0)], [model.xp.array(source1)])[0] source0_sentence = ' '.join([source0_words[x] for x in source0]) source1_sentence = ' '.join([source1_words[x] for x in source1]) target_sentence = ' '.join([target_words[y] for y in target]) result_sentence = ' '.join([target_words[y] for y in result]) print('# source0 : ' + source0_sentence) print('# source1 : ' + source1_sentence) print('# result : ' + result_sentence) print('# expect : ' + target_sentence) trainer.extend(translate, trigger=(args.validation_interval, 'iteration')) trainer.extend(CalculateBleu(model, valid_data, 'validation/main/bleu', device=args.gpu), trigger=(args.validation_interval, 'iteration')) dev_iter = chainer.iterators.SerialIterator(valid_data, args.batchsize, repeat=False, shuffle=False) dev_eval = extensions.Evaluator(dev_iter, model, device=args.gpu, converter=convert) dev_eval.name = 'valid' trainer.extend(dev_eval, trigger=(args.validation_interval, 'iteration')) if args.test_source0 and args.test_source1 and args.test_target: test_data = make_data_tuple(source0=(source0_ids, args.test_source0), source1=(source1_ids, args.test_source1), target=(target_ids, args.test_target)) trainer.extend(CalculateBleu(model, test_data, 'test/main/bleu', device=args.gpu), trigger=(args.test_interval, 'iteration')) print('start training') if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) save_args(args, args.out) trainer.run() if args.save: # Save a snapshot chainer.serializers.save_npz(args.out + "/trainer.npz", trainer) chainer.serializers.save_npz(args.out + "/model.npz", model)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--outdir', type=str, default='results', help='Directory path to save output files.' ' If it does not exist, it will be created.') parser.add_argument( '--env', type=str, choices=[ 'Pendulum-v0', 'AntBulletEnv-v0', 'HalfCheetahBulletEnv-v0', 'HumanoidBulletEnv-v0', 'HopperBulletEnv-v0', 'Walker2DBulletEnv-v0' ], help= 'OpenAI Gym env and Pybullet (roboschool) env to perform algorithm on.' ) parser.add_argument('--num-envs', type=int, default=1, help='Number of envs run in parallel.') parser.add_argument('--seed', type=int, default=0, help='Random seed [0, 2 ** 32)') parser.add_argument('--gpu', type=int, default=0, help='GPU to use, set to -1 if no GPU.') parser.add_argument('--load', type=str, default='', help='Directory to load agent from.') parser.add_argument('--load-demo', type=str, default='', help='Directory to load replay buffer of demo from.') parser.add_argument( '--expert-num-episode', type=int, default=0, help='the number of expert trajectory, if 0, no create demo mode.') parser.add_argument('--absorb', action='store_true', help='Add absorb state or do not.') parser.add_argument('--lamda', type=float, default=1, help='reguralized lambda') parser.add_argument('--reward-func', action='store_true', help='Add absorb state or do not.') parser.add_argument('--steps', type=int, default=10**6, help='Total number of timesteps to train the agent.') parser.add_argument('--eval-n-runs', type=int, default=10, help='Number of episodes run for each evaluation.') parser.add_argument('--eval-interval', type=int, default=5000, help='Interval in timesteps between evaluations.') parser.add_argument('--replay-start-size', type=int, default=10000, help='Minimum replay buffer size before ' + 'performing gradient updates.') parser.add_argument('--batch-size', type=int, default=100, help='Minibatch size') parser.add_argument('--learning-rate', type=float, default=1e-3, help='Learning rate of optimizers') parser.add_argument('--render', action='store_true', help='Render env states in a GUI window.') parser.add_argument('--demo', action='store_true', help='Just run evaluation, not training.') parser.add_argument('--pretrain', type=int, default=0, help='The number of pretrain iterations') parser.add_argument('--monitor', action='store_true', help='Wrap env with gym.wrappers.Monitor.') parser.add_argument('--log-interval', type=int, default=1000, help='Interval in timesteps between outputting log' ' messages during training') parser.add_argument('--logger-level', type=int, default=logging.INFO, help='Level of the root logger.') parser.add_argument('--policy-output-scale', type=float, default=1., help='Weight initialization scale of polity output.') parser.add_argument('--debug', action='store_true', help='Debug mode.') args = parser.parse_args() if args.expert_num_episode > 0: args.absorb = True logging.basicConfig(level=args.logger_level) if args.debug: chainer.set_debug(True) dir_name = f'{args.env}_{args.seed}' dir_name += '_demo' if args.demo else '' dir_name += '_absorb' if args.absorb else '' dir_name += '_reward' if args.reward_func else '' # dir_name += f'_{args.lamda}' args.outdir = experiments.prepare_output_dir(args, args.outdir, argv=sys.argv, time_format=dir_name) print('Output files are saved in {}'.format(args.outdir)) # Set a random seed used in ChainerRL misc.set_random_seed(args.seed, gpus=(args.gpu, )) # Set different random seeds for different subprocesses. # If seed=0 and processes=4, subprocess seeds are [0, 1, 2, 3]. # If seed=1 and processes=4, subprocess seeds are [4, 5, 6, 7]. process_seeds = np.arange(args.num_envs) + args.seed * args.num_envs assert process_seeds.max() < 2**32 def make_env(process_idx, test): env = gym.make(args.env) # Unwrap TimiLimit wrapper assert isinstance(env, gym.wrappers.TimeLimit) env = env.env # Use different random seeds for train and test envs process_seed = int(process_seeds[process_idx]) env_seed = 2**32 - 1 - process_seed if test else process_seed env.seed(env_seed) if isinstance(env.observation_space, Box): # Cast observations to float32 because our model uses float32 env = chainerrl.wrappers.CastObservationToFloat32(env) else: env = atari_wrappers.wrap_deepmind(atari_wrappers.make_atari( args.env, max_frames=None), episode_life=not test, clip_rewards=not test) # if args.absorb: # if isinstance(env.observation_space, Box): # env = wrappers.AbsorbingWrapper(env) # else: # raise NotImplementedError('Currently, my AbsorobingWrapper is not available for Discrete observation.') if isinstance(env.action_space, Box): # Normalize action space to [-1, 1]^n env = wrappers.NormalizeActionSpace(env) if args.monitor: env = gym.wrappers.Monitor(env, args.outdir) if args.render: env = chainerrl.wrappers.Render(env) return env def make_batch_env(test): return chainerrl.envs.MultiprocessVectorEnv([ functools.partial(make_env, idx, test) for idx, env in enumerate(range(args.num_envs)) ]) sample_env = make_env(process_idx=0, test=False) timestep_limit = sample_env.spec.tags.get( 'wrapper_config.TimeLimit.max_episode_steps') obs_space = sample_env.observation_space action_space = sample_env.action_space print('Observation space:', obs_space) print('Action space:', action_space) if isinstance(obs_space, Box): head = network_sqil.FCHead() phi = lambda x: x else: head = network_sqil.CNNHead(n_input_channels=4) phi = lambda x: np.asarray(x, dtype=np.float32) / 255 if isinstance(action_space, Box): action_size = action_space.low.size policy = network_sqil.GaussianPolicy(copy.deepcopy(head), action_size) q_func1 = network_sqil.QSAFunction(copy.deepcopy(head), action_size) q_func2 = network_sqil.QSAFunction(copy.deepcopy(head), action_size) def burnin_action_func(): """Select random actions until model is updated one or more times.""" return np.random.uniform(action_space.low, action_space.high).astype(np.float32) else: action_size = action_space.n policy = network_sqil.SoftmaxPolicy(copy.deepcopy(head), action_size) q_func1 = network_sqil.QSFunction(copy.deepcopy(head), action_size) q_func2 = network_sqil.QSFunction(copy.deepcopy(head), action_size) def burnin_action_func(): return np.random.randint(0, action_size) policy_optimizer = optimizers.Adam(args.learning_rate).setup(policy) policy_optimizer.add_hook(GradientClipping(40)) q_func1_optimizer = optimizers.Adam(args.learning_rate).setup(q_func1) q_func2_optimizer = optimizers.Adam(args.learning_rate).setup(q_func2) if args.reward_func: reward_func = network_sqil.FCRewardFunction(action_size) reward_func_optimizer = optimizers.Adam( args.learning_rate).setup(reward_func) else: reward_func = None reward_func_optimizer = None # Draw the computational graph and save it in the output directory. fake_obs = chainer.Variable(policy.xp.zeros_like(obs_space.low, dtype=np.float32)[None], name='observation') fake_action = chainer.Variable(policy.xp.zeros_like( action_space.low, dtype=np.float32)[None], name='action') fake_absorb = chainer.Variable(policy.xp.zeros_like( [1], dtype=np.float32)[None], name='absorb') chainerrl.misc.draw_computational_graph( [policy(fake_obs, fake_absorb).sample()], os.path.join(args.outdir, 'policy')) chainerrl.misc.draw_computational_graph( [q_func1(fake_obs, fake_absorb, fake_action)], os.path.join(args.outdir, 'q_func1')) chainerrl.misc.draw_computational_graph( [q_func2(fake_obs, fake_absorb, fake_action)], os.path.join(args.outdir, 'q_func2')) if args.absorb: absorb_state = sample_env.observation_space.sample() * 0 absorb_action = sample_env.action_space.sample() * 0 rbuf = AbsorbReplayBuffer(5 * 10**5, absorb_state, absorb_action) else: rbuf = replay_buffer.ReplayBuffer(5 * 10**5) rbuf_demo = replay_buffer.ReplayBuffer(5 * 10**5) if len(args.load_demo) > 0: rbuf_demo.load(os.path.join(args.load_demo, 'replay')) if args.absorb: from convert_to_absorb_replay import convert rbuf_demo = convert(rbuf_demo) assert isinstance(rbuf_demo, AbsorbReplayBuffer) else: assert isinstance(rbuf_demo, replay_buffer.ReplayBuffer) # Hyperparameters in http://arxiv.org/abs/1802.09477 agent = sqil.SQIL(policy, q_func1, q_func2, reward_func, policy_optimizer, q_func1_optimizer, q_func2_optimizer, reward_func_optimizer, rbuf, rbuf_demo, gamma=0.99, is_discrete=isinstance(action_space, Discrete), replay_start_size=args.replay_start_size, gpu=args.gpu, minibatch_size=args.batch_size, phi=phi, burnin_action_func=burnin_action_func, entropy_target=-action_size if isinstance( action_space, Box) else -np.log( (1.0 / action_size)) * 0.98, temperature_optimizer=chainer.optimizers.Adam(3e-4), lamda=args.lamda) if args.load: agent.load(args.load, args.expert_num_episode == 0) if args.demo: eval_stats = experiments.eval_performance( env=make_env(process_idx=0, test=True), agent=agent, n_steps=None, n_episodes=args.eval_n_runs, max_episode_len=timestep_limit, ) print('n_runs: {} mean: {} median: {} stdev {}'.format( args.eval_n_runs, eval_stats['mean'], eval_stats['median'], eval_stats['stdev'])) else: experiments.train_agent_with_evaluation( agent=agent, env=make_env(process_idx=0, test=False), eval_env=make_env(process_idx=0, test=True), outdir=args.outdir, steps=args.steps, eval_n_steps=None, eval_n_episodes=args.eval_n_runs, eval_interval=args.eval_interval, # log_interval=args.log_interval, train_max_episode_len=timestep_limit, eval_max_episode_len=timestep_limit, )
def tearDown(self): chainer.set_debug(self.default_debug)
def main(): logging.basicConfig( format='%(asctime)s : %(threadName)s : %(levelname)s : %(message)s', level=logging.INFO) import argparse parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-g', default=-1, type=int, help='GPU ID (negative value indicates CPU)') parser.add_argument('--batchsize', dest='batchsize', type=int, default=32, help='learning minibatch size') parser.add_argument('--batchsize_semi', dest='batchsize_semi', type=int, default=64, help='learning minibatch size') parser.add_argument('--n_epoch', dest='n_epoch', type=int, default=30, help='n_epoch') parser.add_argument('--pretrained_model', dest='pretrained_model', type=str, default='', help='pretrained_model') parser.add_argument('--use_unlabled_to_vocab', dest='use_unlabled_to_vocab', type=int, default=1, help='use_unlabled_to_vocab') parser.add_argument('--use_rational', dest='use_rational', type=int, default=0, help='use_rational') parser.add_argument('--save_name', dest='save_name', type=str, default='sentiment_model', help='save_name') parser.add_argument('--n_layers', dest='n_layers', type=int, default=1, help='n_layers') parser.add_argument('--alpha', dest='alpha', type=float, default=0.001, help='alpha') parser.add_argument('--alpha_decay', dest='alpha_decay', type=float, default=0.0, help='alpha_decay') parser.add_argument('--clip', dest='clip', type=float, default=5.0, help='clip') parser.add_argument('--debug_mode', dest='debug_mode', type=int, default=0, help='debug_mode') parser.add_argument('--use_exp_decay', dest='use_exp_decay', type=int, default=1, help='use_exp_decay') parser.add_argument('--load_trained_lstm', dest='load_trained_lstm', type=str, default='', help='load_trained_lstm') parser.add_argument('--freeze_word_emb', dest='freeze_word_emb', type=int, default=0, help='freeze_word_emb') parser.add_argument('--dropout', dest='dropout', type=float, default=0.50, help='dropout') parser.add_argument('--use_adv', dest='use_adv', type=int, default=0, help='use_adv') parser.add_argument('--xi_var', dest='xi_var', type=float, default=1.0, help='xi_var') parser.add_argument('--xi_var_first', dest='xi_var_first', type=float, default=1.0, help='xi_var_first') parser.add_argument('--lower', dest='lower', type=int, default=1, help='lower') parser.add_argument('--nl_factor', dest='nl_factor', type=float, default=1.0, help='nl_factor') parser.add_argument('--min_count', dest='min_count', type=int, default=1, help='min_count') parser.add_argument('--ignore_unk', dest='ignore_unk', type=int, default=0, help='ignore_unk') parser.add_argument('--use_semi_data', dest='use_semi_data', type=int, default=0, help='use_semi_data') parser.add_argument('--add_labeld_to_unlabel', dest='add_labeld_to_unlabel', type=int, default=1, help='add_labeld_to_unlabel') parser.add_argument('--norm_sentence_level', dest='norm_sentence_level', type=int, default=1, help='norm_sentence_level') parser.add_argument('--dataset', default='imdb', choices=['imdb', 'elec', 'rotten', 'dbpedia', 'rcv1']) parser.add_argument('--eval', dest='eval', type=int, default=0, help='eval') parser.add_argument('--emb_dim', dest='emb_dim', type=int, default=256, help='emb_dim') parser.add_argument('--hidden_dim', dest='hidden_dim', type=int, default=1024, help='hidden_dim') parser.add_argument('--hidden_cls_dim', dest='hidden_cls_dim', type=int, default=30, help='hidden_cls_dim') parser.add_argument('--adaptive_softmax', dest='adaptive_softmax', type=int, default=1, help='adaptive_softmax') parser.add_argument('--random_seed', dest='random_seed', type=int, default=1234, help='random_seed') parser.add_argument('--n_class', dest='n_class', type=int, default=2, help='n_class') parser.add_argument('--word_only', dest='word_only', type=int, default=0, help='word_only') args = parser.parse_args() batchsize = args.batchsize batchsize_semi = args.batchsize_semi print(args) random.seed(args.random_seed) np.random.seed(args.random_seed) os.environ["CHAINER_SEED"] = str(args.random_seed) os.makedirs("models", exist_ok=True) if args.debug_mode: chainer.set_debug(True) use_unlabled_to_vocab = args.use_unlabled_to_vocab lower = args.lower == 1 n_char_vocab = 1 n_class = 2 if args.dataset == 'imdb': vocab_obj, dataset, lm_data, t_vocab = utils.load_dataset_imdb( include_pretrain=use_unlabled_to_vocab, lower=lower, min_count=args.min_count, ignore_unk=args.ignore_unk, use_semi_data=args.use_semi_data, add_labeld_to_unlabel=args.add_labeld_to_unlabel) (train_x, train_x_len, train_y, dev_x, dev_x_len, dev_y, test_x, test_x_len, test_y) = dataset vocab, vocab_count = vocab_obj n_class = 2 if args.use_semi_data: semi_train_x, semi_train_x_len = lm_data print('train_vocab_size:', t_vocab) vocab_inv = dict([(widx, w) for w, widx in vocab.items()]) print('vocab_inv:', len(vocab_inv)) xp = cuda.cupy if args.gpu >= 0 else np if args.gpu >= 0: cuda.get_device(args.gpu).use() xp.random.seed(args.random_seed) n_vocab = len(vocab) model = net.uniLSTM_VAT(n_vocab=n_vocab, emb_dim=args.emb_dim, hidden_dim=args.hidden_dim, use_dropout=args.dropout, n_layers=args.n_layers, hidden_classifier=args.hidden_cls_dim, use_adv=args.use_adv, xi_var=args.xi_var, n_class=n_class, args=args) if args.pretrained_model != '': # load pretrained LM model pretrain_model = lm_nets.RNNForLM(n_vocab, 1024, args.n_layers, 0.50, share_embedding=False, adaptive_softmax=args.adaptive_softmax) serializers.load_npz(args.pretrained_model, pretrain_model) pretrain_model.lstm = pretrain_model.rnn model.set_pretrained_lstm(pretrain_model, word_only=args.word_only) if args.load_trained_lstm != '': serializers.load_hdf5(args.load_trained_lstm, model) if args.gpu >= 0: model.to_gpu() def evaluate(x_set, x_length_set, y_set): chainer.config.train = False chainer.config.enable_backprop = False iteration_list = range(0, len(x_set), batchsize) correct_cnt = 0 total_cnt = 0.0 predicted_np = [] for i_index, index in enumerate(iteration_list): x = [to_gpu(_x) for _x in x_set[index:index + batchsize]] x_length = x_length_set[index:index + batchsize] y = to_gpu(y_set[index:index + batchsize]) output = model(x, x_length) predict = xp.argmax(output.data, axis=1) correct_cnt += xp.sum(predict == y) total_cnt += len(y) accuracy = (correct_cnt / total_cnt) * 100.0 chainer.config.enable_backprop = True return accuracy def get_unlabled(perm_semi, i_index): index = i_index * batchsize_semi sample_idx = perm_semi[index:index + batchsize_semi] x = [to_gpu(semi_train_x[_i]) for _i in sample_idx] x_length = [semi_train_x_len[_i] for _i in sample_idx] return x, x_length base_alpha = args.alpha opt = optimizers.Adam(alpha=base_alpha) opt.setup(model) opt.add_hook(chainer.optimizer.GradientClipping(args.clip)) if args.freeze_word_emb: model.freeze_word_emb() prev_dev_accuracy = 0.0 global_step = 0.0 adv_rep_num_statics = {} adv_rep_pos_statics = {} if args.eval: dev_accuracy = evaluate(dev_x, dev_x_len, dev_y) log_str = ' [dev] accuracy:{}, length:{}'.format(str(dev_accuracy)) logging.info(log_str) # test test_accuracy = evaluate(test_x, test_x_len, test_y) log_str = ' [test] accuracy:{}, length:{}'.format(str(test_accuracy)) logging.info(log_str) for epoch in range(args.n_epoch): logging.info('epoch:' + str(epoch)) # train model.cleargrads() chainer.config.train = True iteration_list = range(0, len(train_x), batchsize) # iteration_list_semi = range(0, len(semi_train_x), batchsize) perm = np.random.permutation(len(train_x)) if args.use_semi_data: perm_semi = [np.random.permutation(len(semi_train_x)) for _ in range(2)] perm_semi = np.concatenate(perm_semi, axis=0) # print 'perm_semi:', perm_semi.shape def idx_func(shape): return xp.arange(shape).astype(xp.int32) sum_loss = 0.0 sum_loss_z = 0.0 sum_loss_z_sparse = 0.0 sum_loss_label = 0.0 avg_rate = 0.0 avg_rate_num = 0.0 correct_cnt = 0 total_cnt = 0.0 N = len(iteration_list) is_adv_example_list = [] is_adv_example_disc_list = [] is_adv_example_disc_craft_list = [] y_np = [] predicted_np = [] save_items = [] for i_index, index in enumerate(iteration_list): global_step += 1.0 model.set_train(True) sample_idx = perm[index:index + batchsize] x = [to_gpu(train_x[_i]) for _i in sample_idx] x_length = [train_x_len[_i] for _i in sample_idx] y = to_gpu(train_y[sample_idx]) d = None # Classification loss output = model(x, x_length) output_original = output loss = F.softmax_cross_entropy(output, y, normalize=True) if args.use_adv or args.use_semi_data: # Adversarial Training if args.use_adv: output = model(x, x_length, first_step=True, d=None) # Adversarial loss (First step) loss_adv_first = F.softmax_cross_entropy(output, y, normalize=True) model.cleargrads() loss_adv_first.backward() if args.use_adv: d = model.d_var.grad d_data = d.data if isinstance(d, chainer.Variable) else d output = model(x, x_length, d=d) # Adversarial loss loss_adv = F.softmax_cross_entropy(output, y, normalize=True) loss += loss_adv * args.nl_factor # Virtual Adversarial Training if args.use_semi_data: x, length = get_unlabled(perm_semi, i_index) output_original = model(x, length) output_vat = model(x, length, first_step=True, d=None) loss_vat_first = net.kl_loss(xp, output_original.data, output_vat) model.cleargrads() loss_vat_first.backward() d_vat = model.d_var.grad output_vat = model(x, length, d=d_vat) loss_vat = net.kl_loss(xp, output_original.data, output_vat) loss += loss_vat predict = xp.argmax(output.data, axis=1) correct_cnt += xp.sum(predict == y) total_cnt += len(y) # update model.cleargrads() loss.backward() opt.update() if args.alpha_decay > 0.0: if args.use_exp_decay: opt.hyperparam.alpha = (base_alpha) * (args.alpha_decay**global_step) else: opt.hyperparam.alpha *= args.alpha_decay # 0.9999 sum_loss += loss.data accuracy = (correct_cnt / total_cnt) * 100.0 logging.info(' [train] sum_loss: {}'.format(sum_loss / N)) logging.info(' [train] apha:{}, global_step:{}'.format(opt.hyperparam.alpha, global_step)) logging.info(' [train] accuracy:{}'.format(accuracy)) model.set_train(False) # dev dev_accuracy = evaluate(dev_x, dev_x_len, dev_y) log_str = ' [dev] accuracy:{}'.format(str(dev_accuracy)) logging.info(log_str) # test test_accuracy = evaluate(test_x, test_x_len, test_y) log_str = ' [test] accuracy:{}'.format(str(test_accuracy)) logging.info(log_str) last_epoch_flag = args.n_epoch - 1 == epoch if prev_dev_accuracy < dev_accuracy: logging.info(' => '.join([str(prev_dev_accuracy), str(dev_accuracy)])) result_str = 'dev_acc_' + str(dev_accuracy) result_str += '_test_acc_' + str(test_accuracy) model_filename = './models/' + '_'.join([args.save_name, str(epoch), result_str]) serializers.save_hdf5(model_filename + '.model', model) prev_dev_accuracy = dev_accuracy
def setUp(self): self.link = links.EmbedID(2, 2) self.t = numpy.array([self.t_value], dtype=numpy.int32) self.original_debug = chainer.is_debug() chainer.set_debug(True)
def main(): parser = argparse.ArgumentParser( description='Condtitional WGAN in Chainer') parser.add_argument('--batchsize', '-b', type=int, default=64, help='Integer of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=1, help='Integer of Epochs') parser.add_argument('--out', '-o', default='result', help='Directory of output result') parser.add_argument('--gpu', '-g', type=int, default=-1, help='Gpu number') parser.add_argument('--resume', '-r', default='', help='start training from snapshot') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--imagedir', '-dir', default=None, help="Directory of image dir") parser.add_argument('--ncritic', '-nc', default=5, help='n_critic') parser.add_argument('--clamp', default=0.01, help='bound of weight clipping in critic') parser.add_argument('--debug', default=False, help='chainer debug mode') parser.add_argument('--distribution', '-dist', default='uniform', help='noise z sampling distribution') args = parser.parse_args() if args.debug: chainer.set_debug(True) # model setup generator = Generator(n_hidden=110, label_num=10, distribution=args.distribution) critic = Critic(batch_num=args.batchsize, label_num=10) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() generator.to_gpu() critic.to_gpu() # optimizer setup # optimizer is need quite tough decision. def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec') return optimizer def make_optimizer_SGD(model, lr=0.01): optimizer = optimizers.SGD(lr) optimizer.setup(model) return optimizer def make_optimizer_RMS(model, lr=0.0002): optimizer = optimizers.RMSprop(lr) optimizer.setup(model) return optimizer opt_gen = make_optimizer_RMS(generator) opt_critic = make_optimizer_RMS(critic) # dataset setup if args.imagedir is None: # if imagedir not given, use cifar-10 train, _ = chainer.datasets.get_cifar10(withlabel=True, scale=255.) else: # TODO: change from ImageDataset to Labeled Dataset. p = Path(args.imagedir) img_tuple = {} label_num = 0 datapathstore = {} labeldirs = [x for x in p.iterdir() if x.is_dir()] for label in labeldirs: img_tuple[label.name] = label_num datapathstore[label.name] = map( lambda x: (x, label_num), [str for str in label.iterdir() if str.is_file()]) datalist = [] for str in datapathstore.items(): datalist = datalist + str train = chainer.datasets.ImageDataset(datalist) # train *= 1. / 255. # *er setup train_iter = chainer.iterators.SerialIterator(train, args.batchsize) updater = Updater(models=(generator, critic), iterator=train_iter, optimizer={ 'gen': opt_gen, 'critic': opt_critic }, n_critic=args.ncritic, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) # extentions snapshot_interval = (1000, 'iteration') display_interval = (100, 'iteration') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( generator, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( critic, 'critic_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport( ['epoch', 'iteration', 'gen_loss', 'critic_loss']), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=20)) trainer.extend(out_generated_random_image(generator, critic, 10, 10, 10, dst='uniform'), trigger=snapshot_interval) # trainer.extend( # extensions.PlotReport(['gen/loss', 'critic/loss', 'classifier/loss'], # 'epoch', file_name='plot_{.updater.epoch}.png', grid=True) # ) # FLY TO THE FUTURE! if args.resume: chainer.serializers.load_npz(args.resume, trainer) trainer.run()
def main(): logging.basicConfig( format='%(asctime)s : %(threadName)s : %(levelname)s : %(message)s', level=logging.INFO) import argparse parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-g', default=-1, type=int, help='GPU ID (negative value indicates CPU)') parser.add_argument('--batchsize', dest='batchsize', type=int, default=32, help='learning minibatch size') parser.add_argument('--batchsize_semi', dest='batchsize_semi', type=int, default=64, help='learning minibatch size') parser.add_argument('--n_epoch', dest='n_epoch', type=int, default=30, help='n_epoch') parser.add_argument('--pretrained_model', dest='pretrained_model', type=str, default='', help='pretrained_model') parser.add_argument('--use_unlabled_to_vocab', dest='use_unlabled_to_vocab', type=int, default=1, help='use_unlabled_to_vocab') parser.add_argument('--use_rational', dest='use_rational', type=int, default=0, help='use_rational') parser.add_argument('--save_name', dest='save_name', type=str, default='sentiment_model', help='save_name') parser.add_argument('--n_layers', dest='n_layers', type=int, default=1, help='n_layers') parser.add_argument('--alpha', dest='alpha', type=float, default=0.001, help='alpha') parser.add_argument('--alpha_decay', dest='alpha_decay', type=float, default=0.0, help='alpha_decay') parser.add_argument('--clip', dest='clip', type=float, default=5.0, help='clip') parser.add_argument('--debug_mode', dest='debug_mode', type=int, default=0, help='debug_mode') parser.add_argument('--use_exp_decay', dest='use_exp_decay', type=int, default=1, help='use_exp_decay') parser.add_argument('--load_trained_lstm', dest='load_trained_lstm', type=str, default='', help='load_trained_lstm') parser.add_argument('--freeze_word_emb', dest='freeze_word_emb', type=int, default=0, help='freeze_word_emb') parser.add_argument('--dropout', dest='dropout', type=float, default=0.50, help='dropout') parser.add_argument('--use_adv', dest='use_adv', type=int, default=0, help='use_adv') parser.add_argument('--xi_var', dest='xi_var', type=float, default=1.0, help='xi_var') parser.add_argument('--xi_var_first', dest='xi_var_first', type=float, default=1.0, help='xi_var_first') parser.add_argument('--lower', dest='lower', type=int, default=1, help='lower') parser.add_argument('--nl_factor', dest='nl_factor', type=float, default=1.0, help='nl_factor') parser.add_argument('--min_count', dest='min_count', type=int, default=1, help='min_count') parser.add_argument('--ignore_unk', dest='ignore_unk', type=int, default=0, help='ignore_unk') parser.add_argument('--use_semi_data', dest='use_semi_data', type=int, default=0, help='use_semi_data') parser.add_argument('--add_labeld_to_unlabel', dest='add_labeld_to_unlabel', type=int, default=1, help='add_labeld_to_unlabel') parser.add_argument('--norm_sentence_level', dest='norm_sentence_level', type=int, default=1, help='norm_sentence_level') parser.add_argument('--dataset', default='imdb', choices=['imdb', 'elec', 'rotten', 'dbpedia', 'rcv1']) parser.add_argument('--eval', dest='eval', type=int, default=0, help='eval') parser.add_argument('--emb_dim', dest='emb_dim', type=int, default=256, help='emb_dim') parser.add_argument('--hidden_dim', dest='hidden_dim', type=int, default=1024, help='hidden_dim') parser.add_argument('--hidden_cls_dim', dest='hidden_cls_dim', type=int, default=30, help='hidden_cls_dim') parser.add_argument('--adaptive_softmax', dest='adaptive_softmax', type=int, default=1, help='adaptive_softmax') parser.add_argument('--random_seed', dest='random_seed', type=int, default=1234, help='random_seed') parser.add_argument('--n_class', dest='n_class', type=int, default=2, help='n_class') parser.add_argument('--word_only', dest='word_only', type=int, default=0, help='word_only') args = parser.parse_args() batchsize = args.batchsize batchsize_semi = args.batchsize_semi print(args) random.seed(args.random_seed) np.random.seed(args.random_seed) os.environ["CHAINER_SEED"] = str(args.random_seed) os.makedirs("models", exist_ok=True) if args.debug_mode: chainer.set_debug(True) use_unlabled_to_vocab = args.use_unlabled_to_vocab lower = args.lower == 1 n_char_vocab = 1 n_class = 2 if args.dataset == 'imdb': vocab_obj, dataset, lm_data, t_vocab = utils.load_dataset_imdb( include_pretrain=use_unlabled_to_vocab, lower=lower, min_count=args.min_count, ignore_unk=args.ignore_unk, use_semi_data=args.use_semi_data, add_labeld_to_unlabel=args.add_labeld_to_unlabel) (train_x, train_x_len, train_y, dev_x, dev_x_len, dev_y, test_x, test_x_len, test_y) = dataset vocab, vocab_count = vocab_obj n_class = 2 if args.use_semi_data: semi_train_x, semi_train_x_len = lm_data print('train_vocab_size:', t_vocab) vocab_inv = dict([(widx, w) for w, widx in vocab.items()]) print('vocab_inv:', len(vocab_inv)) xp = cuda.cupy if args.gpu >= 0 else np if args.gpu >= 0: cuda.get_device(args.gpu).use() xp.random.seed(args.random_seed) n_vocab = len(vocab) model = net.uniLSTM_VAT(n_vocab=n_vocab, emb_dim=args.emb_dim, hidden_dim=args.hidden_dim, use_dropout=args.dropout, n_layers=args.n_layers, hidden_classifier=args.hidden_cls_dim, use_adv=args.use_adv, xi_var=args.xi_var, n_class=n_class, args=args) if args.pretrained_model != '': # load pretrained LM model pretrain_model = lm_nets.RNNForLM( n_vocab, 1024, args.n_layers, 0.50, share_embedding=False, adaptive_softmax=args.adaptive_softmax) serializers.load_npz(args.pretrained_model, pretrain_model) pretrain_model.lstm = pretrain_model.rnn model.set_pretrained_lstm(pretrain_model, word_only=args.word_only) if args.load_trained_lstm != '': serializers.load_hdf5(args.load_trained_lstm, model) if args.gpu >= 0: model.to_gpu() def evaluate(x_set, x_length_set, y_set): chainer.config.train = False chainer.config.enable_backprop = False iteration_list = range(0, len(x_set), batchsize) correct_cnt = 0 total_cnt = 0.0 predicted_np = [] for i_index, index in enumerate(iteration_list): x = [to_gpu(_x) for _x in x_set[index:index + batchsize]] x_length = x_length_set[index:index + batchsize] y = to_gpu(y_set[index:index + batchsize]) output = model(x, x_length) predict = xp.argmax(output.data, axis=1) correct_cnt += xp.sum(predict == y) total_cnt += len(y) accuracy = (correct_cnt / total_cnt) * 100.0 chainer.config.enable_backprop = True return accuracy def get_unlabled(perm_semi, i_index): index = i_index * batchsize_semi sample_idx = perm_semi[index:index + batchsize_semi] x = [to_gpu(semi_train_x[_i]) for _i in sample_idx] x_length = [semi_train_x_len[_i] for _i in sample_idx] return x, x_length base_alpha = args.alpha opt = optimizers.Adam(alpha=base_alpha) opt.setup(model) opt.add_hook(chainer.optimizer.GradientClipping(args.clip)) if args.freeze_word_emb: model.freeze_word_emb() prev_dev_accuracy = 0.0 global_step = 0.0 adv_rep_num_statics = {} adv_rep_pos_statics = {} if args.eval: dev_accuracy = evaluate(dev_x, dev_x_len, dev_y) log_str = ' [dev] accuracy:{}, length:{}'.format(str(dev_accuracy)) logging.info(log_str) # test test_accuracy = evaluate(test_x, test_x_len, test_y) log_str = ' [test] accuracy:{}, length:{}'.format(str(test_accuracy)) logging.info(log_str) for epoch in range(args.n_epoch): logging.info('epoch:' + str(epoch)) # train model.cleargrads() chainer.config.train = True iteration_list = range(0, len(train_x), batchsize) # iteration_list_semi = range(0, len(semi_train_x), batchsize) perm = np.random.permutation(len(train_x)) if args.use_semi_data: perm_semi = [ np.random.permutation(len(semi_train_x)) for _ in range(2) ] perm_semi = np.concatenate(perm_semi, axis=0) # print 'perm_semi:', perm_semi.shape def idx_func(shape): return xp.arange(shape).astype(xp.int32) sum_loss = 0.0 sum_loss_z = 0.0 sum_loss_z_sparse = 0.0 sum_loss_label = 0.0 avg_rate = 0.0 avg_rate_num = 0.0 correct_cnt = 0 total_cnt = 0.0 N = len(iteration_list) is_adv_example_list = [] is_adv_example_disc_list = [] is_adv_example_disc_craft_list = [] y_np = [] predicted_np = [] save_items = [] for i_index, index in enumerate(iteration_list): global_step += 1.0 model.set_train(True) sample_idx = perm[index:index + batchsize] x = [to_gpu(train_x[_i]) for _i in sample_idx] x_length = [train_x_len[_i] for _i in sample_idx] y = to_gpu(train_y[sample_idx]) d = None # Classification loss output = model(x, x_length) output_original = output loss = F.softmax_cross_entropy(output, y, normalize=True) if args.use_adv or args.use_semi_data: # Adversarial Training if args.use_adv: output = model(x, x_length, first_step=True, d=None) # Adversarial loss (First step) loss_adv_first = F.softmax_cross_entropy(output, y, normalize=True) model.cleargrads() loss_adv_first.backward() if args.use_adv: d = model.d_var.grad d_data = d.data if isinstance(d, chainer.Variable) else d output = model(x, x_length, d=d) # Adversarial loss loss_adv = F.softmax_cross_entropy(output, y, normalize=True) loss += loss_adv * args.nl_factor # Virtual Adversarial Training if args.use_semi_data: x, length = get_unlabled(perm_semi, i_index) output_original = model(x, length) output_vat = model(x, length, first_step=True, d=None) loss_vat_first = net.kl_loss(xp, output_original.data, output_vat) model.cleargrads() loss_vat_first.backward() d_vat = model.d_var.grad output_vat = model(x, length, d=d_vat) loss_vat = net.kl_loss(xp, output_original.data, output_vat) loss += loss_vat predict = xp.argmax(output.data, axis=1) correct_cnt += xp.sum(predict == y) total_cnt += len(y) # update model.cleargrads() loss.backward() opt.update() if args.alpha_decay > 0.0: if args.use_exp_decay: opt.hyperparam.alpha = (base_alpha) * (args.alpha_decay** global_step) else: opt.hyperparam.alpha *= args.alpha_decay # 0.9999 sum_loss += loss.data accuracy = (correct_cnt / total_cnt) * 100.0 logging.info(' [train] sum_loss: {}'.format(sum_loss / N)) logging.info(' [train] apha:{}, global_step:{}'.format( opt.hyperparam.alpha, global_step)) logging.info(' [train] accuracy:{}'.format(accuracy)) model.set_train(False) # dev dev_accuracy = evaluate(dev_x, dev_x_len, dev_y) log_str = ' [dev] accuracy:{}'.format(str(dev_accuracy)) logging.info(log_str) # test test_accuracy = evaluate(test_x, test_x_len, test_y) log_str = ' [test] accuracy:{}'.format(str(test_accuracy)) logging.info(log_str) last_epoch_flag = args.n_epoch - 1 == epoch if prev_dev_accuracy < dev_accuracy: logging.info(' => '.join( [str(prev_dev_accuracy), str(dev_accuracy)])) result_str = 'dev_acc_' + str(dev_accuracy) result_str += '_test_acc_' + str(test_accuracy) model_filename = './models/' + '_'.join( [args.save_name, str(epoch), result_str]) serializers.save_hdf5(model_filename + '.model', model) prev_dev_accuracy = dev_accuracy
def setUp(self): self.x = numpy.random.uniform(-1, 1, (1, 2)).astype(numpy.float32) self.t = numpy.array([self.t_value], dtype=numpy.int32) self.original_debug = chainer.is_debug() chainer.set_debug(True)
def tearDown(self): chainer.set_debug(self.original_debug)
parser.add_argument('--seed', default=0, type=int, help='random seed') parser.add_argument('--batchsize', default=128, type=int, help='batchsize') parser.add_argument('--iteration', default=100000, type=int, help='iteration') parser.add_argument('--a-dim', default=100, type=int) parser.add_argument('--z-dim', default=100, type=int) parser.add_argument('--h-dim', default=500, type=int) parser.add_argument('--beta', default=1, type=float) parser.add_argument('--alpha', default=3e-4, type=float) parser.add_argument('--debug', action='store_true') parser.add_argument('--verbose', action='store_true') parser.add_argument('--binarize', action='store_true') parser.add_argument('--pruning', action='store_true') args = parser.parse_args() if args.debug: chainer.set_debug(True) np.random.seed(args.seed) if args.gpu >= 0: cuda.cupy.random.seed(args.seed) (x_labeled, y_labeled, x_test, y_test, x_unlabeled, D, T) = data.load_mnist(pruning=args.pruning) labeled_data = feeder.DataFeeder((x_labeled, y_labeled)) test_data = feeder.DataFeeder((x_test, y_test)) unlabeled_data = feeder.DataFeeder(x_unlabeled) N_labeled = len(labeled_data) N_unlabeled = len(unlabeled_data)
def setUp(self): self.x = np.array([1], np.float32) chainer.set_debug(True)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--outdir', type=str, default='results', help='Directory path to save output files.' ' If it does not exist, it will be created.') parser.add_argument( '--env', type=str, choices=[ 'Pendulum-v0', 'AntBulletEnv-v0', 'HalfCheetahBulletEnv-v0', 'HumanoidBulletEnv-v0', 'HopperBulletEnv-v0', 'Walker2DBulletEnv-v0' ], help= 'OpenAI Gym env and Pybullet (roboschool) env to perform algorithm on.' ) parser.add_argument('--num-envs', type=int, default=1, help='Number of envs run in parallel.') parser.add_argument('--seed', type=int, default=0, help='Random seed [0, 2 ** 32)') parser.add_argument('--gpu', type=int, default=0, help='GPU to use, set to -1 if no GPU.') parser.add_argument('--load', type=str, default='', help='Directory to load agent from.') parser.add_argument( '--expert-num-episode', type=int, default=0, help='the number of expert trajectory, if 0, no create demo mode.') parser.add_argument('--steps', type=int, default=10**6, help='Total number of timesteps to train the agent.') parser.add_argument('--eval-n-runs', type=int, default=10, help='Number of episodes run for each evaluation.') parser.add_argument('--eval-interval', type=int, default=5000, help='Interval in timesteps between evaluations.') parser.add_argument('--replay-start-size', type=int, default=10000, help='Minimum replay buffer size before ' + 'performing gradient updates.') parser.add_argument('--batch-size', type=int, default=256, help='Minibatch size') parser.add_argument('--render', action='store_true', help='Render env states in a GUI window.') parser.add_argument('--demo', action='store_true', help='Just run evaluation, not training.') parser.add_argument('--monitor', action='store_true', help='Wrap env with gym.wrappers.Monitor.') parser.add_argument('--log-interval', type=int, default=1000, help='Interval in timesteps between outputting log' ' messages during training') parser.add_argument('--logger-level', type=int, default=logging.INFO, help='Level of the root logger.') parser.add_argument('--policy-output-scale', type=float, default=1., help='Weight initialization scale of polity output.') parser.add_argument('--debug', action='store_true', help='Debug mode.') args = parser.parse_args() logging.basicConfig(level=args.logger_level) if args.debug: chainer.set_debug(True) if args.expert_num_episode == 0: args.outdir = experiments.prepare_output_dir( args, args.outdir, argv=sys.argv, time_format=f'{args.env}_{args.seed}') else: args.outdir = experiments.prepare_output_dir( args, args.outdir, argv=sys.argv, time_format=f'{args.env}_{args.expert_num_episode}expert') args.replay_start_size = 1e8 print('Output files are saved in {}'.format(args.outdir)) # Set a random seed used in ChainerRL misc.set_random_seed(args.seed, gpus=(args.gpu, )) # Set different random seeds for different subprocesses. # If seed=0 and processes=4, subprocess seeds are [0, 1, 2, 3]. # If seed=1 and processes=4, subprocess seeds are [4, 5, 6, 7]. process_seeds = np.arange(args.num_envs) + args.seed * args.num_envs assert process_seeds.max() < 2**32 def make_env(process_idx, test): env = gym.make(args.env) # Unwrap TimiLimit wrapper assert isinstance(env, gym.wrappers.TimeLimit) env = env.env # Use different random seeds for train and test envs process_seed = int(process_seeds[process_idx]) env_seed = 2**32 - 1 - process_seed if test else process_seed env.seed(env_seed) if isinstance(env.observation_space, Box): # Cast observations to float32 because our model uses float32 env = chainerrl.wrappers.CastObservationToFloat32(env) else: env = atari_wrappers.wrap_deepmind(atari_wrappers.make_atari( args.env, max_frames=None), episode_life=not test, clip_rewards=not test) if isinstance(env.action_space, Box): # Normalize action space to [-1, 1]^n env = wrappers.NormalizeActionSpace(env) if args.monitor: env = gym.wrappers.Monitor(env, args.outdir) if args.render: env = chainerrl.wrappers.Render(env) return env def make_batch_env(test): return chainerrl.envs.MultiprocessVectorEnv([ functools.partial(make_env, idx, test) for idx, env in enumerate(range(args.num_envs)) ]) sample_env = make_env(process_idx=0, test=False) timestep_limit = sample_env.spec.tags.get( 'wrapper_config.TimeLimit.max_episode_steps') obs_space = sample_env.observation_space action_space = sample_env.action_space print('Observation space:', obs_space) print('Action space:', action_space) if isinstance(obs_space, Box): head = network.FCHead() phi = lambda x: x else: head = network.CNNHead(n_input_channels=4) phi = lambda x: np.asarray(x, dtype=np.float32) / 255 if isinstance(action_space, Box): action_size = action_space.low.size policy = network.GaussianPolicy(copy.deepcopy(head), action_size) q_func1 = network.QSAFunction(copy.deepcopy(head), action_size) q_func2 = network.QSAFunction(copy.deepcopy(head), action_size) def burnin_action_func(): """Select random actions until model is updated one or more times.""" return np.random.uniform(action_space.low, action_space.high).astype(np.float32) else: action_size = action_space.n policy = network.SoftmaxPolicy(copy.deepcopy(head), action_size) q_func1 = network.QSFunction(copy.deepcopy(head), action_size) q_func2 = network.QSFunction(copy.deepcopy(head), action_size) def burnin_action_func(): return np.random.randint(0, action_size) policy_optimizer = optimizers.Adam(3e-4).setup(policy) q_func1_optimizer = optimizers.Adam(3e-4).setup(q_func1) q_func2_optimizer = optimizers.Adam(3e-4).setup(q_func2) # Draw the computational graph and save it in the output directory. # fake_obs = chainer.Variable( # policy.xp.zeros_like(obs_space.low, dtype=np.float32)[None], # name='observation') # fake_action = chainer.Variable( # policy.xp.zeros_like(action_space.low, dtype=np.float32)[None], # name='action') # chainerrl.misc.draw_computational_graph( # [policy(fake_obs)], os.path.join(args.outdir, 'policy')) # chainerrl.misc.draw_computational_graph( # [q_func1(fake_obs, fake_action)], os.path.join(args.outdir, 'q_func1')) # chainerrl.misc.draw_computational_graph( # [q_func2(fake_obs, fake_action)], os.path.join(args.outdir, 'q_func2')) rbuf = replay_buffer.ReplayBuffer(10**6) # Hyperparameters in http://arxiv.org/abs/1802.09477 agent = sac.SoftActorCritic( policy, q_func1, q_func2, policy_optimizer, q_func1_optimizer, q_func2_optimizer, rbuf, gamma=0.99, is_discrete=isinstance(action_space, Discrete), replay_start_size=args.replay_start_size, gpu=args.gpu, minibatch_size=args.batch_size, phi=phi, burnin_action_func=burnin_action_func, entropy_target=-action_size if isinstance(action_space, Box) else -np.log((1.0 / action_size)) * 0.98, temperature_optimizer=chainer.optimizers.Adam(3e-4), ) if len(args.load) > 0: agent.load(args.load, args.expert_num_episode == 0) if args.demo: eval_stats = experiments.eval_performance( env=make_env(process_idx=0, test=True), agent=agent, n_steps=None, n_episodes=args.eval_n_runs, max_episode_len=timestep_limit, ) print('n_runs: {} mean: {} median: {} stdev {}'.format( args.eval_n_runs, eval_stats['mean'], eval_stats['median'], eval_stats['stdev'])) elif args.expert_num_episode > 0: episode_r = 0 env = sample_env episode_len = 0 t = 0 logger = logging.getLogger(__name__) episode_results = [] try: for ep in range(args.expert_num_episode): obs = env.reset() r = 0 while True: # a_t action = agent.act_and_train(obs, r) # o_{t+1}, r_{t+1} obs, r, done, info = env.step(action) t += 1 episode_r += r episode_len += 1 reset = (episode_len == timestep_limit or info.get('needs_reset', False)) if done or reset: agent.stop_episode_and_train(obs, r, done=done) logger.info('outdir:%s step:%s episode:%s R:%s', args.outdir, t, ep, episode_r) episode_results.append(episode_r) episode_r = 0 episode_len = 0 break logger.info('mean: %s', sum(episode_results) / len(episode_results)) except (Exception, KeyboardInterrupt): raise # Save save_name = os.path.join( os.path.join('demos', f'{args.expert_num_episode}_episode'), args.env) makedirs(save_name, exist_ok=True) agent.replay_buffer.save(os.path.join(save_name, 'replay')) else: experiments.train_agent_with_evaluation( agent=agent, env=make_env(process_idx=0, test=False), eval_env=make_env(process_idx=0, test=True), outdir=args.outdir, steps=args.steps, eval_n_steps=None, eval_n_episodes=args.eval_n_runs, eval_interval=args.eval_interval, # log_interval=args.log_interval, train_max_episode_len=timestep_limit, eval_max_episode_len=timestep_limit, )
def setUp(self): self.x = numpy.random.uniform(-1, 1, (2, 2)).astype(numpy.float32) # `0` is required to avoid NaN self.t = numpy.array([self.t_value, 0], dtype=numpy.int32) self.original_debug = chainer.is_debug() chainer.set_debug(True)
def main(args): chainer.set_debug(True) # Initialize the model to train model = models.archs[args.arch]() if args.finetune and hasattr(model, 'finetuned_model_path'): utils.finetuning.load_param(model.finetuned_model_path, model, args.ignore) if args.initmodel: print('Load model from', args.initmodel) chainer.serializers.load_npz(args.initmodel, model) if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() # Make the GPU current #if args.test: #cuda.cudnn_enabled = False model.to_gpu() nowt = datetime.datetime.today() outputdir = os.path.join(args.out, args.arch, 'extract') if args.initmodel is not None: outputdir = os.path.dirname(args.initmodel) if args.indices is None: args.indices = os.path.join(outputdir, 'features', 'top_' + args.layer + '.txt') # Load the datasets and mean file mean = None if hasattr(model, 'mean_value'): mean = makeMeanImage(model.mean_value) else: mean = np.load(args.mean) assert mean is not None if args.indices is None: args.indices = os.path.join(args.out, args.arch, 'extract', 'top_' + args.layer + '.txt') #top_path = os.path.join(args.out, args.arch, 'extract', 'top_' + args.layer + '.txt') #train = ppds.PreprocessedDataset(args.train, args.root, mean, model.insize) val = ppds.PreprocessedDataset(args.val, args.root, mean, model.insize, False, args.indices) # These iterators load the images with subprocesses running in parallel to # the training/validation. #train_iter = chainer.iterators.MultiprocessIterator( # train, args.batchsize, shuffle=False, n_processes=args.loaderjob) #val_iter = chainer.iterators.MultiprocessIterator( # val, args.val_batchsize, repeat=False, shuffle=False, n_processes=args.loaderjob) val_iter = chainer.iterators.SerialIterator(val, args.val_batchsize, repeat=False, shuffle=False) # Set up an optimizer optimizer = chainer.optimizers.MomentumSGD() optimizer.setup(model) # Set up a trainer updater = training.StandardUpdater(val_iter, optimizer, device=args.gpu) trainer = training.Trainer(updater, (1, 'epoch'), outputdir) #val_interval = (10 if args.test else int(len(train) / args.batchsize)), 'iteration' val_interval = (1, 'iteration') #snapshot_interval = (10, 'iteration') if args.test else (2, 'epoch') log_interval = (10, 'iteration') # Copy the chain with shared parameters to flip 'train' flag only in test eval_model = model.copy() eval_model.train = False val_acquirer = utils.DeconvAcquirer(val_iter, eval_model, device=args.gpu) val_acquirer.mean = mean val_acquirer.layer_rank = eval_model.layer_rank[args.layer] val_acquirer.layer_name = args.layer val_acquirer.operation = args.operation val_acquirer.fixed_RMS = args.rms val_acquirer.top = args.top val_acquirer.n_features = val.cols if 'googlenet' in args.arch: val_acquirer.lastname = 'validation/main/loss3' trainer.extend(val_acquirer, trigger=val_interval) #trainer.extend(extensions.dump_graph('main/loss')) #trainer.extend(extensions.snapshot(), trigger=snapshot_interval) #trainer.extend(extensions.snapshot_object( # model, 'model_iter_{.updater.iteration}'), trigger=snapshot_interval) # Be careful to pass the interval directly to LogReport # (it determines when to emit log rather than when to read observations) #trainer.extend(extensions.LogReport(trigger=log_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', ]), trigger=log_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) #trainer.extend(extensions.ExponentialShift('lr', args.gamma), # trigger=(1, 'epoch')) if args.resume: chainer.serializers.load_npz(args.resume, trainer) #if not args.test: # trainer.run() # chainer.serializers.save_npz(outputdir + '/model', model) results = val_acquirer(trainer) results['outputdir'] = outputdir #if eval_model.layer_rank[args.layer] == 1: # save_first_conv_filter(os.path.join(outputdir, args.layer), # model[args.layer].W.data, cols = args.cols, pad = args.pad, # scale = args.scale, gamma = args.gamma) #if args.test: #print(val_acquirer.confmat) #categories = utils.io.load_categories(args.categories) #confmat_csv_name = args.initmodel + '.csv' #confmat_fig_name = args.initmodel + '.eps' #utils.io.save_confmat_csv(confmat_csv_name, val_acquirer.confmat, categories) #utils.io.save_confmat_fig(confmat_fig_name, val_acquirer.confmat, categories, # mode="rate", saveFormat="eps") return results
def main(): args = parse_args() if args.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) config = configparser.ConfigParser() logger.info("read {}".format(args.config_path)) config.read(args.config_path, "UTF-8") logger.info("setup devices") if chainer.backends.cuda.available: devices = setup_devices(config["training_param"]["gpus"]) else: # cpu run devices = {"main": -1} seed = config.getint("training_param", "seed") logger.info("set random seed {}".format(seed)) set_random_seed(devices, seed) result = os.path.expanduser(config["result"]["dir"]) destination = os.path.join(result, "pose") logger.info("> copy code to {}".format(os.path.join(result, "src"))) save_files(result) logger.info("> copy config file to {}".format(destination)) if not os.path.exists(destination): os.makedirs(destination) shutil.copy(args.config_path, os.path.join(destination, "config.ini")) logger.info("{} chainer debug".format("enable" if args.debug else "disable")) chainer.set_debug(args.debug) chainer.global_config.autotune = True chainer.cuda.set_max_workspace_size(11388608) chainer.config.cudnn_fast_batch_normalization = True logger.info("> get dataset") train_set, val_set, hand_param = select_dataset(config, return_data=["train_set", "val_set", "hand_param"]) model = select_model(config, hand_param) logger.info("> transform dataset") train_set = TransformDataset(train_set, model.encode) val_set = TransformDataset(val_set, model.encode) logger.info("> size of train_set is {}".format(len(train_set))) logger.info("> size of val_set is {}".format(len(val_set))) logger.info("> create iterators") batch_size = config.getint("training_param", "batch_size") n_processes = config.getint("training_param", "n_processes") train_iter = chainer.iterators.MultiprocessIterator( train_set, batch_size, n_processes=n_processes ) test_iter = chainer.iterators.MultiprocessIterator( val_set, batch_size, repeat=False, shuffle=False, n_processes=n_processes, ) logger.info("> setup optimizer") optimizer = chainer.optimizers.MomentumSGD() optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.0005)) logger.info("> setup parallel updater devices={}".format(devices)) updater = training.updaters.ParallelUpdater(train_iter, optimizer, devices=devices) logger.info("> setup trainer") trainer = training.Trainer( updater, (config.getint("training_param", "train_iter"), "iteration"), destination, ) logger.info("> setup extensions") trainer.extend( extensions.LinearShift("lr", value_range=(config.getfloat("training_param", "learning_rate"), 0), time_range=(0, config.getint("training_param", "train_iter")) ), trigger=(1, "iteration") ) trainer.extend(extensions.Evaluator(test_iter, model, device=devices["main"])) if extensions.PlotReport.available(): trainer.extend(extensions.PlotReport([ "main/loss", "validation/main/loss", ], "epoch", file_name="loss.png")) trainer.extend(extensions.LogReport()) trainer.extend(extensions.observe_lr()) trainer.extend(extensions.PrintReport([ "epoch", "elapsed_time", "lr", "main/loss", "validation/main/loss", "main/loss_resp", "validation/main/loss_resp", "main/loss_iou", "validation/main/loss_iou", "main/loss_coor", "validation/main/loss_coor", "main/loss_size", "validation/main/loss_size", "main/loss_limb", "validation/main/loss_limb", "main/loss_vect_cos", "validation/main/loss_vect_cos", "main/loss_vect_norm", "validation/main/loss_vect_cos", "main/loss_vect_square", "validation/main/loss_vect_square", ])) trainer.extend(extensions.ProgressBar()) trainer.extend(extensions.snapshot(filename="best_snapshot"), trigger=training.triggers.MinValueTrigger("validation/main/loss")) trainer.extend(extensions.snapshot_object(model, filename="bestmodel.npz"), trigger=training.triggers.MinValueTrigger("validation/main/loss")) logger.info("> start training") trainer.run()