sicara / tf-explain

Interpretability Methods for tf.keras models with Tensorflow 2.x
https://tf-explain.readthedocs.io
MIT License
1.02k stars 112 forks source link

Unsupported model architecture for VanillaGradients #182

Open khaledbouabdallah opened 2 years ago

khaledbouabdallah commented 2 years ago

I'm trying to use VanillaGradients for a single image, but I'm getting this warning

UserWarning: Unsupported model architecture for VanillaGradients. The last two layers of the model should be: a layer which computes class scores with no activation, followed by an activation layer.

VanillaGradients call :

img = tf.keras.preprocessing.image.load_img(image_path,target_size=IMG_SHAPE)
img = tf.keras.preprocessing.image.img_to_array(img)
img = np.array(img) / 255

data = ([img], None)

explainer = VanillaGradients()
grid = explainer.explain(data,model,0)

Model Declaration

model = Sequential()
model.add(Conv2D(32 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu' , input_shape = (150,150,3)))
model.add(BatchNormalization())
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.1))
model.add(BatchNormalization())
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))
model.add(BatchNormalization())
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(Conv2D(128 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(Conv2D(256 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(Flatten())
model.add(Dense(units = 128 , activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(units = 1 , activation = 'sigmoid'))

Notes

E0HYL commented 2 years ago

I met the same problem when running with a model where the last layer is Dense with an activation function. I checked the paper of Vinilla Gradients. In Section 2, the authors pointed that

It should be noted that we used the (unnormalised) class scores S_c, rather than the class posteriors, returned by the soft-max layer... The reason is that the maximisation of the class posterior can be achieved by minimising the scores of other classes.

Guess this is the reason why the warning is raised.