-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.py
161 lines (119 loc) · 4.49 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
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
import os
import argparse
import time
import torch
import numpy as np
from datetime import datetime
# Apex for mixed-precision training
from apex import amp
# TensorBoard
from torch.utils.tensorboard import SummaryWriter
from model import load_model, save_model
from data.loaders import librispeech_loader
from validation import validate_speakers
#### pass configuration
from experiment import ex
def train(args, model, optimizer, writer):
# get datasets and dataloaders
(train_loader, train_dataset, test_loader, test_dataset,) = librispeech_loader(
args, num_workers=args.num_workers
)
total_step = len(train_loader)
print_idx = 100
# at which step to validate training
validation_idx = 1000
best_loss = 0
start_time = time.time()
global_step = 0
for epoch in range(args.start_epoch, args.start_epoch + args.num_epochs):
loss_epoch = 0
for step, (audio, filename, _, start_idx) in enumerate(train_loader):
start_time = time.time()
if step % validation_idx == 0:
validate_speakers(args, train_dataset, model, optimizer, epoch, step, global_step, writer)
audio = audio.to(args.device)
# forward
loss = model(audio)
# accumulate losses for all GPUs
loss = loss.mean()
# torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10)
# backward, depending on mixed-precision
model.zero_grad()
if args.fp16:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
if step % print_idx == 0:
examples_per_second = args.batch_size / (time.time() - start_time)
print(
"[Epoch {}/{}] Train step {:04d}/{:04d} \t Examples/s = {:.2f} \t "
"Loss = {:.4f} \t Time/step = {:.4f}".format(
epoch,
args.num_epochs,
step,
len(train_loader),
examples_per_second,
loss,
time.time() - start_time,
)
)
writer.add_scalar("Loss/train_step", loss, global_step)
loss_epoch += loss
global_step += 1
avg_loss = loss_epoch / len(train_loader)
writer.add_scalar("Loss/train", avg_loss, epoch)
ex.log_scalar("loss.train", avg_loss, epoch)
conv = 0
for idx, layer in enumerate(model.module.model.modules()):
if isinstance(layer, torch.nn.Conv1d):
writer.add_histogram(
"Conv/weights-{}".format(conv),
layer.weight,
global_step=global_step,
)
conv += 1
if isinstance(layer, torch.nn.GRU):
writer.add_histogram(
"GRU/weight_ih_l0", layer.weight_ih_l0, global_step=global_step
)
writer.add_histogram(
"GRU/weight_hh_l0", layer.weight_hh_l0, global_step=global_step
)
if avg_loss > best_loss:
best_loss = avg_loss
save_model(args, model, optimizer, best=True)
# save current model state
save_model(args, model, optimizer)
args.current_epoch += 1
@ex.automain
def main(_run, _log):
args = argparse.Namespace(**_run.config)
if len(_run.observers) > 1:
out_dir = _run.observers[1].dir
else:
out_dir = _run.observers[0].dir
args.out_dir = out_dir
# set start time
args.time = time.ctime()
# Device configuration
args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
args.current_epoch = args.start_epoch
# set random seeds
np.random.seed(args.seed)
torch.manual_seed(args.seed)
# load model
model, optimizer = load_model(args)
# initialize TensorBoard
tb_dir = os.path.join(out_dir, _run.experiment_info["name"])
os.makedirs(tb_dir)
writer = SummaryWriter(log_dir=tb_dir)
# writer.add_graph(model.module, torch.rand(args.batch_size, 1, 20480).to(args.device))
try:
train(args, model, optimizer, writer)
except KeyboardInterrupt:
print("Interrupting training, saving model")
save_model(args, model, optimizer)
if __name__ == "__main__":
main()