-
Notifications
You must be signed in to change notification settings - Fork 153
/
inferencer.py
220 lines (169 loc) · 8.08 KB
/
inferencer.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
import torch
from audio_zen.acoustics.feature import mag_phase
from audio_zen.acoustics.mask import decompress_cIRM
from audio_zen.inferencer.base_inferencer import BaseInferencer
def cumulative_norm(input):
eps = 1e-10
device = input.device
data_type = input.dtype
n_dim = input.ndim
assert n_dim in (3, 4)
if n_dim == 3:
n_channels = 1
batch_size, n_freqs, n_frames = input.size()
else:
batch_size, n_channels, n_freqs, n_frames = input.size()
input = input.reshape(batch_size * n_channels, n_freqs, n_frames)
step_sum = torch.sum(input, dim=1) # [B, T]
step_pow_sum = torch.sum(torch.square(input), dim=1)
cumulative_sum = torch.cumsum(step_sum, dim=-1) # [B, T]
cumulative_pow_sum = torch.cumsum(step_pow_sum, dim=-1) # [B, T]
entry_count = torch.arange(
n_freqs, n_freqs * n_frames + 1, n_freqs, dtype=data_type, device=device
)
entry_count = entry_count.reshape(1, n_frames) # [1, T]
entry_count = entry_count.expand_as(cumulative_sum) # [1, T] => [B, T]
cum_mean = cumulative_sum / entry_count # B, T
cum_var = (
cumulative_pow_sum - 2 * cum_mean * cumulative_sum
) / entry_count + cum_mean.pow(
2
) # B, T
cum_std = (cum_var + eps).sqrt() # B, T
cum_mean = cum_mean.reshape(batch_size * n_channels, 1, n_frames)
cum_std = cum_std.reshape(batch_size * n_channels, 1, n_frames)
x = (input - cum_mean) / cum_std
if n_dim == 4:
x = x.reshape(batch_size, n_channels, n_freqs, n_frames)
return x
class Inferencer(BaseInferencer):
def __init__(self, config, checkpoint_path, output_dir):
super().__init__(config, checkpoint_path, output_dir)
@torch.no_grad()
def mag(self, noisy, inference_args):
noisy_complex = self.torch_stft(noisy)
noisy_mag, noisy_phase = mag_phase(noisy_complex) # [B, F, T] => [B, 1, F, T]
enhanced_mag = self.model(noisy_mag.unsqueeze(1)).squeeze(1)
enhanced = self.torch_istft(
(enhanced_mag, noisy_phase), length=noisy.size(-1), input_type="mag_phase"
)
enhanced = enhanced.detach().squeeze(0).cpu().numpy()
return enhanced
@torch.no_grad()
def scaled_mask(self, noisy, inference_args):
noisy_complex = self.torch_stft(noisy)
noisy_mag, noisy_phase = mag_phase(noisy_complex)
# [B, F, T] => [B, 1, F, T] => model => [B, 2, F, T] => [B, F, T, 2]
noisy_mag = noisy_mag.unsqueeze(1)
scaled_mask = self.model(noisy_mag)
scaled_mask = scaled_mask.permute(0, 2, 3, 1)
enhanced_complex = noisy_complex * scaled_mask
enhanced = self.torch_istft(enhanced_complex, length=noisy.size(-1))
enhanced = enhanced.detach().squeeze(0).cpu().numpy()
return enhanced
@torch.no_grad()
def sub_band_crm_mask(self, noisy, inference_args):
pad_mode = inference_args["pad_mode"]
n_neighbor = inference_args["n_neighbor"]
noisy = noisy.cpu().numpy().reshape(-1)
noisy_D = self.librosa_stft(noisy)
noisy_real = torch.tensor(noisy_D.real, device=self.device)
noisy_imag = torch.tensor(noisy_D.imag, device=self.device)
noisy_mag = torch.sqrt(
torch.square(noisy_real) + torch.square(noisy_imag)
) # [F, T]
n_freqs, n_frames = noisy_mag.size()
noisy_mag = noisy_mag.reshape(1, 1, n_freqs, n_frames)
noisy_mag_padded = self._unfold(
noisy_mag, pad_mode, n_neighbor
) # [B, N, C, F_s, T] <=> [1, 257, 1, 31, T]
noisy_mag_padded = noisy_mag_padded.squeeze(0).squeeze(
1
) # [257, 31, 200] <=> [B, F_s, T]
pred_crm = self.model(noisy_mag_padded).detach() # [B, 2, T] <=> [F, 2, T]
pred_crm = pred_crm.permute(0, 2, 1).contiguous() # [B, T, 2]
lim = 9.99
pred_crm = (
lim * (pred_crm >= lim)
- lim * (pred_crm <= -lim)
+ pred_crm * (torch.abs(pred_crm) < lim)
)
pred_crm = -10 * torch.log((10 - pred_crm) / (10 + pred_crm))
enhanced_real = pred_crm[:, :, 0] * noisy_real - pred_crm[:, :, 1] * noisy_imag
enhanced_imag = pred_crm[:, :, 1] * noisy_real + pred_crm[:, :, 0] * noisy_imag
enhanced_real = enhanced_real.cpu().numpy()
enhanced_imag = enhanced_imag.cpu().numpy()
enhanced = self.librosa_istft(enhanced_real + 1j * enhanced_imag, length=len(noisy))
return enhanced
@torch.no_grad()
def full_band_crm_mask(self, noisy, inference_args):
noisy_mag, _, noisy_real, noisy_imag = self.torch_stft(noisy)
noisy_mag = noisy_mag.unsqueeze(1)
pred_crm = self.model(noisy_mag)
pred_crm = pred_crm.permute(0, 2, 3, 1)
pred_crm = decompress_cIRM(pred_crm)
enhanced_real = pred_crm[..., 0] * noisy_real - pred_crm[..., 1] * noisy_imag
enhanced_imag = pred_crm[..., 1] * noisy_real + pred_crm[..., 0] * noisy_imag
enhanced = self.torch_istft(
(enhanced_real, enhanced_imag), length=noisy.size(-1), input_type="real_imag"
)
enhanced = enhanced.detach().squeeze(0).cpu().numpy()
return enhanced
@torch.no_grad()
def overlapped_chunk(self, noisy, inference_args):
noisy = noisy.squeeze(0)
num_mics = 8
chunk_length = 16000 * inference_args["chunk_length"]
chunk_hop_length = chunk_length // 2
num_chunks = int(noisy.shape[-1] / chunk_hop_length) + 1
win = torch.hann_window(chunk_length, device=noisy.device)
prev = None
enhanced = None
# Mock the silent segment of speech to prevent the speech from being processed at the beginning
for chunk_idx in range(num_chunks):
if chunk_idx == 0:
pad = torch.zeros((num_mics, 256), device=noisy.device)
chunk_start_position = chunk_idx * chunk_hop_length
chunk_end_position = chunk_start_position + chunk_length
# concat([(8, 256), (..., ... + chunk_length)])
noisy_chunk = torch.cat(
(pad, noisy[:, chunk_start_position:chunk_end_position]), dim=1
)
enhanced_chunk = self.model(noisy_chunk.unsqueeze(0))
enhanced_chunk = torch.squeeze(enhanced_chunk)
enhanced_chunk = enhanced_chunk[256:]
# Save the prior half chunk,
cur = enhanced_chunk[: chunk_length // 2]
# only for the 1st chunk,no overlap for the very 1st chunk prior half
prev = enhanced_chunk[chunk_length // 2 :] * win[chunk_length // 2 :]
else:
# use the previous noisy data as the pad
pad = noisy[
:, (chunk_idx * chunk_hop_length - 256) : (chunk_idx * chunk_hop_length)
]
chunk_start_position = chunk_idx * chunk_hop_length
chunk_end_position = chunk_start_position + chunk_length
noisy_chunk = torch.cat(
(pad, noisy[:8, chunk_start_position:chunk_end_position]), dim=1
)
enhanced_chunk = self.model(noisy_chunk.unsqueeze(0))
enhanced_chunk = torch.squeeze(enhanced_chunk)
enhanced_chunk = enhanced_chunk[256:]
enhanced_chunk = enhanced_chunk * win[: len(enhanced_chunk)]
tmp = enhanced_chunk[: chunk_length // 2]
cur = tmp[: min(len(tmp), len(prev))] + prev[: min(len(tmp), len(prev))]
prev = enhanced_chunk[chunk_length // 2 :]
if enhanced is None:
enhanced = cur
else:
enhanced = torch.cat((enhanced, cur), dim=0)
enhanced = enhanced[: noisy.shape[1]]
return enhanced.detach().squeeze(0).cpu().numpy()
@torch.no_grad()
def time_domain(self, noisy, inference_args):
noisy = noisy.to(self.device)
enhanced = self.model(noisy)
return enhanced.detach().squeeze().cpu().numpy()
if __name__ == "__main__":
a = torch.rand(10, 2, 161, 200)
print(cumulative_norm(a).shape)