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

build(deps-dev): bump mypy from 1.8.0 to 1.10.0 #243

Merged
merged 3 commits into from
Apr 25, 2024
Merged
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
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ test = [
]
quality = [
"ruff==0.4.1",
"mypy==1.8.0",
"mypy==1.10.0",
"types-Pillow",
"pre-commit>=3.0.0,<4.0.0",
]
docs = [
Expand All @@ -77,7 +78,8 @@ dev = [
"pytest-pretty>=1.0.0,<2.0.0",
# style
"ruff==0.4.1",
"mypy==1.8.0",
"mypy==1.10.0",
"types-Pillow",
"pre-commit>=3.0.0,<4.0.0",
# docs
"sphinx>=3.0.0,!=3.5.0",
Expand Down
4 changes: 2 additions & 2 deletions torchcam/methods/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from abc import abstractmethod
from functools import partial
from types import TracebackType
from typing import Any, List, Optional, Tuple, Type, Union, cast
from typing import Any, List, Optional, Tuple, Type, Union

import torch
import torch.nn.functional as F
Expand Down Expand Up @@ -267,4 +267,4 @@ def _fuse_cams(cams: List[Tensor], target_shape: Tuple[int, int]) -> Tensor:
]

# Fuse them
return cast(Tensor, torch.stack(scaled_cams).max(dim=0).values.squeeze(1))
return torch.stack(scaled_cams).max(dim=0).values.squeeze(1)
4 changes: 2 additions & 2 deletions torchcam/methods/gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.

from functools import partial
from typing import Any, List, Optional, Tuple, Union, cast
from typing import Any, List, Optional, Tuple, Union

import torch
from torch import Tensor, nn
Expand Down Expand Up @@ -404,4 +404,4 @@ def _get_weights(self, class_idx: Union[int, List[int]], scores: Tensor, **kwarg
@staticmethod
def _scale_cams(cams: List[Tensor], gamma: float = 2.0) -> List[Tensor]:
# cf. Equation 9 in the paper
return [torch.tanh(cast(Tensor, gamma * cam)) for cam in cams]
return [torch.tanh(gamma * cam) for cam in cams]
14 changes: 7 additions & 7 deletions torchcam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.

from typing import cast

import numpy as np
from matplotlib import colormaps as cm
from PIL import Image
from PIL.Image import Image, Resampling, fromarray


def overlay_mask(img: Image.Image, mask: Image.Image, colormap: str = "jet", alpha: float = 0.7) -> Image.Image:
def overlay_mask(img: Image, mask: Image, colormap: str = "jet", alpha: float = 0.7) -> Image:
"""Overlay a colormapped mask on a background image

>>> from PIL import Image
Expand All @@ -31,17 +33,15 @@ def overlay_mask(img: Image.Image, mask: Image.Image, colormap: str = "jet", alp
TypeError: when the arguments have invalid types
ValueError: when the alpha argument has an incorrect value
"""
if not isinstance(img, Image.Image) or not isinstance(mask, Image.Image):
if not isinstance(img, Image) or not isinstance(mask, Image):
raise TypeError("img and mask arguments need to be PIL.Image")

if not isinstance(alpha, float) or alpha < 0 or alpha >= 1:
raise ValueError("alpha argument is expected to be of type float between 0 and 1")

cmap = cm.get_cmap(colormap)
# Resize mask and apply colormap
overlay = mask.resize(img.size, resample=Image.BICUBIC)
overlay = mask.resize(img.size, resample=Resampling.BICUBIC)
overlay = (255 * cmap(np.asarray(overlay) ** 2)[:, :, :3]).astype(np.uint8)
# Overlay the image with the mask
overlayed_img = Image.fromarray((alpha * np.asarray(img) + (1 - alpha) * overlay).astype(np.uint8))

return overlayed_img
return fromarray((alpha * np.asarray(img) + (1 - alpha) * cast(np.ndarray, overlay)).astype(np.uint8))
Loading