-
Notifications
You must be signed in to change notification settings - Fork 22
/
tools_for_model.py
812 lines (665 loc) · 28 KB
/
tools_for_model.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
import torch
import torch.nn as nn
import numpy as np
import time
import torch.nn.functional as F
from scipy.signal import get_window
import matplotlib.pylab as plt
from pesq import pesq
from pystoi import stoi
############################################################################
# for convolutional STFT #
############################################################################
def init_kernels(win_len, win_inc, fft_len, win_type=None, invers=False):
if win_type == 'None' or win_type is None:
window = np.ones(win_len)
else:
window = get_window(win_type, win_len, fftbins=True) # **0.5
N = fft_len
fourier_basis = np.fft.rfft(np.eye(N))[:win_len]
real_kernel = np.real(fourier_basis)
imag_kernel = np.imag(fourier_basis)
kernel = np.concatenate([real_kernel, imag_kernel], 1).T
if invers:
kernel = np.linalg.pinv(kernel).T
kernel = kernel * window
kernel = kernel[:, None, :]
return torch.from_numpy(kernel.astype(np.float32)), torch.from_numpy(window[None, :, None].astype(np.float32))
class ConvSTFT(nn.Module):
def __init__(self, win_len, win_inc, fft_len=None, win_type='hamming', feature_type='real', fix=True):
super(ConvSTFT, self).__init__()
if fft_len == None:
self.fft_len = np.int(2 ** np.ceil(np.log2(win_len)))
else:
self.fft_len = fft_len
kernel, _ = init_kernels(win_len, win_inc, self.fft_len, win_type)
# self.weight = nn.Parameter(kernel, requires_grad=(not fix))
self.register_buffer('weight', kernel)
self.feature_type = feature_type
self.stride = win_inc
self.win_len = win_len
self.dim = self.fft_len
def forward(self, inputs):
if inputs.dim() == 2:
inputs = torch.unsqueeze(inputs, 1)
inputs = F.pad(inputs, [self.win_len - self.stride, self.win_len - self.stride])
outputs = F.conv1d(inputs, self.weight, stride=self.stride)
if self.feature_type == 'complex':
return outputs
else:
dim = self.dim // 2 + 1
real = outputs[:, :dim, :]
imag = outputs[:, dim:, :]
mags = torch.sqrt(real ** 2 + imag ** 2)
phase = torch.atan2(imag, real)
return mags, phase
class ConviSTFT(nn.Module):
def __init__(self, win_len, win_inc, fft_len=None, win_type='hamming', feature_type='real', fix=True):
super(ConviSTFT, self).__init__()
if fft_len == None:
self.fft_len = np.int(2**np.ceil(np.log2(win_len)))
else:
self.fft_len = fft_len
kernel, window = init_kernels(win_len, win_inc, self.fft_len, win_type, invers=True)
#self.weight = nn.Parameter(kernel, requires_grad=(not fix))
self.register_buffer('weight', kernel)
self.feature_type = feature_type
self.win_type = win_type
self.win_len = win_len
self.stride = win_inc
self.stride = win_inc
self.dim = self.fft_len
self.register_buffer('window', window)
self.register_buffer('enframe', torch.eye(win_len)[:,None,:])
def forward(self, inputs, phase=None):
"""
inputs : [B, N+2, T] (complex spec) or [B, N//2+1, T] (mags)
phase: [B, N//2+1, T] (if not none)
"""
if phase is not None:
real = inputs * torch.cos(phase)
imag = inputs * torch.sin(phase)
inputs = torch.cat([real, imag], 1)
outputs = F.conv_transpose1d(inputs, self.weight, stride=self.stride)
# this is from torch-stft: https://github.com/pseeth/torch-stft
t = self.window.repeat(1, 1, inputs.size(-1)) ** 2
coff = F.conv_transpose1d(t, self.enframe, stride=self.stride)
outputs = outputs / (coff + 1e-8)
# outputs = torch.where(coff == 0, outputs, outputs/coff)
outputs = outputs[..., self.win_len - self.stride:-(self.win_len - self.stride)]
return outputs
############################################################################
# for complex rnn #
############################################################################
def get_casual_padding1d():
pass
def get_casual_padding2d():
pass
class cPReLU(nn.Module):
def __init__(self, complex_axis=1):
super(cPReLU, self).__init__()
self.r_prelu = nn.PReLU()
self.i_prelu = nn.PReLU()
self.complex_axis = complex_axis
def forward(self, inputs):
real, imag = torch.chunk(inputs, 2, self.complex_axis)
real = self.r_prelu(real)
imag = self.i_prelu(imag)
return torch.cat([real, imag], self.complex_axis)
class NavieComplexLSTM(nn.Module):
def __init__(self, input_size, hidden_size, projection_dim=None, bidirectional=False, batch_first=False):
super(NavieComplexLSTM, self).__init__()
self.input_dim = input_size // 2
self.rnn_units = hidden_size // 2
self.real_lstm = nn.LSTM(self.input_dim, self.rnn_units, num_layers=1, bidirectional=bidirectional,
batch_first=False)
self.imag_lstm = nn.LSTM(self.input_dim, self.rnn_units, num_layers=1, bidirectional=bidirectional,
batch_first=False)
if bidirectional:
bidirectional = 2
else:
bidirectional = 1
if projection_dim is not None:
self.projection_dim = projection_dim // 2
self.r_trans = nn.Linear(self.rnn_units * bidirectional, self.projection_dim)
self.i_trans = nn.Linear(self.rnn_units * bidirectional, self.projection_dim)
else:
self.projection_dim = None
def forward(self, inputs):
if isinstance(inputs, list):
real, imag = inputs
elif isinstance(inputs, torch.Tensor):
real, imag = torch.chunk(inputs, -1)
r2r_out = self.real_lstm(real)[0]
r2i_out = self.imag_lstm(real)[0]
i2r_out = self.real_lstm(imag)[0]
i2i_out = self.imag_lstm(imag)[0]
real_out = r2r_out - i2i_out
imag_out = i2r_out + r2i_out
if self.projection_dim is not None:
real_out = self.r_trans(real_out)
imag_out = self.i_trans(imag_out)
# print(real_out.shape,imag_out.shape)
return [real_out, imag_out]
def flatten_parameters(self):
self.imag_lstm.flatten_parameters()
self.real_lstm.flatten_parameters()
def complex_cat(inputs, axis):
real, imag = [], []
for idx, data in enumerate(inputs):
r, i = torch.chunk(data, 2, axis) # x = torch.chunk(x, n, dim) >> x의 dim 차원을 n개씩 잘라서 뽑아옴
real.append(r)
imag.append(i)
real = torch.cat(real, axis) # torch.cat : 차원 늘리기
imag = torch.cat(imag, axis)
outputs = torch.cat([real, imag], axis)
return outputs
class ComplexConv2d(nn.Module):
def __init__(
self,
in_channels,
out_channels,
kernel_size=(1, 1),
stride=(1, 1),
padding=(0, 0),
dilation=1,
groups=1,
causal=True,
complex_axis=1,
):
'''
in_channels: real+imag
out_channels: real+imag
kernel_size : input [B,C,D,T] kernel size in [D,T]
padding : input [B,C,D,T] padding in [D,T]
causal: if causal, will padding time dimension's left side,
otherwise both
'''
super(ComplexConv2d, self).__init__()
self.in_channels = in_channels // 2
self.out_channels = out_channels // 2
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.causal = causal
self.groups = groups
self.dilation = dilation
self.complex_axis = complex_axis
self.real_conv = nn.Conv2d(self.in_channels, self.out_channels, kernel_size, self.stride,
padding=[self.padding[0], 0], dilation=self.dilation, groups=self.groups)
self.imag_conv = nn.Conv2d(self.in_channels, self.out_channels, kernel_size, self.stride,
padding=[self.padding[0], 0], dilation=self.dilation, groups=self.groups)
nn.init.normal_(self.real_conv.weight.data, std=0.05)
nn.init.normal_(self.imag_conv.weight.data, std=0.05)
nn.init.constant_(self.real_conv.bias, 0.)
nn.init.constant_(self.imag_conv.bias, 0.)
def forward(self, inputs):
if self.padding[1] != 0 and self.causal:
inputs = F.pad(inputs, [self.padding[1], 0, 0, 0])
else:
inputs = F.pad(inputs, [self.padding[1], self.padding[1], 0, 0])
if self.complex_axis == 0:
real = self.real_conv(inputs)
imag = self.imag_conv(inputs)
real2real, imag2real = torch.chunk(real, 2, self.complex_axis)
real2imag, imag2imag = torch.chunk(imag, 2, self.complex_axis)
else:
if isinstance(inputs, torch.Tensor):
real, imag = torch.chunk(inputs, 2, self.complex_axis)
real2real = self.real_conv(real, )
imag2imag = self.imag_conv(imag, )
real2imag = self.imag_conv(real)
imag2real = self.real_conv(imag)
real = real2real - imag2imag
imag = real2imag + imag2real
out = torch.cat([real, imag], self.complex_axis)
return out
class ComplexConvTranspose2d(nn.Module):
def __init__(
self,
in_channels,
out_channels,
kernel_size=(1, 1),
stride=(1, 1),
padding=(0, 0),
output_padding=(0, 0),
causal=False,
complex_axis=1,
groups=1
):
'''
in_channels: real+imag
out_channels: real+imag
'''
super(ComplexConvTranspose2d, self).__init__()
self.in_channels = in_channels // 2
self.out_channels = out_channels // 2
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.output_padding = output_padding
self.groups = groups
self.real_conv = nn.ConvTranspose2d(self.in_channels, self.out_channels, kernel_size, self.stride,
padding=self.padding, output_padding=output_padding, groups=self.groups)
self.imag_conv = nn.ConvTranspose2d(self.in_channels, self.out_channels, kernel_size, self.stride,
padding=self.padding, output_padding=output_padding, groups=self.groups)
self.complex_axis = complex_axis
nn.init.normal_(self.real_conv.weight, std=0.05)
nn.init.normal_(self.imag_conv.weight, std=0.05)
nn.init.constant_(self.real_conv.bias, 0.)
nn.init.constant_(self.imag_conv.bias, 0.)
def forward(self, inputs):
if isinstance(inputs, torch.Tensor):
real, imag = torch.chunk(inputs, 2, self.complex_axis)
elif isinstance(inputs, tuple) or isinstance(inputs, list):
real = inputs[0]
imag = inputs[1]
if self.complex_axis == 0:
real = self.real_conv(inputs)
imag = self.imag_conv(inputs)
real2real, imag2real = torch.chunk(real, 2, self.complex_axis)
real2imag, imag2imag = torch.chunk(imag, 2, self.complex_axis)
else:
if isinstance(inputs, torch.Tensor):
real, imag = torch.chunk(inputs, 2, self.complex_axis)
real2real = self.real_conv(real, )
imag2imag = self.imag_conv(imag, )
real2imag = self.imag_conv(real)
imag2real = self.real_conv(imag)
real = real2real - imag2imag
imag = real2imag + imag2real
out = torch.cat([real, imag], self.complex_axis)
return out
# Source: https://github.com/ChihebTrabelsi/deep_complex_networks/tree/pytorch
# from https://github.com/IMLHF/SE_DCUNet/blob/f28bf1661121c8901ad38149ea827693f1830715/models/layers/complexnn.py#L55
class ComplexBatchNorm(torch.nn.Module):
def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True,
track_running_stats=True, complex_axis=1):
super(ComplexBatchNorm, self).__init__()
self.num_features = num_features // 2
self.eps = eps
self.momentum = momentum
self.affine = affine
self.track_running_stats = track_running_stats
self.complex_axis = complex_axis
if self.affine:
self.Wrr = torch.nn.Parameter(torch.Tensor(self.num_features))
self.Wri = torch.nn.Parameter(torch.Tensor(self.num_features))
self.Wii = torch.nn.Parameter(torch.Tensor(self.num_features))
self.Br = torch.nn.Parameter(torch.Tensor(self.num_features))
self.Bi = torch.nn.Parameter(torch.Tensor(self.num_features))
else:
self.register_parameter('Wrr', None)
self.register_parameter('Wri', None)
self.register_parameter('Wii', None)
self.register_parameter('Br', None)
self.register_parameter('Bi', None)
if self.track_running_stats:
self.register_buffer('RMr', torch.zeros(self.num_features))
self.register_buffer('RMi', torch.zeros(self.num_features))
self.register_buffer('RVrr', torch.ones(self.num_features))
self.register_buffer('RVri', torch.zeros(self.num_features))
self.register_buffer('RVii', torch.ones(self.num_features))
self.register_buffer('num_batches_tracked', torch.tensor(0, dtype=torch.long))
else:
self.register_parameter('RMr', None)
self.register_parameter('RMi', None)
self.register_parameter('RVrr', None)
self.register_parameter('RVri', None)
self.register_parameter('RVii', None)
self.register_parameter('num_batches_tracked', None)
self.reset_parameters()
def reset_running_stats(self):
if self.track_running_stats:
self.RMr.zero_()
self.RMi.zero_()
self.RVrr.fill_(1)
self.RVri.zero_()
self.RVii.fill_(1)
self.num_batches_tracked.zero_()
def reset_parameters(self):
self.reset_running_stats()
if self.affine:
self.Br.data.zero_()
self.Bi.data.zero_()
self.Wrr.data.fill_(1)
self.Wri.data.uniform_(-.9, +.9) # W will be positive-definite
self.Wii.data.fill_(1)
def _check_input_dim(self, xr, xi):
assert (xr.shape == xi.shape)
assert (xr.size(1) == self.num_features)
def forward(self, inputs):
# self._check_input_dim(xr, xi)
xr, xi = torch.chunk(inputs, 2, axis=self.complex_axis)
exponential_average_factor = 0.0
if self.training and self.track_running_stats:
self.num_batches_tracked += 1
if self.momentum is None: # use cumulative moving average
exponential_average_factor = 1.0 / self.num_batches_tracked.item()
else: # use exponential moving average
exponential_average_factor = self.momentum
#
# NOTE: The precise meaning of the "training flag" is:
# True: Normalize using batch statistics, update running statistics
# if they are being collected.
# False: Normalize using running statistics, ignore batch statistics.
#
training = self.training or not self.track_running_stats
redux = [i for i in reversed(range(xr.dim())) if i != 1]
vdim = [1] * xr.dim()
vdim[1] = xr.size(1)
#
# Mean M Computation and Centering
#
# Includes running mean update if training and running.
#
if training:
Mr, Mi = xr, xi
for d in redux:
Mr = Mr.mean(d, keepdim=True)
Mi = Mi.mean(d, keepdim=True)
if self.track_running_stats:
self.RMr.lerp_(Mr.squeeze(), exponential_average_factor)
self.RMi.lerp_(Mi.squeeze(), exponential_average_factor)
else:
Mr = self.RMr.view(vdim)
Mi = self.RMi.view(vdim)
xr, xi = xr - Mr, xi - Mi
#
# Variance Matrix V Computation
#
# Includes epsilon numerical stabilizer/Tikhonov regularizer.
# Includes running variance update if training and running.
#
if training:
Vrr = xr * xr
Vri = xr * xi
Vii = xi * xi
for d in redux:
Vrr = Vrr.mean(d, keepdim=True)
Vri = Vri.mean(d, keepdim=True)
Vii = Vii.mean(d, keepdim=True)
if self.track_running_stats:
self.RVrr.lerp_(Vrr.squeeze(), exponential_average_factor)
self.RVri.lerp_(Vri.squeeze(), exponential_average_factor)
self.RVii.lerp_(Vii.squeeze(), exponential_average_factor)
else:
Vrr = self.RVrr.view(vdim)
Vri = self.RVri.view(vdim)
Vii = self.RVii.view(vdim)
Vrr = Vrr + self.eps
Vri = Vri
Vii = Vii + self.eps
#
# Matrix Inverse Square Root U = V^-0.5
#
# sqrt of a 2x2 matrix,
# - https://en.wikipedia.org/wiki/Square_root_of_a_2_by_2_matrix
tau = Vrr + Vii
delta = torch.addcmul(Vrr * Vii, -1, Vri, Vri)
s = delta.sqrt()
t = (tau + 2 * s).sqrt()
# matrix inverse, http://mathworld.wolfram.com/MatrixInverse.html
rst = (s * t).reciprocal()
Urr = (s + Vii) * rst
Uii = (s + Vrr) * rst
Uri = (- Vri) * rst
#
# Optionally left-multiply U by affine weights W to produce combined
# weights Z, left-multiply the inputs by Z, then optionally bias them.
#
# y = Zx + B
# y = WUx + B
# y = [Wrr Wri][Urr Uri] [xr] + [Br]
# [Wir Wii][Uir Uii] [xi] [Bi]
#
if self.affine:
Wrr, Wri, Wii = self.Wrr.view(vdim), self.Wri.view(vdim), self.Wii.view(vdim)
Zrr = (Wrr * Urr) + (Wri * Uri)
Zri = (Wrr * Uri) + (Wri * Uii)
Zir = (Wri * Urr) + (Wii * Uri)
Zii = (Wri * Uri) + (Wii * Uii)
else:
Zrr, Zri, Zir, Zii = Urr, Uri, Uri, Uii
yr = (Zrr * xr) + (Zri * xi)
yi = (Zir * xr) + (Zii * xi)
if self.affine:
yr = yr + self.Br.view(vdim)
yi = yi + self.Bi.view(vdim)
outputs = torch.cat([yr, yi], self.complex_axis)
return outputs
def extra_repr(self):
return '{num_features}, eps={eps}, momentum={momentum}, affine={affine}, ' \
'track_running_stats={track_running_stats}'.format(**self.__dict__)
def complex_cat(inputs, axis):
real, imag = [], []
for idx, data in enumerate(inputs):
r, i = torch.chunk(data, 2, axis)
real.append(r)
imag.append(i)
real = torch.cat(real, axis)
imag = torch.cat(imag, axis)
outputs = torch.cat([real, imag], axis)
return outputs
############################################################################
# for data normalization #
############################################################################
# get mu and sig
def get_mu_sig(data):
"""Compute mean and standard deviation vector of input data
Returns:
mu: mean vector (#dim by one)
sig: standard deviation vector (#dim by one)
"""
# Initialize array.
data_num = len(data)
mu_utt = []
tmp_utt = []
for n in range(data_num):
dim = len(data[n])
mu_utt_tmp = np.zeros(dim)
mu_utt.append(mu_utt_tmp)
tmp_utt_tmp = np.zeros(dim)
tmp_utt.append(tmp_utt_tmp)
# Get mean.
for n in range(data_num):
mu_utt[n] = np.mean(data[n], 0)
mu = mu_utt
# Get standard deviation.
for n in range(data_num):
tmp_utt[n] = np.mean(np.square(data[n] - mu[n]), 0)
sig = np.sqrt(tmp_utt)
# Assign unit variance.
for n in range(len(sig)):
if sig[n] < 1e-5:
sig[n] = 1.0
return np.float16(mu), np.float16(sig)
def get_statistics_inp(inp):
"""Get statistical parameter of input data.
Args:
inp: input data
Returns:
mu_inp: mean vector of input data
sig_inp: standard deviation vector of input data
"""
mu_inp, sig_inp = get_mu_sig(inp)
return mu_inp, sig_inp
############################################################################
# for scores #
############################################################################
def cal_pesq(dirty_wavs, clean_wavs):
pesq_scores = []
for i in range(len(dirty_wavs)):
pesq_score = pesq(cfg.FS, clean_wavs[i], dirty_wavs[i], "wb")
pesq_scores.append(pesq_score)
return pesq_scores
def cal_stoi(dirty_wavs, clean_wavs):
stoi_scores = []
for i in range(len(dirty_wavs)):
stoi_score = stoi(clean_wavs[i], dirty_wavs[i], cfg.FS, extended=False)
stoi_scores.append(stoi_score)
return stoi_scores
############################################################################
# for plotting the samples #
############################################################################
def hann_window(win_samp):
tmp = np.arange(1, win_samp + 1, 1.0, dtype=np.float64)
window = 0.5 - 0.5 * np.cos((2.0 * np.pi * tmp) / (win_samp + 1))
return np.float32(window)
def fig2np(fig):
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
return data
def plot_spectrogram_to_numpy(input_wav, fs, n_fft, n_overlap, win, mode, clim, label):
# cuda to cpu
input_wav = input_wav.cpu().detach().numpy()
fig, ax = plt.subplots(figsize=(12, 3))
if mode == 'phase':
pxx, freq, t, cax = plt.specgram(input_wav, NFFT=int(n_fft), Fs=int(fs), window=win, noverlap=n_overlap, cmap='jet',
mode=mode)
else:
pxx, freq, t, cax = plt.specgram(input_wav, NFFT=int(n_fft), Fs=int(fs), window=win, noverlap=n_overlap, cmap='jet')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.tight_layout()
plt.clim(clim)
if label is None:
fig.colorbar(cax)
else:
fig.colorbar(cax, label=label)
fig.canvas.draw()
data = fig2np(fig)
plt.close()
return data
def plot_mask_to_numpy(mask, fs, n_fft, n_overlap, win, clim1, clim2, cmap):
frame_num = mask.shape[0]
shift_length = n_overlap
frame_length = n_fft
signal_length = frame_num * shift_length + frame_length
xt = np.arange(0, np.floor(10 * signal_length / fs) / 10, step=0.5) / (signal_length / fs) * frame_num + 1e-8
yt = (n_fft / 2) / (fs / 1000 / 2) * np.arange(0, (fs / 1000 / 2) + 1)
fig, ax = plt.subplots(figsize=(12, 3))
im = ax.imshow(np.transpose(mask), aspect='auto', origin='lower', interpolation='none', cmap=cmap)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (kHz)')
plt.xticks(xt, np.arange(0, np.floor(10 * (signal_length / fs)) / 10, step=0.5))
plt.yticks(yt, np.int16(np.linspace(0, int((fs / 1000) / 2), len(yt))))
plt.tight_layout()
plt.colorbar(im, ax=ax)
im.set_clim(clim1, clim2)
fig.canvas.draw()
data = fig2np(fig)
plt.close()
return data
def plot_error_to_numpy(estimated, target, fs, n_fft, n_overlap, win, mode, clim1, clim2, label):
fig, ax = plt.subplots(figsize=(12, 3))
if mode == None:
pxx1, freq, t, cax = plt.specgram(estimated, NFFT=n_fft, Fs=int(fs), window=win, noverlap=n_overlap, cmap='jet')
pxx2, freq, t, cax = plt.specgram(target, NFFT=n_fft, Fs=int(fs), window=win, noverlap=n_overlap, cmap='jet')
im = ax.imshow(10 * np.log10(pxx1) - 10 * np.log10(pxx2), aspect='auto', origin='lower', interpolation='none',
cmap='jet')
else:
pxx1, freq, t, cax = plt.specgram(estimated, NFFT=n_fft, Fs=int(fs), window=win, noverlap=n_overlap, cmap='jet',
mode=mode)
pxx2, freq, t, cax = plt.specgram(target, NFFT=n_fft, Fs=int(fs), window=win, noverlap=n_overlap, cmap='jet',
mode=mode)
im = ax.imshow(pxx1 - pxx2, aspect='auto', origin='lower', interpolation='none', cmap='jet')
frame_num = pxx1.shape[1]
shift_length = n_overlap
frame_length = n_fft
signal_length = frame_num * shift_length + frame_length
xt = np.arange(0, np.floor(10 * (signal_length / fs)) / 10, step=0.5) / (signal_length / fs) * frame_num
yt = (n_fft / 2) / (fs / 1000 / 2) * np.arange(0, (fs / 1000 / 2) + 1)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (kHz)')
plt.xticks(xt, np.arange(0, np.floor(10 * (signal_length / fs)) / 10, step=0.5))
plt.yticks(yt, np.int16(np.linspace(0, int((fs / 1000) / 2), len(yt))))
plt.tight_layout()
plt.colorbar(im, ax=ax, label=label)
im.set_clim(clim1, clim2)
fig.canvas.draw()
data = fig2np(fig)
plt.close()
return data
############################################################################
# for run.py #
############################################################################
def near_avg_index(array):
array_mean = np.mean(array)
distance_arr = []
for i in range(len(array)):
val = array[i]
distance = abs(array_mean - val)
distance_arr.append(distance)
index = distance_arr.index(min(distance_arr))
return index
def max_index(array):
array_max = np.max(array)
for i in range(len(array)):
val = array[i]
if val == array_max:
index = i
return index
def min_index(array):
array_min = np.min(array)
for i in range(len(array)):
val = array[i]
if val == array_min:
index = i
return index
class Bar(object):
def __init__(self, dataloader):
if not hasattr(dataloader, 'dataset'):
raise ValueError('Attribute `dataset` not exists in dataloder.')
if not hasattr(dataloader, 'batch_size'):
raise ValueError('Attribute `batch_size` not exists in dataloder.')
self.dataloader = dataloader
self.iterator = iter(dataloader)
self.dataset = dataloader.dataset
self.batch_size = dataloader.batch_size
self._idx = 0
self._batch_idx = 0
self._time = []
self._DISPLAY_LENGTH = 50
def __len__(self):
return len(self.dataloader)
def __iter__(self):
return self
def __next__(self):
if len(self._time) < 2:
self._time.append(time.time())
self._batch_idx += self.batch_size
if self._batch_idx > len(self.dataset):
self._batch_idx = len(self.dataset)
try:
batch = next(self.iterator)
self._display()
except StopIteration:
raise StopIteration()
self._idx += 1
if self._idx >= len(self.dataloader):
self._reset()
return batch
def _display(self):
if len(self._time) > 1:
t = (self._time[-1] - self._time[-2])
eta = t * (len(self.dataloader) - self._idx)
else:
eta = 0
rate = self._idx / len(self.dataloader)
len_bar = int(rate * self._DISPLAY_LENGTH)
bar = ('=' * len_bar + '>').ljust(self._DISPLAY_LENGTH, '.')
idx = str(self._batch_idx).rjust(len(str(len(self.dataset))), ' ')
tmpl = '\r{}/{}: [{}] - ETA {:.1f}s'.format(
idx,
len(self.dataset),
bar,
eta
)
print(tmpl, end='')
if self._batch_idx == len(self.dataset):
print()
def _reset(self):
self._idx = 0
self._batch_idx = 0
self._time = []