keisen / tf-keras-vis

Neural network visualization toolkit for tf.keras
https://keisen.github.io/tf-keras-vis-docs/
MIT License
311 stars 45 forks source link

Gradcam does not work with keras.layers.convolutional.Conv layers #62

Closed ShawnHymel closed 3 years ago

ShawnHymel commented 3 years ago

I have the following model created and trained with Keras:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 28, 28, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 14, 14, 16)        4624      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 16)          0         
_________________________________________________________________
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dropout (Dropout)            (None, 784)               0         
_________________________________________________________________
y_pred (Dense)               (None, 5)                 3925      
=================================================================
Total params: 8,869
Trainable params: 8,869
Non-trainable params: 0
_________________________________________________________________

When I print out the layers, I get the following:

<keras.layers.convolutional.Conv2D object at 0x7f30f8d9e310>
<keras.layers.pooling.MaxPooling2D object at 0x7f30f8bbfdd0>
<keras.layers.convolutional.Conv2D object at 0x7f30f8f5edd0>
<keras.layers.pooling.MaxPooling2D object at 0x7f30f8bddf50>
<keras.layers.core.Flatten object at 0x7f30f8be2b90>
<keras.layers.core.Dropout object at 0x7f30f8be8250>
<keras.layers.core.Dense object at 0x7f30f8be8bd0>

I try to run the following:

def model_modifier(cloned_model):
  cloned_model.layers[-1].activation = activations.linear
  return cloned_model

gradcam = Gradcam(model, model_modifier=model_modifier, clone=False)
cams = gradcam(score, images, seek_penultimate_conv_layer=True)

I get the following error:

ValueError: ('Unable to determine penultimate `Conv` layer. `penultimate_layer`=', -1)

From what I can tell, Gradcam is attempting to look for tensorflow.python.keras.layers.convolutional.Conv layers as opposed to keras.layers.convolutional.Conv layers (these are apparently different types of objects from what I can tell).

Even if I call:

cams = gradcam(score, images, seek_penultimate_conv_layer=False, penultimate_layer=2)

The cams variable comes back as all 0s. This does not seem right. Any help would be appreciated.

keisen commented 3 years ago

Hi @ShawnHymel .

You're using original Keras, aren't you. Unfortunately, as README.md said, tf-keras-vis is for debugging tf.keras models in Tensorflow2.0+, not original Keras.

I recommend you to use tf.keras or , if you're using tensorflow1.x, you can use keras-vis!

Thanks!

ShawnHymel commented 3 years ago

Ah, makes sense, thank you. The network was trained with Edge Impulse, which appears to use Keras (not tf.keras), so I don't think I'll be able to use tf-keras-vis for this particular project. Thank you for putting together a great visualization library, though!