Tensorflow:将恒定张量从预训练的Vgg模型转换为变量 [英] Tensorflow: Convert constant tensor from pre-trained Vgg model to variable

查看:115
本文介绍了Tensorflow:将恒定张量从预训练的Vgg模型转换为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何将从预训练的Vgg16模型加载的恒定张量转换为tf.Variable张量?这样做的动机是我需要计算相对于Conv4_3层内核的特定损耗的梯度,但是,该内核似乎设置为tf.Constant类型,并且未被tf.Optimizer.compute_gradients方法接受.

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)

我尝试过的是使用tf.assign方法将内核更新为初始值设置为原始内核的变量类型张量,但是它给出了TypeError: Input 'ref' of 'Assign' Op requires l-value input

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!

更新:我根据预训练的Vgg16 Tensorflow模型下载了预训练的模型,然后通过以下方式加载模型:

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() 

以上所有代码均在名为vgg的类中定义.

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

推荐答案

未从预训练模型中获取变量的原因可以在

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.

我推荐此vgg模型.

这篇关于Tensorflow:将恒定张量从预训练的Vgg模型转换为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆