【问题标题】:How to pass images through TensorFlow-Slim VGG Pre-Trained Net in Batches?如何通过 TensorFlow-Slim VGG 预训练网络批量传递图像?
【发布时间】:2017-12-06 02:19:30
【问题描述】:

我想通过网络传递图像以完成迁移学习任务。在以下代码中,我正在构建图形,然后获取全连接层的输出。我想批量获取输出,因为我有一个包含超过 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 参考,但没有找到答案。

【问题讨论】:

    标签: python tensorflow tf-slim vgg-net


    【解决方案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})
    

    【讨论】:

    • 我在创建保护程序对象时得到ValueError: No variables to save。看起来它在提供占位符而不是图像本身时无法构建图形。我正在运行 tensorflow v1.2.0。
    • 感谢您的示例。我注意到我在图形范围之外创建了 image 占位符。这就是为什么 Saver 没有找到任何变量的原因。将其移入内部解决了问题。
    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 2017-04-02
    • 2017-06-15
    • 2017-01-19
    • 2016-05-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多