-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_output.py
357 lines (327 loc) · 9.25 KB
/
test_output.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
import numpy as np
import torch
import torchaudio
import torch.nn.functional as F
import matplotlib.pyplot as plt
import os
from psycho_acoustic_loss import psycho_acoustic_loss, compute_STFT
def main():
# Load audio
fs = 44100
N = 1024
nfilts = 64
mT_dB_shift = 20
normalize = False
if normalize:
ref_dB = 120
waveform, sample_rate = torchaudio.load("test_wavs/v_gt.wav")
audio_gt = waveform[0]
waveform, sample_rate = torchaudio.load("test_wavs/v_good.wav")
audio_good = waveform[0]
waveform, sample_rate = torchaudio.load("test_wavs/v_bad.wav")
assert fs == sample_rate
audio_bad = waveform[0]
waveform, sample_rate = torchaudio.load("test_wavs/v_gt_quiet.wav")
audio_gt_quiet = waveform[0]
waveform, sample_rate = torchaudio.load("test_wavs/v_gt_rev_quiet.wav")
audio_gt_rev_quiet = waveform[0]
# Compute STFT
ys_gt = compute_STFT(audio_gt, N=N, normalize=normalize).unsqueeze(0).unsqueeze(0)
ys_good = (
compute_STFT(audio_good, N=N, normalize=normalize).unsqueeze(0).unsqueeze(0)
)
ys_bad = compute_STFT(audio_bad, N=N, normalize=normalize).unsqueeze(0).unsqueeze(0)
ys_gt_quiet = (
compute_STFT(audio_gt_quiet, N=N, normalize=normalize).unsqueeze(0).unsqueeze(0)
)
ys_gt_rev_quiet = (
compute_STFT(audio_gt_rev_quiet, N=N, normalize=normalize)
.unsqueeze(0)
.unsqueeze(0)
)
mse_loss_good_gt = F.mse_loss(ys_gt, ys_good)
print("mse_loss_good_gt", mse_loss_good_gt.item()) # loss should be small
mse_loss_bad_gt = F.mse_loss(ys_gt, ys_bad)
print("mse_loss_bad_gt", mse_loss_bad_gt.item()) # loss should be large
print(
"mse loss: good/bad ratio, small is better",
mse_loss_good_gt.item() / mse_loss_bad_gt.item(),
)
print("====================================================")
# SMR_weighted
ploss_good = psycho_acoustic_loss(
ys_good,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="SMR_weighted",
use_LTQ=False,
mT_shift=0.00,
)
print("SMR_weighted loss: good, gt", ploss_good.item())
ploss_bad = psycho_acoustic_loss(
ys_bad,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="SMR_weighted",
use_LTQ=False,
mT_shift=0.00,
)
print("SMR_weighted loss: bad, gt", ploss_bad.item())
print(
"SMR_weighted loss: good/bad ratio, small is better",
ploss_good.item() / ploss_bad.item(),
)
print("====================================================")
# MTD
ploss_good = psycho_acoustic_loss(
ys_good,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="MTD",
use_LTQ=False,
)
print("psy loss: good, gt", ploss_good.item())
ploss_bad = psycho_acoustic_loss(
ys_bad,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="MTD",
use_LTQ=False,
)
print("MTD: bad, gt", ploss_bad.item())
print(
"MTD: good/bad ratio, small is better",
ploss_good.item() / ploss_bad.item(),
)
print("====================================================")
# MTWSD_scaled
ploss_good = psycho_acoustic_loss(
ys_good,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="MTWSD_scaled",
use_LTQ=False,
)
print("MTWSD_scaled: good, gt", ploss_good.item())
ploss_bad = psycho_acoustic_loss(
ys_bad,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="MTWSD_scaled",
use_LTQ=False,
)
print("MTWSD_scaled: bad, gt", ploss_bad.item())
print(
"MTWSD_scaled: good/bad ratio, small is better",
ploss_good.item() / ploss_bad.item(),
)
print("====================================================")
# MTWSD + LTQ
ploss_good = psycho_acoustic_loss(
ys_good,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="MTWSD",
use_LTQ=True,
use_dB=False,
)
print("MTWSD + LTQ: good, gt", ploss_good.item())
ploss_bad = psycho_acoustic_loss(
ys_bad,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="MTWSD",
use_LTQ=True,
use_dB=False,
)
print("MTWSD + LTQ: bad, gt", ploss_bad.item())
print(
"MTWSD + LTQ: good/bad ratio, small is better",
ploss_good.item() / ploss_bad.item(),
)
print("====================================================")
# LTQ_weighted
ploss_good = psycho_acoustic_loss(
ys_good,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="LTQ_weighted",
use_LTQ=True,
mT_shift=0.0,
use_dB=False,
)
print(f"LTQ_weighted: good, gt", ploss_good.item())
ploss_bad = psycho_acoustic_loss(
ys_bad,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="LTQ_weighted",
use_LTQ=True,
mT_shift=0.0,
use_dB=False,
)
print(f"LTQ_weighted: bad, gt", ploss_bad.item())
print(
f"LTQ_weighted: good/bad ratio, small is better",
ploss_good.item() / ploss_bad.item(),
)
print("====================================================")
# SAL
ploss_good = psycho_acoustic_loss(
ys_good,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="SAL",
use_LTQ=True,
mT_shift=0.0,
use_dB=True,
)
print(f"SAL: good, gt", ploss_good.item())
ploss_bad = psycho_acoustic_loss(
ys_bad,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="SAL",
use_LTQ=True,
mT_shift=0.0,
use_dB=True,
)
print(f"SAL: bad, gt", ploss_bad.item())
print(
f"SAL: good/bad ratio, small is better",
ploss_good.item() / ploss_bad.item(),
)
print("====================================================")
# SAL SoftPlus
ploss_good = psycho_acoustic_loss(
ys_good,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="SAL_softplus",
use_LTQ=False,
mT_shift=0.0,
use_dB=False,
)
print(f"SAL_softplus: good, gt", ploss_good.item())
ploss_bad = psycho_acoustic_loss(
ys_bad,
ys_gt,
fs=sample_rate,
N=N,
nfilts=nfilts,
method="SAL_softplus",
use_LTQ=False,
mT_shift=0.0,
use_dB=False,
)
print(f"SAL_softplus: bad, gt", ploss_bad.item())
print(
f"SAL_softplus: good/bad ratio, small is better",
ploss_good.item() / ploss_bad.item(),
)
print("====================================================")
# print("====================================================")
# mse_loss_quiet = F.mse_loss(ys_gt_quiet, ys_gt_rev_quiet)
# print("ys_gt_quiet", ys_gt_quiet.shape)
# print("ys_gt_rev_quiet", ys_gt_rev_quiet.shape)
# print("ys_gt", ys_gt.shape)
# mse_loss_quiet_gt = F.mse_loss(ys_gt, ys_gt_quiet)
# print("mse_loss_quiet", mse_loss_quiet.item())
# print("mse_loss_quiet_gt", mse_loss_quiet_gt.item())
# print(
# "mse loss: quiet/gt ratio, small is better",
# mse_loss_quiet.item() / mse_loss_quiet_gt.item(),
# )
# ploss_quiet = psycho_acoustic_loss(
# ys_gt_rev_quiet,
# ys_gt_quiet,
# fs=sample_rate,
# N=N,
# nfilts=nfilts,
# use_weighting=True,
# use_LTQ=True,
# use_dB=False,
# )
# ploss_quiet_gt = psycho_acoustic_loss(
# ys_gt_quiet,
# ys_gt,
# fs=sample_rate,
# N=N,
# nfilts=nfilts,
# use_weighting=True,
# use_LTQ=True,
# use_dB=False,
# )
# print("psy loss: quiet, rev_quiet", ploss_quiet.item())
# print("psy loss: quiet_gt, gt", ploss_quiet_gt.item())
# print(
# "psy loss: quiet/quiet_gt ratio, small is better",
# ploss_quiet.item() / ploss_quiet_gt.item(),
# )
# ploss_quiet = psycho_acoustic_loss(
# ys_gt_rev_quiet,
# ys_gt_quiet,
# fs=sample_rate,
# N=N,
# nfilts=nfilts,
# use_weighting=False,
# use_LTQ=True,
# use_dB=True,
# )
# ploss_quiet_gt = psycho_acoustic_loss(
# ys_gt_quiet,
# ys_gt,
# fs=sample_rate,
# N=N,
# nfilts=nfilts,
# use_weighting=False,
# use_LTQ=True,
# use_dB=True,
# )
# print("psy loss without LTQ: quiet, rev_quiet", ploss_quiet.item())
# print("psy loss without LTQ: quiet_gt, gt", ploss_quiet_gt.item())
# print(
# "psy loss without LTQ: quiet/quiet_gt ratio, small is better",
# ploss_quiet.item() / ploss_quiet_gt.item(),
# )
# # only test
# ys_gt_noise = torch.rand(ys_gt.shape)
# ys_gt_sin = torch.sin(ys_gt)
# ploss = psycho_acoustic_loss(
# ys_gt_rev_quiet,
# ys_gt_noise,
# fs=sample_rate,
# N=N,
# nfilts=nfilts,
# use_weighting=True,
# use_LTQ=True,
# use_dB=True,
# )
if __name__ == "__main__":
main()