def gen_predict_graph(predictor): """ call it at last , build predict graph the probelm here is you can not change like beam size later... """ #-----discriminant and generative score = predictor.init_predict() tf.add_to_collection('score', score) if algos_factory.is_discriminant(FLAGS.algo): tf.add_to_collection('textsim_score', predictor.textsim_score) #-----generateive if algos_factory.is_generative(FLAGS.algo): exact_score = predictor.init_predict(exact_loss=True) tf.add_to_collection('exact_score', exact_score) init_predict_text = functools.partial(predictor.init_predict_text, beam_size=FLAGS.beam_size, convert_unk=False) text, text_score = init_predict_text( decode_method=FLAGS.seq_decode_method) beam_text, beam_text_score = init_predict_text( decode_method=SeqDecodeMethod.beam) tf.add_to_collection('text', text) tf.add_to_collection('text_score', text_score) tf.add_to_collection('beam_text', beam_text) tf.add_to_collection('beam_text_score', beam_text_score)
def gen_predict_graph(predictor): """ call it at last , build predict graph the probelm here is you can not change like beam size later... """ #-----discriminant and generative predictor.init_predict() #here self add all score ops #-----generateive if algos_factory.is_generative(FLAGS.algo): exact_score = predictor.init_predict(exact_loss=True) tf.add_to_collection('exact_score', exact_score) exact_prob = predictor.init_predict(exact_prob=True) tf.add_to_collection('exact_prob', exact_prob) ##TODO # beam_size = tf.placeholder_with_default(FLAGS.beam_size, shape=None) # tf.add_to_collection('beam_size_feed', beam_size) init_predict_text = functools.partial(predictor.init_predict_text, beam_size=FLAGS.beam_size, convert_unk=False) text, text_score = init_predict_text( decode_method=FLAGS.seq_decode_method) #greedy init_predict_text( decode_method=SeqDecodeMethod.outgraph_beam) #outgraph beam_text, beam_text_score = init_predict_text( decode_method=SeqDecodeMethod.ingraph_beam) #ingraph must at last tf.add_to_collection('text', text) tf.add_to_collection('text_score', text_score) tf.add_to_collection('beam_text', beam_text) tf.add_to_collection('beam_text_score', beam_text_score)
def gen_validate(input_app, input_results, trainer, predictor): gen_eval_feed_dict = None eval_ops = None train_with_validation = input_results[ input_app.input_valid_name] is not None deal_eval_results = None if train_with_validation and not FLAGS.train_only: eval_image_name, eval_image_feature, eval_text, eval_text_str = input_results[ input_app.input_valid_name] if input_results[input_app.input_valid_neg_name]: eval_neg_image_name, eval_neg_image_feature, eval_neg_text, eval_neg_text_str = input_results[ input_app.input_valid_neg_name] if not FLAGS.neg_left: eval_neg_image = None eval_neg_text_ = eval_neg_text if not FLAGS.neg_right: eval_neg_text_ = None if algos_factory.is_generative(FLAGS.algo): eval_neg_image_feature = None eval_neg_text_ = None eval_loss = trainer.build_train_graph(eval_image_feature, eval_text, eval_neg_image_feature, eval_neg_text_) eval_scores = tf.get_collection('scores')[-1] eval_ops = [eval_loss] if algos_factory.is_generative(FLAGS.algo): eval_neg_text = None eval_neg_text_str = None if FLAGS.show_eval and (predictor is not None): eval_ops, deal_eval_results = \ gen_evalulate( input_app, input_results, predictor, eval_ops, eval_scores, eval_neg_text, eval_neg_text_str) else: deal_eval_results = lambda x: melt.print_results( x, ['eval_batch_loss']) return eval_ops, gen_eval_feed_dict, deal_eval_results
def gen_predict_graph(predictor): """ call it at last , build predict graph the probelm here is you can not change like beam size later... """ #-----discriminant and generative predictor.init_predict() #here self add all score ops #-----generateive if algos_factory.is_generative(FLAGS.algo): exact_score = predictor.init_predict(exact_loss=True) tf.add_to_collection('exact_score', exact_score) exact_prob = predictor.init_predict(exact_prob=True) tf.add_to_collection('exact_prob', exact_prob) ##TODO # beam_size = tf.placeholder_with_default(FLAGS.beam_size, shape=None) # tf.add_to_collection('beam_size_feed', beam_size) init_predict_text = predictor.init_predict_text text, text_score = init_predict_text( decode_method=FLAGS.seq_decode_method) #greedy tf.add_to_collection('text', text) tf.add_to_collection('text_score', text_score) try: init_predict_text(decode_method=SeqDecodeMethod.outgraph_beam, beam_size=FLAGS.beam_size) #outgraph except Exception: print(traceback.format_exc(), file=sys.stderr) print('warning: outgraph beam search not supported', file=sys.stderr) pass full_text, full_text_score = init_predict_text( decode_method=SeqDecodeMethod.ingraph_beam, beam_size=100) tf.add_to_collection('full_text', full_text) tf.add_to_collection('full_text_score', full_text_score) #if not FLAGS.reinforcement_learning: beam_text, beam_text_score = init_predict_text( decode_method=SeqDecodeMethod.ingraph_beam, beam_size=FLAGS.beam_size, logprobs_history=True, alignment_history=True) #ingraph must at last #else: # beam_text, beam_text_score = tf.expand_dims(text, 1), tf.expand_dims(text_score, 1) tf.add_to_collection('beam_text', beam_text) tf.add_to_collection('beam_text_score', beam_text_score) tf.add_to_collection('beam_logprobs_history', predictor.decoder.log_probs_history) tf.add_to_collection('beam_alignment_history', predictor.decoder.alignment_history) predictor.logprobs_history = False predictor.alignment_history = False
def train_process(trainer, predictor=None): input_app = InputApp.InputApp() input_results = input_app.gen_input() with tf.variable_scope(FLAGS.main_scope) as scope: ops, gen_feed_dict, deal_results = gen_train(input_app, input_results, trainer) scope.reuse_variables() #saving predict graph, so later can direclty predict without building from scratch #also used in gen validate if you want to use direclty predict as evaluate per epoch if predictor is not None and FLAGS.gen_predict: gen_predict_graph(predictor) eval_ops, gen_eval_feed_dict, deal_eval_results = gen_validate( input_app, input_results, trainer, predictor) metric_eval_fn = None if FLAGS.metric_eval: #generative can do this also but it is slow so just ingore this if not algos_factory.is_generative(FLAGS.algo): metric_eval_fn = lambda: evaluator.evaluate_scores(predictor, random=True) init_fn = None summary_excls = None if not FLAGS.pre_calc_image_feature: init_fn = melt.image.create_image_model_init_fn( FLAGS.image_model_name, FLAGS.image_checkpoint_file) if predictor is not None and FLAGS.gen_predict: #need to excl InceptionV3 summarys why inceptionV3 op might need image_feature_feed if gen_predict #gen_eval_feed_dict = lambda: {predictor.image_feature_feed: [melt.image.read_image(FLAGS.one_image)]} #gen_eval_feed_dict = lambda: {predictor.image_feature_feed: ['']} summary_excls = [FLAGS.image_model_name] melt.print_global_varaiables() melt.apps.train_flow( ops, gen_feed_dict_fn=gen_feed_dict, deal_results_fn=deal_results, eval_ops=eval_ops, gen_eval_feed_dict_fn=gen_eval_feed_dict, deal_eval_results_fn=deal_eval_results, optimizer=FLAGS.optimizer, learning_rate=FLAGS.learning_rate, num_steps_per_epoch=input_app.num_steps_per_epoch, model_dir=FLAGS.model_dir, metric_eval_fn=metric_eval_fn, summary_excls=summary_excls, init_fn=init_fn, sess=sess ) #notice if use melt.constant in predictor then must pass sess
def train(): input_app = InputApp.InputApp() input_results = input_app.gen_input() with tf.variable_scope(FLAGS.main_scope) as scope: trainer, predictor = algos_factory.gen_trainer_and_predictor( FLAGS.algo) logging.info('trainer:{}'.format(trainer)) logging.info('predictor:{}'.format(predictor)) ops, gen_feed_dict, deal_results = gen_train(input_app, input_results, trainer) scope.reuse_variables() algos_factory.set_eval_mode(trainer) if predictor is not None and FLAGS.gen_predict: beam_text, beam_text_score = gen_predict_graph(predictor) eval_ops, gen_eval_feed_dict, deal_eval_results = gen_validate( input_app, input_results, trainer, predictor) metric_eval_fn = None if FLAGS.metric_eval: if not algos_factory.is_generative( FLAGS.algo) or FLAGS.assistant_model_dir: metric_eval_fn = lambda: evaluator.evaluate_scores(predictor, random=True) melt.print_global_varaiables() melt.apps.train_flow( ops, gen_feed_dict_fn=gen_feed_dict, deal_results_fn=deal_results, eval_ops=eval_ops, gen_eval_feed_dict_fn=gen_eval_feed_dict, deal_eval_results_fn=deal_eval_results, optimizer=FLAGS.optimizer, learning_rate=FLAGS.learning_rate, num_steps_per_epoch=input_app.num_steps_per_epoch, model_dir=FLAGS.model_dir, metric_eval_fn=metric_eval_fn, restore_scope= global_scope, #only restore global scope as might evaluator has another predictor in graph with another scope name like dual_bow sess=sess ) #notice if use melt.constant in predictor then must pass sess
def train(): input_app = InputApp.InputApp() input_results = input_app.gen_input() with tf.variable_scope(FLAGS.main_scope) as scope: trainer, predictor = algos_factory.gen_trainer_and_predictor( FLAGS.algo) logging.info('trainer:{}'.format(trainer)) logging.info('predictor:{}'.format(predictor)) algos_factory.set_eval_mode(trainer) ops, gen_feed_dict, deal_results = gen_train(input_app, input_results, trainer) scope.reuse_variables() algos_factory.set_eval_mode(trainer) if predictor is not None and FLAGS.gen_predict: gen_predict_graph(predictor) eval_ops, gen_eval_feed_dict, deal_eval_results = gen_validate( input_app, input_results, trainer, predictor) metric_eval_fn = None if FLAGS.metric_eval: #generative can do this also but it is slow so just ingore this if not algos_factory.is_generative(FLAGS.algo): metric_eval_fn = lambda: evaluator.evaluate_scores(predictor, random=True) melt.print_global_varaiables() melt.apps.train_flow( ops, gen_feed_dict_fn=gen_feed_dict, deal_results_fn=deal_results, eval_ops=eval_ops, gen_eval_feed_dict_fn=gen_eval_feed_dict, deal_eval_results_fn=deal_eval_results, optimizer=FLAGS.optimizer, learning_rate=FLAGS.learning_rate, num_steps_per_epoch=input_app.num_steps_per_epoch, model_dir=FLAGS.model_dir, metric_eval_fn=metric_eval_fn, sess=sess ) #notice if use melt.constant in predictor then must pass sess
def train_process(trainer, predictor=None): input_app = InputApp.InputApp() input_results = input_app.gen_input() with tf.variable_scope(FLAGS.main_scope) as scope: ops, gen_feed_dict, deal_results = gen_train( input_app, input_results, trainer) scope.reuse_variables() if predictor is not None and FLAGS.gen_predict: beam_text, beam_text_score = gen_predict_graph(predictor) eval_ops, gen_eval_feed_dict, deal_eval_results = gen_validate( input_app, input_results, trainer, predictor) metric_eval_fn = None if FLAGS.metric_eval: #generative can do this also but it is slow so just ingore this if not algos_factory.is_generative(FLAGS.algo): metric_eval_fn = lambda: evaluator.evaluate_scores(predictor, random=True) if FLAGS.mode == 'train': melt.print_global_varaiables() melt.apps.train_flow(ops, gen_feed_dict_fn=gen_feed_dict, deal_results_fn=deal_results, eval_ops=eval_ops, gen_eval_feed_dict_fn=gen_eval_feed_dict, deal_eval_results_fn=deal_eval_results, optimizer=FLAGS.optimizer, learning_rate=FLAGS.learning_rate, num_steps_per_epoch=input_app.num_steps_per_epoch, model_dir=FLAGS.model_dir, metric_eval_fn=metric_eval_fn, sess=sess)#notice if use melt.constant in predictor then must pass sess else: #test predict predictor.load(FLAGS.model_dir) import conf from conf import TEXT_MAX_WORDS, INPUT_TEXT_MAX_WORDS, NUM_RESERVED_IDS, ENCODE_UNK print('-------------------------', tf.get_collection('scores')) #TODO: now copy from prpare/gen-records.py def _text2ids(text, max_words): word_ids = text2ids.text2ids(text, seg_method=FLAGS.seg_method, feed_single=FLAGS.feed_single, allow_all_zero=True, pad=False) word_ids_length = len(word_ids) word_ids = word_ids[:max_words] word_ids = gezi.pad(word_ids, max_words, 0) return word_ids input_texts = [ #'包邮买二送一性感女内裤低腰诱惑透视蕾丝露臀大蝴蝶三角内裤女夏-淘宝网', '大棚辣椒果实变小怎么办,大棚辣椒果实变小防治措施', ] for input_text in input_texts: word_ids = _text2ids(input_text, INPUT_TEXT_MAX_WORDS) print('word_ids', word_ids, 'len:', len(word_ids)) print(text2ids.ids2text(word_ids)) #similar as inference.py this is only ok for no attention mode TODO FIXME texts, scores = sess.run([tf.get_collection('text')[0], tf.get_collection('text_score')[0]], feed_dict={'seq2seq/model_init_1/input_text:0' : [word_ids]}) print(texts[0], text2ids.ids2text(texts[0]), scores[0]) texts, scores = sess.run([beam_text, beam_text_score], feed_dict={predictor.input_text_feed: [word_ids]}) texts = texts[0] scores = scores[0] for text, score in zip(texts, scores): print(text, text2ids.ids2text(text), score) input_texts = [ '大棚辣椒果实变小怎么办,大棚辣椒果实变小防治措施', #'包邮买二送一性感女内裤低腰诱惑透视蕾丝露臀大蝴蝶三角内裤女夏-淘宝网', "宝宝太胖怎么办呢", '大棚辣椒果实变小怎么办,大棚辣椒果实变小防治措施', #'大棚辣椒果实变小怎么办,大棚辣椒果实变小防治措施', #'邹红建是阿拉斯加', ] word_ids_list = [_text2ids(input_text, INPUT_TEXT_MAX_WORDS) for input_text in input_texts] timer = gezi.Timer() texts_list, scores_list = sess.run([beam_text, beam_text_score], feed_dict={predictor.input_text_feed: word_ids_list}) for texts, scores in zip(texts_list, scores_list): for text, score in zip(texts, scores): print(text, text2ids.ids2text(text), score, math.log(score)) print('beam_search using time(ms):', timer.elapsed_ms())
def train(): input_app = InputApp.InputApp() input_results = input_app.gen_input() with tf.variable_scope(FLAGS.main_scope) as scope: trainer, predictor = algos_factory.gen_trainer_and_predictor( FLAGS.algo) logging.info('trainer:{}'.format(trainer)) logging.info('predictor:{}'.format(predictor)) ops, gen_feed_dict, deal_results = gen_train(input_app, input_results, trainer) scope.reuse_variables() algos_factory.set_eval_mode(trainer) #saving predict graph, so later can direclty predict without building from scratch #also used in gen validate if you want to use direclty predict as evaluate per epoch if predictor is not None and FLAGS.gen_predict: gen_predict_graph(predictor) eval_ops, gen_eval_feed_dict, deal_eval_results = gen_validate( input_app, input_results, trainer, predictor) metric_eval_fn = None if FLAGS.metric_eval: eval_rank = FLAGS.eval_rank and (not algos_factory.is_generative( FLAGS.algo) or FLAGS.assistant_model_dir) eval_translation = FLAGS.eval_translation and algos_factory.is_generative( FLAGS.algo) metric_eval_fn = lambda: evaluator.evaluate(predictor, random=True, eval_rank=eval_rank, eval_translation= eval_translation) init_fn = None restore_fn = None summary_excls = None if not FLAGS.pre_calc_image_feature: init_fn = melt.image.image_processing.create_image_model_init_fn( FLAGS.image_model_name, FLAGS.image_checkpoint_file) if melt.checkpoint_exists_in(FLAGS.model_dir): if not melt.varname_in_checkpoint(FLAGS.image_model_name, FLAGS.model_dir): restore_fn = init_fn #melt.print_global_varaiables() melt.apps.train_flow( ops, gen_feed_dict_fn=gen_feed_dict, deal_results_fn=deal_results, eval_ops=eval_ops, gen_eval_feed_dict_fn=gen_eval_feed_dict, deal_eval_results_fn=deal_eval_results, optimizer=FLAGS.optimizer, learning_rate=FLAGS.learning_rate, num_steps_per_epoch=input_app.num_steps_per_epoch, model_dir=FLAGS.model_dir, metric_eval_fn=metric_eval_fn, summary_excls=summary_excls, init_fn=init_fn, restore_fn=restore_fn, sess=sess ) #notice if use melt.constant in predictor then must pass sess
def train(): input_app = InputApp.InputApp() input_results = input_app.gen_input() global_scope = melt.apps.train.get_global_scope() with tf.variable_scope(global_scope) as global_scope: with tf.variable_scope(FLAGS.main_scope) as scope: trainer, validator, predictor = algos_factory.gen_all(FLAGS.algo) if FLAGS.reinforcement_learning: from deepiu.image_caption.algos.show_and_tell import RLInfo trainer.rl = RLInfo() if FLAGS.scene_model: #TODO scene model not support show_eval right now due to batch size eg 3 for show_eval from deepiu.image_caption.algos.show_and_tell import SceneInfo trainer.scene = SceneInfo(FLAGS.batch_size) validator.scene = SceneInfo(FLAGS.eval_batch_size) predictor.scene = SceneInfo() logging.info('trainer:{}'.format(trainer)) logging.info('predictor:{}'.format(predictor)) ops, gen_feed_dict, deal_results = gen_train( input_app, input_results, trainer) scope.reuse_variables() #saving predict graph, so later can direclty predict without building from scratch #also used in gen validate if you want to use direclty predict as evaluate per epoch if predictor is not None and FLAGS.gen_predict: gen_predict_graph(predictor) eval_ops, gen_eval_feed_dict, deal_eval_results = gen_validate( input_app, input_results, validator, predictor) metric_eval_fn = None if FLAGS.metric_eval: if FLAGS.scene_model: evaluator.gen_feed_dict_fn = lambda image_features: gen_predict_scene_feed_dict( predictor, image_features) eval_rank = FLAGS.eval_rank and ( not algos_factory.is_generative(FLAGS.algo) or FLAGS.assistant_model_dir) eval_translation = FLAGS.eval_translation and algos_factory.is_generative( FLAGS.algo) metric_eval_fn = lambda: evaluator.evaluate( predictor, random=True, eval_rank=eval_rank, eval_translation=eval_translation) # NOTCIE in empty scope now, image model need to escape all scopes! summary_excls = None init_fn, restore_fn = image_util.get_init_restore_fn() with tf.variable_scope(global_scope): #melt.print_global_varaiables() melt.apps.train_flow( ops, names=names, gen_feed_dict_fn=gen_feed_dict, deal_results_fn=deal_results, eval_ops=eval_ops, eval_names=eval_names, gen_eval_feed_dict_fn=gen_eval_feed_dict, deal_eval_results_fn=deal_eval_results, optimizer=FLAGS.optimizer, learning_rate=FLAGS.learning_rate, num_steps_per_epoch=input_app.num_steps_per_epoch, metric_eval_fn=metric_eval_fn, summary_excls=summary_excls, init_fn=init_fn, restore_fn=restore_fn, sess=sess ) # notice if use melt.constant in predictor then must pass sess
def gen_validate(input_app, input_results, validator, predictor): gen_eval_feed_dict = None eval_ops = None train_with_validation = input_results[ input_app.input_valid_name] is not None scene_train_with_validation = input_results[ input_app.scene_input_valid_name] is not None deal_eval_results = None if train_with_validation and not FLAGS.train_only: eval_image_name, eval_image_feature, eval_text, eval_text_str = input_results[ input_app.input_valid_name] global geval_image_name geval_image_name = eval_image_name if input_results[input_app.input_valid_neg_name]: eval_neg_image_name, eval_neg_image_feature, eval_neg_text, eval_neg_text_str = input_results[ input_app.input_valid_neg_name] if not FLAGS.neg_left: eval_neg_image_feature = None eval_neg_text_ = eval_neg_text if not FLAGS.neg_right: eval_neg_text_ = None if algos_factory.is_generative(FLAGS.algo): eval_neg_image_feature = None eval_neg_text_ = None global eval_names with tf.device('/gpu:0'): eval_main_loss = validator.build_train_graph( eval_image_feature, eval_text, eval_neg_image_feature, eval_neg_text_) eval_scores = tf.get_collection('eval_scores')[-1] eval_ops = [eval_main_loss] eval_names = ['eval_main_loss'] if scene_train_with_validation: eval_image_name, eval_image, eval_label, eval_text_str = input_results[ input_app.scene_input_valid_name] with tf.device('/gpu:1'): eval_scene_loss = validator.build_scene_graph( eval_image, eval_label) eval_loss = eval_main_loss + eval_scene_loss eval_ops.insert(0, eval_loss) eval_names.insert(0, 'eal_loss') eval_ops += [ eval_scene_loss, validator.scene_recall_at_1, validator.scene_recall_at_k ] eval_names += [ 'eval_scene_loss', 'eval_scene_recall@1', 'eval_scene_recall@3' ] if algos_factory.is_generative(FLAGS.algo): eval_neg_text = None eval_neg_text_str = None if FLAGS.show_eval and (predictor is not None): eval_ops, deal_eval_results = \ gen_evalulate( input_app, input_results, predictor, eval_ops, eval_scores, eval_neg_text, eval_neg_text_str) else: #deal_eval_results = None def _deal_eval_results(results): print(results) deal_eval_results = _deal_eval_results def _gen_feed_dict(): feed_dict = {} return feed_dict def _gen_scene_feed_dict(): return gen_scene_feed_dict(validator, geval_image_name) def _gen_all_feed_dict(): feed_dict = _gen_feed_dict() if FLAGS.scene_model: feed_dict = gezi.merge_dicts(feed_dict, _gen_scene_feed_dict()) return feed_dict gen_eval_feed_dict = _gen_all_feed_dict return eval_ops, gen_eval_feed_dict, deal_eval_results