假设我在TensorFlow中有一个典型的CNN模型.
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
典型的前向传球可以这样做:
images = input()
logits = inference(images)
output = sess.run([logits])
现在假设我的输入函数现在返回一对参数,left_images和right_images(立体相机).我想将right_images运行到conv_28,将left_images运行到fc_30.所以这样的事情
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: The value of a feed cannot be a tf.Tensor object.
Acceptable feed values include Python scalars, strings, lists, or
numpy ndarrays.
我想避免评估输入然后将其反馈给TensorFlow.使用不同的参数调用两次推理也不起作用,因为像conv_layer这样的函数会创建变量.
是否可以使用不同的输入张量重新运行网络?
最佳答案
Tensorflow shared Variables正是您要找的.在推理中用tf.get_variable()替换tf.Variable的所有调用.然后你可以运行:
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 training toolkit(查看training代码).我利用这种技术在同一TensorFlow图中运行验证和训练.