summaryrefslogtreecommitdiff
path: root/training/run_experiment.py
blob: 289866e6684b29b86134bd29f1743bdb0dcccab0 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Script to run experiments."""
from datetime import datetime
import importlib
from pathlib import Path
from typing import Dict, List, NamedTuple, Optional, Union, Type

import click
from loguru import logger
from omegaconf import OmegaConf
import pytorch_lightning as pl
import torch
from torch import nn
from torchsummary import summary
from tqdm import tqdm
import wandb


SEED = 4711
EXPERIMENTS_DIRNAME = Path(__file__).parents[0].resolve() / "experiments"


def _configure_logging(log_dir: Optional[Path], verbose: int = 0) -> None:
    """Configure the loguru logger for output to terminal and disk."""

    def _get_level(verbose: int) -> str:
        """Sets the logger level."""
        levels = {0: "WARNING", 1: "INFO", 2: "DEBUG"}
        verbose = min(verbose, 2)
        return levels[verbose]

    # Have to remove default logger to get tqdm to work properly.
    logger.remove()

    # Fetch verbosity level.
    level = _get_level(verbose)

    logger.add(lambda msg: tqdm.write(msg, end=""), colorize=True, level=level)
    if log_dir is not None:
        logger.add(
            str(log_dir / "train.log"),
            format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}",
        )


def _import_class(module_and_class_name: str) -> type:
    """Import class from module."""
    module_name, class_name = module_and_class_name.rsplit(".", 1)
    module = importlib.import_module(module_name)
    return getattr(module, class_name)


def _configure_callbacks(
    args: List[Union[OmegaConf, NamedTuple]]
) -> List[Type[pl.callbacks.Callback]]:
    """Configures lightning callbacks."""
    pl_callbacks = [
        getattr(pl.callbacks, callback.type)(**callback.args) for callback in args
    ]
    return pl_callbacks


def _configure_logger(
        network: Type[nn.Module], args: Dict, use_wandb: bool
) -> pl.loggers.WandbLogger:
    """Configures lightning logger."""
    if use_wandb:
        pl_logger = pl.loggers.WandbLogger()
        pl_logger.watch(network)
        pl_logger.log_hyperparams(vars(args))
    else:
        pl_logger = pl.logger.TensorBoardLogger("training/logs")
    return pl_logger


def _save_best_weights(
    callbacks: List[Type[pl.callbacks.Callback]], use_wandb: bool
) -> None:
    """Saves the best model."""
    model_checkpoint_callback = next(
        callback
        for callback in callbacks
        if isinstance(callback, pl.callbacks.ModelCheckpoint)
    )
    best_model_path = model_checkpoint_callback.best_model_path
    if best_model_path:
        logger.info(f"Best model saved at: {best_model_path}")
        if use_wandb:
            logger.info("Uploading model to W&B...")
            wandb.save(best_model_path)


def _load_lit_model(lit_model_class: type, network: Type[nn.Module], config: OmegaConf) -> Type[pl.LightningModule]:
    """Load lightning model."""
    if config.load_checkpoint is not None:
        logger.info(
            f"Loading network weights from checkpoint: {config.load_checkpoint}"
        )
        return lit_model_class.load_from_checkpoint(
            config.load_checkpoint, network=network, **config.model.args
        )
    return lit_model_class(network=network, **config.model.args)


def run(path: str, train: bool, test: bool, tune: bool, use_wandb: bool) -> None:
    """Runs experiment."""
    logger.info("Starting experiment...")

    # Seed everything in the experiment.
    logger.info(f"Seeding everthing with seed={SEED}")
    pl.utilities.seed.seed_everything(SEED)

    # Load config.
    logger.info(f"Loading config from: {path}")
    config = OmegaConf.load(path)

    # Load classes.
    data_module_class = _import_class(f"text_recognizer.data.{config.data.type}")
    network_class = _import_class(f"text_recognizer.networks.{config.network.type}")
    lit_model_class = _import_class(f"text_recognizer.models.{config.model.type}")

    # Initialize data object and network.
    data_module = data_module_class(**config.data.args)
    network = network_class(**data_module.config(), **config.network.args)

    # Load callback and logger.
    callbacks = _configure_callbacks(config.callbacks)
    pl_logger = _configure_logger(network, config, use_wandb)

    # Load ligtning model.
    lit_model = _load_lit_model(lit_model_class, network, config)

    trainer = pl.Trainer(
        **config.trainer.args,
        callbacks=callbacks,
        logger=pl_logger,
        weigths_save_path="training/logs",
    )

    if tune:
        logger.info(f"Tuning learning rate and batch size...")
        trainer.tune(lit_model, datamodule=data_module)

    if train:
        logger.info(f"Training network...")
        trainer.fit(lit_model, datamodule=data_module)

    if test:
        logger.info(f"Testing network...")
        trainer.test(lit_model, datamodule=data_module)

    _save_best_weights(callbacks, use_wandb)


@click.command()
@click.option("-f", "--experiment_config", type=str, help="Path to experiment config.")
@click.option("--use_wandb", is_flag=True, help="If true, do use wandb for logging.")
@click.option(
    "--tune", is_flag=True, help="If true, tune hyperparameters for training."
)
@click.option("--train", is_flag=True, help="If true, train the model.")
@click.option("--test", is_flag=True, help="If true, test the model.")
@click.option("-v", "--verbose", count=True)
def cli(
    experiment_config: str,
    use_wandb: bool,
    tune: bool,
    train: bool,
    test: bool,
    verbose: int,
) -> None:
    """Run experiment."""
    _configure_logging(None, verbose=verbose)
    run(path=experiment_config, train=train, test=test, tune=tune, use_wandb=use_wandb)


if __name__ == "__main__":
    cli()