Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename AnomalyModule to AnomalibModule #2423

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/markdown/guides/developer/sdd.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ and depth data.

Anomalib provides a collection of anomaly models within the image and video
domains. The models are implemented sub-classing PyTorch Lightning's
`LightningModule` class, which is called `AnomalyModule`, which provides a set
`LightningModule` class, which is called `AnomalibModule`, which provides a set
of APIs for defining the model architecture, loss function, and optimization
algorithm. The models are designed to be modular and extensible, allowing users
to easily modify the model architecture and training workflow based on their
Expand Down
4 changes: 2 additions & 2 deletions src/anomalib/callbacks/model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import torch
from lightning.pytorch import Callback, Trainer

from anomalib.models.components import AnomalyModule
from anomalib.models.components import AnomalibModule

logger = logging.getLogger(__name__)

Expand All @@ -27,7 +27,7 @@ class LoadModelCallback(Callback):
def __init__(self, weights_path: str) -> None:
self.weights_path = weights_path

def setup(self, trainer: Trainer, pl_module: AnomalyModule, stage: str | None = None) -> None:
def setup(self, trainer: Trainer, pl_module: AnomalibModule, stage: str | None = None) -> None:
"""Call when inference begins.

Loads the model weights from ``weights_path`` into the PyTorch module.
Expand Down
4 changes: 2 additions & 2 deletions src/anomalib/callbacks/tiler_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from lightning.pytorch.callbacks import Callback

from anomalib.data.utils.tiler import ImageUpscaleMode, Tiler
from anomalib.models.components import AnomalyModule
from anomalib.models.components import AnomalibModule

__all__ = ["TilerConfigurationCallback"]

Expand Down Expand Up @@ -61,7 +61,7 @@
del trainer, stage # These variables are not used.

if self.enable:
if isinstance(pl_module, AnomalyModule) and hasattr(pl_module.model, "tiler"):
if isinstance(pl_module, AnomalibModule) and hasattr(pl_module.model, "tiler"):

Check warning on line 64 in src/anomalib/callbacks/tiler_configuration.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/callbacks/tiler_configuration.py#L64

Added line #L64 was not covered by tests
pl_module.model.tiler = Tiler(
tile_size=self.tile_size,
stride=self.stride,
Expand Down
14 changes: 7 additions & 7 deletions src/anomalib/callbacks/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from anomalib.data.utils.image import save_image, show_image
from anomalib.loggers import AnomalibWandbLogger
from anomalib.loggers.base import ImageLoggerBase
from anomalib.models import AnomalyModule
from anomalib.models import AnomalibModule

Check warning on line 19 in src/anomalib/callbacks/visualizer.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/callbacks/visualizer.py#L19

Added line #L19 was not covered by tests
from anomalib.utils.visualization import (
BaseVisualizer,
GeneratorResult,
Expand Down Expand Up @@ -77,7 +77,7 @@
def on_test_batch_end(
self,
trainer: Trainer,
pl_module: AnomalyModule,
pl_module: AnomalibModule,
outputs: STEP_OUTPUT | None,
batch: Any, # noqa: ANN401
batch_idx: int,
Expand Down Expand Up @@ -114,7 +114,7 @@
if self.log:
self._add_to_logger(result, pl_module, trainer)

def on_test_end(self, trainer: Trainer, pl_module: AnomalyModule) -> None:
def on_test_end(self, trainer: Trainer, pl_module: AnomalibModule) -> None:

Check warning on line 117 in src/anomalib/callbacks/visualizer.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/callbacks/visualizer.py#L117

Added line #L117 was not covered by tests
for generator in self.generators:
if generator.visualize_on == VisualizationStep.STAGE_END:
for result in generator(trainer=trainer, pl_module=pl_module):
Expand All @@ -135,28 +135,28 @@
def on_predict_batch_end(
self,
trainer: Trainer,
pl_module: AnomalyModule,
pl_module: AnomalibModule,
outputs: STEP_OUTPUT | None,
batch: Any, # noqa: ANN401
batch_idx: int,
dataloader_idx: int = 0,
) -> None:
return self.on_test_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx)

def on_predict_end(self, trainer: Trainer, pl_module: AnomalyModule) -> None:
def on_predict_end(self, trainer: Trainer, pl_module: AnomalibModule) -> None:

Check warning on line 146 in src/anomalib/callbacks/visualizer.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/callbacks/visualizer.py#L146

Added line #L146 was not covered by tests
return self.on_test_end(trainer, pl_module)

@staticmethod
def _add_to_logger(
result: GeneratorResult,
module: AnomalyModule,
module: AnomalibModule,
trainer: Trainer,
) -> None:
"""Add image to logger.

Args:
result (GeneratorResult): Output from the generators.
module (AnomalyModule): LightningModule from which the global step is extracted.
module (AnomalibModule): LightningModule from which the global step is extracted.
trainer (Trainer): Trainer object.
"""
# Store names of logger and the logger in a dict
Expand Down
10 changes: 5 additions & 5 deletions src/anomalib/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from anomalib.data import AnomalibDataModule
from anomalib.engine import Engine
from anomalib.models import AnomalyModule
from anomalib.models import AnomalibModule
from anomalib.utils.config import update_config

except ImportError:
Expand Down Expand Up @@ -166,7 +166,7 @@ def add_trainer_arguments(self, parser: ArgumentParser, subcommand: str) -> None
self._add_default_arguments_to_parser(parser)
self._add_trainer_arguments_to_parser(parser, add_optimizer=True, add_scheduler=True)
parser.add_subclass_arguments(
AnomalyModule,
AnomalibModule,
"model",
fail_untyped=False,
required=True,
Expand All @@ -186,7 +186,7 @@ def add_train_arguments(self, parser: ArgumentParser) -> None:
self._add_default_arguments_to_parser(parser)
self._add_trainer_arguments_to_parser(parser, add_optimizer=True, add_scheduler=True)
parser.add_subclass_arguments(
AnomalyModule,
AnomalibModule,
"model",
fail_untyped=False,
required=True,
Expand All @@ -205,7 +205,7 @@ def add_predict_arguments(self, parser: ArgumentParser) -> None:
self._add_default_arguments_to_parser(parser)
self._add_trainer_arguments_to_parser(parser)
parser.add_subclass_arguments(
AnomalyModule,
AnomalibModule,
"model",
fail_untyped=False,
required=True,
Expand All @@ -228,7 +228,7 @@ def add_export_arguments(self, parser: ArgumentParser) -> None:
self._add_default_arguments_to_parser(parser)
self._add_trainer_arguments_to_parser(parser)
parser.add_subclass_arguments(
AnomalyModule,
AnomalibModule,
"model",
fail_untyped=False,
required=True,
Expand Down
56 changes: 28 additions & 28 deletions src/anomalib/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from anomalib.callbacks.timer import TimerCallback
from anomalib.data import AnomalibDataModule, AnomalibDataset, PredictDataset
from anomalib.deploy import CompressionType, ExportType
from anomalib.models import AnomalyModule
from anomalib.models import AnomalibModule
from anomalib.utils.path import create_versioned_dir
from anomalib.visualization import ImageVisualizer

Expand Down Expand Up @@ -64,11 +64,11 @@ class _TrainerArgumentsCache:
def __init__(self, **kwargs) -> None:
self._cached_args = {**kwargs}

def update(self, model: AnomalyModule) -> None:
def update(self, model: AnomalibModule) -> None:
"""Replace cached arguments with arguments retrieved from the model.

Args:
model (AnomalyModule): The model used for training
model (AnomalibModule): The model used for training
"""
for key, value in model.trainer_arguments.items():
if key in self._cached_args and self._cached_args[key] != value:
Expand All @@ -77,7 +77,7 @@ def update(self, model: AnomalyModule) -> None:
)
self._cached_args[key] = value

def requires_update(self, model: AnomalyModule) -> bool:
def requires_update(self, model: AnomalibModule) -> bool:
return any(self._cached_args.get(key, None) != value for key, value in model.trainer_arguments.items())

@property
Expand Down Expand Up @@ -152,14 +152,14 @@ def trainer(self) -> Trainer:
return self._trainer

@property
def model(self) -> AnomalyModule:
def model(self) -> AnomalibModule:
"""Property to get the model.

Raises:
UnassignedError: When the model is not assigned yet.

Returns:
AnomalyModule: Anomaly model.
AnomalibModule: Anomaly model.
"""
if not self.trainer.lightning_module:
msg = "Trainer does not have a model assigned yet."
Expand Down Expand Up @@ -190,7 +190,7 @@ def best_model_path(self) -> str | None:

def _setup_workspace(
self,
model: AnomalyModule,
model: AnomalibModule,
train_dataloaders: TRAIN_DATALOADERS | None = None,
val_dataloaders: EVAL_DATALOADERS | None = None,
test_dataloaders: EVAL_DATALOADERS | None = None,
Expand All @@ -205,7 +205,7 @@ def _setup_workspace(
other artifacts will be saved in this directory.

Args:
model (AnomalyModule): Input model.
model (AnomalibModule): Input model.
train_dataloaders (TRAIN_DATALOADERS | None, optional): Train dataloaders.
Defaults to ``None``.
val_dataloaders (EVAL_DATALOADERS | None, optional): Validation dataloaders.
Expand Down Expand Up @@ -255,7 +255,7 @@ def _setup_workspace(
root_dir = Path(self._cache.args["default_root_dir"]) / model.name / dataset_name / category
self._cache.args["default_root_dir"] = create_versioned_dir(root_dir) if versioned_dir else root_dir / "latest"

def _setup_trainer(self, model: AnomalyModule) -> None:
def _setup_trainer(self, model: AnomalibModule) -> None:
"""Instantiate the trainer based on the model parameters."""
# Check if the cache requires an update
if self._cache.requires_update(model):
Expand Down Expand Up @@ -291,7 +291,7 @@ def _setup_dataset_task(
)
data.task = self.task

def _setup_anomalib_callbacks(self, model: AnomalyModule) -> None:
def _setup_anomalib_callbacks(self, model: AnomalibModule) -> None:
"""Set up callbacks for the trainer."""
_callbacks: list[Callback] = []

Expand Down Expand Up @@ -325,7 +325,7 @@ def _setup_anomalib_callbacks(self, model: AnomalyModule) -> None:

@staticmethod
def _should_run_validation(
model: AnomalyModule,
model: AnomalibModule,
ckpt_path: str | Path | None,
) -> bool:
"""Check if we need to run validation to collect normalization statistics and thresholds.
Expand All @@ -341,7 +341,7 @@ def _should_run_validation(
are available. If neither is available, we can't run validation.

Args:
model (AnomalyModule): Model passed to the entrypoint.
model (AnomalibModule): Model passed to the entrypoint.
dataloaders (EVAL_DATALOADERS | None): Dataloaders passed to the entrypoint.
datamodule (AnomalibDataModule | None): Lightning datamodule passed to the entrypoint.
ckpt_path (str | Path | None): Checkpoint path passed to the entrypoint.
Expand All @@ -357,7 +357,7 @@ def _should_run_validation(

def fit(
self,
model: AnomalyModule,
model: AnomalibModule,
train_dataloaders: TRAIN_DATALOADERS | None = None,
val_dataloaders: EVAL_DATALOADERS | None = None,
datamodule: AnomalibDataModule | None = None,
Expand All @@ -366,7 +366,7 @@ def fit(
"""Fit the model using the trainer.

Args:
model (AnomalyModule): Model to be trained.
model (AnomalibModule): Model to be trained.
train_dataloaders (TRAIN_DATALOADERS | None, optional): Train dataloaders.
Defaults to None.
val_dataloaders (EVAL_DATALOADERS | None, optional): Validation dataloaders.
Expand Down Expand Up @@ -411,7 +411,7 @@ def fit(

def validate(
self,
model: AnomalyModule | None = None,
model: AnomalibModule | None = None,
dataloaders: EVAL_DATALOADERS | None = None,
ckpt_path: str | Path | None = None,
verbose: bool = True,
Expand All @@ -420,7 +420,7 @@ def validate(
"""Validate the model using the trainer.

Args:
model (AnomalyModule | None, optional): Model to be validated.
model (AnomalibModule | None, optional): Model to be validated.
Defaults to None.
dataloaders (EVAL_DATALOADERS | None, optional): Dataloaders to be used for
validation.
Expand Down Expand Up @@ -460,7 +460,7 @@ def validate(

def test(
self,
model: AnomalyModule | None = None,
model: AnomalibModule | None = None,
dataloaders: EVAL_DATALOADERS | None = None,
ckpt_path: str | Path | None = None,
verbose: bool = True,
Expand All @@ -472,7 +472,7 @@ def test(
finally tests the model.

Args:
model (AnomalyModule | None, optional):
model (AnomalibModule | None, optional):
The model to be tested.
Defaults to None.
dataloaders (EVAL_DATALOADERS | None, optional):
Expand Down Expand Up @@ -545,7 +545,7 @@ def test(
if model:
self._setup_trainer(model)
elif not self.model:
msg = "`Engine.test()` requires an `AnomalyModule` when it hasn't been passed in a previous run."
msg = "`Engine.test()` requires an `AnomalibModule` when it hasn't been passed in a previous run."
raise RuntimeError(msg)

self._setup_dataset_task(dataloaders)
Expand All @@ -556,7 +556,7 @@ def test(

def predict(
self,
model: AnomalyModule | None = None,
model: AnomalibModule | None = None,
dataloaders: EVAL_DATALOADERS | None = None,
datamodule: AnomalibDataModule | None = None,
dataset: Dataset | PredictDataset | None = None,
Expand All @@ -570,7 +570,7 @@ def predict(
validation dataloader is available. Finally, predicts using the model.

Args:
model (AnomalyModule | None, optional):
model (AnomalibModule | None, optional):
Model to be used for prediction.
Defaults to None.
dataloaders (EVAL_DATALOADERS | None, optional):
Expand Down Expand Up @@ -623,7 +623,7 @@ def predict(
```
"""
if not (model or self.model):
msg = "`Engine.predict()` requires an `AnomalyModule` when it hasn't been passed in a previous run."
msg = "`Engine.predict()` requires an `AnomalibModule` when it hasn't been passed in a previous run."
raise ValueError(msg)

if ckpt_path:
Expand Down Expand Up @@ -668,7 +668,7 @@ def predict(

def train(
self,
model: AnomalyModule,
model: AnomalibModule,
train_dataloaders: TRAIN_DATALOADERS | None = None,
val_dataloaders: EVAL_DATALOADERS | None = None,
test_dataloaders: EVAL_DATALOADERS | None = None,
Expand All @@ -678,7 +678,7 @@ def train(
"""Fits the model and then calls test on it.

Args:
model (AnomalyModule): Model to be trained.
model (AnomalibModule): Model to be trained.
train_dataloaders (TRAIN_DATALOADERS | None, optional): Train dataloaders.
Defaults to None.
val_dataloaders (EVAL_DATALOADERS | None, optional): Validation dataloaders.
Expand Down Expand Up @@ -731,7 +731,7 @@ def train(

def export(
self,
model: AnomalyModule,
model: AnomalibModule,
export_type: ExportType | str,
export_root: str | Path | None = None,
input_size: tuple[int, int] | None = None,
Expand All @@ -744,7 +744,7 @@ def export(
r"""Export the model in PyTorch, ONNX or OpenVINO format.

Args:
model (AnomalyModule): Trained model.
model (AnomalibModule): Trained model.
export_type (ExportType): Export type.
export_root (str | Path | None, optional): Path to the output directory. If it is not set, the model is
exported to trainer.default_root_dir. Defaults to None.
Expand Down Expand Up @@ -832,15 +832,15 @@ def from_config(
cls: type["Engine"],
config_path: str | Path,
**kwargs,
) -> tuple["Engine", AnomalyModule, AnomalibDataModule]:
) -> tuple["Engine", AnomalibModule, AnomalibDataModule]:
"""Create an Engine instance from a configuration file.

Args:
config_path (str | Path): Path to the full configuration file.
**kwargs (dict): Additional keyword arguments.

Returns:
tuple[Engine, AnomalyModule, AnomalibDataModule]: Engine instance.
tuple[Engine, AnomalibModule, AnomalibDataModule]: Engine instance.

Example:
The following example shows training with full configuration file:
Expand Down
Loading
Loading