本文整理汇总了Python中configuration.ModelConfig方法的典型用法代码示例。如果您正苦于以下问题:Python configuration.ModelConfig方法的具体用法?Python configuration.ModelConfig怎么用?Python configuration.ModelConfig使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类configuration
的用法示例。
在下文中一共展示了configuration.ModelConfig方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_model
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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
示例2: testCallModelFnWithPlaceholders
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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())
示例3: model_fn
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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)
示例4: input_fn
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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
}
示例5: main
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
def main():
config = configuration.ModelConfig(data_filename="input_seqs_train")
train(config)
示例6: main
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
def main():
config = configuration.ModelConfig(data_filename="input_seqs_eval")
train(config)
示例7: main
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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)
示例8: main
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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)
示例9: main
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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)
示例10: main
# 需要导入模块: import configuration [as 别名]
# 或者: from configuration import ModelConfig [as 别名]
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)