在 Tensorflow 中,如果元图使用 TFRecord 输入(没有占位符),如何使用恢复的元图 [英] In Tensorflow, how to use a restored meta-graph if the meta graph was feeding with TFRecord input (without placeholders)
问题描述
我用 TFRecord 输入管道训练了一个网络.换句话说,没有占位符.简单的例子是:
I trained a network with TFRecord input pipeline. In other words, there was no placeholders. Simple example would be:
input, truth = _get_next_batch() # TFRecord. `input` is not a tf.placeholder
net = Model(input)
net.set_loss(truth)
optimizer = tf...(net.loss)
假设,我获得了三个文件,ckpt-20000.meta
、ckpt-20000.data-0000-of-0001
、ckpt-20000.索引
.我明白,以后可以使用 .meta
文件导入元图并访问张量,例如:
Let's say, I acquired three files, ckpt-20000.meta
, ckpt-20000.data-0000-of-0001
, ckpt-20000.index
. I understood that, later one can import the meta-graph using the .meta
file and access tensors such as:
new_saver = tf.train.import_meta_graph('ckpt-20000.meta')
new_saver.restore(sess, 'ckpt-20000')
logits = tf.get_collection("logits")[0]
然而,元图在管道中从一开始就没有占位符.有没有一种方法可以使用元图和输入的查询推理?
However, the meta-graph does not have a placeholder from the beginning in the pipeline. Is there a way that I can use meta-graph and query inference of an input?
有关信息,在查询应用程序(或脚本)中,我曾经使用占位符和恢复的模型权重定义模型(见下文).我想知道我是否可以直接使用元图而不重新定义,因为它会更简单.
For information, in a query application (or a script), I used to define a model with a placeholder and restored model weights (see below). I am wondering if I can just utilize the meta-graph without re-definition since it would be much more simple.
input = tf.placeholder(...)
net = Model(input)
tf.restore(sess, 'ckpt-2000')
lgt = sess.run(net.logits, feed_dict = {input:img})
推荐答案
您可以构建一个使用 placeholder_with_default()
作为输入的图,因此可以同时使用 TFRecord 输入管道
以及 feed_dict{}
.
You can build a graph that uses placeholder_with_default()
for the inputs, so can use both TFRecord input pipeline
as well as feed_dict{}
.
示例:
input, truth = _get_next_batch()
_x = tf.placeholder_with_default(input, shape=[...], name='input')
_y = tf.placeholder_with_default(truth, shape-[...], name='label')
net = Model(_x)
net.set_loss(_y)
optimizer = tf...(net.loss)
然后在推理过程中,
loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
new_saver = tf.train.import_meta_graph('ckpt-20000.meta')
new_saver.restore(sess, 'ckpt-20000')
# Get the tensors by their variable name
input = loaded_graph.get_tensor_by_name('input:0')
logits = loaded_graph.get_tensor_by_name(...)
# Now you can feed the inputs to your tensors
lgt = sess.run(logits, feed_dict = {input:img})
在上面的例子中,如果你不提供输入,那么输入将从 TFRecord 输入管道
读取.
In the above example, if you don't feed input, then the input will be read from the TFRecord input pipeline
.
这篇关于在 Tensorflow 中,如果元图使用 TFRecord 输入(没有占位符),如何使用恢复的元图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!