I am trying to compute gradients of one of CNN filters from VGG16 w.r.t an image input using tensorflow-gpu version 2.4.1 and Keras version 2.4.3 with the following code:
from keras.applications import VGG16
from keras import backend as K
model = VGG16(weights = 'imagenet',
include_top = False)
layer_name = 'block3_conv1'
filter_index = 0
layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])
grads = K.gradients(loss, model.input)[0]
this results in the following error:
RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
Also trying to use tf.GradientTape raised another error:
with tf.GradientTape() as gtape:
grads = gtape.gradient(loss, model.input)
AttributeError: 'KerasTensor' object has no attribute '_id'
trying to disable eager execution did not work either:
tf.compat.v1.disable_eager_execution()
since it returns gradients as None. I would appreciate any kind of information about any way to resolve this issue. Thanks in advance.