在keras中实现Grad Cam时出现未连接渐变错误



我正在尝试创建一个热图,显示我的CNN正在寻找的位置,以便对图像进行分类。对于分类任务,我在故障零件和非故障零件之间做出决定(因此只有二进制分类(。我试图复制这个代码。然而,我看到的是,他们使用了整个《盗梦空间》网络,而没有改变顶层。我现在的问题是,我不知道如何正确连接层,这样我就可以使用梯度函数来反向传播从我的模型末尾(有一个神经元的密集层(到初始网络中最后一个卷积层("mixed10"(的损失。到目前为止,我收到了一个断言错误,其中包含未连接梯度的消息

我训练的模型:

def create_model():
model_inception= InceptionV3(include_top=False, weights='imagenet',input_shape=(299,299,3))
model_inception.trainable=False
model = Sequential()
model.add(model_inception)
model.add(GlobalAveragePooling2D())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy', metrics=['accuracy'])
return model

型号摘要

Grad_CAM代码:

layer_name = 'mixed10'
image = np.expand_dims(X_valid[0], 0)
#input layter to inception
input_lay = model.get_layer(index=0).layers[0].input
#Heatmap creatd from ConvLayer
conv_output_lay = model.get_layer(index=0).get_layer(layer_name).output
#Output Layer of the network
output_lay = model.get_layer(index=-1).output
#Connect conv_output with model.input
incept_part_till_conv = Model(input_lay,conv_output_lay)
conv_output = incept_part_till_conv(model.input)
gradModel = Model(
inputs=[model.input],
outputs=[conv_output, 
model.output])
# record operations for automatic differentiation
with tf.GradientTape() as tape:
# cast the image tensor to a float-32 data type, pass the
# image through the gradient model, and grab the loss
# associated with the specific class index
inputs = tf.cast(image, tf.float32)
(convOutputs, predictions) = gradModel(inputs)
loss = predictions[:]
# use automatic differentiation to compute the gradients
grads = tape.gradient(loss, convOutputs)

然后我收到错误消息。如果有人能给我一些如何让它发挥作用的建议,那将是非常棒的。谢谢

唯一让我印象深刻的是,您需要计算磁带范围之外的梯度(就像他们在示例中所做的那样(:

# record operations for automatic differentiation
with tf.GradientTape() as tape:
# cast the image tensor to a float-32 data type, pass the
# image through the gradient model, and grab the loss
# associated with the specific class index
inputs = tf.cast(image, tf.float32)
(convOutputs, predictions) = gradModel(inputs)
loss = predictions[:]
# use automatic differentiation to compute the gradients
grads = tape.gradient(loss, convOutputs)

最新更新