Prediction explanations¶
explain
¶
High-level prediction debugging for one image.
torchcam.explain.PredictionExplanation
dataclass
¶
PredictionExplanation(logits: Tensor, predicted_class_idx: int, expected_class_idx: int | None, cams: Mapping[int, tuple[Tensor, ...]], method: str, target_layers: tuple[str, ...], model: str, input_shape: tuple[int, ...], versions: Mapping[str, str], class_names: tuple[str, ...] | None = None)
Prediction, class activation maps, and reproducibility metadata for one image.
torchcam.explain.PredictionExplanation.save
¶
Save NumPy maps, heatmaps, overlays, and a completion manifest to a new directory.
| PARAMETER | DESCRIPTION |
|---|---|
directory
|
new output directory |
image
|
source image used for full-size overlays
TYPE:
|
alpha
|
source-image opacity in the overlay
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
output directory |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
if the image is not a PIL image |
FileExistsError
|
if the output directory already exists |
Source code in torchcam/explain.py
torchcam.explain.explain
¶
explain(model: Module, input_tensor: Tensor, *, expected_class_idx: int | None = None, class_names: Sequence[str] | None = None, method: type[_CAM] = GradCAM, target_layer: Module | str | list[Module | str] | None = None, method_kwargs: Mapping[str, Any] | None = None) -> PredictionExplanation
Explain the predicted and optional expected class for one 2D image.
| RETURNS | DESCRIPTION |
|---|---|
PredictionExplanation
|
detached prediction evidence and CAMs |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
if called from inference mode |
ValueError
|
if an argument, model output, or CAM has an unsupported value |