gpt4 book ai didi

Tensorflow:将常量张量从预训练的 Vgg 模型转换为变量

转载 作者:行者123 更新时间:2023-12-04 19:32:18 24 4
gpt4 key购买 nike

我的问题是如何将从预训练 Vgg16 模型加载的常量张量转换为 tf.Variable 张量?动机是我需要计算关于 Conv4_3 层内核的特定损失的梯度,但是,内核似乎设置为 tf.Constant 类型并且它不被 tf.Optimizer.compute_gradients 方法。

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 'Assign' Op 的 'ref' 需要左值输入

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

那么,我该如何实现呢?非常感谢!

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

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的类中。

最佳答案

您没有从预训练模型中获取变量的原因可以在 this answer 中解释。 .简而言之,tf.import_graph_def 只是恢复图的结构,没有变量。

一个解决方案是自己构建模型,使用与预训练模型相同的变量名。然后加载预训练模型并为每个变量分配特定参数。

我推荐this vgg model .

关于Tensorflow:将常量张量从预训练的 Vgg 模型转换为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com