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.

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.

story(self, explanation: dict, explainer_output: DashAI.back.core.artifacts.ArtifactGroup) -> Optional[DashAI.back.core.utils.MultilingualString]

Defined on GradCam

Describe, in words, the prediction the heatmap overlay explains.

Parameters

explanation : dict
Output of :meth:explain_instance.
explainer_output : ArtifactGroup
The group previously returned by :meth:plot, titled "Image {n}".

Returns

Optional[MultilingualString]
The narrative in every supported language, or None if explainer_output is not a recognised "Image N" group.

get_credential(self, name: str)

Defined on ConfigObject

Resolve a registered credential component by name.

Parameters

name : str
Credential component class name (e.g. "HuggingFaceCredential").

Returns

BaseCredential
An instance of the requested credential component.

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.