我想创建一个自定义层来计算Keras中的梯度。我写了定制层,但是失败了。该概念之后是grad-cam,我打算使用渐变图进行上采样。
代码如下。
from keras import backend as K
from keras.engine.topology import Layer
class grad_cam(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(grad_cam, self).__init__(**kwargs)
def build(self, input_shape):
assert isinstance(input_shape, list)
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[0][1], self.output_dim),
initializer='uniform',
trainable=True)
super(grad_cam, self).build(input_shape)
def call(self, x):
assert isinstance(x, list)
last_layer, conv_layer = x
loss = K.max(last_layer)
grad_wrt_fmap = K.gradients(loss,conv_layer)
return K.dot(grad_wrt_fmap, self.kernel)
def compute_output_shape(self, input_shape):
assert isinstance(input_shape, list)
shape_a, shape_b = input_shape
return shape_b
模型架构:
base_model = DenseNet121(include_top = False, weights='imagenet')
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer
predictions = Dense(len(sequence), activation='sigmoid')(x)
predictions = grad_cam(base_model.output.shape)([predictions, base_model.output])
print(predictions)
model = Model(inputs=base_model.input, outputs=predictions)
model.summary()
但是,我遇到了错误问题。
ValueError: Can't convert Python sequence with mixed types to Tensor.
我不知道如何解决它。请帮我解决!谢谢!