forked from openvinotoolkit/anomalib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
79 lines (57 loc) · 2.52 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Anomalib Training Script.
This script reads the name of the model or config file from command
line, train/test the anomaly model to get quantitative and qualitative
results.
"""
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import logging
import warnings
from argparse import ArgumentParser, Namespace
from pytorch_lightning import Trainer, seed_everything
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
logger = logging.getLogger("anomalib")
def get_parser() -> ArgumentParser:
"""Get parser.
Returns:
ArgumentParser: The parser object.
"""
parser = ArgumentParser()
parser.add_argument("--model", type=str, default="padim", help="Name of the algorithm to train/test")
parser.add_argument("--config", type=str, required=False, help="Path to a model config file")
parser.add_argument("--log-level", type=str, default="INFO", help="<DEBUG, INFO, WARNING, ERROR>")
return parser
def train(args: Namespace):
"""Train an anomaly model.
Args:
args (Namespace): The arguments from the command line.
"""
configure_logger(level=args.log_level)
if args.log_level == "ERROR":
warnings.filterwarnings("ignore")
config = get_configurable_parameters(model_name=args.model, config_path=args.config)
if config.project.get("seed") is not None:
seed_everything(config.project.seed)
datamodule = get_datamodule(config)
model = get_model(config)
experiment_logger = get_experiment_logger(config)
callbacks = get_callbacks(config)
trainer = Trainer(**config.trainer, logger=experiment_logger, callbacks=callbacks)
logger.info("Training the model.")
trainer.fit(model=model, datamodule=datamodule)
logger.info("Loading the best model weights.")
load_model_callback = LoadModelCallback(weights_path=trainer.checkpoint_callback.best_model_path)
trainer.callbacks.insert(0, load_model_callback) # pylint: disable=no-member
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)
if __name__ == "__main__":
args = get_parser().parse_args()
train(args)