1

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.

stop-cran
  • 4,229
  • 2
  • 30
  • 47
Thiago
  • 652
  • 1
  • 9
  • 29

1 Answers1

1

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})
Vijay Mariappan
  • 16,921
  • 3
  • 40
  • 59
  • I get `ValueError: No variables to save` when creating the saver object. Looks like it is not able to build the graph when feeding a placeholder instead of the images themselves. I'm running tensorflow v1.2.0. – Thiago Jul 02 '17 at 23:11
  • 1
    check the file here: https://drive.google.com/open?id=0BwoPpNIm4bJqcUR5MTIxV1pKa28 – Vijay Mariappan Jul 02 '17 at 23:17
  • Thanks for the example. I noticed that I was creating the `image` placeholder outside the graph scope. That's why the Saver was not finding any variables. Moving it inside fixed the problem. – Thiago Jul 02 '17 at 23:36