forked from Blinorot/pytorch_project_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
76 lines (58 loc) · 2.2 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
import random
import warnings
import hydra
import numpy as np
import torch
from hydra.utils import instantiate
from src.datasets.data_utils import get_dataloaders
from src.trainer import Trainer
from src.utils.init_utils import setup_saving_and_logging
warnings.filterwarnings("ignore", category=UserWarning)
def set_random_seed(seed):
# fix random seeds for reproducibility
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
# benchmark=True works faster but reproducibility decreases
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
@hydra.main(version_base=None, config_path="src/configs", config_name="baseline")
def main(config):
set_random_seed(config.trainer.seed)
logger = setup_saving_and_logging(config)
if config.trainer.device == "auto":
device = "cuda" if torch.cuda.is_available() else "cpu"
else:
device = config.trainer.device
# setup data_loader instances
dataloaders, batch_transforms = get_dataloaders(config)
# build model architecture, then print to console
model = instantiate(config.model).to(device)
logger.info(model)
# get function handles of loss and metrics
loss_function = instantiate(config.loss_function).to(device)
metrics = instantiate(config.metrics)
# build optimizer, learning rate scheduler
trainable_params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = instantiate(config.optimizer, params=trainable_params)
lr_scheduler = instantiate(config.lr_scheduler, optimizer=optimizer)
# epoch_len = number of iterations for iteration-based training
# epoch_len = None or len(dataloader) for epoch-based training
epoch_len = config.trainer.get("epoch_len")
trainer = Trainer(
model=model,
criterion=loss_function,
metrics=metrics,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
config=config,
device=device,
dataloaders=dataloaders,
epoch_len=epoch_len,
logger=logger,
batch_transforms=batch_transforms,
skip_oom=config.trainer.get("skip_oom", True),
)
trainer.train()
if __name__ == "__main__":
main()