-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_valid_test.py
660 lines (561 loc) · 23.6 KB
/
train_valid_test.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import imp
from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
import torch.utils.data as data
from torch.utils.data import DataLoader
from torchvision.transforms import Compose
import torchaudio
from torch.utils.tensorboard import SummaryWriter
from basic import Count
from core.registry import CONFIG
from models.torch.dfsmn import DfsmnModel
from models.torch.bidfsmn import BiDfsmnModel, BiDfsmnModel_thinnable, DfsmnModel_pre
from speech_commands.dataset.speech_commands import SpeechCommandV1
from speech_commands.dataset.transform import ChangeAmplitude, \
FixAudioLength, ChangeSpeedAndPitchAudio, TimeshiftAudio
from torch_utils import mixup
from pytorch_wavelets import DWTForward, DWTInverse
def loss_term(A):
a = torch.abs(A)
Q = a * a
return Q
def total_loss(Q_s, Q_t):
Q_s = loss_term(Q_s)
Q_t = loss_term(Q_t)
Q_s_norm = Q_s / torch.norm(Q_s, p=2)
Q_t_norm = Q_t / torch.norm(Q_t, p=2)
tmp = Q_s_norm - Q_t_norm
loss = torch.norm(tmp, p=2)
return loss
def pass_filter(x, select_pass, J=1, wave='haar', mode='zero'):
xfm = DWTForward(J=J, mode=mode, wave=wave) # Accepts all wave types available to PyWavelets
ifm = DWTInverse(mode=mode, wave=wave)
if x.is_cuda:
xfm, ifm = xfm.cuda(), ifm.cuda()
if len(x.shape) == 3:
yl, yh = xfm(x.unsqueeze(1))
elif len(x.shape) == 4:
yl, yh = xfm(x)
else:
assert(False) # error
if select_pass == 'high':
yl.zero_()
y = ifm((yl, yh))
if len(x.shape) == 3:
y = y.squeeze(1)
return y
def get_model2(model_type: str, in_channels=1, **kwargs):
if model_type == 'Vgg19Bn':
return Vgg19BN(in_channels=in_channels, **kwargs) # [Batch, 1, 32, 32]
elif model_type == 'Mobilenetv1':
return MobileNetV1(in_channels=in_channels, **kwargs)
elif model_type == 'Mobilenetv2':
return MobileNetV2(in_channels=in_channels, **kwargs)
elif model_type == 'BCResNet':
return BCResNet(in_channels=in_channels, **kwargs) # [Batch, 1, 40, 32]
elif model_type == 'fsmn':
return FSMN(in_channels=in_channels, **kwargs)
elif model_type == 'Dfsmn':
return DfsmnModel(in_channels=in_channels, **kwargs)
elif model_type == 'BiDfsmn':
return BiDfsmnModel(in_channels=in_channels, **kwargs)
elif model_type == 'BiDfsmn_thinnable_pre':
return DfsmnModel_pre(in_channels=in_channels, **kwargs)
elif model_type == 'BiDfsmn_thinnable':
return BiDfsmnModel_thinnable(in_channels=in_channels, **kwargs)
else:
raise RuntimeError('unsupport model type: ', model_type)
def get_model(model_type: str, in_channels=1, method="no", **kwargs):
if method == "no":
model = get_model2(model_type, in_channels, **kwargs)
return model
else:
from basic import Count, Modify
model = get_model2(model_type, in_channels, **kwargs)
model.method = method
cnt = Count(model)
model, _ = Modify(model, method=method, id=0, first=1, last=cnt)
return model
def create_dataloader(dataset_type, configs, use_gpu, version):
train_transform = Compose([
ChangeAmplitude(),
ChangeSpeedAndPitchAudio(),
TimeshiftAudio(),
FixAudioLength(),
torchaudio.transforms.MelSpectrogram(sample_rate=16000,
n_fft=2048,
hop_length=512,
n_mels=configs.n_mels,
normalized=True),
torchaudio.transforms.AmplitudeToDB(),
])
valid_transform = Compose([
FixAudioLength(),
torchaudio.transforms.MelSpectrogram(sample_rate=16000,
n_fft=2048,
hop_length=512,
n_mels=configs.n_mels,
normalized=True),
torchaudio.transforms.AmplitudeToDB(),
])
dataset_train = SpeechCommandV1(configs.dataroot,
subset='training',
download=True,
transform=train_transform,
num_classes=configs.num_classes,
noise_ratio=0.3,
noise_max_scale=0.3,
cache_origin_data=False,
version=version)
dataset_valid = SpeechCommandV1(configs.dataroot,
subset='validation',
download=True,
transform=valid_transform,
num_classes=configs.num_classes,
cache_origin_data=True,
version=version)
dataset_test = SpeechCommandV1(configs.dataroot,
subset='testing',
download=True,
transform=valid_transform,
num_classes=configs.num_classes,
cache_origin_data=True,
version=version)
dataset_dict = {
'training': dataset_train,
'validation': dataset_valid,
'testing': dataset_test
}
return DataLoader(dataset_dict[dataset_type],
batch_size=configs.batch_size,
shuffle=dataset_type == 'training',
sampler=None,
pin_memory=use_gpu,
num_workers=16,
persistent_workers=True)
def create_lr_schedule(configs, optimizer):
if configs.lr_scheduler == 'plateau':
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
patience=configs.lr_scheduler_patience,
factor=configs.lr_scheduler_gamma)
elif configs.lr_scheduler == 'step':
lr_scheduler = torch.optim.lr_scheduler.StepLR(
optimizer,
step_size=configs.lr_scheduler_stepsize,
gamma=configs.lr_scheduler_gamma,
last_epoch=configs.epoch - 1)
elif configs.lr_scheduler == 'cosin':
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, T_max=configs.epoch)
else:
raise RuntimeError('unsupported lr schedule type: ',
configs.lr_scheduler)
return lr_scheduler
def create_optimizer(configs, model):
if configs.optim == 'sgd':
optimizer = torch.optim.SGD(model.parameters(),
lr=configs.lr,
momentum=0.9,
weight_decay=configs.weight_decay)
else:
optimizer = torch.optim.Adam(model.parameters(),
lr=configs.lr,
weight_decay=configs.weight_decay)
return optimizer
weights = [1, 0.5, 0.25]
loss_lim = 50.0
distillation_pred = torch.nn.MSELoss()
pred = False
def train_epoch_distill(model: nn.Module,
teacher_model: nn.Module,
optimizer,
criterion,
data_loader: data.DataLoader,
epoch,
with_gpu,
log_iter=10,
writer: SummaryWriter = None,
mixup_alpha=0,
distill_alpha=0,
select_pass='no',
J=1,
num_classes=None):
"""
training one epoch
"""
model.train()
if with_gpu:
model = model.cuda()
pbar = tqdm(data_loader, unit="audios", unit_scale=data_loader.batch_size)
epoch_size = len(data_loader)
running_loss = 0
i = 0
for inputs, target in pbar:
if with_gpu:
inputs = inputs.cuda()
target = target.cuda()
if 0 < mixup_alpha < 1:
inputs, target = mixup.mixup(inputs, target,
np.random.beta(mixup_alpha, mixup_alpha),
num_classes)
# forward
teacher_out, teacher_feature = teacher_model(inputs)
if select_pass != 'no':
teacher_feature = [f1 / torch.std(f1) + f2 / torch.std(f2) for f1, f2 in [(pass_filter(f, select_pass=select_pass, J=J), f) for f in teacher_feature]]
loss = 0
if model.__class__.__name__[-9:] != 'thinnable':
out, feature = model(inputs)
if 0 < mixup_alpha < 1:
loss_one_hot = mixup.naive_cross_entropy_loss(out, target)
else:
loss_one_hot = criterion(out, target)
if hasattr(model, 'method') and model.method == 'Laq':
distr_loss1, distr_loss2 = model.laq_loss(inputs)
distr_loss1 = distr_loss1.mean()
distr_loss2 = distr_loss2.mean()
# remove distrloss after args.distr_epoch epochs
if epoch < 100:
loss = loss + (distr_loss1 + distr_loss2)
loss = loss + loss_one_hot
if len(teacher_feature) % len(feature) == 0:
loss_distill = None
for k in range(len(feature)):
j = int((len(teacher_feature) / len(feature)) * (k+1) - 1)
if loss_distill == None:
# loss_distill = distillation(feature[j] / torch.std(feature[j]), teacher_feature[k] / torch.std(teacher_feature[k]))
loss_distill = total_loss(feature[k], teacher_feature[j])
else:
# loss_distill += distillation(feature[j] / torch.std(feature[j]), teacher_feature[k] / torch.std(teacher_feature[k]))
loss_distill = loss_distill + total_loss(feature[k], teacher_feature[j])
loss = loss + loss_distill * distill_alpha
if pred:
loss_pred = distillation_pred(out, teacher_out)
loss = loss + loss_pred * distill_alpha
else:
print ('Distiilation Error: teacher {}, student {}!'.format(len(teacher_feature), len(feature)))
else:
for op in range(model.thin_n):
weight = weights[op]
out, feature = model(inputs, op)
if 0 < mixup_alpha < 1:
loss_one_hot = mixup.naive_cross_entropy_loss(out, target)
else:
loss_one_hot = criterion(out, target)
loss = loss + loss_one_hot * weight
if len(teacher_feature) % len(feature) == 0:
loss_distill = None
for k in range(len(feature)):
j = int((len(teacher_feature) / len(feature)) * (k+1) - 1)
if loss_distill == None:
# loss_distill = distillation(feature[j] / torch.std(feature[j]), teacher_feature[k] / torch.std(teacher_feature[k]))
loss_distill = total_loss(feature[k], teacher_feature[j])
else:
# loss_distill += distillation(feature[j] / torch.std(feature[j]), teacher_feature[k] / torch.std(teacher_feature[k]))
loss_distill = loss_distill + total_loss(feature[k], teacher_feature[j])
loss = loss + loss_distill * distill_alpha * weight
if pred:
loss_pred = distillation_pred(out, teacher_out)
loss = loss + loss_pred * distill_alpha * weight
else:
print ('Distiilation Error: teacher {}, student {}!'.format(len(teacher_feature), len(feature)))
# backprop
optimizer.zero_grad()
loss.backward()
# if loss.item() > loss_lim:
# nn.utils.clip_grad_norm_(model.parameters(), max_norm=5, norm_type=2)
# print('[loss ont_hot]: %.4f, [loss distill]: %.4f' % (loss_one_hot, loss_distill))
optimizer.step()
running_loss += loss.item()
if i % log_iter == 0 and writer is not None:
writer.add_scalar('Train/iter_loss', loss.item(),
i + epoch * epoch_size)
writer.file_writer.flush()
# update the progress bar
pbar.set_postfix({
'loss': "%.05f" % (loss.item()),
})
i += 1
running_loss /= i
if writer is not None:
writer.add_scalar('Train/epoch_loss', running_loss, epoch)
writer.file_writer.flush()
return running_loss
def train_epoch(model: nn.Module,
optimizer,
criterion,
data_loader: data.DataLoader,
epoch,
with_gpu,
log_iter=10,
writer: SummaryWriter = None,
mixup_alpha=0,
num_classes=None):
"""
training one epoch
"""
model.train()
if with_gpu:
model = model.cuda()
pbar = tqdm(data_loader, unit="audios", unit_scale=data_loader.batch_size)
epoch_size = len(data_loader)
if model.__class__.__name__[-9:] != 'thinnable':
running_loss = 0
i = 0
for feat, target in pbar:
if with_gpu:
feat = feat.cuda()
target = target.cuda()
if 0 < mixup_alpha < 1:
feat, target = mixup.mixup(feat, target,
np.random.beta(mixup_alpha, mixup_alpha),
num_classes)
# forward
out = model(feat)
if 0 < mixup_alpha < 1:
loss = mixup.naive_cross_entropy_loss(out, target)
else:
loss = criterion(out, target)
if hasattr(model, 'method') and model.method == 'Laq':
distr_loss1, distr_loss2 = model.laq_loss(feat)
distr_loss1 = distr_loss1.mean()
distr_loss2 = distr_loss2.mean()
# remove distrloss after args.distr_epoch epochs
if epoch < 100:
loss = loss + (distr_loss1 + distr_loss2)
# backprop
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % log_iter == 0 and writer is not None:
writer.add_scalar('Train/iter_loss', loss.item(),
i + epoch * epoch_size)
writer.file_writer.flush()
# update the progress bar
pbar.set_postfix({
'loss': "%.05f" % (loss.item()),
})
i += 1
running_loss /= i
if writer is not None:
writer.add_scalar('Train/epoch_loss', running_loss, epoch)
writer.file_writer.flush()
return running_loss
else:
thin_n = model.thin_n
running_loss = 0
i = 0
for inputs, target in pbar:
if with_gpu:
inputs = inputs.cuda()
target = target.cuda()
if 0 < mixup_alpha < 1:
inputs, target = mixup.mixup(inputs, target,
np.random.beta(mixup_alpha, mixup_alpha),
num_classes)
loss = 0
# forward
for op in range(thin_n):
weight = weights[op]
out = model(inputs, op)
if 0 < mixup_alpha < 1:
loss += mixup.naive_cross_entropy_loss(out, target) * weight
else:
loss += criterion(out, target) * weight
# backprop
optimizer.zero_grad()
loss.backward()
# if loss.item() > loss_lim:
# nn.utils.clip_grad_norm_(model.parameters(), max_norm=5, norm_type=2)
# print('[loss ont_hot]: %.4f, [loss distill]: %.4f' % (loss_one_hot, loss_distill))
optimizer.step()
running_loss += loss.item()
if i % log_iter == 0 and writer is not None:
writer.add_scalar('Train/iter_loss', loss.item(),
i + epoch * epoch_size)
writer.file_writer.flush()
# update the progress bar
pbar.set_postfix({
'loss': "%.05f" % (loss.item()),
})
i += 1
running_loss /= i
if writer is not None:
writer.add_scalar('Train/epoch_loss', running_loss, epoch)
writer.file_writer.flush()
return running_loss
def valid_epoch_distill(model: nn.Module,
criterion,
data_loader: data.DataLoader,
epoch,
with_gpu,
log_iter=10,
writer: SummaryWriter = None):
"""
valid on dataset
"""
model.eval()
if with_gpu:
model = model.cuda()
pbar = tqdm(data_loader, unit="audios", unit_scale=data_loader.batch_size)
epoch_size = len(data_loader)
if model.__class__.__name__[-9:] != 'thinnable':
running_loss = 0
running_acc = 0
i = 0
with torch.no_grad():
for feat, target in pbar:
if with_gpu:
feat = feat.cuda()
target = target.cuda()
# forward
out, feature = model(feat)
loss = criterion(out, target)
pred = out.max(1, keepdim=True)[1]
acc = pred.eq(target.view_as(pred)).sum() / target.size(0)
running_loss += loss.item()
running_acc += acc.item()
# log per 10 iter
if i % log_iter == 0 and writer is not None:
writer.add_scalar('Valid/iter_loss', loss.item(),
i + epoch * epoch_size)
writer.file_writer.flush()
# update the progress bar
pbar.set_postfix({
'loss': "%.05f" % (loss.item()),
})
i += 1
running_acc /= i
running_loss /= i
# log for tensorboard
if writer is not None:
writer.add_scalar('Valid/epoch_loss', running_loss, epoch)
writer.add_scalar('Valid/epoch_accuracy', running_acc, epoch)
writer.file_writer.flush()
return running_loss, running_acc
else:
thin_n = model.thin_n
running_loss = 0.0
running_acc = [0 for op in range(thin_n)]
i = 0
with torch.no_grad():
for feat, target in pbar:
if with_gpu:
feat = feat.cuda()
target = target.cuda()
# forward
for op in range(thin_n):
out, feature = model(feat, op)
loss = criterion(out, target)
pred = out.max(1, keepdim=True)[1]
acc = pred.eq(target.view_as(pred)).sum() / target.size(0)
running_loss += loss.item()
running_acc[op] += acc.item()
# log per 10 iter
if i % log_iter == 0 and writer is not None:
writer.add_scalar('Valid/iter_loss[%d]' % [8, 4, 2, 1][op], loss.item(),
i + epoch * epoch_size)
writer.file_writer.flush()
# update the progress bar
pbar.set_postfix({
'loss': "%.05f" % (loss.item()),
})
i += 1
running_acc = [acc / i for acc in running_acc]
running_loss = running_loss / i
# log for tensorboard
if writer is not None:
writer.add_scalar('Valid/epoch_loss', running_loss, epoch)
for op in range(thin_n):
writer.add_scalar('Valid/epoch_accuracy_%d' % [8, 4, 2, 1][op], running_acc[op], epoch)
writer.file_writer.flush()
return running_loss, running_acc
def valid_epoch(model: nn.Module,
criterion,
data_loader: data.DataLoader,
epoch,
with_gpu,
log_iter=10,
writer: SummaryWriter = None):
"""
valid on dataset
"""
model.eval()
if with_gpu:
model = model.cuda()
pbar = tqdm(data_loader, unit="audios", unit_scale=data_loader.batch_size)
epoch_size = len(data_loader)
if model.__class__.__name__[-9:] != 'thinnable':
running_loss = 0
running_acc = 0
i = 0
with torch.no_grad():
for feat, target in pbar:
if with_gpu:
feat = feat.cuda()
target = target.cuda()
# forward
out = model(feat)
loss = criterion(out, target)
pred = out.max(1, keepdim=True)[1]
acc = pred.eq(target.view_as(pred)).sum() / target.size(0)
running_loss += loss.item()
running_acc += acc.item()
# log per 10 iter
if i % log_iter == 0 and writer is not None:
writer.add_scalar('Valid/iter_loss', loss.item(),
i + epoch * epoch_size)
writer.file_writer.flush()
# update the progress bar
pbar.set_postfix({
'loss': "%.05f" % (loss.item()),
})
i += 1
running_acc /= i
running_loss /= i
# log for tensorboard
if writer is not None:
writer.add_scalar('Valid/epoch_loss', running_loss, epoch)
writer.add_scalar('Valid/epoch_accuracy', running_acc, epoch)
writer.file_writer.flush()
return running_loss, running_acc
else:
thin_n = model.thin_n
running_loss = 0
running_acc = [0 for op in range(thin_n)]
i = 0
with torch.no_grad():
for feat, target in pbar:
if with_gpu:
feat = feat.cuda()
target = target.cuda()
# forward
for op in range(thin_n):
out = model(feat, op)
loss = criterion(out, target)
pred = out.max(1, keepdim=True)[1]
acc = pred.eq(target.view_as(pred)).sum() / target.size(0)
running_loss += loss.item()
running_acc[op] += acc.item()
# log per 10 iter
if i % log_iter == 0 and writer is not None:
writer.add_scalar('Valid/iter_loss[%d]' % [8, 4, 2, 1][op], loss.item(),
i + epoch * epoch_size)
writer.file_writer.flush()
# update the progress bar
pbar.set_postfix({
'loss': "%.05f" % (loss.item()),
})
i += 1
running_acc = [acc / i for acc in running_acc]
running_loss = running_loss / i
# log for tensorboard
if writer is not None:
writer.add_scalar('Valid/epoch_loss', running_loss, epoch)
for op in range(thin_n):
writer.add_scalar('Valid/epoch_accuracy_%d' % [8, 4, 2, 1][op], running_acc[op], epoch)
writer.file_writer.flush()
return running_loss, running_acc