-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
175 lines (129 loc) · 6.39 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import itertools
import os
import time
import argparse
import json
import torch
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DistributedSampler, DataLoader
import torch.multiprocessing as mp
from torch.distributed import init_process_group
from torch.nn.parallel import DistributedDataParallel
from dataset import Dataset, amp_pha_spectra, get_dataset_filelist
from models import NSPP_Model, losses
from utils import AttrDict, build_env, scan_checkpoint, load_checkpoint, save_checkpoint, save_info
torch.backends.cudnn.benchmark = True
def train(h):
torch.cuda.manual_seed(h.seed)
device = torch.device('cuda:0')
NSPP = NSPP_Model(h).to(device)
print(NSPP)
os.makedirs(h.checkpoint_path, exist_ok=True)
print("checkpoints directory : ", h.checkpoint_path)
if os.path.isdir(h.checkpoint_path):
cp = scan_checkpoint(h.checkpoint_path, 'NSPP_')
info = scan_checkpoint(h.checkpoint_path, 'info_')
steps = 0
if cp is None:
state_dict_info = None
last_epoch = -1
else:
state_dict = load_checkpoint(cp, device)
state_dict_info = load_checkpoint(info, device)
NSPP.load_state_dict(state_dict['model'])
steps = state_dict_info['steps'] + 1
last_epoch = state_dict_info['epoch']
optim = torch.optim.AdamW(NSPP.parameters(), h.learning_rate, betas=[h.adam_b1, h.adam_b2])
if state_dict_info is not None:
optim.load_state_dict(state_dict_info['optim'])
scheduler = torch.optim.lr_scheduler.ExponentialLR(optim, gamma=h.lr_decay, last_epoch=last_epoch)
training_filelist, validation_filelist = get_dataset_filelist(h.input_training_wav_list, h.input_validation_wav_list)
trainset = Dataset(training_filelist, h.segment_size, h.n_fft,
h.hop_size, h.win_size, h.sampling_rate, split=True, shuffle=True, n_cache_reuse=0, device=device)
train_loader = DataLoader(trainset, num_workers=h.num_workers, shuffle=False,
sampler=None,
batch_size=h.batch_size,
pin_memory=True,
drop_last=True)
validset = Dataset(validation_filelist, h.segment_size, h.n_fft,
h.hop_size, h.win_size, h.sampling_rate, split=False, shuffle=False, n_cache_reuse=0, device=device)
validation_loader = DataLoader(validset, num_workers=1, shuffle=False,
sampler=None,
batch_size=1,
pin_memory=True,
drop_last=True)
sw = SummaryWriter(os.path.join(h.checkpoint_path, 'logs'))
NSPP.train()
for epoch in range(max(0, last_epoch), h.training_epochs):
start = time.time()
print("Epoch: {}".format(epoch+1))
for i, batch in enumerate(train_loader):
start_b = time.time()
log_amplitude, phase = batch
log_amplitude = torch.autograd.Variable(log_amplitude.to(device, non_blocking=True))
phase = torch.autograd.Variable(phase.to(device, non_blocking=True))
phase_g = NSPP(log_amplitude)
optim.zero_grad()
L_IP, L_GD, L_IAF = losses(phase, phase_g, h.n_fft, phase.size()[-1])
loss_all = L_IP + L_GD + L_IAF
loss_all.backward()
optim.step()
if steps % h.stdout_interval == 0:
print('Steps : {:d}, Total Loss: {:4.3f}, Instantaneous Phase Loss : {:4.3f}, Group Delay Loss : {:4.3f}, Instantaneous Angular Frequency Loss : {:4.3f}, s/b : {:4.3f}'.
format(steps, loss_all, L_IP, L_GD, L_IAF, time.time() - start_b))
if steps % h.checkpoint_interval == 0 and steps != 0:
checkpoint_path = "{}/NSPP_{:08d}".format(h.checkpoint_path, steps)
info_path = "{}/info_{:08d}".format(h.checkpoint_path, steps)
save_checkpoint(checkpoint_path,
{'model': NSPP.state_dict()})
save_info(info_path,
{'optim': optim.state_dict(),
'steps': steps,
'epoch': epoch})
if steps % h.summary_interval == 0:
sw.add_scalar("Training/Total Loss", loss_all, steps)
if steps % h.validation_interval == 0 and steps != 0:
NSPP.eval()
torch.cuda.empty_cache()
val_L_IP_total = 0
val_L_GD_total = 0
val_L_IAF_total = 0
with torch.no_grad():
for j, batch in enumerate(validation_loader):
log_amplitude, phase = batch
phase_g = NSPP(log_amplitude.to(device))
phase = torch.autograd.Variable(phase.to(device, non_blocking=True))
val_L_IP, val_L_GD, val_L_IAF = losses(phase, phase_g, h.n_fft, phase.size()[-1])
val_L_IP_total += val_L_IP.item()
val_L_GD_total += val_L_GD.item()
val_L_IAF_total += val_L_IAF.item()
val_L_IP = val_L_IP_total / (j+1)
val_L_GD = val_L_GD_total / (j+1)
val_L_IAF = val_L_IAF_total / (j+1)
sw.add_scalar("Validation/Total Loss", val_L_IP + val_L_GD + val_L_IAF, steps)
sw.add_scalar("Validation/Instantaneous Phase Loss", val_L_IP, steps)
sw.add_scalar("Validation/Group Delay Loss", val_L_GD, steps)
sw.add_scalar("Validation/Instantaneous Angular Frequency Loss", val_L_IAF, steps)
NSPP.train()
steps += 1
scheduler.step()
print('Time taken for epoch {} is {} sec\n'.format(epoch + 1, int(time.time() - start)))
def main():
print('NSPP Training..')
config_file = 'config.json'
with open(config_file) as f:
data = f.read()
json_config = json.loads(data)
h = AttrDict(json_config)
build_env(config_file, 'config.json', h.checkpoint_path)
torch.manual_seed(h.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(h.seed)
else:
pass
train(h)
if __name__ == '__main__':
main()