GradCam
Gradient-based class activation maps for image classifiers.
Grad-CAM backpropagates the score of the predicted class to the last convolutional layer and weights its activation maps by the averaged gradients, producing a heatmap of the image regions that most influenced the prediction. This is a white-box method: it requires a torch module with a convolutional backbone, so it works with all DashAI image classifiers except the MLP (use Occlusion Saliency there instead).
Implemented on top of the pytorch-grad-cam library.
References
- [1] Selvaraju, R.R. et al. (2017). "Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization." ICCV 2017. https://arxiv.org/abs/1610.02391
- [2] https://github.com/jacobgil/pytorch-grad-cam
Parameters
- method : string, default=
gradcam - CAM variant: 'gradcam' (original), 'gradcam++' (better for multiple occurrences of a class) or 'eigencam' (gradient-free, first principal component of activations).
Methods
explain_instance(self, instances)
GradCamCompute a class activation map for each image.
Parameters
- instances : DashAIDataset
- Images to be explained; the first column must contain images.
Returns
- dict
- Dictionary with, for each image, the resized image, the CAM heatmap and the model prediction.
fit(self, background_dataset, **kwargs)
GradCamStore class names in the model's class-index order.
Parameters
- background_dataset : Tuple[DatasetDict, DatasetDict]
- Tuple
(x, y)with the dataset splits. - **kwargs : Any
- Ignored; present for interface compatibility.
Returns
- GradCam
- The fitted explainer instance (
self).
plot(self, explanation: dict) -> List[DashAI.back.core.artifacts.GroupedArtifacts]
GradCamRender each image as a heatmap overlay plus a text summary.
Parameters
- explanation : dict
- Dictionary with the explanation generated by the explainer.
Returns
- List[GroupedArtifacts]
- A single grouped artifact with one group per explained image, each holding that image's heatmap overlay and text summary.
get_schema(cls) -> dict
ConfigObjectGenerates the component related Json Schema.
Returns
- dict
- Dictionary representing the Json Schema of the component.
validate_and_transform(self, raw_data: dict) -> dict
ConfigObjectIt takes the data given by the user to initialize the model and returns it with all the objects that the model needs to work.
Parameters
- raw_data : dict
- A dictionary with the data provided by the user to initialize the model.
Returns
- dict
- A validated dictionary with the necessary objects.