如何通过TensorFlow-Slim VGG预训练网络批量传递图像?

时间:2017-07-02 20:33:36

标签: python tensorflow tf-slim vgg-net

我想通过网络传递图像以进行传输学习任务。在下面的代码中,我构建图形,然后获得完全连接层的输出。我想分批获得输出,因为我有一个超过20k图像的数组。

vgg.vgg_16(images)要求images成为一组图片。我尝试输入一个输入占位符(在查看docs之后),但在加载检查点时出现错误There are no variables to save

我可以一次提供vgg.vgg_16(images)几张图片,但我需要为每批加载检查点。我很确定有更好的方法可以做到这一点。有什么例子或参考我可以看一下吗?

from tensorflow.contrib import slim
from tensorflow.contrib.slim.nets import vgg

images = np.array(read_images(val_filenames[:4], 224, 224), dtype=np.float32) # load images and resize to 224 x 224


vgg_graph = tf.Graph()

with vgg_graph.as_default():
    with slim.arg_scope(vgg.vgg_arg_scope()):
        outputs, end_points = vgg.vgg_16(images, is_training=False)

    fc6 = end_points['vgg_16/fc6']


with tf.Session(graph=vgg_graph) as sess:
    saver = tf.train.Saver()
    saver.restore(sess, 'checkpoints/vgg_16.ckpt')

    # pass images through the network
    fc6_output = sess.run(fc6)

我还尝试过thisthis次参考,但我找不到答案。

1 个答案:

答案 0 :(得分:1)

您可以创建一个placeholder,然后将其传递给vgg网络。将您的代码更改为:

images = tf.placeholder(tf.float32, shape=[batch_size, height, width, channels])

with slim.arg_scope(vgg.vgg_arg_scope()):
    outputs, end_points = vgg.vgg_16(images, is_training=False)

在训练期间,将输入传递给网络:

fc6_output = sess.run(fc6, feed_dict={images:batch_images})