-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
157 lines (144 loc) · 4.32 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import math
import os
from optimizer import AdamLinearDecay
import torch
from config import get_args
from data import get_loaders
from logger import get_logger
from loss import get_criterion
from models import get_model
from utils import calc_acc, load_json
class Trainer:
def __init__(
self,
train_loader,
test_loader,
model,
optimizer,
criterion,
outdir,
epochs,
device,
logger
) -> None:
self.train_loader = train_loader
self.test_loader = test_loader
self.model = model.to(device)
self.optimizer = optimizer
self.criterion = criterion.to(device)
self.outdir = outdir
self.epochs = epochs
self.device = device
self.logger = logger
self._loss = math.inf
self._last = math.inf
def is_terminate(self):
# TODO
return False
def fit(self):
for epoch in range(self.epochs):
self.train()
self.test()
self.logger.log()
if self._last < self._loss:
self._loss = self._last
self.export_ckpt(epoch)
def export_ckpt(self, epoch: int):
path = os.path.join(self.outdir, f'checkpoint_{epoch}.pt')
print(f'checkpoint {path} saved!')
print('=' * 50)
torch.save(self.model.state_dict(), path)
def train(self):
self.model.train()
total_loss = 0
all_preds = []
all_targets = []
for batch in self.train_loader:
[speech, cls] = batch
speech = speech.to(self.device)
cls = cls.to(self.device)
self.optimizer.zero_grad()
preds = self.model(speech)
loss = self.criterion(preds, cls)
loss.backward()
self.optimizer.step()
total_loss += loss.item()
self.logger.record(
'train_loss', total_loss / len(self.train_loader)
)
for batch in self.train_loader:
[speech, cls] = batch
speech = speech.to(self.device)
cls = cls.to(self.device)
preds = self.model(speech)
all_preds.append(torch.argmax(preds.cpu(), dim=-1))
all_targets.append(cls.cpu())
self.logger.record(
'train_acc', calc_acc(
torch.hstack(all_preds),
torch.hstack(all_targets)
)
)
self.optimizer.update_lr()
self.logger.record(
'lr', self.optimizer.get_lr()
)
@torch.no_grad()
def test(self):
self.model.eval()
total_loss = 0
all_preds = []
all_targets = []
for batch in self.test_loader:
[speech, cls] = batch
speech = speech.to(self.device)
cls = cls.to(self.device)
preds = self.model(speech)
loss = self.criterion(preds, cls)
all_preds.append(torch.argmax(preds.cpu(), dim=-1))
all_targets.append(cls.cpu())
total_loss += loss.item()
self.logger.record(
'test_loss', total_loss / len(self.train_loader)
)
self.logger.record(
'test_acc', calc_acc(
torch.hstack(all_preds),
torch.hstack(all_targets)
)
)
self._last = total_loss
def get_optim(cfg, model):
return AdamLinearDecay(
model.parameters(), lr=cfg.lr, epochs=cfg.epochs
)
def get_trainer(cfg):
cls_mapper = load_json(cfg.cls_mapper)
n_classes = len(cls_mapper)
model = get_model(
cfg, n_classes=n_classes
)
optimizer = get_optim(cfg, model)
criterion = get_criterion(cfg)
logger = get_logger(cfg)
train_loader, test_laoder = get_loaders(
cfg, cls_mapper=cls_mapper
)
if os.path.exists(cfg.outdir) is False:
os.mkdir(cfg.outdir)
trainer = Trainer(
train_loader=train_loader,
test_loader=test_laoder,
model=model,
optimizer=optimizer,
criterion=criterion,
epochs=cfg.epochs,
outdir=cfg.outdir,
device=cfg.device,
logger=logger
)
return trainer
if __name__ == '__main__':
cfg = get_args()
print(cfg)
get_trainer(cfg).fit()