如何在单个 运行 中从 Inception-v3 检索 fc 和 softmax 层的输出?
How can I retrieve the output from both fc and softmax layers from Inception-v3 in a single run?
我想提取 'pool_3:0'
和 'softmax:0'
层的输出。我可以 运行 模型两次,并且对于每个 运行,提取单层的输出,但这有点浪费。是否可以 运行只对模型执行一次?
我正在使用 classify_image.py
提供的示例。这是相关的片段:
def run_inference_on_image(image_data):
create_graph()
with tf.Session() as sess:
# Some useful tensors:
# 'softmax:0': A tensor containing the normalized prediction across
# 1000 labels.
# 'pool_3:0': A tensor containing the next-to-last layer containing 2048
# float description of the image.
# 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
# encoding of the image.
# Runs the softmax tensor by feeding the image_data as input to the graph.
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
predictions = sess.run(softmax_tensor,
{'DecodeJpeg:0': image_data})
predictions = np.squeeze(predictions)
# Creates node ID --> English string lookup.
node_lookup = NodeLookup()
top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
for node_id in top_k:
human_string = node_lookup.id_to_string(node_id)
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
return predictions
您可以将张量列表传递给 Session.run()
,TensorFlow 将共享为计算它们所做的工作:
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
pool_3 = sess.graph.get_tensor_by_name('pool_3:0')
predictions, pool3_val = sess.run([softmax_tensor, pool_3],
{'DecodeJpeg:0': image_data})
我想提取 'pool_3:0'
和 'softmax:0'
层的输出。我可以 运行 模型两次,并且对于每个 运行,提取单层的输出,但这有点浪费。是否可以 运行只对模型执行一次?
我正在使用 classify_image.py
提供的示例。这是相关的片段:
def run_inference_on_image(image_data):
create_graph()
with tf.Session() as sess:
# Some useful tensors:
# 'softmax:0': A tensor containing the normalized prediction across
# 1000 labels.
# 'pool_3:0': A tensor containing the next-to-last layer containing 2048
# float description of the image.
# 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
# encoding of the image.
# Runs the softmax tensor by feeding the image_data as input to the graph.
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
predictions = sess.run(softmax_tensor,
{'DecodeJpeg:0': image_data})
predictions = np.squeeze(predictions)
# Creates node ID --> English string lookup.
node_lookup = NodeLookup()
top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
for node_id in top_k:
human_string = node_lookup.id_to_string(node_id)
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
return predictions
您可以将张量列表传递给 Session.run()
,TensorFlow 将共享为计算它们所做的工作:
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
pool_3 = sess.graph.get_tensor_by_name('pool_3:0')
predictions, pool3_val = sess.run([softmax_tensor, pool_3],
{'DecodeJpeg:0': image_data})