python - 如何通过TensorFlow-Slim VGG Pre-Trained Net批量传递图像?

标签 python tensorflow tf-slim vgg-net

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

vgg.vgg_16(images) 要求 images 是图像数组。我尝试提供输入占位符(查看 docs 后),但在加载检查点时出现错误没有要保存的变量

我可以一次提供 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引用资料,但我没有找到答案。

最佳答案

您可以创建一个占位符,并将其传递到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})

关于python - 如何通过TensorFlow-Slim VGG Pre-Trained Net批量传递图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44874973/

相关文章:

python 用序列设置数组元素时出错

python - tensorflow 评估和提前停止会产生无限溢出错误

tensorflow - 在tf-slim中实现混合精度训练

python - 用于打印输出的合理 python 源代码换行

python - 解析 Openweather API Python

tensorflow - 值错误: Didn't find op for builtin opcode 'RESIZE_BILINEAR' version '3' Registration failed

python - 使用预训练的 Inception_v4 模型

tensorflow - tf-slim 批量规范 : different behaviour between training/inference mode

python - Emacs:Pymacs 不使用 Carbon Emacs 加载 ropemacs

python - 将 Python 生成器解压为参数——内存效率高吗?