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

Improve handling of test_split_mode='none' and val_split_mode='none' #801

Merged
merged 3 commits into from
Dec 20, 2022
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
20 changes: 18 additions & 2 deletions anomalib/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from omegaconf import DictConfig, ListConfig, OmegaConf

from anomalib.data.utils import TestSplitMode, ValSplitMode


def _get_now_str(timestamp: float) -> str:
"""Standard format for datetimes is defined here."""
Expand Down Expand Up @@ -176,10 +178,24 @@ def update_datasets_config(config: Union[DictConfig, ListConfig]) -> Union[DictC
warn(
DeprecationWarning(
"The 'split_ratio' parameter is deprecated and will be removed in a future release. Please use "
"'normal_split_ratio' instead."
"'test_split_ratio' instead."
)
)
config.dataset.normal_split_ratio = config.dataset.split_ratio
config.dataset.test_split_ratio = config.dataset.split_ratio

if config.dataset.get("test_split_mode") == TestSplitMode.NONE and config.dataset.get("val_split_mode") in [
ValSplitMode.SAME_AS_TEST,
ValSplitMode.FROM_TEST,
]:
warn(
f"val_split_mode {config.dataset.val_split_mode} not allowed for test_split_mode = 'none'. "
"Setting val_split_mode to 'none'."
)
config.dataset.val_split_mode = ValSplitMode.NONE

if config.dataset.get("val_split_mode") == ValSplitMode.NONE and config.trainer.limit_val_batches != 0.0:
warn("Running without validation set. Setting trainer.limit_val_batches to 0.")
config.trainer.limit_val_batches = 0.0
return config


Expand Down
7 changes: 4 additions & 3 deletions anomalib/data/base/datamodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ def _create_test_split(self):
if self.test_data.has_normal:
# split the test data into normal and anomalous so these can be processed separately
normal_test_data, self.test_data = split_by_label(self.test_data)
else:
# when the user did not provide any normal images for testing, we sample some from the training set
elif self.test_split_mode != TestSplitMode.NONE:
# when the user did not provide any normal images for testing, we sample some from the training set,
# except when the user explicitly requested no test splitting.
logger.info(
"No normal test images found. Sampling from training set using a split ratio of %d",
self.test_split_ratio,
Expand All @@ -132,7 +133,7 @@ def _create_test_split(self):
self.test_data += normal_test_data
elif self.test_split_mode == TestSplitMode.SYNTHETIC:
self.test_data = SyntheticAnomalyDataset.from_dataset(normal_test_data)
else:
elif self.test_split_mode != TestSplitMode.NONE:
raise ValueError(f"Unsupported Test Split Mode: {self.test_split_mode}")

def _create_val_split(self):
Expand Down
7 changes: 4 additions & 3 deletions tools/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from anomalib.config import get_configurable_parameters
from anomalib.data import get_datamodule
from anomalib.data.utils import TestSplitMode
from anomalib.models import get_model
from anomalib.utils.callbacks import LoadModelCallback, get_callbacks
from anomalib.utils.loggers import configure_logger, get_experiment_logger
Expand Down Expand Up @@ -63,11 +64,11 @@ def train():
load_model_callback = LoadModelCallback(weights_path=trainer.checkpoint_callback.best_model_path)
trainer.callbacks.insert(0, load_model_callback)

if len(datamodule.test_data) != 0:
if config.dataset.test_split_mode == TestSplitMode.NONE:
logger.info("No test set provided. Skipping test stage.")
else:
logger.info("Testing the model.")
trainer.test(model=model, datamodule=datamodule)
else:
logger.info("No anomalous images found in dataset. Skipping test stage.")


if __name__ == "__main__":
Expand Down