-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_DA2.py
311 lines (254 loc) · 10.8 KB
/
train_DA2.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 7 16:00:00 2020
@author: CITI
"""
import os
import numpy as np
from models.DrumAwareBeatTracker2 import DrumAwareBeatTracker as RNNmodel
import drumaware_dataset as mmdataset
from torch.utils.data import DataLoader
import tqdm
import torch
import torch.nn as nn
from pathlib import Path
import json
#import utils
import da_utils as utils
import time
from lookahead_pytorch import Lookahead
import sys
def train(model, device, train_loader, optimizer):
model.train()
train_loss = 0
## separate the loss history of each head
ou_loss = 0
ou_drum_loss = 0
fuser_loss = 0
mix_loss = 0
nodrum_loss = 0
drum_loss= 0
pbar = tqdm.tqdm(train_loader, disable = False)
for x, y, x_nodrum, x_drum in pbar:
# break
pbar.set_description("Training batch")
x, y, x_nodrum, x_drum = x.to(device), y.to(device), x_nodrum.to(device), x_drum.to(device)
optimizer.zero_grad()
beat_fused, beat_mix, beat_nodrum, beat_drum, x_nodrum_hat, x_drum_hat = model(x)
beat_fused = beat_fused.reshape((-1, 3))
beat_mix = beat_mix.reshape((-1, 3))
beat_nodrum = beat_nodrum.reshape((-1, 3))
beat_drum = beat_drum.reshape((-1, 3))
y = y.reshape((-1)).to(dtype = torch.long) # required type of loss function
weights = [1, 200, 67] # nonbeat, beat, downbeat
class_weights = torch.FloatTensor(weights).to(device)
CE = nn.CrossEntropyLoss(weight = class_weights)
loss_SourceSep = torch.nn.functional.mse_loss(x_nodrum_hat, x_nodrum)
loss_DrumSourceSep = torch.nn.functional.mse_loss(x_drum_hat, x_drum)
loss_fused = CE(beat_fused, y)
loss_mix = CE(beat_mix, y)
loss_nodrum = CE(beat_nodrum, y)
loss_drum = CE(beat_drum, y)
loss = loss_fused + loss_mix + loss_nodrum + 50*loss_SourceSep + loss_drum + 50*loss_DrumSourceSep
loss.backward()
ou_loss += loss_SourceSep.item()
ou_drum_loss += loss_DrumSourceSep.item()
fuser_loss += loss_fused.item()
mix_loss += loss_mix.item()
nodrum_loss += loss_nodrum.item()
drum_loss += loss_drum.item()
train_loss += loss
optimizer.step()
return train_loss/len(train_loader.dataset), [ou_loss/len(train_loader.dataset),
ou_drum_loss/len(train_loader.dataset),
fuser_loss/len(train_loader.dataset),
mix_loss/len(train_loader.dataset),
nodrum_loss/len(train_loader.dataset),
drum_loss/len(train_loader.dataset)]
def valid(model, device, valid_loader ):
model.eval()
valid_loss = 0
## separate the loss history of each head
ou_loss = 0
ou_drum_loss = 0
fuser_loss = 0
mix_loss = 0
nodrum_loss = 0
drum_loss= 0
with torch.no_grad():
for x, y, x_nodrum, x_drum in valid_loader:
x, y, x_nodrum, x_drum = x.to(device), y.to(device), x_nodrum.to(device), x_drum.to(device)
beat_fused, beat_mix, beat_nodrum, beat_drum, x_nodrum_hat, x_drum_hat= model(x) # beat activations (batch, timestep, 3) ==> nonbeat(0), donwbeat(1), beat(2)
beat_fused = beat_fused.reshape((-1, 3))
beat_mix = beat_mix.reshape((-1, 3))
beat_nodrum = beat_nodrum.reshape((-1, 3))
beat_drum = beat_drum.reshape((-1, 3))
y = y.reshape((-1)).to(dtype = torch.long) # required type of loss function
weights = [1, 200, 67] # nonbeat, beat, downbeat
class_weights = torch.FloatTensor(weights).to(device)
CE = nn.CrossEntropyLoss(weight = class_weights)
loss_SourceSep = torch.nn.functional.mse_loss(x_nodrum_hat, x_nodrum)
loss_DrumSourceSep = torch.nn.functional.mse_loss(x_drum_hat, x_drum)
loss_fused = CE(beat_fused, y)
loss_mix = CE(beat_mix, y)
loss_nodrum = CE(beat_nodrum, y)
loss_drum = CE(beat_drum, y)
loss = loss_fused + loss_mix + loss_nodrum + 50*loss_SourceSep + loss_drum + 50*loss_DrumSourceSep
ou_loss += loss_SourceSep.item()
ou_drum_loss += loss_DrumSourceSep.item()
fuser_loss += loss_fused.item()
mix_loss += loss_mix.item()
nodrum_loss += loss_nodrum.item()
drum_loss += loss_drum.item()
valid_loss += loss
return valid_loss/len(valid_loader.dataset), [ou_loss/len(valid_loader.dataset),
ou_drum_loss/len(valid_loader.dataset),
fuser_loss/len(valid_loader.dataset),
mix_loss/len(valid_loader.dataset),
nodrum_loss/len(valid_loader.dataset),
drum_loss/len(valid_loader.dataset)]
def main():
### you may modify the experiment params/info here:
cuda_num = 0 # int(sys.argv[1])
cuda_str = 'cuda:'+str(cuda_num)
device = torch.device(cuda_str if torch.cuda.is_available() else 'cpu')
train_epochs = 2000
# must assign
date = '0615'
exp_num = 1 #sys.argv[2]
exp_name = 'RNNBeat_DA2_V'+ str(exp_num)+'_'+date
exp_dir = os.path.join('./experiments', exp_name)
target_jsonpath = exp_dir
lr = 1e-2
patience = 20
### model settings, no need to modify
model_type = 'DA2'
model_simpname = 'DA2'
model_dir = exp_dir
model_setting = dict(
OU_chkpnt = None,
DrumOU_chkpnt = None,
DrumBeat_chkpnt = None,
NDrumBeat_chkpnt = None,
MixBeat_chkpnt = None,
FuserBeat_chkpnt = None,
fixed_DrumOU = False,
fixed_drum = False,
fixed_OU = False,
fixed_mix = False,
fixed_nodrum = False,
fixed_fuser = False,
mix_2stage_fsize = 25,
mix_out_features = 3,
nodrum_2stage_fsize = 25,
nodrum_out_features = 3,
drum_2stage_fsize = 25,
drum_out_features = 3,
fuser_2stage_fsize = 0,
fuser_out_features = 3,)
model_info = dict(model_type = model_type,
model_simpname = model_simpname,
model_dir = model_dir,
model_setting = model_setting)
if not os.path.exists(exp_dir):
Path(exp_dir).mkdir(parents = True, exist_ok = True)
mix_main_dir = './datasets/original'
mix_dataset_dirs = os.listdir(mix_main_dir)
mixtrainset = utils.getMixset(mix_dataset_dirs, folderName = 'features', abname = 'train_dataset.ab' )
mixvalidset = utils.getMixset(mix_dataset_dirs, folderName= 'features', abname = 'valid_dataset.ab' )
trainset = mixtrainset
validset = mixvalidset
train_loader = DataLoader( trainset, batch_size=4, shuffle=True)
valid_loader = DataLoader( validset, batch_size = 2, shuffle = True)
model = RNNmodel(
**model_setting)
model.cuda(cuda_num)
optimizer = torch.optim.Adam(
model.parameters(),
lr=lr,
weight_decay= 0.00001
)
optimizer = Lookahead(optimizer)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
factor=0.3,
patience=80,
cooldown=10
)
es = utils.EarlyStopping(patience= patience)
t = tqdm.trange(1, train_epochs +1, disable = False)
train_losses = []
valid_losses = []
sep_trainloss_hist = []
sep_validloss_hist = []
train_times = []
lr_change_epoch = []
best_epoch = 0
stop_t = 0
for epoch in t:
t.set_description("Training Epoch")
end = time.time()
train_loss, sep_trainloss = train(model, device, train_loader, optimizer)
valid_loss, sep_validloss = valid(model, device, valid_loader)
scheduler.step(valid_loss)
train_losses.append(train_loss.item())
valid_losses.append(valid_loss.item())
sep_trainloss_hist.append(sep_trainloss)
sep_validloss_hist.append(sep_validloss)
t.set_postfix(
train_loss=train_loss.item(), val_loss=valid_loss.item()
)
stop = es.step(valid_loss.item())
if valid_loss.item() == es.best:
best_epoch = epoch
utils.save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_loss': es.best,
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict()
},
is_best=valid_loss.item() == es.best,
path=exp_dir,
target='RNNBeatProc'
)
# save params
params = {
'epochs_trained': epoch,
'best_loss': es.best,
'best_epoch': best_epoch,
'train_loss_history': train_losses,
'valid_loss_history': valid_losses,
'train_time_history': train_times,
'sep_trainloss_hist': sep_trainloss_hist,
'sep_validloss_hist': sep_validloss_hist,
'num_bad_epochs': es.num_bad_epochs,
'lr_change_epoch': lr_change_epoch,
'stop_t': stop_t,
'model_info': model_info,
}
with open(os.path.join(target_jsonpath, 'RNNbeat' + '.json'), 'w') as outfile:
outfile.write(json.dumps(params, indent=4, sort_keys=True))
train_times.append(time.time() - end)
if stop:
print("Apply Early Stopping and retrain")
stop_t +=1
if stop_t >=5:
break
lr = lr*0.2
lr_change_epoch.append(epoch)
optimizer = torch.optim.Adam(
model.parameters(),
lr=lr,
weight_decay= 0.00001
)
optimizer = Lookahead(optimizer)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
factor=0.3,
patience=80,
cooldown=10
)
es = utils.EarlyStopping(patience= patience, best_loss = es.best)
if __name__ == "__main__":
main()