我用TFRecord输入管道训练了一个网络.换句话说,没有占位符.简单的例子是:
input, truth = _get_next_batch() # TFRecord. `input` is not a tf.placeholder
net = Model(input)
net.set_loss(truth)
optimizer = tf...(net.loss)
Run Code Online (Sandbox Code Playgroud)
比方说,我获得了三个文件,ckpt-20000.meta,ckpt-20000.data-0000-of-0001,ckpt-20000.index.我明白了,以后可以使用该.meta文件导入元图并访问张量,例如:
new_saver = tf.train.import_meta_graph('ckpt-20000.meta')
new_saver.restore(sess, 'ckpt-20000')
logits = tf.get_collection("logits")[0]
Run Code Online (Sandbox Code Playgroud)
但是,元图在管道中从一开始就没有占位符.有没有办法可以使用元图和输入的查询推断?
有关信息,在查询应用程序(或脚本)中,我曾经使用占位符和恢复的模型权重定义模型(参见下文).我想知道我是否可以在没有重新定义的情况下使用元图,因为它会更加简单.
input = tf.placeholder(...)
net = Model(input)
tf.restore(sess, 'ckpt-2000')
lgt = sess.run(net.logits, feed_dict = {input:img})
Run Code Online (Sandbox Code Playgroud)
你可以建立一个使用图形placeholder_with_default()的输入,这样可以同时使用TFRecord input pipeline,以及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)
Run Code Online (Sandbox Code Playgroud)
然后在推理期间,
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})
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,如果你不输入输入,那么输入将从中读取TFRecord input pipeline.
| 归档时间: |
|
| 查看次数: |
2347 次 |
| 最近记录: |