-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac1356d
commit 974a934
Showing
9 changed files
with
39 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ Gradient | |
InputTimesGradient | ||
SmoothGrad | ||
IntegratedGradients | ||
GradCAM | ||
``` | ||
|
||
# Input augmentations | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Gradient methods: | ||
const REF_SMILKOV_SMOOTHGRAD = "Smilkov et al., *SmoothGrad: removing noise by adding noise*" | ||
const REF_SUNDARARAJAN_AXIOMATIC = "Sundararajan et al., *Axiomatic Attribution for Deep Networks*" | ||
const REF_SELVARAJU_GRADCAM = "Selvaraju et al., *Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
GradCAM(feature_layers, adaptation_layers) | ||
Calculates the Gradient-weighted Class Activation Map (GradCAM). | ||
GradCAM provides a visual explanation of the regions with significant neuron importance for the model's classification decision. | ||
# Parameters | ||
- `feature_layers`: The layers of a convolutional neural network (CNN) responsible for extracting feature maps. | ||
- `adaptation_layers`: The layers of the CNN used for adaptation and classification. | ||
# Note | ||
Flux is not required for GradCAM. | ||
GradCAM is compatible with a wide variety of CNN model-families. | ||
# References | ||
- $REF_SELVARAJU_GRADCAM | ||
""" | ||
struct GradCAM{F,A} <: AbstractXAIMethod | ||
feature_layers::F | ||
adaptation_layers::A | ||
end | ||
function (analyzer::GradCAM)(input, ns::AbstractNeuronSelector) | ||
A = analyzer.feature_layers(input) # feature map | ||
feature_map_size = size(A, 1) * size(A, 2) | ||
|
||
# Determine neuron importance αₖᶜ = 1/Z * ∑ᵢ ∑ⱼ ∂yᶜ / ∂Aᵢⱼᵏ | ||
grad, output, output_indices = gradient_wrt_input(analyzer.adaptation_layers, A, ns) | ||
αᶜ = sum(grad; dims=(1, 2)) / feature_map_size | ||
Lᶜ = max.(sum(αᶜ .* A; dims=3), 0) | ||
return Explanation(Lᶜ, output, output_indices, :GradCAM, :cam, nothing) | ||
end |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters