TensorFlow:使用不同的输入张量重新运行网络? [英] TensorFlow: Rerun network with a different input tensor?

查看:65
本文介绍了TensorFlow:使用不同的输入张量重新运行网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 TensorFlow 中有一个典型的 CNN 模型.

Suppose I have a typical CNN model in TensorFlow.

def inference(images):
    # images: 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    conv_1 = conv_layer(images, 64, 7, 2)
    pool_2 = pooling_layer(conv_1, 2, 2)
    conv_3 = conv_layer(pool_2, 192, 3, 1)
    pool_4 = pooling_layer(conv_3, 2, 2)
    ...
    conv_28 = conv_layer(conv_27, 1024, 3, 1)
    fc_29 = fc_layer(conv_28, 512)
    fc_30 = fc_layer(fc_29, 4096)
    return fc_30

典型的前传可以这样完成:

A typical forward pass could be done like this:

images = input()
logits = inference(images)
output = sess.run([logits])

现在假设我的 input 函数现在返回一对参数,left_imagesright_images(立体相机).我想运行 right_imagesconv_28left_imagesfc_30.所以像这样

Now suppose my input function now returns a pair of arguments, left_images and right_images (stereo camera). I want to run right_images up to conv_28 and left_images up to fc_30. So something like this

images = tf.placeholder(tf.float32, [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3])
left_images, right_images = input()
conv_28, fc_30 = inference(images)
right_images_val = sess.run([conv_28], feed_dict={images: right_images})
left_images_val = sess.run([fc_30], feed_dict={images: left_images})

然而这失败了

TypeError:提要的值不能是 tf.Tensor 对象.可接受的提要值包括 Python 标量、字符串、列表或numpy ndarrays.

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

我想避免必须评估 inputs 然后将其反馈给 TensorFlow.使用不同的参数两次调用 inference 也将不起作用,因为像 conv_layer 这样的函数会创建变量.

I want to avoid having to evaluate inputs to then feed it back to TensorFlow. Calling inference twice with different arguments will also not work because functions like conv_layer create variables.

是否可以使用不同的输入张量重新运行网络?

Is it possible to rerun the network with a different input tensor?

推荐答案

Tensorflow 共享变量 正是您要找的.在推理中将 tf.Variable 的所有调用替换为 tf.get_variable().然后你可以运行:

Tensorflow shared Variables is what you are looking for. Replace all calls of tf.Variable with tf.get_variable() in inference. Then you can run:

images_left, images_right = input()
with tf.variable_scope("logits") as scope:
    logits_left = inference(images_left)
    scope.reuse_variables()
    logits_right = inference(images_right)
output = sess.run([logits_left, logits_right])

在第二次推理调用中不会再次创建变量.使用相同的权重处理左右图像.另请查看我的 Tensorflow CNN 培训工具包(查看 训练 代码).我利用这种技术在同一个 TensorFlow 图中运行验证和训练.

Variables are not created again in the second call of inference. Left and right images are processed using the same weights. Also check out my Tensorflow CNN training toolkit (Look at training code). I utilize this technique to run validation and training forwards in the same TensorFlow graph.

这篇关于TensorFlow:使用不同的输入张量重新运行网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆