3

My question is how can I convert a constant tensor loaded from a pre-trained Vgg16 model to a tf.Variable tensor? The motivation is that I need to compute the gradient of a specific loss with respect to the Conv4_3 layers' kernel, however, the kernel were seems set to a tf.Constant type and it is not accepted by tf.Optimizer.compute_gradients method.

F = vgg.graph.get_tensor_by_name('pretrained_vgg16/conv4_3/filter:0')
G = optimizer.compute_gradients(losses, var_list=[F])

# TypeError: Argument is not a tf.Variable: Tensor("pretrained_vgg16/conv4_3/filter:0", shape=(3, 3, 512, 512), dtype=float32)

What I have tried is to use tf.assign method to update the kernel to a variable type tensor with initial value set to be the original kernel, but it gives a TypeError: Input 'ref' of 'Assign' Op requires l-value input

F = tf.assign(F, tf.Variable(F, trainable=False))

So, how can I achieve that? Many thanks in advance!

Update: I download the pretrained model according to Pretrained Vgg16 Tensorflow model and then I loaded the model by:

with open('vgg16.tfmodel', mode='rb') as f:
    fileContent = f.read()

graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)

# Map input tensor
inputs = tf.placeholder("float", [1, 224, 224, 3], name='inputs')
tf.import_graph_def(graph_def, input_map={ "images": inputs }, name='pretrained_vgg16')

graph = tf.get_default_graph() 

All the code above is defined in a class named vgg.

Xer
  • 493
  • 1
  • 6
  • 17
  • Could you please describe the way you load pre-trained model? Maybe post the code of conv4_3 would be helpful. – Da Tong Oct 16 '16 at 15:06
  • Yes, of course, and I have updated the post. However I actually did not implement the conv4_3 layer, it comes with the 'vgg16.tfmodel' file. – Xer Oct 16 '16 at 15:33

1 Answers1

3

The reason why you did not get variables from the pre-trained model could be explained in this answer. Briefly, tf.import_graph_def just restore the structure of a graph, without the variables.

A solution to this is to build the model yourself, with same variable name to the pre-trained model. Then load pre-trained model and assign every variable with specific parameter.

I recommend this vgg model.

Community
  • 1
  • 1
Da Tong
  • 2,018
  • 18
  • 25