keisen / tf-keras-vis

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

Error when using custom convolutional layer #4

Closed pir127 closed 4 years ago

pir127 commented 4 years ago

Hello,

I have a CNN with custom convolutional layers and the name of those layers are not instances of tensorflow.python.keras.layers.convolutional

Therefore, the function _find_penultimate_output always returns None and raises en Error. I had to comment out a few lines in _find_penultimate_output to be able to obtain a grad-CAM, those lines:

if layer is not None:
            layer = find_layer(model, lambda l: isinstance(l, Conv), offset=layer)

Not a clean solution but it can help if someone else has this issue, and would be great if you could release an official fix for custom convolutional layers.

And by the way, would also be more than great to have an option to return the non-interpolated grad-CAM as for custom convolutional layers the interpolation may be special.

Thanks so much !

keisen commented 4 years ago

Thank you for your error reporting! As you said, the problem should be fixed.

I believe that Gradcam#__call__ should be improved to have seek_penultimate_layer argument that is a flag. When seek_penultimate_layer is False, _find_penultimate_output would return the output that is indicated by penultimate_layer argument. That is, you won't need to comment out above codes.

class Gradcam(ModelVisualization):
    def __call__(self,
                 loss,
                 seed_input,
                 penultimate_layer=-1,
                 seek_penultimate_layer=True,
                 activation_modifier=lambda cam: K.relu(cam),
                 normalize_gradient=True):

What do you think about this plan?

keisen commented 4 years ago

would also be more than great to have an option to return the non-interpolated grad-CAM as for custom convolutional layers the interpolation may be special.

I couldn't understand the non-interpolated grad-CAM . Could you be more specific?

Thanks!

pir127 commented 4 years ago

Hi Keisen,

I just meant that it could be useful in some very specific cases to return the grad-CAM vectors before they undergo the interpolation step (Line 68 of gradcam.py).

Thanks for the fix, and thanks so much for sharing this code.

keisen commented 4 years ago

I got it. I'll solve it that was opened issues/#8.