gradCAM
Syntax
Description
returns the gradient-weighted class activation mapping (Grad-CAM) map of the change in the
classification score of input scoreMap
= gradCAM(net
,X
,classIdx
)X
, when the network
net
evaluates the class score for the class given by
classIdx
. Use this function to explain network predictions and
check that your network is focusing on the right parts of the data.
The Grad-CAM interpretability technique uses the gradients of the classification score with respect to the final convolutional feature map. The parts of an observation with a large value for the Grad-CAM map are those that most impact the network score for that class.
Use this syntax to compute the Grad-CAM map for classification tasks.
returns the Grad-CAM importance map using a reduction function.
scoreMap
= gradCAM(net
,X
,reductionFcn
)reductionFcn
is a function handle that reduces the output activations
of the reduction layer to a scalar value. This scalar fulfills the role of the class score
for classification tasks, and generalizes the Grad-CAM technique to nonclassification
tasks, such as regression.
The gradCAM
function computes the Grad-CAM map by differentiating
the reduced output of the reduction layer with respect to the features in the feature
layer. gradCAM
automatically selects reduction and feature layers to
use when computing the map. To specify these layers, use the
'ReductionLayer'
and 'FeatureLayer'
name-value
arguments.
Use this syntax to compute the Grad-CAM map for nonclassification tasks.
[
also returns the names of the feature layer and reduction layer used to compute the
Grad-CAM map. Use this syntax with any of the input-argument combinations in previous
syntaxes. scoreMap
,featureLayer
,reductionLayer
] = gradCAM(___)
___ = gradCAM(___,
specifies options using one or more name-value arguments in addition to the input
arguments in previous syntaxes. For example, Name,Value
)ReductionLayer="prob"
sets
the reduction layer to the net
layer named "prob"
.
Examples
Input Arguments
Output Arguments
More About
Tips
The
reductionFcn
function receives the output from the reduction layer as a traceddlarray
object. The function must reduce this output to a scalardlarray
, whichgradCAM
then differentiates with respect to the activations of the feature layer. For example, to compute the Grad-CAM map for channel 208 of the softmax activations of a network, the reduction function is@(x)(x(208))
. This function receives the activations and extracts the 208th channel.The
gradCAM
function automatically chooses reduction and feature layers to use when computing the Grad-CAM map. For some networks, the chosen layers might not be suitable. For example, if your network has multiple layers that can be used as the feature layer, then the function chooses one of those layers, but its choice might not be the most suitable. For such networks, specify which feature layer to use using theFeatureLayer
name-value argument.To display the Grad-CAM map as an overlay on a grayscale image, you must first rescale the map to be in the range [0, 1]. For example,
scoreMapRescaled = rescale(scoreMap);
.
References
[1] Kudo, Mineichi, Jun Toyama, and Masaru Shimbo. “Multidimensional Curve Classification Using Passing-through Regions.” Pattern Recognition Letters 20, no. 11–13 (November 1999): 1103–11. https://doi.org/10.1016/S0167-8655(99)00077-X.
[2] UCI Machine Learning Repository: Japanese Vowels Dataset. https://archive.ics.uci.edu/ml/datasets/Japanese+Vowels.
[3] Selvaraju, Ramprasaath R., Michael Cogswell, Abhishek Das, Ramakrishna Vedantam, Devi Parikh, and Dhruv Batra. “Grad-CAM: Visual Explanations from Deep Networks via Gradient-Based Localization.” 2017 (October 2017): 618–626, https://doi.org/10.1109/ICCV.2017.74.
[4] Vinogradova, Kira, Alexandr Dibrov, and Gene Myers. “Towards Interpretable Semantic Segmentation via Gradient-Weighted Class Activation Mapping.” Proceedings of the AAAI Conference on Artificial Intelligence 34, no. 10 (April 2020): 13943–13944, https://doi.org/10.1609/aaai.v34i10.7244.
Extended Capabilities
Version History
Introduced in R2021aSee Also
dlnetwork
| testnet
| minibatchpredict
| scores2label
| predict
| forward
| occlusionSensitivity
| imageLIME
Topics
- Grad-CAM Reveals the Why Behind Deep Learning Decisions
- Explore Semantic Segmentation Network Using Grad-CAM
- Explore Quantized Semantic Segmentation Network Using Grad-CAM
- Understand Network Predictions Using LIME
- Understand Network Predictions Using Occlusion
- Multilabel Image Classification Using Deep Learning