GradCAM¶
-
class
tensornet.gradcam.
GradCAM
(model: torch.nn.modules.module.Module, layer_name: str)[source]¶ Calculate GradCAM salinecy map.
Note: The current implemenation supports only ResNet models. The class can be extended to add support for other models.
- Parameters
model (torch.nn.Module) – A model instance.
layer_name (str) – Name of the layer in model for which the map will be calculated.
-
__call__
(input: tuple, class_idx: Optional[int] = None, retain_graph: bool = False) → Tuple[torch.Tensor][source]¶ - Parameters
input (tuple) – Input image with shape of (1, 3, H, W)
class_idx (
int
, optional) – Class index for calculating GradCAM. If not specified, the class index that makes the highest model prediction score will be used.
- Returns
2-element tuple containing
(torch.tensor): saliency map of the same spatial dimension with input.
(torch.tensor): model output.
-
class
tensornet.gradcam.
GradCAMPP
(model: torch.nn.modules.module.Module, layer_name: str)[source]¶ Calculate GradCAM++ salinecy map.
It inherits the GradCAM class so the definition for all the methods is exactly the same as its parent class.
Note: The current implemenation supports only ResNet models. The class can be extended to add support for other models.
-
class
tensornet.gradcam.
GradCAMView
(model: torch.nn.modules.module.Module, layers: List[str], device: Union[str, torch.device], mean: Union[float, tuple], std: Union[float, tuple])[source]¶ Create GradCAM and GradCAM++.
Note: The current implemenation of GradCAM and GradCAM++ supports only ResNet models. The class can be extended to add support for other models.
- Parameters
model (torch.nn.Module) – Trained model.
layers (list) – List of layers to show GradCAM on.
device (
str
ortorch.device
) – GPU or CPU.mean (
float
ortuple
) – Mean of the dataset.std (
float
ortuple
) – Standard Deviation of the dataset.
-
cam
(norm_img_class_list: List[Union[Dict[str, Union[torch.Tensor, int]], torch.Tensor]])[source]¶ Get CAM for a list of images.
- Parameters
norm_img_class_list (list) – List of dictionaries or list of images. If dict, each dict contains keys ‘image’ and ‘class’ having values ‘normalized_image’ and ‘class_idx’ respectively. class_idx is optional. If class_idx is not given then the model prediction will be used and the parameter should just be a list of images. Each image should be of type torch.Tensor
-
__call__
(norm_img_class_list: List[Union[Dict[str, Union[torch.Tensor, int]], torch.Tensor]]) → List[Dict[str, Union[numpy.ndarray, Dict[str, numpy.ndarray]]]][source]¶ Get GradCAM for a list of images.
- Parameters
norm_img_class_list (list) – List of dictionaries or list of images. If dict, each dict contains keys ‘image’ and ‘class’ having values ‘normalized_image’ and ‘class_idx’ respectively. class_idx is optional. If class_idx is not given then the model prediction will be used and the parameter should just be a list of images. Each image should be of type torch.Tensor
-
tensornet.gradcam.
visualize_cam
(mask: torch.Tensor, img: torch.Tensor, alpha: float = 1.0) → Tuple[torch.Tensor][source]¶ Make heatmap from mask and synthesize GradCAM result image using heatmap and img.
- Parameters
mask (torch.tensor) – mask shape of (1, 1, H, W) and each element has value in range [0, 1]
img (torch.tensor) – img shape of (1, 3, H, W) and each pixel value is in range [0, 1]
- Returns
2-element tuple containing
(torch.tensor): heatmap img shape of (3, H, W)
(torch.tensor): synthesized GradCAM result of same shape with heatmap.