Skip to content

Commit

Permalink
style: Fixes ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
frgfm committed Sep 16, 2023
1 parent 1f94494 commit 5392dd7
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 45 deletions.
2 changes: 1 addition & 1 deletion demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

def main():
# Wide mode
st.set_page_config(layout="wide")
st.set_page_config(page_title="TorchCAM - Class activation explorer", layout="wide")

# Designing the interface
st.title("TorchCAM: class activation explorer")
Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
from datetime import datetime
from pathlib import Path

sys.path.insert(0, os.path.abspath("../.."))
sys.path.insert(0, Path().resolve().parent.parent)
import torchcam

# -- Project information -----------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion scripts/eval_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import math
import os
from functools import partial
from pathlib import Path

import torch
from torch.utils.data import SequentialSampler
Expand Down Expand Up @@ -49,7 +50,7 @@ def main(args):
)

ds = ImageFolder(
os.path.join(args.data_path, "val"),
Path(args.data_path).joinpath("val"),
T.Compose(eval_tf),
)
loader = torch.utils.data.DataLoader(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# Dynamically set the __version__ attribute
cwd = Path(__file__).parent.absolute()
with open(cwd.joinpath("torchcam", "version.py"), "w", encoding="utf-8") as f:
with cwd.joinpath("torchcam", "version.py").open("w", encoding="utf-8") as f:
f.write(f"__version__ = '{VERSION}'\n")

setup(name=PKG_NAME, version=VERSION)
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
def mock_img_tensor():
try:
# Get a dog image
URL = "https://www.woopets.fr/assets/races/000/066/big-portrait/border-collie.jpg"
response = requests.get(URL, timeout=5)
url = "https://www.woopets.fr/assets/races/000/066/big-portrait/border-collie.jpg"
response = requests.get(url, timeout=5)

# Forward an image
pil_img = Image.open(BytesIO(response.content), mode="r").convert("RGB")
Expand Down
32 changes: 16 additions & 16 deletions tests/test_methods_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def test_base_cam_constructor(mock_img_model):
for p in model.parameters():
p.requires_grad_(False)
# Check that multiple target layers is disabled for base CAM
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="base CAM does not support multiple target layers"):
activation.CAM(model, ["classifier.1", "classifier.2"])

# FC layer checks
with pytest.raises(TypeError):
with pytest.raises(TypeError, match="invalid argument type for `target_layer`"):
activation.CAM(model, fc_layer=3)


Expand All @@ -26,16 +26,16 @@ def _verify_cam(activation_map, output_size):


@pytest.mark.parametrize(
"cam_name, target_layer, fc_layer, num_samples, output_size, batch_size",
("cam_name", "target_layer", "fc_layer", "num_samples", "output_size", "batch_size"),
[
["CAM", None, None, None, (7, 7), 1],
["CAM", None, None, None, (7, 7), 2],
["CAM", None, "classifier.1", None, (7, 7), 1],
["CAM", None, lambda m: m.classifier[1], None, (7, 7), 1],
["ScoreCAM", "features.16.conv.3", None, None, (7, 7), 1],
["ScoreCAM", lambda m: m.features[16].conv[3], None, None, (7, 7), 1],
["SSCAM", "features.16.conv.3", None, 4, (7, 7), 1],
["ISCAM", "features.16.conv.3", None, 4, (7, 7), 1],
("CAM", None, None, None, (7, 7), 1),
("CAM", None, None, None, (7, 7), 2),
("CAM", None, "classifier.1", None, (7, 7), 1),
("CAM", None, lambda m: m.classifier[1], None, (7, 7), 1),
("ScoreCAM", "features.16.conv.3", None, None, (7, 7), 1),
("ScoreCAM", lambda m: m.features[16].conv[3], None, None, (7, 7), 1),
("SSCAM", "features.16.conv.3", None, 4, (7, 7), 1),
("ISCAM", "features.16.conv.3", None, 4, (7, 7), 1),
],
)
def test_img_cams(cam_name, target_layer, fc_layer, num_samples, output_size, batch_size, mock_img_tensor):
Expand Down Expand Up @@ -70,12 +70,12 @@ def test_cam_conv1x1(mock_fullyconv_model):


@pytest.mark.parametrize(
"cam_name, target_layer, num_samples, output_size",
("cam_name", "target_layer", "num_samples", "output_size"),
[
["CAM", "0.3", None, (1, 8, 16, 16)],
["ScoreCAM", "0.3", None, (1, 8, 16, 16)],
["SSCAM", "0.3", 4, (1, 8, 16, 16)],
["ISCAM", "0.3", 4, (1, 8, 16, 16)],
("CAM", "0.3", None, (1, 8, 16, 16)),
("ScoreCAM", "0.3", None, (1, 8, 16, 16)),
("SSCAM", "0.3", 4, (1, 8, 16, 16)),
("ISCAM", "0.3", 4, (1, 8, 16, 16)),
],
)
def test_video_cams(cam_name, target_layer, num_samples, output_size, mock_video_model, mock_video_tensor):
Expand Down
15 changes: 8 additions & 7 deletions tests/test_methods_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def test_cam_precheck(mock_img_model, mock_img_tensor):


@pytest.mark.parametrize(
"input_shape, spatial_dims",
("input_shape", "spatial_dims"),
[
[(8, 8), None],
[(8, 8, 8), None],
[(8, 8, 8), 2],
[(8, 8, 8, 8), None],
[(8, 8, 8, 8), 3],
((8, 8), None),
((8, 8, 8), None),
((8, 8, 8), 2),
((8, 8, 8, 8), None),
((8, 8, 8, 8), 3),
],
)
def test_cam_normalize(input_shape, spatial_dims):
Expand All @@ -72,7 +72,8 @@ def test_cam_normalize(input_shape, spatial_dims):
assert normalized_tensor.shape == input_shape
# Value check
assert not torch.any(torch.isnan(normalized_tensor))
assert torch.all(normalized_tensor <= 1) and torch.all(normalized_tensor >= 0)
assert torch.all(normalized_tensor <= 1)
assert torch.all(normalized_tensor >= 0)


def test_cam_remove_hooks(mock_img_model):
Expand Down
28 changes: 14 additions & 14 deletions tests/test_methods_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def _verify_cam(activation_map, output_size):


@pytest.mark.parametrize(
"cam_name, target_layer, output_size, batch_size",
("cam_name", "target_layer", "output_size", "batch_size"),
[
["GradCAM", "features.18.0", (7, 7), 1],
["GradCAMpp", "features.18.0", (7, 7), 1],
["SmoothGradCAMpp", lambda m: m.features[18][0], (7, 7), 1],
["SmoothGradCAMpp", "features.18.0", (7, 7), 1],
["XGradCAM", "features.18.0", (7, 7), 1],
["LayerCAM", "features.18.0", (7, 7), 1],
["LayerCAM", "features.18.0", (7, 7), 2],
("GradCAM", "features.18.0", (7, 7), 1),
("GradCAMpp", "features.18.0", (7, 7), 1),
("SmoothGradCAMpp", lambda m: m.features[18][0], (7, 7), 1),
("SmoothGradCAMpp", "features.18.0", (7, 7), 1),
("XGradCAM", "features.18.0", (7, 7), 1),
("LayerCAM", "features.18.0", (7, 7), 1),
("LayerCAM", "features.18.0", (7, 7), 2),
],
)
def test_img_cams(cam_name, target_layer, output_size, batch_size, mock_img_tensor):
Expand Down Expand Up @@ -60,13 +60,13 @@ def test_img_cams(cam_name, target_layer, output_size, batch_size, mock_img_tens


@pytest.mark.parametrize(
"cam_name, target_layer, output_size",
("cam_name", "target_layer", "output_size"),
[
["GradCAM", "0.3", (1, 8, 16, 16)],
["GradCAMpp", "0.3", (1, 8, 16, 16)],
["SmoothGradCAMpp", "0.3", (1, 8, 16, 16)],
["XGradCAM", "0.3", (1, 8, 16, 16)],
["LayerCAM", "0.3", (1, 8, 16, 16)],
("GradCAM", "0.3", (1, 8, 16, 16)),
("GradCAMpp", "0.3", (1, 8, 16, 16)),
("SmoothGradCAMpp", "0.3", (1, 8, 16, 16)),
("XGradCAM", "0.3", (1, 8, 16, 16)),
("LayerCAM", "0.3", (1, 8, 16, 16)),
],
)
def test_video_cams(cam_name, target_layer, output_size, mock_video_model, mock_video_tensor):
Expand Down
7 changes: 6 additions & 1 deletion torchcam/methods/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ def __init__(
def __enter__(self) -> "_CAM":
return self

def __exit__(self, exct_type: Type[BaseException], exce_value: BaseException, traceback: TracebackType) -> None:
def __exit__(
self,
exct_type: Union[Type[BaseException], None],
exce_value: Union[BaseException, None],
traceback: Union[TracebackType, None],
) -> None:
self.remove_hooks()
self.reset_hooks()

Expand Down

0 comments on commit 5392dd7

Please sign in to comment.