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

πŸ”¨ Refactor Engine.predict method #1772

Merged
merged 6 commits into from
Feb 27, 2024
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
4 changes: 4 additions & 0 deletions src/anomalib/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
_LIGHTNING_AVAILABLE = True
try:
from lightning.pytorch import Trainer
from lightning.pytorch.core.datamodule import LightningDataModule
from torch.utils.data import DataLoader, Dataset

from anomalib.data import AnomalibDataModule, AnomalibDataset
Expand Down Expand Up @@ -296,6 +297,9 @@ def instantiate_classes(self) -> None:
# the minor change here is that engine is instantiated instead of trainer
self.config_init = self.parser.instantiate_classes(self.config)
self.datamodule = self._get(self.config_init, "data")
if isinstance(self.datamodule, Dataset):
kwargs = {f"{self.config.subcommand}_dataset": self.datamodule}
self.datamodule = LightningDataModule.from_datasets(**kwargs)
self.model = self._get(self.config_init, "model")
self._configure_optimizers_method_to_model()
self.instantiate_engine()
Expand Down
37 changes: 19 additions & 18 deletions src/anomalib/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def _setup_anomalib_callbacks(self) -> None:
def _should_run_validation(
self,
model: AnomalyModule,
dataloaders: EVAL_DATALOADERS | AnomalibDataModule | None,
dataloaders: EVAL_DATALOADERS | None,
datamodule: AnomalibDataModule | None,
ckpt_path: str | None,
) -> bool:
Expand All @@ -326,7 +326,7 @@ def _should_run_validation(

Args:
model (AnomalyModule): Model passed to the entrypoint.
dataloaders (EVAL_DATALOADERS | AnomalibDataModule | None): Dataloaders 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 | None): Checkpoint path passed to the entrypoint.

Expand All @@ -348,7 +348,7 @@ def _should_run_validation(
def fit(
self,
model: AnomalyModule,
train_dataloaders: TRAIN_DATALOADERS | AnomalibDataModule | None = None,
train_dataloaders: TRAIN_DATALOADERS | None = None,
val_dataloaders: EVAL_DATALOADERS | None = None,
datamodule: AnomalibDataModule | None = None,
ckpt_path: str | None = None,
Expand All @@ -357,7 +357,7 @@ def fit(

Args:
model (AnomalyModule): Model to be trained.
train_dataloaders (TRAIN_DATALOADERS | AnomalibDataModule | None, optional): Train dataloaders.
train_dataloaders (TRAIN_DATALOADERS | None, optional): Train dataloaders.
Defaults to None.
val_dataloaders (EVAL_DATALOADERS | None, optional): Validation dataloaders.
Defaults to None.
Expand Down Expand Up @@ -392,7 +392,7 @@ def fit(
def validate(
self,
model: AnomalyModule | None = None,
dataloaders: EVAL_DATALOADERS | AnomalibDataModule | None = None,
dataloaders: EVAL_DATALOADERS | None = None,
ckpt_path: str | None = None,
verbose: bool = True,
datamodule: AnomalibDataModule | None = None,
Expand All @@ -402,7 +402,7 @@ def validate(
Args:
model (AnomalyModule | None, optional): Model to be validated.
Defaults to None.
dataloaders (EVAL_DATALOADERS | AnomalibDataModule | None, optional): Dataloaders to be used for
dataloaders (EVAL_DATALOADERS | None, optional): Dataloaders to be used for
validation.
Defaults to None.
ckpt_path (str | None, optional): Checkpoint path. If provided, the model will be loaded from this path.
Expand Down Expand Up @@ -439,7 +439,7 @@ def validate(
def test(
self,
model: AnomalyModule | None = None,
dataloaders: EVAL_DATALOADERS | AnomalibDataModule | None = None,
dataloaders: EVAL_DATALOADERS | None = None,
ckpt_path: str | None = None,
verbose: bool = True,
datamodule: AnomalibDataModule | None = None,
Expand All @@ -453,7 +453,7 @@ def test(
model (AnomalyModule | None, optional):
The model to be tested.
Defaults to None.
dataloaders (EVAL_DATALOADERS | AnomalibDataModule | None, optional):
dataloaders (EVAL_DATALOADERS | None, optional):
An iterable or collection of iterables specifying test samples.
Defaults to None.
ckpt_path (str | None, optional):
Expand Down Expand Up @@ -526,13 +526,12 @@ def test(
self.trainer.validate(model, dataloaders, None, verbose=False, datamodule=datamodule)
return self.trainer.test(model, dataloaders, ckpt_path, verbose, datamodule)

# TODO(ashwinvaidya17): revisit typing of data args
# https://github.com/openvinotoolkit/anomalib/issues/1638
def predict(
self,
model: AnomalyModule | None = None,
dataloaders: EVAL_DATALOADERS | AnomalibDataModule | None = None,
datamodule: AnomalibDataModule | Dataset | PredictDataset | None = None,
dataloaders: EVAL_DATALOADERS | None = None,
datamodule: AnomalibDataModule | None = None,
dataset: Dataset | PredictDataset | None = None,
return_predictions: bool | None = None,
ckpt_path: str | None = None,
) -> _PREDICT_OUTPUT | None:
Expand All @@ -545,14 +544,17 @@ def predict(
model (AnomalyModule | None, optional):
Model to be used for prediction.
Defaults to None.
dataloaders (EVAL_DATALOADERS | AnomalibDataModule | None, optional):
dataloaders (EVAL_DATALOADERS | None, optional):
An iterable or collection of iterables specifying predict samples.
Defaults to None.
datamodule (AnomalibDataModule | None, optional):
A :class:`~lightning.pytorch.core.datamodule.AnomalibDataModule` that defines
the :class:`~lightning.pytorch.core.hooks.DataHooks.predict_dataloader` hook.
The datamodule can also be a dataset that will be wrapped in a torch Dataloader.
Defaults to None.
dataset (Dataset | PredictDataset | None, optional):
A :class:`~torch.utils.data.Dataset` or :class:`~anomalib.data.PredictDataset` that will be used
to create a dataloader. Defaults to None.
return_predictions (bool | None, optional):
Whether to return predictions.
``True`` by default except when an accelerator that spawns processes is used (not supported).
Expand Down Expand Up @@ -598,9 +600,8 @@ def predict(
logger.warning("ckpt_path is not provided. Model weights will not be loaded.")

# Handle the instance when a dataset is passed to the predict method
if datamodule is not None and isinstance(datamodule, Dataset):
dataloader = DataLoader(datamodule)
datamodule = None
if dataset is not None:
dataloader = DataLoader(dataset)
if dataloaders is None:
dataloaders = dataloader
elif isinstance(dataloaders, DataLoader):
Expand Down Expand Up @@ -628,7 +629,7 @@ def predict(
def train(
self,
model: AnomalyModule,
train_dataloaders: TRAIN_DATALOADERS | AnomalibDataModule | None = None,
train_dataloaders: TRAIN_DATALOADERS | None = None,
val_dataloaders: EVAL_DATALOADERS | None = None,
test_dataloaders: EVAL_DATALOADERS | None = None,
datamodule: AnomalibDataModule | None = None,
Expand All @@ -638,7 +639,7 @@ def train(

Args:
model (AnomalyModule): Model to be trained.
train_dataloaders (TRAIN_DATALOADERS | AnomalibDataModule | None, optional): Train dataloaders.
train_dataloaders (TRAIN_DATALOADERS | None, optional): Train dataloaders.
Defaults to None.
val_dataloaders (EVAL_DATALOADERS | None, optional): Validation dataloaders.
Defaults to None.
Expand Down
Loading