-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
243 lines (209 loc) · 8.34 KB
/
solver.py
File metadata and controls
243 lines (209 loc) · 8.34 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import time
import numpy as np
from sklearn import metrics
import datetime
import tqdm
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
from torch.autograd import Variable
import model as Model
class Solver(object):
def __init__(self, data_loader, config):
# data loader
self.data_loader = data_loader
self.data_path = config.data_path
self.input_length = config.input_length
# training settings
self.n_epochs = config.n_epochs
self.lr = config.lr
self.use_tensorboard = config.use_tensorboard
# model path and step size
self.model_save_path = config.model_save_path
self.model_load_path = config.model_load_path
self.model_name = config.model_type
if config.aug:
self.model_name += f'_aug_{config.aug_prob}'
self.log_step = config.log_step
self.batch_size = config.batch_size
self.model_type = config.model_type
# cuda
self.is_cuda = torch.cuda.is_available()
# Build model
self.get_dataset()
self.build_model()
# Tensorboard
self.writer = SummaryWriter()
def get_dataset(self):
self.valid_list = np.load('./split/valid.npy')
self.binary = np.load('./split/binary.npy')
def get_model(self):
if self.model_type == 'fcn':
return Model.FCN()
elif self.model_type == 'musicnn':
return Model.Musicnn()
elif self.model_type == 'crnn':
return Model.CRNN()
elif self.model_type == 'sample':
return Model.SampleCNN()
elif self.model_type == 'se':
return Model.SampleCNNSE()
elif self.model_type == 'short':
return Model.ShortChunkCNN()
elif self.model_type == 'short_res':
return Model.ShortChunkCNN_Res()
elif self.model_type == 'hcnn':
return Model.HarmonicCNN()
elif self.model_type == "vit":
return Model.ViT()
def build_model(self):
# model
self.model = self.get_model()
# cuda
if self.is_cuda:
self.model.cuda()
# load pretrained model
if len(self.model_load_path) > 1:
self.load(self.model_load_path)
# optimizers
self.optimizer = torch.optim.Adam(self.model.parameters(), self.lr, weight_decay=1e-4)
def load(self, filename):
S = torch.load(filename)
if 'spec.mel_scale.fb' in S.keys():
self.model.spec.mel_scale.fb = S['spec.mel_scale.fb']
self.model.load_state_dict(S)
def to_var(self, x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
def get_loss_function(self):
return nn.BCELoss()
def train(self):
# Start training
start_t = time.time()
current_optimizer = 'adam'
reconst_loss = self.get_loss_function()
best_metric = 0
drop_counter = 0
# Iterate
for epoch in range(self.n_epochs):
ctr = 0
drop_counter += 1
self.model = self.model.train()
for x, y in self.data_loader:
ctr += 1
# Forward
x = self.to_var(x)
y = self.to_var(y)
out = self.model(x)
# Backward
loss = reconst_loss(out, y)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# Log
self.print_log(epoch, ctr, loss, start_t)
self.writer.add_scalar('Loss/train', loss.item(), epoch)
# validation
best_metric = self.validation(best_metric, epoch)
# schedule optimizer
current_optimizer, drop_counter = self.opt_schedule(current_optimizer, drop_counter)
print("[%s] Train finished. Elapsed: %s"
% (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
datetime.timedelta(seconds=time.time() - start_t)))
def opt_schedule(self, current_optimizer, drop_counter):
# adam to sgd
if current_optimizer == 'adam' and drop_counter == 80:
self.load(os.path.join(self.model_save_path, f'{self.model_name}.pth'))
self.optimizer = torch.optim.SGD(self.model.parameters(), 0.001,
momentum=0.9, weight_decay=0.0001,
nesterov=True)
current_optimizer = 'sgd_1'
drop_counter = 0
print('sgd 1e-3')
# first drop
if current_optimizer == 'sgd_1' and drop_counter == 20:
self.load(os.path.join(self.model_save_path, f'{self.model_name}.pth'))
for pg in self.optimizer.param_groups:
pg['lr'] = 0.0001
current_optimizer = 'sgd_2'
drop_counter = 0
print('sgd 1e-4')
# second drop
if current_optimizer == 'sgd_2' and drop_counter == 20:
self.load(os.path.join(self.model_save_path, f'{self.model_name}.pth'))
for pg in self.optimizer.param_groups:
pg['lr'] = 0.00001
current_optimizer = 'sgd_3'
print('sgd 1e-5')
return current_optimizer, drop_counter
def save(self, filename):
model = self.model.state_dict()
torch.save({'model': model}, filename)
def get_tensor(self, fn):
# load audio
npy_path = os.path.join('data', 'npy', fn.split('/')[1][:-3]) + 'npy'
raw = np.load(npy_path, mmap_mode='r')
# split chunk
length = len(raw)
hop = (length - self.input_length) // self.batch_size
x = torch.zeros(self.batch_size, self.input_length)
for i in range(self.batch_size):
x[i] = torch.Tensor(raw[i*hop:i*hop+self.input_length]).unsqueeze(0)
return x
def get_auc(self, est_array, gt_array):
roc_aucs = metrics.roc_auc_score(gt_array, est_array, average='macro')
pr_aucs = metrics.average_precision_score(gt_array, est_array, average='macro')
print('roc_auc: %.4f' % roc_aucs)
print('pr_auc: %.4f' % pr_aucs)
return roc_aucs, pr_aucs
def print_log(self, epoch, ctr, loss, start_t):
if (ctr) % self.log_step == 0:
print("[%s] Epoch [%d/%d] Iter [%d/%d] train loss: %.4f Elapsed: %s" %
(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
epoch+1, self.n_epochs, ctr, len(self.data_loader), loss.item(),
datetime.timedelta(seconds=time.time()-start_t)))
def validation(self, best_metric, epoch):
_, _, loss = self.get_validation_score(epoch)
score = 1 - loss
if score > best_metric:
print('best model!')
best_metric = score
torch.save(self.model.state_dict(),os.path.join(self.model_save_path, f'{self.model_name}.pth'))
return best_metric
def get_validation_score(self, epoch):
self.model = self.model.eval()
est_array = []
gt_array = []
losses = []
reconst_loss = self.get_loss_function()
index = 0
for line in tqdm.tqdm(self.valid_list):
ix, fn = line.split('\t')
# load and split
x = self.get_tensor(fn)
# ground truth
ground_truth = self.binary[int(ix)]
# forward
x = self.to_var(x)
y = torch.tensor(np.repeat(ground_truth.astype('float32')[np.newaxis, :], self.batch_size, axis=0))
if self.is_cuda:
y = y.cuda()
out = self.model(x)
loss = reconst_loss(out, y)
losses.append(float(loss.data))
out = out.detach().cpu()
# estimate
estimated = np.array(out).mean(axis=0)
est_array.append(estimated)
gt_array.append(ground_truth)
index += 1
est_array, gt_array = np.array(est_array), np.array(gt_array)
loss = np.mean(losses)
print('loss: %.4f' % loss)
roc_auc, pr_auc = self.get_auc(est_array, gt_array)
self.writer.add_scalar('Loss/valid', loss, epoch)
self.writer.add_scalar('AUC/ROC', roc_auc, epoch)
self.writer.add_scalar('AUC/PR', pr_auc, epoch)
return roc_auc, pr_auc, loss