Error in computing gradients in keras(tensorflow backend)

Viewed 901

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.

1 Answers

Let layer = model.get_layer(layer_name)

First, you need to construct the model graph

from tensorflow.keras import models
heatmap_model = models.Model([model.inputs],[layer.output,model.output])

Then you need to run tf.GradientTape()

with tf.GradientTape() as gtape:
  layer_output, predictions = heatmap_model(img)
  loss = predictions[:,np.argmax(predictions)] 
  grads = gtape.gradient(loss, layer_output)

Note that gtape.gradient has layer_output instead of layer.output

Related