-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
66 lines (48 loc) · 2 KB
/
main.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
"""
Main file
We will run the whole program from here
"""
import torch
import hydra
from train import train
from dataset import MyDataset
from models.base_model import MyModel
from torch.utils.data import DataLoader
from utils import main_utils, train_utils
from utils.train_logger import TrainLogger
from omegaconf import DictConfig, OmegaConf
torch.backends.cudnn.benchmark = True
@hydra.main(config_path="config", config_name='config')
def main(cfg: DictConfig) -> None:
"""
Run the code following a given configuration
:param cfg: configuration file retrieved from hydra framework
"""
main_utils.init(cfg)
logger = TrainLogger(exp_name_prefix=cfg['main']['experiment_name_prefix'], logs_dir=cfg['main']['paths']['logs'])
logger.write(OmegaConf.to_yaml(cfg))
# Set seed for results reproduction
main_utils.set_seed(cfg['main']['seed'])
# Load dataset
train_dataset = MyDataset(path=cfg['main']['paths']['train'])
val_dataset = MyDataset(path=cfg['main']['paths']['validation'])
train_loader = DataLoader(train_dataset, cfg['train']['batch_size'], shuffle=True,
num_workers=cfg['main']['num_workers'])
eval_loader = DataLoader(val_dataset, cfg['train']['batch_size'], shuffle=True,
num_workers=cfg['main']['num_workers'])
# Init model
model = MyModel(num_hid=cfg['train']['num_hid'], dropout=cfg['train']['dropout'])
# TODO: Add gpus_to_use
if cfg['main']['parallel']:
model = torch.nn.DataParallel(model)
if torch.cuda.is_available():
model = model.cuda()
logger.write(main_utils.get_model_string(model))
# Run model
train_params = train_utils.get_train_params(cfg)
# Report metrics and hyper parameters to tensorboard
metrics = train(model, train_loader, eval_loader, train_params, logger)
hyper_parameters = main_utils.get_flatten_dict(cfg['train'])
logger.report_metrics_hyper_params(hyper_parameters, metrics)
if __name__ == '__main__':
main()