From 5392dd7daaeeb5fb753d1d8ac3ccbd2524aa6333 Mon Sep 17 00:00:00 2001 From: F-G Fernandez <26927750+frgfm@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:44:23 +0200 Subject: [PATCH] style: Fixes ruff --- demo/app.py | 2 +- docs/source/conf.py | 4 ++-- scripts/eval_perf.py | 3 ++- setup.py | 2 +- tests/conftest.py | 4 ++-- tests/test_methods_activation.py | 32 ++++++++++++++++---------------- tests/test_methods_core.py | 15 ++++++++------- tests/test_methods_gradient.py | 28 ++++++++++++++-------------- torchcam/methods/core.py | 7 ++++++- 9 files changed, 52 insertions(+), 45 deletions(-) diff --git a/demo/app.py b/demo/app.py index f09e6dc..7653821 100644 --- a/demo/app.py +++ b/demo/app.py @@ -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") diff --git a/docs/source/conf.py b/docs/source/conf.py index 80ca2b2..f0cf0f9 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -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 ----------------------------------------------------- diff --git a/scripts/eval_perf.py b/scripts/eval_perf.py index 79d9ac9..e641ce7 100644 --- a/scripts/eval_perf.py +++ b/scripts/eval_perf.py @@ -11,6 +11,7 @@ import math import os from functools import partial +from pathlib import Path import torch from torch.utils.data import SequentialSampler @@ -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( diff --git a/setup.py b/setup.py index 92ad5df..ccc9ccc 100644 --- a/setup.py +++ b/setup.py @@ -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) diff --git a/tests/conftest.py b/tests/conftest.py index 6621145..fe2c3b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/test_methods_activation.py b/tests/test_methods_activation.py index a581ff1..8f1e8eb 100644 --- a/tests/test_methods_activation.py +++ b/tests/test_methods_activation.py @@ -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) @@ -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): @@ -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): diff --git a/tests/test_methods_core.py b/tests/test_methods_core.py index c7dd6fc..c5d4e56 100644 --- a/tests/test_methods_core.py +++ b/tests/test_methods_core.py @@ -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): @@ -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): diff --git a/tests/test_methods_gradient.py b/tests/test_methods_gradient.py index cfd41a2..0def8eb 100644 --- a/tests/test_methods_gradient.py +++ b/tests/test_methods_gradient.py @@ -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): @@ -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): diff --git a/torchcam/methods/core.py b/torchcam/methods/core.py index a515baa..e89979e 100644 --- a/torchcam/methods/core.py +++ b/torchcam/methods/core.py @@ -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()