Fix penultimate layer search condition #85
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi there!
Currently the last
Conv
layer is being automatically used for GradCAM if not specified differently. We noticed that for some models like MobileNetV3 this results in the wrong layer being used (as already mentioned in #61).Specifically to MobileNetV3 this has some problems:
Conv2D
layer causing this layer with shape(None, 1, 1, 1024)
to be selected as the penultimate layer. Obviously this will result in incorrect/useless GradCAM images.Conv
layer manually however does not include some important activations which occasionally causes inverted GradCAM images (see GradCam Issues with MobileNetV3 #61).We propose to use a different search condition which searches for the last layer with four dimensions and a width and height of more than 1. This will help both problems mentioned above:
Conv
layer anymore resulting in non-inverted GradCAM images.A similar implementation is being used in sicara/tf-explain. Their implementation would however still be affected by the first problem.
I added some quick tests which could be extended in the future.
Let me know what you think!