Skip to content

Commit 28815db

Browse files
authored
🐞 fix(utils): revert OptionalImport class to handle missing optional dependencies (#2956)
* fix(data): enable pin_memory for DataLoader instances across the codebase This commit updates various DataLoader instances in the project to enable the option, enhancing performance for data loading on GPU. Changes were made in the following files: - : Updated train and test DataLoader configurations. - : Modified datamodule DataLoader to include . - : Added to evaluation DataLoader. - : Updated DataLoader for datasets to utilize . - : Enabled for reference dataset DataLoader. - : Adjusted inference DataLoader to include . These changes aim to optimize memory usage and improve data transfer speeds during model training and inference. Signed-off-by: samet-akcay <samet.akcay@intel.com> * refactor(models): streamline decoder retrieval in function This commit refactors the function in to utilize a dictionary mapping for decoder architectures, improving readability and maintainability. The previous conditional checks have been replaced with a more efficient approach, enhancing the overall structure of the code. Signed-off-by: samet-akcay <samet.akcay@intel.com> * fix(model): update logit_scale initialization to use torch.log for consistency This commit modifies the initialization of the logit_scale parameter in the CLIP model to utilize torch.log instead of np.log. This change ensures consistency in tensor operations and improves compatibility with PyTorch's computation graph. Signed-off-by: samet-akcay <samet.akcay@intel.com> * fix(model): update anomaly map generation to use torch tensors for calculations This commit modifies the anomaly map generation logic to utilize PyTorch tensors instead of NumPy arrays for various calculations. This change enhances compatibility with the PyTorch computation graph and improves performance by leveraging GPU acceleration. Key updates include the conversion of statistical calculations and tensor operations to use PyTorch functions, ensuring consistency in tensor handling throughout the code. Signed-off-by: samet-akcay <samet.akcay@intel.com> * refactor(model): enhance anomaly map generation with PyTorch for statistical calculations This commit refactors the anomaly map generation logic to replace NumPy-based statistical calculations with PyTorch equivalents, specifically using the distribution for computing tau. Additionally, it improves precision handling by allowing the use of float64 in high precision mode. The changes streamline the computation process and maintain compatibility with the PyTorch computation graph. Signed-off-by: samet-akcay <samet.akcay@intel.com> * chore(license): update license year Signed-off-by: samet-akcay <samet.akcay@intel.com> * fix(download): enhance URL validation and update download logic This commit improves the URL validation in the download function to ensure only http and https schemes are allowed. Additionally, it adds comments to clarify the safety of using under these conditions, enhancing code readability and security awareness. Signed-off-by: samet-akcay <samet.akcay@intel.com> * 🗑️ chore(imports): remove OptionalImport class for optional dependency handling Signed-off-by: samet-akcay <samet.akcay@intel.com> * 🐛 fix(imports): Replace OptionalImport with dummy classes for missing dependencies This commit removes the OptionalImport class and introduces dummy classes for various loggers and video utilities when their respective dependencies are not installed. The changes enhance error messaging by providing clear instructions for installation, improving user experience when encountering missing packages. Affected files: - video.py - comet.py - mlflow.py - tensorboard.py - wandb.py - ollama.py Signed-off-by: samet-akcay <samet.akcay@intel.com> --------- Signed-off-by: samet-akcay <samet.akcay@intel.com>
1 parent bf7e5bc commit 28815db

File tree

7 files changed

+60
-94
lines changed

7 files changed

+60
-94
lines changed

src/anomalib/data/utils/video.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@
3333
from lightning_utilities.core.imports import module_available
3434

3535
from anomalib.data import VideoItem
36-
from anomalib.utils.imports import OptionalImport
3736

3837
if TYPE_CHECKING or module_available("av"):
3938
from torchvision.datasets.video_utils import VideoClips
4039
else:
41-
VideoClips = OptionalImport(
42-
"av",
43-
"uv pip install av",
44-
"or `uv pip install anomalib[video]`",
45-
)
40+
41+
class VideoClips:
42+
"""Dummy VideoClips class for when av is not installed."""
43+
44+
def __init__(self, *args, **kwargs) -> None: # noqa: ARG002
45+
msg = (
46+
"av is not installed. Please install it using: `uv pip install av` or `uv pip install anomalib[video]`"
47+
)
48+
raise ImportError(msg)
4649

4750

4851
class ClipsIndexer(VideoClips, ABC):

src/anomalib/loggers/comet.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@
3030
from lightning_utilities.core.imports import module_available
3131
from matplotlib.figure import Figure
3232

33-
from anomalib.utils.imports import OptionalImport
34-
3533
from .base import ImageLoggerBase
3634

3735
if TYPE_CHECKING or module_available("comet_ml"):
3836
from lightning.pytorch.loggers.comet import CometLogger
3937
else:
40-
CometLogger = OptionalImport(
41-
"comet-ml",
42-
"uv pip install comet-ml",
43-
"or `uv pip install anomalib[loggers]`",
44-
)
38+
39+
class CometLogger:
40+
"""Dummy CometLogger class for when comet-ml is not installed."""
41+
42+
def __init__(self, *args, **kwargs) -> None: # noqa: ARG002
43+
msg = (
44+
"comet-ml is not installed. Please install it using: "
45+
"`uv pip install comet-ml` or `uv pip install anomalib[loggers]`"
46+
)
47+
raise ImportError(msg)
4548

4649

4750
class AnomalibCometLogger(ImageLoggerBase, CometLogger):

src/anomalib/loggers/mlflow.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@
3030
from lightning_utilities.core.imports import module_available
3131
from matplotlib.figure import Figure
3232

33-
from anomalib.utils.imports import OptionalImport
34-
3533
from .base import ImageLoggerBase
3634

3735
if TYPE_CHECKING or module_available("mlflow"):
3836
from lightning.pytorch.loggers.mlflow import MLFlowLogger
3937
else:
40-
MLFlowLogger = OptionalImport(
41-
"mlflow",
42-
"uv pip install mlflow",
43-
"or `uv pip install anomalib[loggers]`",
44-
)
38+
39+
class MLFlowLogger:
40+
"""Dummy MLFlowLogger class for when mlflow is not installed."""
41+
42+
def __init__(self, *args, **kwargs) -> None: # noqa: ARG002
43+
msg = (
44+
"mlflow is not installed. Please install it using: "
45+
"`uv pip install mlflow` or `uv pip install anomalib[loggers]`"
46+
)
47+
raise ImportError(msg)
4548

4649

4750
class AnomalibMLFlowLogger(ImageLoggerBase, MLFlowLogger):

src/anomalib/loggers/tensorboard.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,21 @@
3131
from lightning_utilities.core.imports import module_available
3232
from matplotlib.figure import Figure
3333

34-
from anomalib.utils.imports import OptionalImport
35-
3634
from .base import ImageLoggerBase
3735

3836
if TYPE_CHECKING or module_available("tensorboard"):
3937
from lightning.pytorch.loggers.tensorboard import TensorBoardLogger
4038
else:
41-
TensorBoardLogger = OptionalImport(
42-
"tensorboard",
43-
"uv pip install tensorboard",
44-
"or `uv pip install anomalib[loggers]`",
45-
)
39+
40+
class TensorBoardLogger:
41+
"""Dummy TensorBoardLogger class for when tensorboard is not installed."""
42+
43+
def __init__(self, *args, **kwargs) -> None: # noqa: ARG002
44+
msg = (
45+
"tensorboard is not installed. Please install it using: "
46+
"`uv pip install tensorboard` or `uv pip install anomalib[loggers]`"
47+
)
48+
raise ImportError(msg)
4649

4750

4851
class AnomalibTensorBoardLogger(ImageLoggerBase, TensorBoardLogger):

src/anomalib/loggers/wandb.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@
3030
from lightning_utilities.core.imports import module_available
3131
from matplotlib.figure import Figure
3232

33-
from anomalib.utils.imports import OptionalImport
34-
3533
from .base import ImageLoggerBase
3634

3735
if TYPE_CHECKING or module_available("wandb"):
3836
import wandb
3937
from lightning.pytorch.loggers.wandb import WandbLogger
4038
else:
4139
wandb = None
42-
WandbLogger = OptionalImport(
43-
"wandb",
44-
"uv pip install wandb",
45-
"or `uv pip install anomalib[loggers]`",
46-
)
40+
41+
class WandbLogger:
42+
"""Dummy WandbLogger class for when wandb is not installed."""
43+
44+
def __init__(self, *args, **kwargs) -> None: # noqa: ARG002
45+
msg = (
46+
"wandb is not installed. Please install it using: "
47+
"`uv pip install wandb` or `uv pip install anomalib[loggers]`"
48+
)
49+
raise ImportError(msg)
4750

4851

4952
if TYPE_CHECKING:

src/anomalib/models/image/vlm_ad/backends/ollama.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,23 @@
4141
from lightning_utilities.core.imports import module_available
4242

4343
from anomalib.models.image.vlm_ad.utils import Prompt
44-
from anomalib.utils.imports import OptionalImport
4544

4645
from .base import Backend
4746

4847
if TYPE_CHECKING or module_available("ollama"):
4948
from ollama import Image, chat
5049
else:
51-
Image = OptionalImport(
52-
"ollama",
53-
"uv pip install ollama",
54-
"or `uv pip install anomalib[vlm]`",
55-
)
50+
51+
class Image:
52+
"""Dummy Image class for when ollama is not installed."""
53+
54+
def __init__(self, *args, **kwargs) -> None: # noqa: ARG002
55+
msg = (
56+
"ollama is not installed. Please install it using: "
57+
"`uv pip install ollama` or `uv pip install anomalib[vlm]`"
58+
)
59+
raise ImportError(msg)
60+
5661
chat = None
5762

5863

src/anomalib/utils/imports.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)