Search code examples
pythontensorflowtf-slimvgg-net

How to pass images through TensorFlow-Slim VGG Pre-Trained Net in Batches?


I want to pass the images through the network for a transfer learning task. In the following code I'm building the graph and then getting the outputs of a fully connected layer. I wanted to get the outputs in batches because I have an array with more than 20k images.

The vgg.vgg_16(images) required images to be an array of images. I tried feeding an input placeholder (after looking at the docs) but when loading the checkpoint I got an error There are no variables to save.

I can feed vgg.vgg_16(images) a few images at a time but I would need to load the checkpoint for each batch. I'm pretty sure there is a better way to do that. Is there any examples or references I can look at?

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)

I also tried this and this references but I didn't find the answers.


Solution

  • You can create a placeholder that you can pass it to vgg network. Change your code to:

    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)
    

    and during training, pass the input to the network:

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