Tensorflow: Convert constant tensor from pre-trained Vgg model to variable

核能气质少年 提交于 2020-01-01 16:21:28

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/40071584/tensorflow-convert-constant-tensor-from-pre-trained-vgg-model-to-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!