diff --git a/.github/workflows/ci_tests_gpu.yml b/.github/workflows/ci_tests_gpu.yml new file mode 100644 index 00000000000..231bd856f38 --- /dev/null +++ b/.github/workflows/ci_tests_gpu.yml @@ -0,0 +1,102 @@ +name: tests + +on: + schedule: + - cron: "0 2 * * 3" # at 02:00 UTC Wednesday + workflow_dispatch: + +permissions: + contents: read + +concurrency: + # automatically cancel the previously triggered workflows when there's a newer version + group: cicd-tests-gpu-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +# Supported versions of Python and PyTorch are listed here for use below and as documentation +env: + # supported versions of Python + PYTHON_VER1: '3.11' + PYTHON_VER2: '3.12' + PYTHON_VER3: '3.13' + # PYTHON_VER4: '3.14' # TODO: not compatible with Torchscript, re-enable once Torchscript removed from MONAI + # PYTHON_VER5: '3.15' + # supported versions of PyTorch + PYTORCH_VER1: '2.12.1' + PYTORCH_VER2: '2.13.0' + PYTORCH_VER3: '2.14.1' + PYTORCH_VER4: '2.15.0' + TORCHVISION_VER1: '0.27.1' # used for testing with lowest PyTorch version (PYTORCH_VER1), update as needed + + TQDM_DISABLE: 1 # squelch TQDM output to reduce log file size + + PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python # https://github.com/Project-MONAI/MONAI/issues/4354 + + BUILD_MONAI: 1 # always build MONAI and ensure it tries to use the compiled extensions + FORCE_CUDA: 1 # ensure CUDA code is built + CUDA_HOME: '/usr/local/cuda' # ensure the CUDA directory is definitely found + TORCH_CUDA_ARCH_LIST: "7.5 8.0 8.6 8.9 9.0+PTX 12.0" # specify supported architectures + +jobs: + + full-dep-gpu: + runs-on: [self-hosted, linux, gpu] + strategy: + fail-fast: false + timeout-minutes: 360 + steps: + - uses: actions/checkout@v7 + - name: Set up Python ${{ env.PYTHON_VER1 }} + uses: actions/setup-python@v6 + with: + python-version: ${{ env.PYTHON_VER1 }} + cache: 'pip' + env: + DEBIAN_FRONTEND: 'noninteractive' + - name: Install the complete dependencies + run: | + python -m pip install --upgrade pip wheel wheel_stub + python -m pip install torch==${PYTORCH_VER1} torchvision==${TORCHVISION_VER1} + python -m pip install --pre -U itk # TODO: needed? + python monai/config/print_dependencies.py build-system | xargs pip install --no-build-isolation + # tests below use the MONAI in the current directory, -e ensures the compiled code stays there and is found + python -m pip install --no-build-isolation torch==${PYTORCH_VER1} torchvision==${TORCHVISION_VER1} -e .[all,testing,nvimgcodec] + shell: bash + - name: Run (almost) all tests + run: | + python -c 'import monai._C; cap=monai._C.max_compute_capability(); assert cap > 0, f"Capabilities too low: {cap}"' + nvidia-smi || true + python monai/config/check_env.py --env --monai + ./runtests.sh -u --net + # env: # FIXME: NGC test failing with "RuntimeError: NGC API response is not ok. Failed to get token." Need to fix key? + # NGC_API_KEY: ${{ secrets.NGC_API_KEY }} + # NGC_ORG: ${{ secrets.NGC_ORG }} + # NGC_TEAM: ${{ secrets.NGC_TEAM }} + + full-dep-gpu-hyena: + runs-on: [self-hosted, linux, gpu] + strategy: + fail-fast: false + timeout-minutes: 30 + steps: + - uses: actions/checkout@v7 + - name: Set up Python ${{ env.PYTHON_VER1 }} + uses: actions/setup-python@v6 + with: + python-version: ${{ env.PYTHON_VER1 }} + cache: 'pip' + env: + DEBIAN_FRONTEND: 'noninteractive' + - name: Install the complete dependencies + run: | + python -m pip install --upgrade pip wheel wheel_stub + python -m pip install torch==${PYTORCH_VER3} torchvision + python monai/config/print_dependencies.py build-system | xargs pip install --no-build-isolation + # tests below use the MONAI in the current directory, -e ensures the compiled code stays there and is found + python -m pip install --no-build-isolation -e .[testing,hyena] + shell: bash + - name: Run Hyena tests + run: | + python -c "from monai.networks.blocks.hyena import is_nvsubquadratic_available; \ + assert is_nvsubquadratic_available(), 'nvsubquadratic must be importable'" + python -m unittest tests.networks.blocks.test_hyena_block tests.networks.nets.test_hyena_nd_unetr tests.networks.nets.test_swin_unetr diff --git a/.gitignore b/.gitignore index d0bdc54018d..c22d2c98026 100644 --- a/.gitignore +++ b/.gitignore @@ -170,3 +170,5 @@ runs *.pth *zarr/* + +uv.lock diff --git a/Dockerfile b/Dockerfile index 79d9d7677e1..65e3d90a1cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,7 +34,7 @@ RUN wget -q ${NGC_CLI_URI} && unzip ngccli_linux.zip && chmod u+x ngc-cli/ngc && rm -rf ngccli_linux.zip ngc-cli.md5 ENV PATH=${PATH}:/opt/tools:/opt/tools/ngc-cli RUN apt-get update \ - && DEBIAN_FRONTEND="noninteractive" apt-get install -y libopenslide0 \ + && DEBIAN_FRONTEND="noninteractive" apt-get install -y libopenslide0 libopenmpi3 \ && rm -rf /var/lib/apt/lists/* # append /opt/tools to runtime path for NGC CLI to be accessible from all file system locations ENV PATH=${PATH}:/opt/tools diff --git a/Dockerfile.slim b/Dockerfile.slim index bfaf897967f..db4954999fa 100644 --- a/Dockerfile.slim +++ b/Dockerfile.slim @@ -61,8 +61,9 @@ FROM ${IMAGE} AS build2 ENV DEBIAN_FRONTEND=noninteractive ENV APT_INSTALL="apt install -y --no-install-recommends" +# install the necessary libraries for running Python and for Triton to build kernels when needed RUN apt update && apt upgrade -y && \ - ${APT_INSTALL} ca-certificates python-is-python3 git libopenslide0 && \ + ${APT_INSTALL} ca-certificates python-is-python3 python3-dev git libopenslide0 build-essential && \ apt clean && \ rm -rf /usr/lib/python*/EXTERNALLY-MANAGED /var/lib/apt/lists/* diff --git a/monai/config/deviceconfig.py b/monai/config/deviceconfig.py index 6a0d2378aa7..2188d1b7132 100644 --- a/monai/config/deviceconfig.py +++ b/monai/config/deviceconfig.py @@ -114,6 +114,12 @@ def print_config(file=sys.stdout): fprint(f"MONAI flags: {HAS_EXT=}, {USE_COMPILED=}, {USE_META_DICT=}", file=file) fprint(f"MONAI rev id: {monai.__revision_id__}", file=file) fprint(f"MONAI __file__: {monai.__file__}", file=file) + + if HAS_EXT: + fprint(f"monai._C.max_compute_capability: {monai._C.max_compute_capability()}", file=file) + fprint(f"monai._C.compiled_with_cuda: {monai._C.compiled_with_cuda()}", file=file) + fprint(f"monai._C.monai_torch_version: {monai._C.monai_torch_version()}", file=file) + fprint("\nOptional dependencies:", file=file) for k, v in get_optional_config_values().items(): fprint(f"{k} version: {v}", file=file) diff --git a/monai/csrc/ext.cpp b/monai/csrc/ext.cpp index 514724fb859..9dbf52ba959 100644 --- a/monai/csrc/ext.cpp +++ b/monai/csrc/ext.cpp @@ -72,6 +72,16 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { #endif }, "Maximum compute capability (major*100+minor) the extension was compiled for, or 0 if unknown"); + m.def("compiled_with_cuda", []() { +#ifdef WITH_CUDA + return 1; +#else + return 0; +#endif + }, "Returns 1 if compiled with WITH_CUDA set, 0 otherwise"); + + m.def("monai_torch_version", []() { return MONAI_TORCH_VERSION; }, "PyTorch version used for compilation"); + // resample m.def("grid_pull", &monai::grid_pull, "GridPull"); m.def("grid_pull_backward", &monai::grid_pull_backward, "GridPull backward"); diff --git a/monai/data/nvimgcodec_pydicom_plugin.py b/monai/data/nvimgcodec_pydicom_plugin.py index 07ba5ca726a..70f690c36c9 100644 --- a/monai/data/nvimgcodec_pydicom_plugin.py +++ b/monai/data/nvimgcodec_pydicom_plugin.py @@ -67,10 +67,14 @@ def register_as_decoder_plugin(module_path: str | None = None) -> bool: """Register the nvImageCodec pydicom decoder plugin.""" if not is_nvimgcodec_available(): _logger.debug("nvImageCodec is not available; skipping pydicom decoder plugin registration.") - return False - if not has_pydicom_plugin: - return False - return bool(pydicom_plugin.register(module_path)) + elif not has_pydicom_plugin: + _logger.debug("nvImageCodec does not have pydicom_plugin; skipping pydicom decoder plugin registration.") + elif not bool(pydicom_plugin.register(module_path)): + _logger.debug("nvImageCodec pydicom_plugin failed to register; skipping pydicom decoder plugin registration.") + else: + return True # every check and register() succeeded + + return False def unregister_as_decoder_plugin() -> bool: diff --git a/monai/engines/trainer.py b/monai/engines/trainer.py index 1f0c75620fa..df1853f7b12 100644 --- a/monai/engines/trainer.py +++ b/monai/engines/trainer.py @@ -51,7 +51,7 @@ def run(self) -> None: # type: ignore[override] If call this function multiple times, it will continuously run from the previous state. """ - self.scaler = torch.cuda.amp.GradScaler() if self.amp else None + self.scaler = torch.amp.GradScaler() if self.amp else None super().run() def get_stats(self, *vars): @@ -613,8 +613,8 @@ def __init__( self.g_inferer = SimpleInferer() if g_inferer is None else g_inferer self.d_inferer = SimpleInferer() if d_inferer is None else d_inferer - self.state.g_scaler = torch.cuda.amp.GradScaler() if self.amp else None - self.state.d_scaler = torch.cuda.amp.GradScaler() if self.amp else None + self.state.g_scaler = torch.amp.GradScaler() if self.amp else None + self.state.d_scaler = torch.amp.GradScaler() if self.amp else None self.optim_set_to_none = optim_set_to_none self._complete_state_dict_user_keys() diff --git a/monai/engines/workflow.py b/monai/engines/workflow.py index ecb0c4a070d..95a85eecb32 100644 --- a/monai/engines/workflow.py +++ b/monai/engines/workflow.py @@ -165,7 +165,7 @@ def set_sampler_epoch(engine: Engine) -> None: self.amp = amp self.to_kwargs = {} if to_kwargs is None else to_kwargs self.amp_kwargs = {} if amp_kwargs is None else amp_kwargs - self.scaler: torch.cuda.amp.GradScaler | None = None + self.scaler: torch.amp.GradScaler | None = None if event_names is None: event_names = [IterationEvents] diff --git a/monai/losses/image_dissimilarity.py b/monai/losses/image_dissimilarity.py index 47cab1a2304..ac4077ab37e 100644 --- a/monai/losses/image_dissimilarity.py +++ b/monai/losses/image_dissimilarity.py @@ -361,7 +361,8 @@ def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor: wa, pa, wb, pb = self.parzen_windowing(pred, target) # (batch, num_sample, num_bin), (batch, 1, num_bin) pab = torch.bmm(wa.permute(0, 2, 1), wb.to(wa)).div(wa.shape[1]) # (batch, num_bins, num_bins) - papb = torch.bmm(pa.permute(0, 2, 1), pb.to(pa)) # (batch, num_bins, num_bins) + papb = (pa.permute(0, 2, 1) * pb).to(pab) # (batch, num_bins, num_bins) + mi = torch.sum( pab * torch.log((pab + self.smooth_nr) / (papb + self.smooth_dr) + self.smooth_dr), dim=(1, 2) ) # (batch) diff --git a/monai/networks/nets/senet.py b/monai/networks/nets/senet.py index 668125b1428..0506961bb57 100644 --- a/monai/networks/nets/senet.py +++ b/monai/networks/nets/senet.py @@ -38,12 +38,12 @@ ] SE_NET_MODELS = { - "senet154": "http://data.lip6.fr/cadene/pretrainedmodels/senet154-c7b49a05.pth", - "se_resnet50": "http://data.lip6.fr/cadene/pretrainedmodels/se_resnet50-ce0d4300.pth", - "se_resnet101": "http://data.lip6.fr/cadene/pretrainedmodels/se_resnet101-7e38fcc6.pth", - "se_resnet152": "http://data.lip6.fr/cadene/pretrainedmodels/se_resnet152-d17c99b7.pth", - "se_resnext50_32x4d": "http://data.lip6.fr/cadene/pretrainedmodels/se_resnext50_32x4d-a260b3a4.pth", - "se_resnext101_32x4d": "http://data.lip6.fr/cadene/pretrainedmodels/se_resnext101_32x4d-3b2fe3d8.pth", + "senet154": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/senet154-c7b49a05.pth", + "se_resnet50": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnet50-ce0d4300.pth", + "se_resnet101": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnet101-7e38fcc6.pth", + "se_resnet152": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnet152-d17c99b7.pth", + "se_resnext50_32x4d": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnext50_32x4d-a260b3a4.pth", + "se_resnext101_32x4d": "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/se_resnext101_32x4d-3b2fe3d8.pth", } diff --git a/pyproject.toml b/pyproject.toml index 01d5aee18b7..df875e38953 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -69,7 +69,7 @@ all = [ "itk>=5.2", "jsonschema", "lmdb", - "matplotlib>=3.6.3", + "matplotlib>=3.11.2", "MetricsReloaded @ git+https://github.com/Project-MONAI/MetricsReloaded@monai-support", "mlflow>=3.15.2", "nibabel", @@ -124,7 +124,7 @@ itk = ["itk>=5.2"] jsonschema = ["jsonschema"] lmdb = ["lmdb"] lpips = ["torchvision"] # compatibility extra; LPIPS is implemented within MONAI -matplotlib = ["matplotlib>=3.6.3"] +matplotlib = ["matplotlib>=3.11.2"] # per #5882 this requirement must be on one line to get removed by sed in weekly-preview.yml metrics_reloaded = ["MetricsReloaded @ git+https://github.com/Project-MONAI/MetricsReloaded@monai-support"] mlflow = ["mlflow>=3.15.2"] @@ -138,6 +138,9 @@ nni = [ # required for GPU accelerated decoding of compressed DICOM images nvimgcodec = [ "pydicom", + # Tighter requirements than just the "cucim" optional dependency to get around a bug with cupy, also cucim>26.8 will + # require Numpy 2.x. + "cucim-cu13>=26.6.0; platform_system == 'Linux' and python_version >= '3.11'", "cupy-cuda13x[ctk]!=14.1.0; platform_system == 'Linux'", "nvidia-nvimgcodec-cu13[all]>=0.8.0; platform_system == 'Linux'" ] diff --git a/runtests.sh b/runtests.sh index 8454c2152f1..617a141dbc3 100755 --- a/runtests.sh +++ b/runtests.sh @@ -736,7 +736,6 @@ fi # fi # unit tests -# TODO: temp skip test_perceptual_loss, revert after #8652 merged # TODO: temp skip test_auto3dseg_ensemble, revert after #8737 resolved if [ $doUnitTests = true ] then @@ -746,7 +745,7 @@ then if (("$testTimeout" > 0)); then timeoutArg="--timeout $testTimeout" fi - ${cmdPrefix}${cmd} ./tests/runner.py -p "^(?!test_integration|test_perceptual_loss|test_auto3dseg_ensemble).*(? None: self.test_dir = tempfile.TemporaryDirectory() diff --git a/tests/integration/test_integration_fast_train.py b/tests/integration/test_integration_fast_train.py index 814c4b182c6..651264f7e5c 100644 --- a/tests/integration/test_integration_fast_train.py +++ b/tests/integration/test_integration_fast_train.py @@ -165,7 +165,7 @@ def test_train_timing(self): # Novograd paper suggests to use a bigger LR than Adam, # because Adam does normalization by element-wise second moments optimizer = Novograd(model.parameters(), learning_rate * 10) - scaler = torch.cuda.amp.GradScaler() + scaler = torch.amp.GradScaler() post_pred = AsDiscrete(argmax=True, to_onehot=2) post_label = AsDiscrete(to_onehot=2) diff --git a/tests/losses/test_perceptual_loss.py b/tests/losses/test_perceptual_loss.py index 051a79fef13..fb282461797 100644 --- a/tests/losses/test_perceptual_loss.py +++ b/tests/losses/test_perceptual_loss.py @@ -22,65 +22,39 @@ from tests.test_utils import assert_allclose, skip_if_downloading_fails, skip_if_quick _, has_torchvision = optional_import("torchvision") +_, has_lpips = optional_import("lpips", name="LPIPS") + +SHAPE1 = (2, 1, 64, 64, 64) +SHAPE2 = (2, 6, 64, 64, 64) + TEST_CASES = [ - [{"spatial_dims": 2, "network_type": "squeeze"}, (2, 1, 64, 64), (2, 1, 64, 64)], - [ - {"spatial_dims": 3, "network_type": "squeeze", "is_fake_3d": True, "fake_3d_ratio": 0.1}, - (2, 1, 64, 64, 64), - (2, 1, 64, 64, 64), - ], - [{"spatial_dims": 2, "network_type": "radimagenet_resnet50"}, (2, 1, 64, 64), (2, 1, 64, 64)], - [{"spatial_dims": 2, "network_type": "radimagenet_resnet50"}, (2, 3, 64, 64), (2, 3, 64, 64)], - [ - {"spatial_dims": 3, "network_type": "radimagenet_resnet50", "is_fake_3d": True, "fake_3d_ratio": 0.1}, - (2, 1, 64, 64, 64), - (2, 1, 64, 64, 64), - ], - [ - {"spatial_dims": 3, "network_type": "medicalnet_resnet10_23datasets", "is_fake_3d": False}, - (2, 1, 64, 64, 64), - (2, 1, 64, 64, 64), - ], - [ - {"spatial_dims": 3, "network_type": "medicalnet_resnet10_23datasets", "is_fake_3d": False}, - (2, 6, 64, 64, 64), - (2, 6, 64, 64, 64), - ], - [ - { - "spatial_dims": 3, - "network_type": "medicalnet_resnet10_23datasets", - "is_fake_3d": False, - "channel_wise": True, - }, - (2, 6, 64, 64, 64), - (2, 6, 64, 64, 64), - ], - [ - {"spatial_dims": 3, "network_type": "medicalnet_resnet50_23datasets", "is_fake_3d": False}, - (2, 1, 64, 64, 64), - (2, 1, 64, 64, 64), - ], - [ - {"spatial_dims": 3, "network_type": "medicalnet_resnet50_23datasets", "is_fake_3d": False}, - (2, 6, 64, 64, 64), - (2, 6, 64, 64, 64), - ], - [ - {"spatial_dims": 3, "network_type": "resnet50", "is_fake_3d": True, "pretrained": True, "fake_3d_ratio": 0.2}, - (2, 1, 64, 64, 64), - (2, 1, 64, 64, 64), - ], + [{"network_type": "squeeze"}, (2, 1, 64, 64), (2, 1, 64, 64)], + [{"network_type": "radimagenet_resnet50"}, (2, 1, 64, 64), (2, 1, 64, 64)], + [{"network_type": "radimagenet_resnet50"}, (2, 3, 64, 64), (2, 3, 64, 64)], + [{"network_type": "squeeze", "is_fake_3d": True, "fake_3d_ratio": 0.1}, SHAPE1, SHAPE1], + [{"network_type": "radimagenet_resnet50", "is_fake_3d": True, "fake_3d_ratio": 0.1}, SHAPE1, SHAPE1], + [{"network_type": "medicalnet_resnet10_23datasets", "is_fake_3d": False}, SHAPE1, SHAPE1], + [{"network_type": "medicalnet_resnet10_23datasets", "is_fake_3d": False}, SHAPE2, SHAPE2], + [{"network_type": "medicalnet_resnet10_23datasets", "is_fake_3d": False, "channel_wise": True}, SHAPE2, SHAPE2], + [{"network_type": "medicalnet_resnet50_23datasets", "is_fake_3d": False}, SHAPE1, SHAPE1], + [{"network_type": "medicalnet_resnet50_23datasets", "is_fake_3d": False}, SHAPE2, SHAPE2], + [{"network_type": "resnet50", "is_fake_3d": True, "pretrained": True, "fake_3d_ratio": 0.2}, SHAPE1, SHAPE1], ] +def _skip_if_no_lpips(network_type): + if not has_lpips and network_type.lower() in {"vgg", "alex", "squeeze"}: + raise unittest.SkipTest(f"LPIPS needed for this test when using {network_type=}.") + + @unittest.skipUnless(has_torchvision, "Requires torchvision") -@skip_if_quick class TestPerceptualLoss(unittest.TestCase): @parameterized.expand(TEST_CASES) + @skip_if_quick def test_shape(self, input_param, input_shape, target_shape): + _skip_if_no_lpips(input_param["network_type"]) with skip_if_downloading_fails(): - loss = PerceptualLoss(**input_param) + loss = PerceptualLoss(spatial_dims=len(input_shape) - 2, **input_param) result = loss(torch.randn(input_shape), torch.randn(target_shape)) if "channel_wise" in input_param.keys() and input_param["channel_wise"]: @@ -89,9 +63,11 @@ def test_shape(self, input_param, input_shape, target_shape): self.assertEqual(result.shape, torch.Size([])) @parameterized.expand(TEST_CASES) + @skip_if_quick def test_identical_input(self, input_param, input_shape, target_shape): + _skip_if_no_lpips(input_param["network_type"]) with skip_if_downloading_fails(): - loss = PerceptualLoss(**input_param) + loss = PerceptualLoss(spatial_dims=len(input_shape) - 2, **input_param) tensor = torch.randn(input_shape) result = loss(tensor, tensor) @@ -100,7 +76,9 @@ def test_identical_input(self, input_param, input_shape, target_shape): else: self.assertEqual(result, torch.Tensor([0.0])) + @skip_if_quick def test_different_shape(self): + _skip_if_no_lpips("squeeze") with skip_if_downloading_fails(): loss = PerceptualLoss(spatial_dims=2, network_type="squeeze") tensor = torch.randn(2, 1, 64, 64) @@ -114,17 +92,17 @@ def test_1d(self): @parameterized.expand(["medicalnet_resnet10_23datasets", "medicalnet_resnet50_23datasets"]) def test_medicalnet_on_2d_data(self, network_type): - with self.assertRaises(ValueError): + with self.assertRaisesRegex(ValueError, ".*networks are only compatible with ``spatial_dims=3``.*"): PerceptualLoss(spatial_dims=2, network_type=network_type) @parameterized.expand(["squeeze", "alex", "vgg", "radimagenet_resnet50", "resnet50"]) def test_channel_wise_with_non_medicalnet(self, network_type): - with self.assertRaises(ValueError): + with self.assertRaisesRegex(ValueError, "Channel-wise loss is only compatible with MedicalNet networks."): PerceptualLoss(spatial_dims=2, network_type=network_type, channel_wise=True) - @parameterized.expand(["squeeze", "alex", "vgg", "radimagenet_resnet50", "resnet50"]) + @parameterized.expand(["squeeze", "alex", "vgg", "resnet50"]) def test_non_medicalnet_3d_without_fake_3d(self, network_type): - with self.assertRaises(ValueError): + with self.assertRaisesRegex(ValueError, "Invalid download model name.*"): PerceptualLoss(spatial_dims=3, network_type=network_type, is_fake_3d=False) def test_normalize_tensor_zero_norm_finite_gradient(self): diff --git a/tests/networks/nets/test_senet.py b/tests/networks/nets/test_senet.py index 90d711d0d98..883c4affa67 100644 --- a/tests/networks/nets/test_senet.py +++ b/tests/networks/nets/test_senet.py @@ -11,11 +11,8 @@ from __future__ import annotations -import os import unittest -from pathlib import Path -from typing import TYPE_CHECKING -from unittest import skipUnless +from unittest.mock import patch import torch from parameterized import parameterized @@ -23,17 +20,37 @@ import monai.networks.nets.senet as se_mod from monai.networks import eval_mode from monai.networks.nets import SENet, SENet154, SEResNet50, SEResNet101, SEResNet152, SEResNext50, SEResNext101 -from monai.utils import optional_import -from tests.test_utils import test_is_quick, test_pretrained_networks, test_script_save, testing_data_config +from tests.test_utils import TESTING_DATA_PATH, test_pretrained_networks, test_script_save, testing_data_config -if TYPE_CHECKING: - import pretrainedmodels +device = "cuda" if torch.cuda.is_available() else "cpu" - has_cadene_pretrain = True -else: - pretrainedmodels, has_cadene_pretrain = optional_import("pretrainedmodels") +TESTING_DATA_URLS = { + "senet154": { + "url": testing_data_config("models", "senet154-c7b49a05", "url"), + "filename": TESTING_DATA_PATH / "senet154-c7b49a05.pth", + }, + "se_resnet50": { + "url": testing_data_config("models", "se_resnet50-ce0d4300", "url"), + "filename": TESTING_DATA_PATH / "se_resnet50-ce0d4300.pth", + }, + "se_resnet101": { + "url": testing_data_config("models", "se_resnet101-7e38fcc6", "url"), + "filename": TESTING_DATA_PATH / "se_resnet101-7e38fcc6.pth", + }, + "se_resnet152": { + "url": testing_data_config("models", "se_resnet152-d17c99b7", "url"), + "filename": TESTING_DATA_PATH / "se_resnet152-d17c99b7.pth", + }, + "se_resnext50_32x4d": { + "url": testing_data_config("models", "se_resnext50_32x4d-a260b3a4", "url"), + "filename": TESTING_DATA_PATH / "se_resnext50_32x4d-a260b3a4.pth", + }, + "se_resnext101_32x4d": { + "url": testing_data_config("models", "se_resnext101_32x4d-3b2fe3d8", "url"), + "filename": TESTING_DATA_PATH / "se_resnext101_32x4d-3b2fe3d8.pth", + }, +} -device = "cuda" if torch.cuda.is_available() else "cpu" NET_ARGS = {"spatial_dims": 3, "in_channels": 2, "num_classes": 2} TEST_CASE_1 = [SENet154, NET_ARGS] @@ -42,18 +59,7 @@ TEST_CASE_4 = [SEResNet152, NET_ARGS] TEST_CASE_5 = [SEResNext50, NET_ARGS] TEST_CASE_6 = [SEResNext101, NET_ARGS] -TEST_CASE_7 = [ - SENet, - { - "spatial_dims": 3, - "in_channels": 2, - "num_classes": 2, - "block": "se_bottleneck", - "layers": (3, 8, 36, 3), - "groups": 64, - "reduction": 16, - }, -] +TEST_CASE_7 = [SENet, {**NET_ARGS, "block": "se_bottleneck", "layers": (3, 8, 36, 3), "groups": 64, "reduction": 16}] TEST_CASE_PRETRAINED_1 = [SEResNet50, {"spatial_dims": 2, "in_channels": 3, "num_classes": 2, "pretrained": True}] @@ -76,52 +82,8 @@ def test_script(self, net, net_args): class TestPretrainedSENET(unittest.TestCase): - def setUp(self): - self.original_urls = se_mod.SE_NET_MODELS.copy() - replace_url = test_is_quick() - if not replace_url: - try: - SEResNet50(pretrained=True, spatial_dims=2, in_channels=3, num_classes=2) - except OSError as rt_e: - print(rt_e) - if "certificate" in str(rt_e): # [SSL: CERTIFICATE_VERIFY_FAILED] - replace_url = True - if replace_url: - testing_dir = Path(__file__).parents[2] / "testing_data" - testing_data_urls = { - "senet154": { - "url": testing_data_config("models", "senet154-c7b49a05", "url"), - "filename": "senet154-c7b49a05.pth", - }, - "se_resnet50": { - "url": testing_data_config("models", "se_resnet50-ce0d4300", "url"), - "filename": "se_resnet50-ce0d4300.pth", - }, - "se_resnet101": { - "url": testing_data_config("models", "se_resnet101-7e38fcc6", "url"), - "filename": "se_resnet101-7e38fcc6.pth", - }, - "se_resnet152": { - "url": testing_data_config("models", "se_resnet152-d17c99b7", "url"), - "filename": "se_resnet152-d17c99b7.pth", - }, - "se_resnext50_32x4d": { - "url": testing_data_config("models", "se_resnext50_32x4d-a260b3a4", "url"), - "filename": "se_resnext50_32x4d-a260b3a4.pth", - }, - "se_resnext101_32x4d": { - "url": testing_data_config("models", "se_resnext101_32x4d-3b2fe3d8", "url"), - "filename": "se_resnext101_32x4d-3b2fe3d8.pth", - }, - } - for item in testing_data_urls: - testing_data_urls[item]["filename"] = os.path.join(testing_dir, testing_data_urls[item]["filename"]) - se_mod.SE_NET_MODELS = testing_data_urls - - def tearDown(self): - se_mod.SE_NET_MODELS = self.original_urls.copy() - @parameterized.expand([TEST_CASE_PRETRAINED_1]) + @patch.dict(se_mod.SE_NET_MODELS, TESTING_DATA_URLS) def test_senet_shape(self, model, input_param): net = test_pretrained_networks(model, input_param, device) input_data = torch.randn(3, 3, 64, 64).to(device) @@ -131,21 +93,6 @@ def test_senet_shape(self, model, input_param): result = net(input_data) self.assertEqual(result.shape, expected_shape) - @parameterized.expand([TEST_CASE_PRETRAINED_1]) - @skipUnless(has_cadene_pretrain, "Requires `pretrainedmodels` package.") - def test_pretrain_consistency(self, model, input_param): - input_data = torch.randn(1, 3, 64, 64).to(device) - net = test_pretrained_networks(model, input_param, device) - with eval_mode(net): - result = net.features(input_data) - cadene_net = pretrainedmodels.se_resnet50().to(device) - with eval_mode(cadene_net): - expected_result = cadene_net.features(input_data) - # The difference between Cadene's senet and our version is that - # we use nn.Linear as the FC layer, but Cadene's version uses - # a conv layer with kernel size equals to 1. It may bring a little difference. - self.assertTrue(torch.allclose(result, expected_result, rtol=1e-5, atol=1e-5)) - if __name__ == "__main__": unittest.main() diff --git a/tests/test_utils.py b/tests/test_utils.py index 9d245f9778a..e717a9316d4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -37,7 +37,7 @@ from pathlib import Path from subprocess import PIPE, Popen from typing import Any -from urllib.error import ContentTooShortError, HTTPError +from urllib.error import ContentTooShortError, HTTPError, URLError import numpy as np import torch @@ -71,7 +71,11 @@ MODULE_PATH = Path(__file__).resolve().parents[1] -DOWNLOAD_EXCEPTS: tuple[type, ...] = (ContentTooShortError, HTTPError, ConnectionError) +TESTS_PATH = MODULE_PATH / "tests" + +TESTING_DATA_PATH = TESTS_PATH / "testing_data" + +DOWNLOAD_EXCEPTS: tuple[type, ...] = (ContentTooShortError, URLError, HTTPError, ConnectionError) if has_req: DOWNLOAD_EXCEPTS += (http_error,) if has_gdown: @@ -94,7 +98,7 @@ def testing_data_config(*keys): """get _test_data_config[keys0][keys1]...[keysN]""" if not _test_data_config: - with open(f"{MODULE_PATH}/tests/testing_data/data_config.json") as c: + with open(f"{TESTING_DATA_PATH}/data_config.json") as c: _config = json.load(c) for k, v in _config.items(): _test_data_config[k] = v diff --git a/tests/testing_data/matshow3d_patch_test.png b/tests/testing_data/matshow3d_patch_test.png index 0a4632a7639..6db22096b4e 100644 Binary files a/tests/testing_data/matshow3d_patch_test.png and b/tests/testing_data/matshow3d_patch_test.png differ diff --git a/tests/testing_data/matshow3d_rgb_test.png b/tests/testing_data/matshow3d_rgb_test.png index 7c8e224c0eb..fb291c7c4d5 100644 Binary files a/tests/testing_data/matshow3d_rgb_test.png and b/tests/testing_data/matshow3d_rgb_test.png differ diff --git a/tests/testing_data/matshow3d_test.png b/tests/testing_data/matshow3d_test.png index d720a0c4071..9900c1bdcf6 100644 Binary files a/tests/testing_data/matshow3d_test.png and b/tests/testing_data/matshow3d_test.png differ diff --git a/tests/testing_data/threadcontainer_plot_test.png b/tests/testing_data/threadcontainer_plot_test.png index af742a88127..d439cc24512 100644 Binary files a/tests/testing_data/threadcontainer_plot_test.png and b/tests/testing_data/threadcontainer_plot_test.png differ diff --git a/tests/visualize/utils/test_matshow3d.py b/tests/visualize/utils/test_matshow3d.py index f10f1ea79de..2328dd6651e 100644 --- a/tests/visualize/utils/test_matshow3d.py +++ b/tests/visualize/utils/test_matshow3d.py @@ -11,10 +11,8 @@ from __future__ import annotations -import os import tempfile import unittest -from pathlib import Path import numpy as np @@ -28,90 +26,68 @@ ) from monai.utils import optional_import from monai.visualize.utils import matshow3d -from tests.test_utils import SkipIfNoModule +from tests.test_utils import TESTING_DATA_PATH, SkipIfNoModule compare_images, _ = optional_import("matplotlib.testing.compare", name="compare_images") -pyplot, has_pyplot = optional_import("matplotlib", name="pyplot") +plt, has_pyplot = optional_import("matplotlib", name="pyplot") + +TOL = 5e-2 + +KEYS = "image" + +BASE_XFORMS = [ + LoadImaged(keys=KEYS), + EnsureChannelFirstd(keys=KEYS, channel_dim="no_channel"), + ScaleIntensityd(keys=KEYS), +] + +INPUT_IMAGE = TESTING_DATA_PATH / "anatomical.nii" @SkipIfNoModule("matplotlib") class TestMatshow3d(unittest.TestCase): def test_3d(self): - test_root = Path(__file__).parents[2] - testing_dir = os.path.join(test_root, "testing_data") - print("test_root: ", testing_dir) - keys = "image" - xforms = Compose( - [ - LoadImaged(keys=keys), - EnsureChannelFirstd(keys=keys, channel_dim="no_channel"), - ScaleIntensityd(keys=keys), - ] - ) - image_path = os.path.join(testing_dir, "anatomical.nii") - ims = xforms({keys: image_path}) + xforms = Compose(BASE_XFORMS) + ims = xforms({KEYS: INPUT_IMAGE}) - fig = pyplot.figure() # external figure - fig, _ = matshow3d(ims[keys], fig=fig, figsize=(2, 2), frames_per_row=5, every_n=2, frame_dim=-1, show=False) + fig = plt.figure() # external figure + fig, _ = matshow3d(ims[KEYS], fig=fig, figsize=(2, 2), frames_per_row=5, every_n=2, frame_dim=-1, show=False) with tempfile.TemporaryDirectory() as tempdir: tempimg = f"{tempdir}/matshow3d_test.png" fig.savefig(tempimg) - comp = compare_images(f"{testing_dir}/matshow3d_test.png", tempimg, 5e-2) - self.assertIsNone(comp, f"value of comp={comp}") # None indicates test passed + comp = compare_images(TESTING_DATA_PATH / "matshow3d_test.png", tempimg, TOL) + self.assertIsNone(comp, f"compare_images result: {comp}") # None indicates test passed - _, axes = pyplot.subplots() - matshow3d(ims[keys], fig=axes, figsize=(2, 2), frames_per_row=5, every_n=2, frame_dim=-1, show=False) + _, axes = plt.subplots() + matshow3d(ims[KEYS], fig=axes, figsize=(2, 2), frames_per_row=5, every_n=2, frame_dim=-1, show=False) def test_samples(self): - test_root = Path(__file__).parents[2] - testing_dir = os.path.join(test_root, "testing_data") - keys = "image" xforms = Compose( - [ - LoadImaged(keys=keys), - EnsureChannelFirstd(keys=keys), - ScaleIntensityd(keys=keys), - RandSpatialCropSamplesd(keys=keys, roi_size=(8, 8, 5), random_size=True, num_samples=10), - ] + [*BASE_XFORMS, RandSpatialCropSamplesd(keys=KEYS, roi_size=(8, 8, 5), random_size=True, num_samples=10)] ) - image_path = os.path.join(testing_dir, "anatomical.nii") xforms.set_random_state(0) - ims = xforms({keys: image_path}) - fig, mat = matshow3d( - [im[keys] for im in ims], title=f"testing {keys}", figsize=(2, 2), frames_per_row=5, every_n=2, show=False - ) + ims = [im[KEYS] for im in xforms({KEYS: INPUT_IMAGE})] + fig, mat = matshow3d(ims, title=f"testing {KEYS}", figsize=(2, 2), frames_per_row=5, every_n=2, show=False) self.assertEqual(mat.dtype, np.float32) with tempfile.TemporaryDirectory() as tempdir: tempimg = f"{tempdir}/matshow3d_patch_test.png" fig.savefig(tempimg) - comp = compare_images(f"{testing_dir}/matshow3d_patch_test.png", tempimg, 5e-2, in_decorator=True) + comp = compare_images(TESTING_DATA_PATH / "matshow3d_patch_test.png", tempimg, TOL, in_decorator=True) if comp: - print("not none comp: ", comp) # matplotlib 3.2.2 - np.testing.assert_allclose(comp["rms"], 30.786983, atol=1e-3, rtol=1e-3) - else: - self.assertIsNone(comp, f"value of comp={comp}") # None indicates test passed + # FIXME: tolerances are too high after changes to Matplotlib around version 3.11 + np.testing.assert_allclose( + comp["rms"], 30.786983, atol=3, rtol=0.1, err_msg=f"compare_images result: {comp}" + ) def test_3d_rgb(self): - test_dir = Path(__file__).parents[2].as_posix() - testing_dir = os.path.join(test_dir, "testing_data") - keys = "image" - xforms = Compose( - [ - LoadImaged(keys=keys), - EnsureChannelFirstd(keys=keys, channel_dim="no_channel"), - ScaleIntensityd(keys=keys), - # change to RGB color image - RepeatChanneld(keys=keys, repeats=3), - ] - ) - image_path = os.path.join(testing_dir, "anatomical.nii") - ims = xforms({keys: image_path}) + xforms = Compose([*BASE_XFORMS, RepeatChanneld(keys=KEYS, repeats=3)]) # RepeatChanneld makes a RGB color image + ims = xforms({KEYS: INPUT_IMAGE}) - fig = pyplot.figure() # external figure + fig = plt.figure() # external figure fig, _ = matshow3d( - volume=ims[keys], + volume=ims[KEYS], fig=fig, figsize=(2, 2), frames_per_row=5, @@ -125,8 +101,8 @@ def test_3d_rgb(self): with tempfile.TemporaryDirectory() as tempdir: tempimg = f"{tempdir}/matshow3d_rgb_test.png" fig.savefig(tempimg) - comp = compare_images(f"{testing_dir}/matshow3d_rgb_test.png", tempimg, 5e-2) - self.assertIsNone(comp, f"value of comp={comp}") # None indicates test passed + comp = compare_images(TESTING_DATA_PATH / "matshow3d_rgb_test.png", tempimg, TOL) + self.assertIsNone(comp, f"compare_images result: {comp}") # None indicates test passed if __name__ == "__main__":