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(directory: str | Path, image: Image, alpha: float = 0.5, *, context: Mapping[str, str] | None = None) -> Path
Save NumPy maps, heatmaps, overlays, and a completion manifest to a new directory.
| PARAMETER | DESCRIPTION |
|---|---|
directory
|
new output directory |
image
|
RGB or grayscale model-view image after spatial transforms, before normalization
TYPE:
|
alpha
|
source-image opacity in the overlay
TYPE:
|
context
|
caller-supplied identifiers, such as checkpoint, preprocessing, sample, split, and group |
| RETURNS | DESCRIPTION |
|---|---|
Path
|
output directory |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
if the image is not a PIL image or context does not map strings to strings |
ValueError
|
if the image mode is unsupported or dimensions differ from the model input |
FileExistsError
|
if the output directory already exists |
Source code in torchcam/explain.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
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 |