微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在获取 MobileNet 的热图时将梯度设为“无”

如何解决为什么在获取 MobileNet 的热图时将梯度设为“无”

我在 MobileNet 模型中添加一个注意力层,如下所示。

mobile = tf.keras.applications.mobilenet.MobileNet(weights='imagenet')

x = mobile.layers[-6].input

if True:
    x = Reshape([7*7,1024])(x)
    att = MultiHeadsAttModel(l=7*7,d=1024,dv=64,dout=1024,nv = 16 )
    x = att([x,x,x])
    x = Reshape([7,7,1024])(x)   
    x = Batchnormalization()(x)

x = mobile.get_layer('global_average_pooling2d')(x)
x = mobile.get_layer('reshape_1')(x)
x = mobile.get_layer('dropout')(x)
x = mobile.get_layer('conv_preds')(x)
x = mobile.get_layer('reshape_2')(x)
output = Dense(units=50,activation='softmax')(x)

model = Model(inputs=mobile.input,outputs=output)

for layer in model.layers[:-23]:
    layer.trainable = False

但是当我获得热图时,它给了我梯度为“无”。在这里,我应该将哪一层作为“last_conv_layer”?我需要改变注意力层的位置吗?添加它的最佳位置是什么?

with tf.GradientTape(persistent=True) as gtape:
    last_conv_layer = model.get_layer('conv_preds')
    iterate = tf.keras.models.Model([model.inputs],[model.output,last_conv_layer.output])
    model_out,last_conv_layer = iterate(img_tensor)
    class_out = model_out[:,np.argmax(model_out[0])]
    grads = gtape.gradient(class_out,last_conv_layer)
    print(grads)

输出

 WARNING:tensorflow:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape,leading to increased cpu and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives.
None
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。