From 3faaca51ded8b32ff4b0e488a773a6c41cb70496 Mon Sep 17 00:00:00 2001 From: Mark Graham Date: Wed, 19 Jul 2023 15:22:31 +0100 Subject: [PATCH] Adds external dependency to relevant files, excludes from min tests and uses optional_import to import it --- docs/source/installation.md | 4 ++-- monai/losses/perceptual.py | 5 ++++- setup.cfg | 3 +++ tests/min_tests.py | 1 + 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/source/installation.md b/docs/source/installation.md index eb7adb06fb..53e5167346 100644 --- a/docs/source/installation.md +++ b/docs/source/installation.md @@ -254,10 +254,10 @@ Since MONAI v0.2.0, the extras syntax such as `pip install 'monai[nibabel]'` is - The options are ``` -[nibabel, skimage, pillow, tensorboard, gdown, ignite, torchvision, itk, tqdm, lmdb, psutil, cucim, openslide, pandas, einops, transformers, mlflow, clearml, matplotlib, tensorboardX, tifffile, imagecodecs, pyyaml, fire, jsonschema, ninja, pynrrd, pydicom, h5py, nni, optuna, onnx, onnxruntime, zarr] +[nibabel, skimage, pillow, tensorboard, gdown, ignite, torchvision, itk, tqdm, lmdb, psutil, cucim, openslide, pandas, einops, transformers, mlflow, clearml, matplotlib, tensorboardX, tifffile, imagecodecs, pyyaml, fire, jsonschema, ninja, pynrrd, pydicom, h5py, nni, optuna, onnx, onnxruntime, zarr, lpips] ``` which correspond to `nibabel`, `scikit-image`, `pillow`, `tensorboard`, -`gdown`, `pytorch-ignite`, `torchvision`, `itk`, `tqdm`, `lmdb`, `psutil`, `cucim`, `openslide-python`, `pandas`, `einops`, `transformers`, `mlflow`, `clearml`, `matplotlib`, `tensorboardX`, `tifffile`, `imagecodecs`, `pyyaml`, `fire`, `jsonschema`, `ninja`, `pynrrd`, `pydicom`, `h5py`, `nni`, `optuna`, `onnx`, `onnxruntime`, and `zarr` respectively. +`gdown`, `pytorch-ignite`, `torchvision`, `itk`, `tqdm`, `lmdb`, `psutil`, `cucim`, `openslide-python`, `pandas`, `einops`, `transformers`, `mlflow`, `clearml`, `matplotlib`, `tensorboardX`, `tifffile`, `imagecodecs`, `pyyaml`, `fire`, `jsonschema`, `ninja`, `pynrrd`, `pydicom`, `h5py`, `nni`, `optuna`, `onnx`, `onnxruntime`, `zarr` and `lpips` respectively. - `pip install 'monai[all]'` installs all the optional dependencies. diff --git a/monai/losses/perceptual.py b/monai/losses/perceptual.py index 8fffb1c870..73d2e83bfa 100644 --- a/monai/losses/perceptual.py +++ b/monai/losses/perceptual.py @@ -13,7 +13,10 @@ import torch import torch.nn as nn -from lpips import LPIPS + +from monai.utils import optional_import + +LPIPS, _ = optional_import("lpips", name="LPIPS") from torchvision.models import ResNet50_Weights, resnet50 from torchvision.models.feature_extraction import create_feature_extractor diff --git a/setup.cfg b/setup.cfg index c218b133ee..5d29d2bce9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -80,6 +80,7 @@ all = onnx>=1.13.0 onnxruntime; python_version <= '3.10' zarr + lpips==0.1.4 nibabel = nibabel ninja = @@ -145,6 +146,8 @@ onnx = onnxruntime; python_version <= '3.10' zarr = zarr +lpips = + lpips==0.1.4 # # workaround https://github.com/Project-MONAI/MONAI/issues/5882 # MetricsReloaded = # MetricsReloaded @ git+https://github.com/Project-MONAI/MetricsReloaded@monai-support#egg=MetricsReloaded diff --git a/tests/min_tests.py b/tests/min_tests.py index e3b09e7c84..e93652d215 100644 --- a/tests/min_tests.py +++ b/tests/min_tests.py @@ -204,6 +204,7 @@ def run_testsuit(): "test_spatial_combine_transforms", "test_bundle_workflow", "test_zarr_avg_merger", + "test_perceptual_loss", ] assert sorted(exclude_cases) == sorted(set(exclude_cases)), f"Duplicated items in {exclude_cases}"