Python configuration.ModelConfig() Examples

The following are 19 code examples of configuration.ModelConfig(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module configuration , or try the search function .
Example #1
Source File: im2txt_inference.py    From uai-sdk with Apache License 2.0 6 votes vote down vote up
def load_model(self):

        print("Loading model with an input size of: [" + str(self.input_width) + "," + str(self.input_height) + "]")
        graph = tf.Graph()
        with graph.as_default():
            model = inference_wrapper.InferenceWrapper()
            restore_fn = model.build_graph_from_config(configuration.ModelConfig(), os.path.join(self.model_dir, "model.ckpt-" + str(self.checkpoint)))
        graph.finalize()

        # Create the vocabulary.
        vocab = vocabulary.Vocabulary(os.path.join(self.model_dir, "word_counts.txt"))

        sess = tf.Session(graph=graph)
        
        restore_fn(sess)
        generator = caption_generator.CaptionGenerator(model, vocab)    

        self._sess = sess
        self._generator = generator
        self._vocab = vocab 
Example #2
Source File: LSTM_train.py    From AssociativeRetrieval with Apache License 2.0 5 votes vote down vote up
def main():
  config = configuration.ModelConfig(data_filename="input_seqs_train")
  train(config) 
Example #3
Source File: train.py    From class-balanced-loss with MIT License 5 votes vote down vote up
def input_fn(params):
  model_config = configuration.ModelConfig()
  model_config.input_file_pattern = params["input_file_pattern"]
  model_config.batch_size = params["batch_size"]
  model_config.mode = params["mode"]
  model = show_and_tell_model.ShowAndTellModel(model_config, mode="train")
  model.build_inputs()
  return {
      "images": model.images,
      "input_seqs": model.input_seqs,
      "target_seqs": model.target_seqs,
      "input_mask": model.input_mask
  } 
Example #4
Source File: train.py    From class-balanced-loss with MIT License 5 votes vote down vote up
def model_fn(features, labels, mode, params):
  im_mode = MODEKEY_TO_MODE[mode]
  model_config = configuration.ModelConfig()
  training_config = configuration.TrainingConfig()
  model = show_and_tell_model.ShowAndTellModel(
      model_config, mode=im_mode, train_inception=FLAGS.train_inception)
  model.build_model_for_tpu(
      images=features["images"],
      input_seqs=features["input_seqs"],
      target_seqs=features["target_seqs"],
      input_mask=features["input_mask"])

  optimizer = tf.train.GradientDescentOptimizer(
      learning_rate=training_config.initial_learning_rate)
  optimizer = tf.contrib.estimator.clip_gradients_by_norm(
      optimizer, training_config.clip_gradients)
  if FLAGS.use_tpu:
    optimizer = tf.contrib.tpu.CrossShardOptimizer(optimizer)
  train_op = optimizer.minimize(
      model.total_loss, global_step=tf.train.get_or_create_global_step())

  def scaffold_fn():
    """Load pretrained Inception checkpoint at initialization time."""
    return tf.train.Scaffold(init_fn=model.init_fn)

  return tf.contrib.tpu.TPUEstimatorSpec(
      mode=mode,
      loss=model.total_loss,
      train_op=train_op,
      scaffold_fn=scaffold_fn) 
Example #5
Source File: show_and_tell_tpu_test.py    From class-balanced-loss with MIT License 5 votes vote down vote up
def testCallModelFnWithPlaceholders(self):
    with _reset_for_test() as session:
      config = configuration.ModelConfig()
      model = show_and_tell_model.ShowAndTellModel(config, mode='train')

      def model_fn(images, input_seq, target_seq, input_mask):
        model.build_model_for_tpu(images, input_seq, target_seq, input_mask)
        return model.total_loss

      images = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
      input_seq = tf.placeholder(tf.int32, shape=(1, 128))
      target_seq = tf.placeholder(tf.int32, shape=(1, 128))
      input_mask = tf.placeholder(tf.int32, shape=(1, 128))

      tpu_model_fn = tpu.rewrite(model_fn,
                                 [images, input_seq, target_seq, input_mask])
      caption = np.random.randint(low=0, high=1000, size=128).reshape((1, 128))
      session.run(tpu.initialize_system())
      session.run(tf.global_variables_initializer())
      inputs = {
          images: np.random.randn(1, 224, 224, 3),
          input_seq: caption,
          target_seq: caption,
          input_mask: np.random.random_integers(0, 1, size=128).reshape(1, 128),
      }
      session.run(tpu_model_fn, inputs)
      session.run(tpu.shutdown_system()) 
Example #6
Source File: show_and_tell_tpu_test.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
def testCallModelFnWithPlaceholders(self):
    with _reset_for_test() as session:
      config = configuration.ModelConfig()
      model = show_and_tell_model.ShowAndTellModel(config, mode='train')

      def model_fn(images, input_seq, target_seq, input_mask):
        model.build_model_for_tpu(images, input_seq, target_seq, input_mask)
        return model.total_loss

      images = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
      input_seq = tf.placeholder(tf.int32, shape=(1, 128))
      target_seq = tf.placeholder(tf.int32, shape=(1, 128))
      input_mask = tf.placeholder(tf.int32, shape=(1, 128))

      tpu_model_fn = tpu.rewrite(model_fn,
                                 [images, input_seq, target_seq, input_mask])
      caption = np.random.randint(low=0, high=1000, size=128).reshape((1, 128))
      session.run(tpu.initialize_system())
      session.run(tf.global_variables_initializer())
      inputs = {
          images: np.random.randn(1, 224, 224, 3),
          input_seq: caption,
          target_seq: caption,
          input_mask: np.random.random_integers(0, 1, size=128).reshape(1, 128),
      }
      session.run(tpu_model_fn, inputs)
      session.run(tpu.shutdown_system()) 
Example #7
Source File: LSTM_eval.py    From AssociativeRetrieval with Apache License 2.0 5 votes vote down vote up
def main():
  config = configuration.ModelConfig(data_filename="input_seqs_eval")
  train(config) 
Example #8
Source File: FW_eval.py    From AssociativeRetrieval with Apache License 2.0 5 votes vote down vote up
def main():
  config = configuration.ModelConfig(data_filename="input_seqs_eval")
  train(config) 
Example #9
Source File: FW_train.py    From AssociativeRetrieval with Apache License 2.0 5 votes vote down vote up
def main():
  config = configuration.ModelConfig(data_filename="input_seqs_train")
  train(config) 
Example #10
Source File: train.py    From tpu_models with Apache License 2.0 5 votes vote down vote up
def input_fn(params):
  model_config = configuration.ModelConfig()
  model_config.input_file_pattern = params["input_file_pattern"]
  model_config.batch_size = params["batch_size"]
  model_config.mode = params["mode"]
  model = show_and_tell_model.ShowAndTellModel(model_config, mode="train")
  model.build_inputs()
  return {
      "images": model.images,
      "input_seqs": model.input_seqs,
      "target_seqs": model.target_seqs,
      "input_mask": model.input_mask
  } 
Example #11
Source File: train.py    From tpu_models with Apache License 2.0 5 votes vote down vote up
def model_fn(features, labels, mode, params):
  im_mode = MODEKEY_TO_MODE[mode]
  model_config = configuration.ModelConfig()
  training_config = configuration.TrainingConfig()
  model = show_and_tell_model.ShowAndTellModel(
      model_config, mode=im_mode, train_inception=FLAGS.train_inception)
  model.build_model_for_tpu(
      images=features["images"],
      input_seqs=features["input_seqs"],
      target_seqs=features["target_seqs"],
      input_mask=features["input_mask"])

  optimizer = tf.train.GradientDescentOptimizer(
      learning_rate=training_config.initial_learning_rate)
  optimizer = tf.contrib.estimator.clip_gradients_by_norm(
      optimizer, training_config.clip_gradients)
  if FLAGS.use_tpu:
    optimizer = tf.contrib.tpu.CrossShardOptimizer(optimizer)
  train_op = optimizer.minimize(
      model.total_loss, global_step=tf.train.get_or_create_global_step())

  def scaffold_fn():
    """Load pretrained Inception checkpoint at initialization time."""
    return tf.train.Scaffold(init_fn=model.init_fn)

  return tf.contrib.tpu.TPUEstimatorSpec(
      mode=mode,
      loss=model.total_loss,
      train_op=train_op,
      scaffold_fn=scaffold_fn) 
Example #12
Source File: show_and_tell_tpu_test.py    From tpu_models with Apache License 2.0 5 votes vote down vote up
def testCallModelFnWithPlaceholders(self):
    with _reset_for_test() as session:
      config = configuration.ModelConfig()
      model = show_and_tell_model.ShowAndTellModel(config, mode='train')

      def model_fn(images, input_seq, target_seq, input_mask):
        model.build_model_for_tpu(images, input_seq, target_seq, input_mask)
        return model.total_loss

      images = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
      input_seq = tf.placeholder(tf.int32, shape=(1, 128))
      target_seq = tf.placeholder(tf.int32, shape=(1, 128))
      input_mask = tf.placeholder(tf.int32, shape=(1, 128))

      tpu_model_fn = tpu.rewrite(model_fn,
                                 [images, input_seq, target_seq, input_mask])
      caption = np.random.randint(low=0, high=1000, size=128).reshape((1, 128))
      session.run(tpu.initialize_system())
      session.run(tf.global_variables_initializer())
      inputs = {
          images: np.random.randn(1, 224, 224, 3),
          input_seq: caption,
          target_seq: caption,
          input_mask: np.random.random_integers(0, 1, size=128).reshape(1, 128),
      }
      session.run(tpu_model_fn, inputs)
      session.run(tpu.shutdown_system()) 
Example #13
Source File: train.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
def input_fn(params):
  model_config = configuration.ModelConfig()
  model_config.input_file_pattern = params["input_file_pattern"]
  model_config.batch_size = params["batch_size"]
  model_config.mode = params["mode"]
  model = show_and_tell_model.ShowAndTellModel(model_config, mode="train")
  model.build_inputs()
  return {
      "images": model.images,
      "input_seqs": model.input_seqs,
      "target_seqs": model.target_seqs,
      "input_mask": model.input_mask
  } 
Example #14
Source File: train.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
def model_fn(features, labels, mode, params):
  im_mode = MODEKEY_TO_MODE[mode]
  model_config = configuration.ModelConfig()
  training_config = configuration.TrainingConfig()
  model = show_and_tell_model.ShowAndTellModel(
      model_config, mode=im_mode, train_inception=FLAGS.train_inception)
  model.build_model_for_tpu(
      images=features["images"],
      input_seqs=features["input_seqs"],
      target_seqs=features["target_seqs"],
      input_mask=features["input_mask"])

  optimizer = tf.train.GradientDescentOptimizer(
      learning_rate=training_config.initial_learning_rate)
  optimizer = tf.contrib.estimator.clip_gradients_by_norm(
      optimizer, training_config.clip_gradients)
  if FLAGS.use_tpu:
    optimizer = tf.contrib.tpu.CrossShardOptimizer(optimizer)
  train_op = optimizer.minimize(
      model.total_loss, global_step=tf.train.get_or_create_global_step())

  def scaffold_fn():
    """Load pretrained Inception checkpoint at initialization time."""
    return tf.train.Scaffold(init_fn=model.init_fn)

  return tf.contrib.tpu.TPUEstimatorSpec(
      mode=mode,
      loss=model.total_loss,
      train_op=train_op,
      scaffold_fn=scaffold_fn) 
Example #15
Source File: run_inference_siamese.py    From polyvore with Apache License 2.0 4 votes vote down vote up
def main(_):
  if os.path.isfile(FLAGS.feature_file):
    print("Feature file already exist.")
    return
  # Build the inference graph.
  g = tf.Graph()
  with g.as_default():
    model_config = configuration.ModelConfig()
    model = polyvore_model.PolyvoreModel(model_config, mode="inference")
    model.build()
    saver = tf.train.Saver()

  g.finalize()
  sess = tf.Session(graph=g)
  saver.restore(sess, FLAGS.checkpoint_path)
  test_json = json.load(open(FLAGS.json_file))
  k = 0

  # Save image ids and features in a dictionary.
  test_features = dict()

  for image_set in test_json:
    set_id = image_set["set_id"]
    image_feat = []
    image_rnn_feat = []
    ids = []
    k = k + 1
    print(str(k) + " : " + set_id)
    for image in image_set["items"]:
      filename = os.path.join(FLAGS.image_dir, set_id,
                              str(image["index"]) + ".jpg")
      with tf.gfile.GFile(filename, "r") as f:
        image_feed = f.read()

      [feat] = sess.run([model.image_embeddings],
                         feed_dict={"image_feed:0": image_feed})
      
      image_name = set_id + "_" + str(image["index"])
      test_features[image_name] = dict()
      test_features[image_name]["image_feat"] = np.squeeze(feat)
  
  with open(FLAGS.feature_file, "wb") as f:
    pkl.dump(test_features, f) 
Example #16
Source File: fill_in_blank.py    From polyvore with Apache License 2.0 4 votes vote down vote up
def main(_):
  # Build the inference graph.
  top_k = 4 # Print the top_k accuracy.
  true_pred = np.zeros(top_k)
  # Load pre-computed image features.
  with open(FLAGS.feature_file, "rb") as f:
    test_data = pkl.load(f)
  test_ids = test_data.keys()
  test_feat = np.zeros((len(test_ids),
                        len(test_data[test_ids[0]]["image_feat"])))
  test_rnn_feat = np.zeros((len(test_ids),
                            len(test_data[test_ids[0]]["image_rnn_feat"])))
  for i, test_id in enumerate(test_ids):
    # Image feature in visual-semantic embedding space.
    test_feat[i] = test_data[test_id]["image_feat"]
    # Image feature in the RNN space.
    test_rnn_feat[i] = test_data[test_id]["image_rnn_feat"]

  g = tf.Graph()
  with g.as_default():
    model_config = configuration.ModelConfig()
    model_config.rnn_type = FLAGS.rnn_type
    model = polyvore_model.PolyvoreModel(model_config, mode="inference")
    model.build()
    saver = tf.train.Saver()
    
    g.finalize()
    with tf.Session() as sess:
      saver.restore(sess, FLAGS.checkpoint_path)
      questions = json.load(open(FLAGS.json_file))
      
      all_pred = []
      set_ids = []
      all_scores = []
      for question in questions:
        score, pred = run_question_inference(sess, question, test_ids,
                                             test_feat, test_rnn_feat,
                                             model_config.num_lstm_units)
        if pred != []:
          all_pred.append(pred)
          all_scores.append(score)
          set_ids.append(question["question"][0].split("_")[0])
          # 0 is the correct answer, iterate over top_k.
          for i in range(top_k):
            if 0 in pred[:i+1]:
              true_pred[i] += 1

      # Print all top-k accuracy.
      for i in range(top_k):
        print("Top %d Accuracy: " % (i + 1))
        print("%d correct answers in %d valid questions." %
                  (true_pred[i], len(all_pred)))
        print("Accuracy: %f" % (true_pred[i] / len(all_pred)))
        
      s = np.empty((len(all_scores),), dtype=np.object)
      for i in range(len(all_scores)):
          s[i] = all_scores[i]

      with open(FLAGS.result_file, "wb") as f:
        pkl.dump({"set_ids": set_ids, "pred": all_pred, "score": s}, f) 
Example #17
Source File: run_inference.py    From polyvore with Apache License 2.0 4 votes vote down vote up
def main(_):
  if os.path.isfile(FLAGS.feature_file):
    print("Feature file already exist.")
    return
  # Build the inference graph.
  g = tf.Graph()
  with g.as_default():
    model_config = configuration.ModelConfig()
    model_config.rnn_type = FLAGS.rnn_type
    model = polyvore_model.PolyvoreModel(model_config, mode="inference")
    model.build()
    saver = tf.train.Saver()

  g.finalize()
  sess = tf.Session(graph=g)
  saver.restore(sess, FLAGS.checkpoint_path)
  test_json = json.load(open(FLAGS.json_file))
  k = 0

  # Save image ids and features in a dictionary.
  test_features = dict()

  for image_set in test_json:
    set_id = image_set["set_id"]
    image_feat = []
    image_rnn_feat = []
    ids = []
    k = k + 1
    print(str(k) + " : " + set_id)
    for image in image_set["items"]:
      filename = os.path.join(FLAGS.image_dir, set_id,
                              str(image["index"]) + ".jpg")
      with tf.gfile.GFile(filename, "r") as f:
        image_feed = f.read()

      [feat, rnn_feat] = sess.run([model.image_embeddings,
                                   model.rnn_image_embeddings],
                                  feed_dict={"image_feed:0": image_feed})
      
      image_name = set_id + "_" + str(image["index"])
      test_features[image_name] = dict()
      test_features[image_name]["image_feat"] = np.squeeze(feat)
      test_features[image_name]["image_rnn_feat"] = np.squeeze(rnn_feat)
  
  with open(FLAGS.feature_file, "wb") as f:
    pkl.dump(test_features, f) 
Example #18
Source File: fill_in_blank_siamese.py    From polyvore with Apache License 2.0 4 votes vote down vote up
def main(_):
  # Build the inference graph.
  top_k = 4 # Print the top_k accuracy.
  true_pred = np.zeros(top_k)
  # Load pre-computed image features.
  with open(FLAGS.feature_file, "rb") as f:
    test_data = pkl.load(f)
  test_ids = test_data.keys()
  test_feat = np.zeros((len(test_ids),
                        len(test_data[test_ids[0]]["image_feat"])))
  for i, test_id in enumerate(test_ids):
    # Image feature in visual-semantic embedding space.
    test_feat[i] = test_data[test_id]["image_feat"]

  g = tf.Graph()
  with g.as_default():
    model_config = configuration.ModelConfig()
    model = polyvore_model.PolyvoreModel(model_config, mode="inference")
    model.build()
    saver = tf.train.Saver()
    
    g.finalize()
    with tf.Session() as sess:
      saver.restore(sess, FLAGS.checkpoint_path)
      questions = json.load(open(FLAGS.json_file))
      
      all_pred = []
      set_ids = []
      all_scores = []
      for question in questions:
        score, pred = run_question_inference(sess, question, test_ids,
                                             test_feat)
        if pred != []:
          all_pred.append(pred)
          all_scores.append(score)
          set_ids.append(question["question"][0].split("_")[0])
          # 0 is the correct answer, iterate over top_k.
          for i in range(top_k):
            if 0 in pred[:i+1]:
              true_pred[i] += 1

      # Print all top-k accuracy.
      for i in range(top_k):
        print("Top %d Accuracy: " % (i + 1))
        print("%d correct answers in %d valid questions." %
                  (true_pred[i], len(all_pred)))
        print("Accuracy: %f" % (true_pred[i] / len(all_pred)))
        
      s = np.empty((len(all_scores),), dtype=np.object)
      for i in range(len(all_scores)):
          s[i] = all_scores[i]

      with open(FLAGS.result_file, "wb") as f:
        pkl.dump({"set_ids": set_ids, "pred": all_pred, "score": s}, f) 
Example #19
Source File: run_inference_vse.py    From polyvore with Apache License 2.0 4 votes vote down vote up
def main(_):
  if os.path.isfile(FLAGS.feature_file):
    print("Feature file already exist.")
    return
  # Build the inference graph.
  g = tf.Graph()
  with g.as_default():
    model_config = configuration.ModelConfig()
    model = polyvore_model.PolyvoreModel(model_config, mode="inference")
    model.build()
    saver = tf.train.Saver()

  g.finalize()
  sess = tf.Session(graph=g)
  saver.restore(sess, FLAGS.checkpoint_path)
  test_json = json.load(open(FLAGS.json_file))
  k = 0

  # Save image ids and features in a dictionary.
  test_features = dict()

  for image_set in test_json:
    set_id = image_set["set_id"]
    image_feat = []
    image_rnn_feat = []
    ids = []
    k = k + 1
    print(str(k) + " : " + set_id)
    for image in image_set["items"]:
      filename = os.path.join(FLAGS.image_dir, set_id,
                              str(image["index"]) + ".jpg")
      with tf.gfile.GFile(filename, "r") as f:
        image_feed = f.read()

      [feat] = sess.run([model.image_embeddings],
                         feed_dict={"image_feed:0": image_feed})
      
      image_name = set_id + "_" + str(image["index"])
      test_features[image_name] = dict()
      test_features[image_name]["image_feat"] = np.squeeze(feat)
  
  with open(FLAGS.feature_file, "wb") as f:
    pkl.dump(test_features, f)