def inference(file, checkpoint_path, vocab_file): # Build the inference_utils graph. g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(configuration.ModelConfig(), checkpoint_path) g.finalize() # Create the vocabulary. vocab = vocabulary.Vocabulary(vocab_file) with tf.Session(graph=g) as sess: # Load the model from checkpoint. restore_fn(sess) # Prepare the caption generator. Here we are implicitly using the default # beam search parameters. See caption_generator.py for a description of the # available beam search parameters. generator = caption_generator.CaptionGenerator(model, vocab) with tf.gfile.GFile(file, "rb") as f: image = f.read() captions = generator.beam_search(sess, image) results = [] for i, caption in enumerate(captions): # Ignore begin and end words. sentence = [vocab.id_to_word(w) for w in caption.sentence[1:-1]] sentence = " ".join(sentence) results.append("%s (p=%f)" % (sentence, math.exp(caption.logprob))) return results
def run(): """Runs evaluation in a loop, and logs summaries to TensorBoard.""" # Create the evaluation directory if it doesn't exist. eval_dir = FLAGS.eval_dir if not tf.gfile.IsDirectory(eval_dir): tf.logging.info("Creating eval directory: %s", eval_dir) tf.gfile.MakeDirs(eval_dir) g = tf.Graph() with g.as_default(): # Build the model for evaluation. model_config = configuration.ModelConfig() model_config.input_file_pattern = FLAGS.input_file_pattern model = show_and_tell_model.ShowAndTellModel(model_config, mode="eval") model.build() # Create the Saver to restore model Variables. saver = tf.train.Saver() # Create the summary operation and the summary writer. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(eval_dir) g.finalize() # Run a new evaluation run every eval_interval_secs. while True: start = time.time() tf.logging.info( "Starting evaluation at " + time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime())) run_once(model, saver, summary_writer, summary_op) time_to_next_eval = start + FLAGS.eval_interval_secs - time.time() if time_to_next_eval > 0: time.sleep(time_to_next_eval)
def main(_): # FLAGS.checkpoint_path = r"C:\Work\08_Project_TellMachine\Output" # FLAGS.vocab_file = r"C:\Work\08_Project_TellMachine\Output\word_counts.txt" # FLAGS.input_files = r"C:\Work\08_Project_TellMachine\Output\667626_18933d713e.jpg" # Build the inference_utils graph. g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(configuration.ModelConfig(), FLAGS.checkpoint_path) g.finalize() # Create the vocabulary. vocab = vocabulary.Vocabulary(FLAGS.vocab_file) filenames = [] for file_pattern in FLAGS.input_files.split(","): filenames.extend(tf.gfile.Glob(file_pattern)) tf.logging.info("Running caption generation on %d files matching %s", len(filenames), FLAGS.input_files) with tf.Session(graph=g) as sess: # Load the model from checkpoint. restore_fn(sess) # Prepare the caption generator. Here we are implicitly using the default # beam search parameters. See caption_generator.py for a description of the # available beam search parameters. generator = caption_generator.CaptionGenerator(model, vocab) for filename in filenames: with tf.gfile.GFile(filename, "rb") as f: image = f.read() captions = generator.beam_search(sess, image) print("Captions for image %s:" % os.path.basename(filename)) for i, caption in enumerate(captions): # Ignore begin and end words. sentence = [ vocab.id_to_word(w) for w in caption.sentence[1:-1] ] sentence = " ".join(sentence) print(" %d) %s (p=%f)" % (i, sentence, math.exp(caption.logprob)))
def main(unused_argv): # FLAGS.train_dir = r"C:\Work\08_Project_TellMachine\Output" # FLAGS.input_file_pattern =r"C:\Work\08_Project_TellMachine\Output\train-?????-of-00006" # FLAGS.inception_checkpoint_file = r"C:\Work\08_Project_TellMachine\Ckpt\inception_v3.ckpt" # FLAGS.number_of_steps = 5 assert FLAGS.input_file_pattern, "--input_file_pattern is required" assert FLAGS.train_dir, "--train_dir is required" model_config = configuration.ModelConfig() model_config.input_file_pattern = FLAGS.input_file_pattern model_config.inception_checkpoint_file = FLAGS.inception_checkpoint_file training_config = configuration.TrainingConfig() # Create training directory. train_dir = FLAGS.train_dir if not tf.gfile.IsDirectory(train_dir): tf.logging.info("Creating training directory: %s", train_dir) tf.gfile.MakeDirs(train_dir) # Build the TensorFlow graph. g = tf.Graph() with g.as_default(): # Build the model. model = show_and_tell_model.ShowAndTellModel( model_config, mode="train", train_inception=FLAGS.train_inception) model.build() # Set up the learning rate. learning_rate_decay_fn = None if FLAGS.train_inception: learning_rate = tf.constant( training_config.train_inception_learning_rate) else: learning_rate = tf.constant(training_config.initial_learning_rate) if training_config.learning_rate_decay_factor > 0: num_batches_per_epoch = ( training_config.num_examples_per_epoch / model_config.batch_size) decay_steps = int(num_batches_per_epoch * training_config.num_epochs_per_decay) def _learning_rate_decay_fn(learning_rate, global_step): return tf.train.exponential_decay( learning_rate, global_step, decay_steps=decay_steps, decay_rate=training_config.learning_rate_decay_factor, staircase=True) learning_rate_decay_fn = _learning_rate_decay_fn # Set up the training ops. train_op = tf.contrib.layers.optimize_loss( loss=model.total_loss, global_step=model.global_step, learning_rate=learning_rate, optimizer=training_config.optimizer, clip_gradients=training_config.clip_gradients, learning_rate_decay_fn=learning_rate_decay_fn) # Set up the Saver for saving and restoring model checkpoints. saver = tf.train.Saver( max_to_keep=training_config.max_checkpoints_to_keep) # Run training. tf.contrib.slim.learning.train(train_op, train_dir, log_every_n_steps=FLAGS.log_every_n_steps, graph=g, global_step=model.global_step, number_of_steps=FLAGS.number_of_steps, init_fn=model.init_fn, saver=saver)
import numpy as np from scipy.misc import imread import os import multiprocessing import sys import json import argparse from caption_generator import * from inference_utils import extract_features, extract_image_id, run_inference from voting import rrv_captions_from_beam import h5py model_config = configuration.ModelConfig() training_config = configuration.TrainingConfig() FLAGS = None verbose = True mode = 'inference' # Run inference but do beam search in batches for better efficiency... def run_inference2(sess, features, generator, data, batch_size): generator.beam_search2(sess, features, batch_size=batch_size) def create_annotations(features, image_names, data,
def main(_): assert FLAGS.file_pattern, "--file_pattern is required" assert FLAGS.train_checkpoints, "--train_checkpoints is required" assert FLAGS.summaries_dir, "--summaries_dir is required" vocab = Vocabulary() model_config = configuration.ModelConfig() training_config = configuration.TrainingConfig() print(FLAGS.learning_rate) training_config.initial_learning_rate = FLAGS.learning_rate sequence_length = model_config.sequence_length batch_size = FLAGS.batch_size summaries_dir = FLAGS.summaries_dir if not tf.gfile.IsDirectory(summaries_dir): tf.logging.info("Creating training directory: %s", summaries_dir) tf.gfile.MakeDirs(summaries_dir) train_checkpoints = FLAGS.train_checkpoints if not tf.gfile.IsDirectory(train_checkpoints): tf.logging.info("Creating training directory: %s", train_checkpoints) tf.gfile.MakeDirs(train_checkpoints) # 数据队列初始化 input_queue = DataReader(FLAGS.dataset_dir, FLAGS.file_pattern, model_config, batch_size=batch_size) g = tf.Graph() with g.as_default(): # 数据队列 with tf.name_scope(None, 'input_queue'): input_images, input_labels = input_queue.read() # 模型建立 model = crnn.CRNN(256, model_config.num_classes, 'train') logits = model.build(input_images) with tf.name_scope(None, 'loss'): loss = tf.reduce_mean( tf.nn.ctc_loss(labels=input_labels, inputs=logits, sequence_length=sequence_length * tf.ones(batch_size, dtype=tf.int32)), name='compute_loss', ) tf.losses.add_loss(loss) total_loss = tf.losses.get_total_loss(False) with tf.name_scope(None, 'decoder'): decoded, _ = tf.nn.ctc_beam_search_decoder( logits, sequence_length * tf.ones(batch_size, dtype=tf.int32), merge_repeated=False, ) with tf.name_scope(None, 'acurracy'): sequence_dist = tf.reduce_mean( tf.edit_distance(tf.cast(decoded[0], tf.int32), input_labels), name='seq_dist', ) preds = tf.sparse_tensor_to_dense(decoded[0], name='prediction') gt_labels = tf.sparse_tensor_to_dense(input_labels, name='Ground_Truth') # print(len(slim.get_model_variables())) # print('>>>>>>>>>>>>>>>>>>>>>>>>>>>') # print(len(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))) # sys.exit() global_step = tf.Variable(initial_value=0, name="global_step", trainable=False, collections=[ tf.GraphKeys.GLOBAL_STEP, tf.GraphKeys.GLOBAL_VARIABLES ]) start_learning_rate = training_config.initial_learning_rate learning_rate = tf.train.exponential_decay( start_learning_rate, global_step, decay_steps=training_config.learning_decay_steps, decay_rate=training_config.learning_rate_decay_factor, staircase=True, ) # summary # Add summaries for variables. for variable in slim.get_model_variables(): tf.summary.histogram(variable.op.name, variable) tf.summary.scalar(name='Seq_Dist', tensor=sequence_dist) tf.summary.scalar(name='global_step', tensor=global_step) tf.summary.scalar(name='learning_rate', tensor=learning_rate) tf.summary.scalar(name='total_loss', tensor=total_loss) # global/secs hook globalhook = tf.train.StepCounterHook( every_n_steps=FLAGS.log_every_n_steps, ) # 保存chekpoints的hook # saver = tf.train.Saver(max_to_keep=training_config.max_checkpoints_to_keep) # saverhook = tf.train.CheckpointSaverHook( # checkpoint_dir=FLAGS.train_checkpoints, # save_steps=2000, # saver=saver, # ) # #保存summaries的hook # merge_summary_op = tf.summary.merge_all() # summaryhook = tf.train.SummarySaverHook( # save_steps=200, # output_dir=FLAGS.summaries_dir, # summary_op=merge_summary_op, # ) # 训练时需要logging的hook tensors_print = { 'global_step': global_step, 'loss': loss, 'Seq_Dist': sequence_dist, # 'accurays':accurays, } loghook = tf.train.LoggingTensorHook( tensors=tensors_print, every_n_iter=FLAGS.log_every_n_steps, ) # 停止hook stophook = tf.train.StopAtStepHook(last_step=FLAGS.number_of_steps) gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) session_config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) # extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # with tf.control_dependencies(extra_update_ops): # optimizer = tf.train.AdadeltaOptimizer( # learning_rate=learning_rate).minimize(loss=total_loss, global_step=global_step) optimizer = tf.train.AdadeltaOptimizer(learning_rate=learning_rate) train_op = tf.contrib.training.create_train_op(total_loss=total_loss, optimizer=optimizer, global_step=global_step) # train_op = tf.group([optimizer, total_loss, sequence_dist]) with tf.train.MonitoredTrainingSession( checkpoint_dir=FLAGS.train_checkpoints, hooks=[globalhook, loghook, stophook], save_checkpoint_secs=180, save_summaries_steps=100, config=session_config) as sess: while not sess.should_stop(): oloss, opreds, ogt_labels = sess.run( [train_op, preds, gt_labels]) accuray = compute_acuracy(opreds, ogt_labels) print("accuracy: %9f" % (accuray))
def main(_): assert FLAGS.file_pattern, "--file_pattern is required" assert FLAGS.train_checkpoints, "--train_checkpoints is required" assert FLAGS.summaries_dir, "--summaries_dir is required" vocab = Vocabulary() model_config = configuration.ModelConfig() training_config = configuration.TrainingConfig() print(FLAGS.learning_rate) training_config.initial_learning_rate = FLAGS.learning_rate sequence_length = model_config.sequence_length batch_size = FLAGS.batch_size summaries_dir = FLAGS.summaries_dir if not tf.gfile.IsDirectory(summaries_dir): tf.logging.info("Creating training directory: %s", summaries_dir) tf.gfile.MakeDirs(summaries_dir) train_checkpoints = FLAGS.train_checkpoints if not tf.gfile.IsDirectory(train_checkpoints): tf.logging.info("Creating training directory: %s", train_checkpoints) tf.gfile.MakeDirs(train_checkpoints) # 数据队列初始化 input_queue = DataReader(FLAGS.dataset_dir, FLAGS.file_pattern, model_config, batch_size=batch_size) g = tf.Graph() with g.as_default(): # 数据队列 with tf.name_scope(None, 'input_queue'): input_images, input_labels = input_queue.read() # 模型建立 model = crnn.CRNN(256, model_config.num_classes, 'train') logits = model.build(input_images) with tf.name_scope(None, 'loss'): loss = tf.reduce_mean( tf.nn.ctc_loss(labels=input_labels, inputs=logits, sequence_length=sequence_length * tf.ones(batch_size, dtype=tf.int32)), name='compute_loss', ) tf.losses.add_loss(loss) total_loss = tf.losses.get_total_loss(False) with tf.name_scope(None, 'decoder'): decoded, _ = tf.nn.ctc_beam_search_decoder( logits, sequence_length * tf.ones(batch_size, dtype=tf.int32), merge_repeated=False, ) with tf.name_scope(None, 'acurracy'): sequence_dist = tf.reduce_mean( tf.edit_distance(tf.cast(decoded[0], tf.int32), input_labels), name='seq_dist', ) preds = tf.sparse_tensor_to_dense(decoded[0], name='prediction') gt_labels = tf.sparse_tensor_to_dense(input_labels, name='Ground_Truth') global_step = tf.Variable(initial_value=0, name="global_step", trainable=False, collections=[ tf.GraphKeys.GLOBAL_STEP, tf.GraphKeys.GLOBAL_VARIABLES ]) # 训练时需要logging的hook tensors_print = { 'global_step': global_step, #'loss': loss, } loghook = tf.train.LoggingTensorHook( tensors=tensors_print, every_n_iter=FLAGS.log_every_n_steps, ) # 停止hook stophook = tf.train.StopAtStepHook(last_step=FLAGS.number_of_steps) gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) session_config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) train_op = tf.assign_add(global_step, tf.constant(1)) session = tf.train.ChiefSessionCreator( config=session_config, checkpoint_dir=FLAGS.train_checkpoints, ) labels_shape = input_labels.dense_shape with tf.train.MonitoredSession(session, hooks=[loghook, stophook]) as sess: while not sess.should_stop(): test_logits, test_images, test_shape, _ = \ sess.run([logits, input_images, labels_shape, input_labels]) if test_logits.shape[ 1] != FLAGS.batch_size or test_images.shape[ 0] != FLAGS.batch_size or test_shape[ 0] != FLAGS.batch_size: print("get it!!!!!") test_loss = sess.run([loss]) sess.run(train_op)