This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
143 lines (115 loc) · 3.88 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
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
# -*- coding: utf-8 -*-
import argparse
import json
import os
from typing import Any, Callable, Iterable, List, Optional
import adabound
import torch
from torch.nn import Module
import torch.optim as module_optim
from torch.utils.data import DataLoader
import data_loader.data_loaders as module_data
import model.loss as module_loss
import model.metric as module_metric
import model.model as module_arch
from trainer import Trainer
from utils import Logger
def get_instance(module, name: str, config: dict, *args) -> Any:
return getattr(module, config[name]["type"])(*args, **config[name]["args"])
def main(config: dict, resume: Optional[str]) -> None:
train_logger: Logger = Logger()
# Instantiate data loaders.
data_loader: DataLoader = get_instance(module_data, "data_loader", config)
valid_data_loader: Optional[DataLoader] = data_loader.split_validation()
# Instantiate model.
model: Module = get_instance(
module_arch, "arch", config, data_loader.dataset.feats
)
print(model)
# Obtain function handles for loss and metrics.
loss: Callable = getattr(module_loss, config["loss"]["type"])
loss_args: dict = config["loss"]["args"]
metrics: List[Callable] = [
getattr(module_metric, met) for met in config["metrics"]
]
metric_args: List[dict] = [
config["metrics"][met] for met in config["metrics"]
]
# Instantiate optimizer and learning rate scheduler.
# Delete every line containing lr_scheduler to disable scheduler.
trainable_params: Iterable[torch.Tensor] = filter(
lambda p: p.requires_grad, model.parameters()
)
optimizer = None
try:
optimizer = get_instance(
module_optim, "optimizer", config, trainable_params
)
except AttributeError:
optimizer = get_instance(
adabound, "optimizer", config, trainable_params
)
lr_scheduler = get_instance(
torch.optim.lr_scheduler, "lr_scheduler", config, optimizer
)
# Instantiate trainer.
trainer: Trainer = Trainer(
model = model,
loss = loss,
loss_args = loss_args,
metrics = metrics,
metric_args = metric_args,
optimizer = optimizer,
config = config,
resume = resume,
data_loader = data_loader,
valid_data_loader = valid_data_loader,
lr_scheduler = lr_scheduler,
train_logger = train_logger
)
trainer.train()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = "Michael's adapted PyTorch Template -- Training"
)
# Not specified as required here as a custom error message is raised below.
parser.add_argument(
"-c",
"--config",
default = None,
type = str,
help = "Config JSON file path."
)
parser.add_argument(
"-r",
"--resume",
default = None,
type = str,
help = "Latest PTH checkpoint file path (default: None)"
)
parser.add_argument(
"-d",
"--device",
default = None,
type = str,
help = "Indices of GPUs to enable (default: all)"
)
args = parser.parse_args()
if args.config:
# Load config file.
config: dict = json.load(open(args.config))
path: str = os.path.join(config["trainer"]["save_dir"], config["name"])
elif args.resume:
# Load config file from checkpoint, in case a new config file is not
# given.
# Use "--config" and "--resume" arguments together to load trained model
# and train more with changed config.
config = torch.load(args.resume)["config"]
else:
raise AssertionError(
"Configuration file need to be specified. Add '-c config.json', for"
+ " example."
)
if args.device:
os.environ["CUDA_VISIBLE_DEVICES"] = args.device
main(config, args.resume)