Gotcha! TF Saver Subset Initialization

While working on the first part of my style-transfer project, I dealt with two main variable groups:

  • The input variable which was the image I was optimizing.
  • The VGG-19 network, whose weights were frozen.

To get both of these interacting properly and functioning correctly within a Tensorflow (TF) environment, the input variable would need to be initialized to an initial value while the vgg19 network would need to be initialized with pre-trained weights.

In the past, I have either initialized the entire network with pre-trained weights or with new, initial values but not a combination. I knew that a TF Saver object would be used to restored the weights like so:

saver.restore(sess, 'vgg_19.ckpt')

Where sess refers to a TF Session and the filepath points to the checkpoint file. Since I had a new input variable that didn’t correspond to one of the pre-trained weights, I assumed that the Saver would skip restoring this and rely on me to initialize it myself later. I was wrong:

NotFoundError: Tensor name "input_var" not found in checkpoint files vgg_19.ckpt

The Saver noted down all the variables present in the graph and expected to find all those variables in the checkpoint. When it didn’t, it complained. I found some guidance using this link which helped me better understand the var_list parameter of the Saver initialization method. This parameter specificied which variables would be saved and restored. The former was a lot easier to grasp than the latter. It struck me as a little counterintutive that in initializing the Saver object, I would be specifying up-front which variables I would be restoring. Having thought about it for awhile now and tinkering with it, it makes a little more sense to me. Or perhaps I’ve just got used to this quirk.

So given my new knowledge, I knew that I had to specify which variables would be loaded from the checkpoint (all the vgg19 variables) and which would not be the responsiblity of the Saver (the new input variable). To get the names of all the variables present in the graph, I used the following:

all_variables = tf.get_collection_ref(tf.GraphKeys.GLOBAL_VARIABLES)

Printing that out, I could see that the first variable was the new input variable and so all_variables[1:] provided me with all the vgg19 variables. So the Saver would be created as such:

saver = tf.train.Saver(var_list=all_variables[1:])

Running the restore method of this object caused no errors!

In my graph, the input variable was at the very start so I could specify the var_list the way I did. Perhaps in yours, the new variable might exist somewhere else in the list or maybe you even have several variables you want the Saver to ignore. In that case, just have a look at all_variables and extract the necessary variables. Maybe you could write a for loop to go through the list and ignore variables with certain names.

So when mixing around new and pre-trained variables, be sure create the Saver object appropriately. If you’d like to play around more, here’s a notebook for you to tinker with!

2020

Guns Germs And Steel

46 minute read

I’ve never been a big history fan. Too many names and dates to memorize. But now, free from the pressure of having to learn for the sake of getting good grad...

Letters From A Stoic

42 minute read

A little while ago, I read Letters from a Stoic by Seneca (translated and edited by Robin Campbell). I got a lot out of reading Letters and wanted to encoura...

Every Tool Is A Hammer

21 minute read

I recently finished reading Every Tool’s a Hammer: Life Is What You Make It1 by Adam Savage. It was an energizing read and I highly recommend this book to fe...

Back to top ↑

2019

Culture Series

46 minute read

Recently, I binged through the Culture series by Ian M. Banks. It was an amazing read and I thought a write up about it might help ground the experience and ...

Learning to Learn

25 minute read

I recently the following on Coursera: Learning How to Learn: Powerful mental tools to help you master tough subjects Mindshift: Break Through Obstacles ...

Back to top ↑

2018

Style Transfer

4 minute read

If you’re reading this, I’m assuming that you’ve read the paper Image Style Transfer Using Convolutional Neural Networks and have some familiarity with it.

Gotcha! Tensor Shape

1 minute read

While working on the second part of my style-transfer project, I needed to obtain the shape of a tensor. I decided to try using the tf.shape function.

Style Reconstruction

11 minute read

If you’re reading this, I’m assuming that you’ve read the paper Image Style Transfer Using Convolutional Neural Networks and have some familiarity with it.

Gotcha! TF Variable Initialization Order

2 minute read

While working on the first part of my style-transfer project, I had: A new input variable which would have to be initialized from scratch. The VGG-19 ne...

Gotcha! TF Input Data Type

1 minute read

While working on the first part of my style-transfer project, I found out the hard way that TF is very sensitive to the network’s input’s data type.

Gotcha! TF Saver Subset Initialization

2 minute read

While working on the first part of my style-transfer project, I dealt with two main variable groups: The input variable which was the image I was optimizi...

Gotcha! Pyplot Image Displays

3 minute read

While working on the first part of my style-transfer project, I used pyplot’s imshow to diplay images in the notebook. However, it took me a little bit of pl...

Gotcha! CV2 and Pyplot Channel Order

less than 1 minute read

While working on the first part of my style-transfer project, I used Open CV’s cv2.imwrite to save images to disk. However, the images seemed to have a weird...

Gotcha! CV2 JPEG vs PNG

1 minute read

While working on the first part of my style-transfer project, I ran into lots of image issues. One of the issues was that cv2 uses a BGR channel order inste...

Content Reconstruction

12 minute read

If you’re reading this, I’m assuming that you’ve read the paper Image Style Transfer Using Convolutional Neural Networks and have some familiarity with it.

First Post

less than 1 minute read

Cue customary Hello World.

Back to top ↑