def build_reader(config): """ Build the data reader for this model. """ train_samples, valid_samples = choose_samples(config.data_dir) train_reader = paddle.batch(paddle.reader.shuffle( reader.train_reader(train_samples), buf_size=102400), batch_size=config.batch_size) # testing data is not shuffled test_reader = paddle.batch(reader.train_reader(valid_samples, is_train=False), batch_size=config.batch_size) return train_reader, test_reader
def infer(model_path, config): assert os.path.exists(model_path), "The model does not exist." paddle.init(use_gpu=True, trainer_count=1) ids_2_word = load_reverse_dict("featurized/vocab.txt") conf = load_config(config) predictions = build_model(conf, is_infer=True) # print(parse_network(predictions)) # for debug print # load the trained models parameters = paddle.parameters.Parameters.from_tar( gzip.open(model_path, "r")) inferer = paddle.inference.Inference( output_layer=predictions, parameters=parameters) _, valid_samples = choose_samples(conf.data_dir) test_reader = reader.train_reader(valid_samples, is_train=False) test_batch = [] for i, item in enumerate(test_reader()): test_batch.append(item) if len(test_batch) == conf.batch_size: infer_a_batch(inferer, test_batch, ids_2_word) test_batch = [] if len(test_batch): infer_a_batch(inferer, test_batch, ids_2_word) test_batch = []
def main(args): # construct the sample input, output, data_size = construct_sample(args) # construct the train program train_program = fluid.Program() startup_program = fluid.Program() with fluid.program_guard(train_program, startup_program): seq2seq_model = SeqModel(args.seq_num, args.batch_size, args.hidden_size) ret_dict = seq2seq_model.build_graph() val_program = train_program.clone() with fluid.program_guard(train_program, startup_program): optimizer = fluid.optimizer.Adam(args.lr) optimizer.minimize(ret_dict.loss) places = fluid.cuda_places() if args.use_cuda else fluid.cpu_places() train_loader = fluid.io.DataLoader.from_generator( feed_list=ret_dict.feed_list, capacity=3, iterable=True) train_loader.set_batch_generator(train_reader(input, output, data_size, args.batch_size), places=places) exe = Executor(places[0]) exe.run(startup_program) # train stage:use data_loader as reader for _ in range(args.epoch): for data in train_loader(): results = exe.run(train_program, feed=data, fetch_list=ret_dict.fetch_list) print("train process loss:{}".format(results[0])) # save the model for inferenceing with fluid.program_guard(train_program, startup_program): fluid.io.save_inference_model(dirname="./model", feeded_var_names=['feat', 'lod'], \ target_vars=[ret_dict.last_predict], executor=exe, export_for_deployment=True) # val stage: use data_loader as reader val_loader = fluid.io.DataLoader.from_generator( feed_list=ret_dict.feed_list, capacity=3, iterable=True) val_loader.set_batch_generator(val_reader(input, output, data_size, output.shape[0]), places=places) for _ in range(1): for data in train_loader: results = exe.run(train_program, feed=data, fetch_list=ret_dict.fetch_list) print("val process loss:{}".format(results[0]))
model = cnn_model.cnn_model(image) cost = fluid.layers.square_error_cost(input=model, label=label) avg_cost = fluid.layers.mean(cost) # 获取训练和测试程序 test_program = fluid.default_main_program().clone(for_test=True) # 定义优化方法 optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.001) opts = optimizer.minimize(avg_cost) # 获取自定义数据 train_reader = paddle.batch(reader=reader.train_reader(train_list, crop_size, resize_size), batch_size=32) test_reader = paddle.batch(reader=reader.test_reader(test_list, crop_size), batch_size=32) # 定义执行器 place = fluid.CUDAPlace(0) # place = fluid.CPUPlace() exe = fluid.Executor(place) # 进行参数初始化 exe.run(fluid.default_startup_program()) # 定义输入数据维度 feeder = fluid.DataFeeder(place=place, feed_list=[image, label]) # 训练 all_test_cost = []
# 获取损失函数和准确率函数 cost = fluid.layers.cross_entropy(input=model, label=label) avg_cost = fluid.layers.mean(cost) acc = fluid.layers.accuracy(input=model, label=label) # 获取训练和测试程序 test_program = fluid.default_main_program().clone(for_test=True) # 定义优化方法 optimizer = fluid.optimizer.AdamOptimizer(learning_rate=1e-3, regularization=fluid.regularizer.L2Decay( regularization_coeff=0.001)) opts = optimizer.minimize(avg_cost) # 获取自定义数据 train_reader = reader.train_reader('dataset/train', batch_size=32) test_reader = reader.test_reader('dataset/test', batch_size=32) # 定义一个使用GPU的执行器 place = fluid.CUDAPlace(0) exe = fluid.Executor(place) # 进行参数初始化 exe.run(fluid.default_startup_program()) # 加载初始化模型 if init_model: fluid.load(program=fluid.default_main_program(), model_path=init_model, executor=exe, var_list=fluid.io.get_program_parameter(fluid.default_main_program())) print("Init model from: %s." % init_model)
def train(topology, train_data_dir=None, test_data_dir=None, word_dict_path=None, label_dict_path=None, model_save_dir="models", batch_size=32, num_passes=10): if not os.path.exists(model_save_dir): os.mkdir(model_save_dir) use_default_data = (train_data_dir is None) if use_default_data: logger.info(("No training data are provided, " "use paddle.dataset.imdb to train the model.")) logger.info("please wait to build the word dictionary ...") word_dict = paddle.dataset.imdb.word_dict() train_reader = paddle.batch(paddle.reader.shuffle( lambda: paddle.dataset.imdb.train(word_dict)(), buf_size=51200), batch_size=100) test_reader = paddle.batch(lambda: paddle.dataset.imdb.test(word_dict) (), batch_size=100) class_num = 2 else: if word_dict_path is None or not os.path.exists(word_dict_path): logger.info(("word dictionary is not given, the dictionary " "is automatically built from the training data.")) build_dict(data_dir=train_data_dir, save_path=word_dict_path, use_col=1, cutoff_fre=5, insert_extra_words=["<UNK>"]) if not os.path.exists(label_dict_path): logger.info(("label dictionary is not given, the dictionary " "is automatically built from the training data.")) # build the label dictionary to map the original string-typed # label into integer-typed index build_dict(data_dir=train_data_dir, save_path=label_dict_path, use_col=0) word_dict = load_dict(word_dict_path) lbl_dict = load_dict(label_dict_path) class_num = len(lbl_dict) logger.info("class number is : %d." % (len(lbl_dict))) train_reader = paddle.batch(paddle.reader.shuffle(reader.train_reader( train_data_dir, word_dict, lbl_dict), buf_size=51200), batch_size=batch_size) if test_data_dir is not None: # here, because training and testing data share a same format, # we still use the reader.train_reader to read the testing data. test_reader = paddle.batch(reader.train_reader( test_data_dir, word_dict, lbl_dict), batch_size=batch_size) else: test_reader = None dict_dim = len(word_dict) logger.info("length of word dictionary is : %d." % (dict_dim)) paddle.init(use_gpu=False, trainer_count=1) # network config cost, prob, label = topology(dict_dim, class_num) # create parameters parameters = paddle.parameters.create(cost) # create optimizer adam_optimizer = paddle.optimizer.Adam( learning_rate=1e-3, regularization=paddle.optimizer.L2Regularization(rate=1e-3), model_average=paddle.optimizer.ModelAverage(average_window=0.5)) # create trainer trainer = paddle.trainer.SGD(cost=cost, extra_layers=paddle.evaluator.auc( input=prob, label=label), parameters=parameters, update_equation=adam_optimizer) # begin training network feeding = {"word": 0, "label": 1} def _event_handler(event): """ Define end batch and end pass event handler """ if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: logger.info( "Pass %d, Batch %d, Cost %f, %s\n" % (event.pass_id, event.batch_id, event.cost, event.metrics)) if isinstance(event, paddle.event.EndPass): if test_reader is not None: result = trainer.test(reader=test_reader, feeding=feeding) logger.info("Test at Pass %d, %s \n" % (event.pass_id, result.metrics)) with gzip.open( os.path.join(model_save_dir, "cnn_params_pass_%05d.tar.gz" % event.pass_id), "w") as f: trainer.save_parameter_to_tar(f) trainer.train(reader=train_reader, event_handler=_event_handler, feeding=feeding, num_passes=num_passes) logger.info("Training has finished.")
# print the result for compare print("%s\ns_POS = %s\np_POS = %s" % ("-" * 40, source_POS, prediction_POS)) if __name__ == "__main__": #define the test_data_dir, word_dict_path, label_dict_path train_data_dir, test_data_dir, word_dict_path, label_dict_path = load_default_data( ) logger.info( "train_data_dir = %s\ntest_data_dir = %s\nword_dict_path = %s\nlabel_dict_path = %s\n" % (train_data_dir, test_data_dir, word_dict_path, label_dict_path)) ##get dictionary logger.info("loading dictionary") word_dict = load_dict(word_dict_path) lbl_dict = load_dict(label_dict_path) ## we use the reader.train_reader to read the testing data. logger.info("loading test reader") test_reader = paddle.batch( reader.train_reader(test_data_dir, word_dict, lbl_dict), batch_size=32) ##running model infer ... logger.info("running model infer ...") epoch_path = "./models/window_epoch3" infer(test_reader, window_size=5, use_cuda=False, model_path=epoch_path)
def train(train_data_dir, test_data_dir, word_dict_path, label_dict_path, model_save_dir): """ :params train_data_path: The path of training data, if this parameter is not specified, imdb dataset will be used to run this example :type train_data_path: str :params test_data_path: The path of testing data, if this parameter is not specified, imdb dataset will be used to run this example :type test_data_path: str :params word_dict_path: The path of word dictionary, if this parameter is not specified, imdb dataset will be used to run this example :type word_dict_path: str :params label_dict_path: The path of label dictionary, if this parameter is not specified, imdb dataset will be used to run this example :type label_dict_path: str :params model_save_dir: dir where models saved :type model_save_dir: str """ if train_data_dir is not None: assert word_dict_path and label_dict_path, ( "The parameter train_data_dir, word_dict_path, label_dict_path " "should be set at the same time.") if not os.path.exists(model_save_dir): os.mkdir(model_save_dir) use_default_data = (train_data_dir is None) if use_default_data: logger.info(("No training data are porivided, " "use imdb to train the model.")) logger.info("Please wait to build the word dictionary ...") word_dict = reader.imdb_word_dict() train_reader = paddle.batch(paddle.reader.shuffle( lambda: reader.imdb_train(word_dict), buf_size=1000), batch_size=100) test_reader = paddle.batch(lambda: reader.imdb_test(word_dict), batch_size=100) class_num = 2 else: if word_dict_path is None or not os.path.exists(word_dict_path): logger.info(("Word dictionary is not given, the dictionary " "is automatically built from the training data.")) # build the word dictionary to map the original string-typed # words into integer-typed index build_word_dict(data_dir=train_data_dir, save_path=word_dict_path, use_col=1, cutoff_fre=0) if not os.path.exists(label_dict_path): logger.info(("Label dictionary is not given, the dictionary " "is automatically built from the training data.")) # build the label dictionary to map the original string-typed # label into integer-typed index build_label_dict(data_dir=train_data_dir, save_path=label_dict_path, use_col=0) word_dict = load_dict(word_dict_path) label_dict = load_dict(label_dict_path) class_num = len(label_dict) logger.info("Class number is : %d." % class_num) train_reader = paddle.batch(paddle.reader.shuffle( reader.train_reader(train_data_dir, word_dict, label_dict), buf_size=conf.buf_size), batch_size=conf.batch_size) if test_data_dir is not None: # here, because training and testing data share a same format, # we still use the reader.train_reader to read the testing data. test_reader = paddle.batch(paddle.reader.shuffle( reader.train_reader(test_data_dir, word_dict, label_dict), buf_size=conf.buf_size), batch_size=conf.batch_size) else: test_reader = None dict_dim = len(word_dict) logger.info("Length of word dictionary is : %d." % (dict_dim)) paddle.init(use_gpu=conf.use_gpu, trainer_count=conf.trainer_count) # create optimizer adam_optimizer = paddle.optimizer.Adam( learning_rate=conf.learning_rate, regularization=paddle.optimizer.L2Regularization( rate=conf.l2_learning_rate), model_average=paddle.optimizer.ModelAverage( average_window=conf.average_window)) # define network topology. cost, prob, label = nested_net(dict_dim, class_num, is_infer=False) # create all the trainable parameters. parameters = paddle.parameters.create(cost) # create the trainer instance. trainer = paddle.trainer.SGD(cost=cost, extra_layers=paddle.evaluator.auc( input=prob, label=label), parameters=parameters, update_equation=adam_optimizer) # feeding dictionary feeding = {"word": 0, "label": 1} def _event_handler(event): """ Define the end batch and the end pass event handler. """ if isinstance(event, paddle.event.EndIteration): if event.batch_id % conf.log_period == 0: logger.info( "Pass %d, Batch %d, Cost %f, %s\n" % (event.pass_id, event.batch_id, event.cost, event.metrics)) if isinstance(event, paddle.event.EndPass): if test_reader is not None: result = trainer.test(reader=test_reader, feeding=feeding) logger.info("Test at Pass %d, %s \n" % (event.pass_id, result.metrics)) with gzip.open( os.path.join(model_save_dir, "params_pass_%05d.tar.gz" % event.pass_id), "w") as f: trainer.save_parameter_to_tar(f) # begin training network trainer.train(reader=train_reader, event_handler=_event_handler, feeding=feeding, num_passes=conf.num_passes) logger.info("Training has finished.")
# 获取损失函数和准确率函数 cost = fluid.layers.cross_entropy(input=model, label=label) avg_cost = fluid.layers.mean(cost) acc = fluid.layers.accuracy(input=model, label=label) # 获取训练和测试程序 test_program = fluid.default_main_program().clone(for_test=True) # 定义优化方法 l2 = fluid.regularizer.L2DecayRegularizer(1e-4) optimizer = fluid.optimizer.AdamOptimizer(learning_rate=1e-3, regularization=l2) opts = optimizer.minimize(avg_cost) # 获取自定义数据 train_reader = paddle.batch(reader=reader.train_reader('images/train.list', crop_size, resize_size), batch_size=32) test_reader = paddle.batch(reader=reader.test_reader('images/test.list', crop_size), batch_size=32) # 定义一个使用GPU的执行器 place = fluid.CUDAPlace(0) # place = fluid.CPUPlace() exe = fluid.Executor(place) # 进行参数初始化 exe.run(fluid.default_startup_program()) # 定义输入数据维度 feeder = fluid.DataFeeder(place=place, feed_list=[image, label])
def main(): # parse the argument parser = argparse.ArgumentParser() parser.add_argument('-m', '--model', help='The model for image classification', choices=[ 'alexnet', 'vgg13', 'vgg16', 'vgg19', 'resnet', 'googlenet', 'inception-resnet-v2', 'inception_v4', 'xception' ]) parser.add_argument( '-r', '--retrain_file', type=str, default='', help="The model file to retrain, none is for train from scratch") args = parser.parse_args() # PaddlePaddle init paddle.init(use_gpu=True, trainer_count=1) image = paddle.layer.data(name="image", type=paddle.data_type.dense_vector(DATA_DIM)) lbl = paddle.layer.data(name="label", type=paddle.data_type.integer_value(CLASS_DIM)) extra_layers = None learning_rate = 0.0001 if args.model == 'alexnet': out = alexnet.alexnet(image, class_dim=CLASS_DIM) elif args.model == 'vgg13': out = vgg.vgg13(image, class_dim=CLASS_DIM) elif args.model == 'vgg16': out = vgg.vgg16(image, class_dim=CLASS_DIM) elif args.model == 'vgg19': out = vgg.vgg19(image, class_dim=CLASS_DIM) elif args.model == 'resnet': conv, pool, out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM) learning_rate = 0.1 elif args.model == 'googlenet': out, out1, out2 = googlenet.googlenet(image, class_dim=CLASS_DIM) loss1 = paddle.layer.cross_entropy_cost(input=out1, label=lbl, coeff=0.3) paddle.evaluator.classification_error(input=out1, label=lbl) loss2 = paddle.layer.cross_entropy_cost(input=out2, label=lbl, coeff=0.3) paddle.evaluator.classification_error(input=out2, label=lbl) extra_layers = [loss1, loss2] elif args.model == 'inception-resnet-v2': assert DATA_DIM == 3 * 331 * 331 or DATA_DIM == 3 * 299 * 299 out = inception_resnet_v2.inception_resnet_v2(image, class_dim=CLASS_DIM, dropout_rate=0.5, data_dim=DATA_DIM) elif args.model == 'inception_v4': conv, pool, out = inception_v4.inception_v4(image, class_dim=CLASS_DIM) elif args.model == 'xception': out = xception.xception(image, class_dim=CLASS_DIM) cost = paddle.layer.classification_cost(input=out, label=lbl) # Create parameters parameters = paddle.parameters.create(cost) for k, v in parameters.__param_conf__.items(): print(" config key {0}\t\t\tval{1}".format(k, v)) print("-" * 50) #print(parameters.__param_conf__[0]) if args.retrain_file is not None and '' != args.retrain_file: print("restore parameters from {0}".format(args.retrain_file)) exclude_params = [ param for param in parameters.names() if param.startswith('___fc_layer_0__') ] parameters.init_from_tar(gzip.open(args.retrain_file), exclude_params) # Create optimizer optimizer = paddle.optimizer.Momentum( momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * BATCH_SIZE), learning_rate=learning_rate / BATCH_SIZE, learning_rate_decay_a=0.1, learning_rate_decay_b=128000 * 35, learning_rate_schedule="discexp", ) train_reader = paddle.batch( paddle.reader.shuffle( # flowers.train(), # To use other data, replace the above line with: reader.train_reader('valid_train0.lst'), buf_size=2048), batch_size=BATCH_SIZE) test_reader = paddle.batch( # flowers.valid(), # To use other data, replace the above line with: reader.test_reader('valid_val.lst'), batch_size=BATCH_SIZE) # Create trainer trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer, extra_layers=extra_layers) # End batch and end pass event handler def event_handler(event): global step global start if isinstance(event, paddle.event.EndIteration): if event.batch_id % 10 == 0: print "\nPass %d, Batch %d, Cost %f, %s, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics, time.time() - start) start = time.time() loss_scalar.add_record(step, event.cost) acc_scalar.add_record( step, 1 - event.metrics['classification_error_evaluator']) start = time.time() step += 1 if event.batch_id % 100 == 0: with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: trainer.save_parameter_to_tar(f) if isinstance(event, paddle.event.EndPass): with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: trainer.save_parameter_to_tar(f) result = trainer.test(reader=test_reader) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) trainer.train(reader=train_reader, num_passes=200, event_handler=event_handler)
if __name__ == "__main__": ## get default file ... logger.info("running model infer ...") img2sent_dict_path, word_dict_path = get_default_dict_path() train_features_path, test_features_path = get_default_img_feat_path() print(img2sent_dict_path, word_dict_path) print(train_features_path, test_features_path) exit() logger.info( "test_features_path = %s\nword_dict_path = %s\nimg2sent_dict_path = %s\n" % (test_features_path, word_dict_path, img2sent_dict_path)) ##get dictionary logger.info("loading dictionary") word_dict = load_pkl(word_dict_path) img2sent_dict = load_pkl(img2sent_dict_path) ## we use the reader.train_reader to read the testing data. logger.info("loading test reader") test_reader = reader.train_reader(test_features_path, img2sent_dict, word_dict) ## running model infer ... logger.info("running model infer ...") epoch_path = "./models/img2sentence_epoch0" infer(test_reader, use_cuda=False, model_path=epoch_path)
def train(topology, train_data_dir=None, test_data_dir=None, word_dict_path=None, label_dict_path=None, model_save_dir="models", use_cuda=False, window_size=5, learning_rate=0.001, batch_size=64, num_passes=10): """ train window_net model or sentence_net model :params train_data_path: path of training data, if this parameter is not specified, Brown Corpus will be used to run this example :type train_data_path: str :params test_data_path: path of testing data, if this parameter is not specified, Brown Corpus will be used to run this example :type test_data_path: str :params word_dict_path: path of word dictionary data, if this parameter is not specified, a default dictionary file will be used to run this example :type word_dict_path: str :params label_dict_path: path of label dictionary data, if this parameter is not specified, a default dictionary file will be used to run this example :type label_dict_path: str :params use_cuda: whether use the cuda :type use_cuda: bool :params window_size: size of window width :type window_size: int :params num_pass: train pass number :type num_pass: int """ if not os.path.exists(model_save_dir): os.mkdir(model_save_dir) use_default_data = (train_data_dir is None) if use_default_data: logger.info(("No training data are provided, " "use Brown corpus to train the model.")) logger.info("downloading Brown corpus...") train_data_dir, test_data_dir, word_dict_path, label_dict_path = load_default_data( ) logger.info("please wait to build the word dictionary ...") if word_dict_path is None or not os.path.exists(word_dict_path): logger.info(("word dictionary is not given, the dictionary " "is automatically built from the training data.")) # build the word dictionary to map the original string-typed # words into integer-typed index build_dict(data_dir=train_data_dir, save_path=word_dict_path, use_col=0, cutoff_fre=1, insert_extra_words=["<UNK>"]) logger.info("the word dictionary path is %s" % word_dict_path) if not os.path.exists(label_dict_path): logger.info(("label dictionary is not given, the dictionary " "is automatically built from the training data.")) # build the label dictionary to map the original string-typed # label into integer-typed index build_dict(data_dir=train_data_dir, save_path=label_dict_path, use_col=1, cutoff_fre=10, insert_extra_words=["<UNK>"]) logger.info("the label dictionary path is %s" % label_dict_path) # get index info word_dict = load_dict(word_dict_path) lbl_dict = load_dict(label_dict_path) class_num = len(lbl_dict) logger.info("class number is : %d." % (len(lbl_dict))) # get train data reader train_reader = paddle.batch(paddle.reader.shuffle(reader.train_reader( train_data_dir, word_dict, lbl_dict, window_size), buf_size=51200), batch_size=batch_size) # get test data reader if test_data_dir is not None: # here, because training and testing data share a same format, # we still use the reader.train_reader to read the testing data. test_reader = paddle.batch(reader.train_reader(test_data_dir, word_dict, lbl_dict, window_size), batch_size=batch_size) else: test_reader = None # get size of word dictionary dict_dim = len(word_dict) + 1 logger.info("length of word dictionary is : %d." % (dict_dim)) # define the input layers data = fluid.layers.data(name="words", shape=[1], dtype="int64", lod_level=1) label = fluid.layers.data(name="label", shape=[1], dtype="int64") # return the network result cost, acc, prediction = topology(data, label, dict_dim, class_num=class_num) # create optimizer sgd_optimizer = fluid.optimizer.Adam(learning_rate=learning_rate) sgd_optimizer.minimize(cost) # create trainer place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() exe = fluid.Executor(place) feeder = fluid.DataFeeder(feed_list=[data, label], place=place) # initialize training network exe.run(fluid.default_startup_program()) prog = fluid.default_main_program() # begin training network for pass_id in range(num_passes): ## running the train data data_size, data_count, total_acc, total_cost = 0, 0, 0.0, 0.0 for i, data_ in enumerate(train_reader()): avg_cost_np, avg_acc_np = exe.run(prog, feed=feeder.feed(data_), fetch_list=[cost, acc]) data_size = len(data_) total_acc += data_size * avg_acc_np total_cost += data_size * avg_cost_np data_count += data_size if (i + 1) % 1000 == 0: logger.info( "pass_id: %d, batch %d, avg_acc: %f, avg_cost: %f" % (pass_id, i + 1, total_acc / data_count, total_cost / data_count)) avg_cost = total_cost / data_count avg_acc = total_acc / data_count logger.info("Train result -- pass_id: %d, avg_acc: %f, avg_cost: %f" % (pass_id, avg_acc, avg_cost)) ## running the test data if test_reader is not None: data_size, data_count, total_acc, total_cost = 0, 0, 0.0, 0.0 for i, data in enumerate(test_reader()): avg_cost_np, avg_acc_np, prediction_np = exe.run( prog, feed=feeder.feed(data), fetch_list=[cost, acc, prediction]) data_size = len(data) total_acc += data_size * avg_acc_np total_cost += data_size * avg_cost_np data_count += data_size avg_cost = total_cost / data_count avg_acc = total_acc / data_count logger.info( "Test result -- pass_id: %d, avg_acc: %f, avg_cost: %f" % (pass_id, avg_acc, avg_cost)) ## save inference model epoch_model = model_save_dir + "/" + args.nn_type + "_epoch" + str( pass_id % 5) logger.info("Saving inference model at %s" % (epoch_model)) ##prediction is the topology return value ##if we use the prediction value as the infer result fluid.io.save_inference_model(epoch_model, ["words"], prediction, exe) logger.info("Training has finished.")
# Obtain network's softmx layer out = squeezenet(image, CLASS_DIM, True) # Classification cost cost = paddle.layer.classification_cost(input=out, label=lbl) # Create parameters # parameters = paddle.parameters.create(cost) with gzip.open('/book/working/models/params_pass_47.tar.gz', 'r') as f: parameters = paddle.parameters.Parameters.from_tar(f) # Read training data train_reader = paddle.batch( paddle.reader.shuffle( reader.train_reader('/book/working/data/train.list', buffered_size=1024), buf_size=20000), batch_size=BATCH_SIZE) # Read testing data test_reader = paddle.batch( reader.test_reader('/book/working/data/val.list', buffered_size=1024), batch_size=BATCH_SIZE) # End batch and end pass event handler def event_handler(event): # Report result of batch. if isinstance(event, paddle.event.EndIteration): print "\nPass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) # Report result of pass. if isinstance(event, paddle.event.EndPass):
cost = fluid.layers.cross_entropy(input=model, label=label) avg_cost = fluid.layers.mean(cost) acc = fluid.layers.accuracy(input=model, label=label) # 获取训练和测试程序 test_program = fluid.default_main_program().clone(for_test=True) # 定义优化方法 l2 = fluid.regularizer.L2DecayRegularizer(1e-4) optimizer = fluid.optimizer.AdamOptimizer(learning_rate=1e-3, regularization=l2) opts = optimizer.minimize(avg_cost) # 获取自定义数据 train_reader = paddle.batch( reader=reader.train_reader('images/train.list', crop_size, resize_size), batch_size=32) test_reader = paddle.batch( reader=reader.test_reader('images/test.list', crop_size), batch_size=32) # 定义一个使用GPU的执行器 place = fluid.CUDAPlace(0) # place = fluid.CPUPlace() exe = fluid.Executor(place) # 进行参数初始化 exe.run(fluid.default_startup_program()) # 定义输入数据维度 feeder = fluid.DataFeeder(place=place, feed_list=[image, label]) # 训练100次
def train(num_passes, batch_size, use_gpu, trainer_count, save_dir_path, encoder_depth, decoder_depth, train_data_path, train_label_path, vocab_path, init_model_path=""): if not os.path.exists(save_dir_path): os.makedirs(save_dir_path) assert os.path.exists(vocab_path), "The vocabs txt file needed!" assert os.path.exists(train_data_path), "The train data file needed!" assert os.path.exists(train_label_path), "The train label file needed!" # initialize PaddlePaddle paddle.init(use_gpu=use_gpu, trainer_count=trainer_count) # define optimizer optimizer = paddle.optimizer.Adam( learning_rate=1e-4, regularization=paddle.optimizer.L2Regularization(rate=1e-5), model_average=paddle.optimizer.ModelAverage( average_window=0.5, max_average_window=2500)) # define topology cost = encoder_decoder_network( word_count=len(open(vocab_path, "r").readlines()), emb_dim=512, encoder_depth=encoder_depth, encoder_hidden_dim=512, decoder_depth=decoder_depth, decoder_hidden_dim=512, bos_id=0, eos_id=1, max_length=50) # create parameters parameters = paddle.parameters.create(cost) # load initial model if init_model_path: load_initial_model(init_model_path, parameters) # define trainer trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer) # define data reader train_reader = paddle.batch( paddle.reader.shuffle( reader.train_reader(train_data_path, train_label_path, vocab_path), buf_size=10240), batch_size=batch_size) # define the event_handler callback def event_handler(event): if isinstance(event, paddle.event.EndIteration): if (not event.batch_id % 10000) and event.batch_id: save_path = os.path.join(save_dir_path, "pass_%05d_batch_%05d.tar.gz" % (event.pass_id, event.batch_id)) save_model(trainer, save_path, parameters) if not event.batch_id % 10: logger.info("Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics)) if isinstance(event, paddle.event.EndPass): save_path = os.path.join(save_dir_path, "pass_%05d.tar.gz" % event.pass_id) save_model(trainer, save_path, parameters) # start training trainer.train( reader=train_reader, event_handler=event_handler, num_passes=num_passes)
def train(train_features_path=None, test_features_path=None, word_dict_path=None, img2sent_dict_path=None, model_save_dir="models", use_cuda=False, learning_rate=0.001, num_passes=10): """ train window_net model or sentence_net model :params train_features_path: path of training data, if this parameter is not specified, flickr30k-images will be used to run this example :type train_features_path: str :params test_features_path: path of testing data, if this parameter is not specified, flickr30k-images will be used to run this example :type test_features_path: str :params word_dict_path: path of word dictionary data, if this parameter is not specified, a default dictionary file will be used to run this example :type word_dict_path: str :params use_cuda: whether use the cuda :type use_cuda: bool :params num_pass: train pass number :type num_pass: int """ if not os.path.exists(model_save_dir): os.mkdir(model_save_dir) use_default_data = (train_features_path is None) if use_default_data: logger.info(("No training data are provided, " "use flickr30k-images to train the model.")) logger.info("downloading flickr30k-images ...") default_data_train_dir, default_data_test_dir, tar_token_filename = load_default_data( ) logger.info("define default path ...") img2sent_dict_path, word_dict_path = get_default_dict_path() train_features_path, test_features_path = get_default_img_feat_path() logger.info("please wait to build the word dictionary ...") if word_dict_path is None or not os.path.exists(word_dict_path): logger.info(("word dictionary is not given, the dictionary " "is automatically built from the training data.")) # build the word dictionary to map the original string-typed # words into integer-typed index build_dict(tar_token_filename, img2sent_dict_path, word_dict_path, minCount=5) logger.info("the word dictionary path is %s" % word_dict_path) # get index info img2sent_dict = load_pkl(img2sent_dict_path) word_dict = load_pkl(word_dict_path) word_num = len(word_dict) logger.info("word number is : %d." % (word_num)) # get train data reader train_reader = paddle.reader.shuffle(reader.train_reader( train_features_path, img2sent_dict, word_dict), buf_size=5120) # get test data reader if train_features_path is not None: # here, because training and testing data share a same format, # we still use the reader.train_reader to read the testing data. test_reader = reader.train_reader(test_features_path, img2sent_dict, word_dict) else: test_reader = None # get size of word dictionary dict_dim = len(word_dict) + 1 logger.info("length of word dictionary is : %d." % (dict_dim)) # define the image2lstm interface input layers hidden = fluid.layers.data(name="hidden", shape=[4096], dtype="float32") cell = fluid.layers.data(name="cell", shape=[4096], dtype="float32") pre_word = fluid.layers.data(name="pre_words", shape=[1], dtype="int64") label = fluid.layers.data(name="words", shape=[1], dtype="int64") # return the network result cost, acc, prediction, prev_hidden, prev_cell = lstm_main( word_dict_dim=dict_dim, prev_hidden=hidden, prev_cell=cell, lstm_him_dim=128, emb_dim=128, pre_word=pre_word, word=label) # create optimizer sgd_optimizer = fluid.optimizer.Adam(learning_rate=learning_rate) sgd_optimizer.minimize(cost) # create trainer place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() exe = fluid.Executor(place) feeder = fluid.DataFeeder(feed_list=[label, pre_word, hidden, cell], place=place) # initialize training network exe.run(fluid.default_startup_program()) prog = fluid.default_main_program() # begin training network for pass_id in range(num_passes): ## running the train data data_size, data_count, total_acc, total_cost = 0, 0, 0.0, 0.0 for i, data_ in enumerate(train_reader()): img_feat, word_list = data_ prev_hidden_, prev_cell_ = img_feat, img_feat for ii, word in enumerate(word_list): if ii == 0: pre_words = word data_lstm = [[word, pre_words, prev_hidden_, prev_cell_]] avg_cost_np, avg_acc_np, prev_hidden_, prev_cell_ = exe.run( prog, feed=feeder.feed(data_lstm), fetch_list=[cost, acc, prev_hidden, prev_cell]) else: pre_words = word_list[ii - 1] data_lstm = [[word, pre_words, prev_hidden_, prev_cell_]] avg_cost_np, avg_acc_np, prev_hidden_, prev_cell_ = exe.run( prog, feed=feeder.feed(data_lstm), fetch_list=[cost, acc, prev_hidden, prev_cell]) data_size = len(word_list) total_acc += data_size * avg_acc_np total_cost += data_size * avg_cost_np data_count += data_size if (i + 5) % 10 == 0: logger.info( "pass_id: %d, batch %d, avg_acc: %f, avg_cost: %f" % (pass_id, i + 1, total_acc / data_count, total_cost / data_count)) avg_cost = total_cost / data_count avg_acc = total_acc / data_count logger.info("Train result -- pass_id: %d, avg_acc: %f, avg_cost: %f" % (pass_id, avg_acc, avg_cost)) ## running the test data if test_reader is not None: data_size, data_count, total_acc, total_cost = 0, 0, 0.0, 0.0 for i, data_ in enumerate(train_reader()): img_feat, word_list = data_ prev_hidden_, prev_cell_ = img_feat, img_feat for ii, word in enumerate(word_list): if ii == 0: pre_words = word data_lstm = [[ word, pre_words, prev_hidden_, prev_cell_ ]] avg_cost_np, avg_acc_np, prev_hidden_, prev_cell_ = exe.run( prog, feed=feeder.feed(data_lstm), fetch_list=[cost, acc, prev_hidden, prev_cell]) else: pre_words = word_list[ii - 1] data_lstm = [[ word, pre_words, prev_hidden_, prev_cell_ ]] avg_cost_np, avg_acc_np, prev_hidden_, prev_cell_ = exe.run( prog, feed=feeder.feed(data_lstm), fetch_list=[cost, acc, prev_hidden, prev_cell]) data_size = len(word_list) total_acc += data_size * avg_acc_np total_cost += data_size * avg_cost_np data_count += data_size print("test ..") avg_cost = total_cost / data_count avg_acc = total_acc / data_count logger.info( "Test result -- pass_id: %d, avg_acc: %f, avg_cost: %f" % (pass_id, avg_acc, avg_cost)) ## save inference model epoch_model = model_save_dir + "/" + "img2sentence_epoch" + str( pass_id % 5) logger.info("Saving inference model at %s" % (epoch_model)) ##prediction is the topology return value ##if we use the prediction value as the infer result fluid.io.save_inference_model(epoch_model, ["hidden", "cell", "pre_words"], [prediction, prev_hidden, prev_cell], exe) logger.info("Training has finished.")