Saltar al contenido principal

GradCam

LocalExplainer
DashAI.back.explainability.explainers.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

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)

Defined on GradCam

Compute 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)

Defined on GradCam

Store 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]

Defined on GradCam

Render 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

Defined on ConfigObject

Generates the component related Json Schema.

Returns

dict
Dictionary representing the Json Schema of the component.

validate_and_transform(self, raw_data: dict) -> dict

Defined on ConfigObject

It 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.