Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type hints #480

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pytorch_grad_cam/base_cam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import numpy.typing as npt
import torch
import ttach as tta
from typing import Callable, List, Tuple, Optional
Expand All @@ -12,7 +13,7 @@ class BaseCAM:
def __init__(self,
model: torch.nn.Module,
target_layers: List[torch.nn.Module],
reshape_transform: Callable = None,
reshape_transform: Optional[Callable] = None,
compute_input_gradient: bool = False,
uses_gradients: bool = True,
tta_transforms: Optional[tta.Compose] = None) -> None:
Expand Down Expand Up @@ -71,8 +72,8 @@ def get_cam_image(self,

def forward(self,
input_tensor: torch.Tensor,
targets: List[torch.nn.Module],
eigen_smooth: bool = False) -> np.ndarray:
targets: Optional[List[torch.nn.Module]],
eigen_smooth: bool = False) -> npt.NDArray[np.float32]:

input_tensor = input_tensor.to(self.device)

Expand Down Expand Up @@ -156,7 +157,7 @@ def aggregate_multi_layers(

def forward_augmentation_smoothing(self,
input_tensor: torch.Tensor,
targets: List[torch.nn.Module],
targets: Optional[List[torch.nn.Module]],
eigen_smooth: bool = False) -> np.ndarray:
cams = []
for transform in self.tta_transforms:
Expand All @@ -180,9 +181,9 @@ def forward_augmentation_smoothing(self,

def __call__(self,
input_tensor: torch.Tensor,
targets: List[torch.nn.Module] = None,
targets: Optional[List[torch.nn.Module]]= None,
aug_smooth: bool = False,
eigen_smooth: bool = False) -> np.ndarray:
eigen_smooth: bool = False) -> npt.NDArray[np.float32]:

# Smooth the CAM result with test time augmentation
if aug_smooth is True:
Expand Down
7 changes: 4 additions & 3 deletions pytorch_grad_cam/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from matplotlib.lines import Line2D
import cv2
import numpy as np
import numpy.typing as npt
import torch
from torchvision.transforms import Compose, Normalize, ToTensor
from typing import List, Dict
Expand Down Expand Up @@ -30,11 +31,11 @@ def deprocess_image(img):
return np.uint8(img * 255)


def show_cam_on_image(img: np.ndarray,
mask: np.ndarray,
def show_cam_on_image(img: npt.NDArray[np.float16] | npt.NDArray[np.float32] | npt.NDArray[np.float64],
mask: npt.NDArray[np.float16] | npt.NDArray[np.float32] | npt.NDArray[np.float64],
use_rgb: bool = False,
colormap: int = cv2.COLORMAP_JET,
image_weight: float = 0.5) -> np.ndarray:
image_weight: float = 0.5) -> npt.NDArray[np.uint8]:
""" This function overlays the cam mask on the image as an heatmap.
By default the heatmap is in BGR format.

Expand Down