forked from hanqingguo/infocom2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_eva_hide.py
268 lines (239 loc) · 13.1 KB
/
metric_eva_hide.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
# Use dataloader to load multiple data, then use inference.py to produce output.
# combine with speech2text.py to produce metrics
from datasets.eva_dataloader_hide import create_dataloader
import wavio
import argparse
from utils.hparams import HParam
import os
import glob
import torch
import librosa
import argparse
from utils.audio import Audio
from model.model import VoiceFilter
from model.embedder import SpeechEmbedder
from utils.speech2text import speech_2_text
from utils.evaluation import tensor_normalize
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
def wav_to_embedding(wav, embedder):
"""
This function takes wav as input, then produce the embedding for given wav
Returns:
embedding as numpy array
"""
wav_mel = audio.get_mel(wav)
wav_ref_mel = torch.from_numpy(wav_mel).float().cuda()
wav_dvec = embedder(wav_ref_mel)
wav_dvector = wav_dvec.unsqueeze(0)
wav_dvector = wav_dvector.cpu().detach().numpy()
return wav_dvector
def init_badwav():
wer = 1
mer = 1
wil = 1
pesq_value = 0
sdr = [-10]
sir = [np.inf]
sar = [-10]
return wer, mer, wil, pesq_value, sdr, sir, sar
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--base_dir', type=str, default='.',
help="Root directory of run.")
parser.add_argument('-c', '--config', type=str, required=True,
help="yaml file for configuration")
parser.add_argument('-e', '--embedder_path', type=str, required=True,
help="path of embedder model pt file")
parser.add_argument('--checkpoint_path', type=str, default=None,
help="path of checkpoint pt file")
parser.add_argument('-m', '--model', type=str, required=True,
help="Name of the model. Used for both logging and saving checkpoints.")
parser.add_argument('-g', '--gpu', type=int, required=True, default='1',
help="ID of the selected gpu. Used for gpu selection.")
parser.add_argument('-o', '--out_dir', type=str, required=True,
help="out directory of result.wav")
# parser.add_argument('-x', '--xlsx', type=str, required=True,
# help="result name of xlsx file")
args = parser.parse_args()
hp = HParam(args.config)
root_dir_test = hp.data.test_dir
alldirs = [x[0] for x in os.walk(root_dir_test)]
# dirs = [leaf for leaf in alldirs if leaf.split('/')[-1].isnumeric()]
dirs = [leaf for leaf in alldirs if leaf.split('/')[-1] == 'conversation' or leaf.split('/')[-1]== '0dB']
speaker_count = 0
for dir in dirs:
speaker_count = speaker_count + 1
print("Speaker : {}/280\n".format(speaker_count))
hp.data.test_dir = dir
testloader = create_dataloader(hp, args, train=False)
result_purified1 = []
result_purified2 = []
result_purified3 = []
result_mixed = []
# result of new measurement
result_purified1_new = []
result_purified2_new = []
result_purified3_new = []
result_mixed_new = []
torch.cuda.set_device(args.gpu)
for batch in testloader:
# length of batch is 1, set in dataloader
ref_mel, eliminated_wav, mixed_wav, expected_hidden_wav, eliminated_mag, expected_hidden_mag, mixed_mag, mixed_phase, dvec_path, eliminated_wav_path, mixed_wav_path = \
batch[0]
# print("eliminated: {}".format(eliminated_wav_path))
print("Mixed: {}".format(mixed_wav_path))
model = VoiceFilter(hp).cuda()
chkpt_model = torch.load(args.checkpoint_path, map_location='cuda:0')['model']
model.load_state_dict(chkpt_model)
model.eval()
embedder = SpeechEmbedder(hp).cuda()
chkpt_embed = torch.load(args.embedder_path)
embedder.load_state_dict(chkpt_embed)
embedder.eval()
audio = Audio(hp)
# dvec_wav, _ = librosa.load(dvec_path, sr=16000)
# ref_mel = audio.get_mel(dvec_wav)
# ref_mel = torch.from_numpy(ref_mel).float().cuda()
ref_mel = ref_mel.cuda()
dvec = embedder(ref_mel)
dvec = dvec.unsqueeze(0) # (1, 256)
mixed_wav, _ = librosa.load(mixed_wav_path, sr=16000)
mixed_mag, mixed_phase = audio.wav2spec(mixed_wav)
mixed_mag = torch.from_numpy(mixed_mag).float().cuda()
mixed_mag = mixed_mag.unsqueeze(0)
shadow_mag = model(mixed_mag, dvec)
# shadow_mag.size() = [1, 301, 601]
recorded_mag = tensor_normalize(mixed_mag + shadow_mag)
recorded_mag = recorded_mag[0].cpu().detach().numpy()
mixed_mag = mixed_mag[0].cpu().detach().numpy()
eliminated_mag = eliminated_mag[0].cpu().detach().numpy()
# recorded_mag = recorded_mag[0].cpu().detach().numpy()
shadow_mag = shadow_mag[0].cpu().detach().numpy()
shadow_wav = audio.spec2wav(shadow_mag, mixed_phase)
# scale is frequency pass to time domain, used on wav signal normalization
recorded_wav1 = audio.spec2wav(recorded_mag, mixed_phase) # path 1
hidden_wav = (mixed_wav + 100 * shadow_wav) / max(abs(mixed_wav + 100 * shadow_wav)) # path 2
os.makedirs(args.out_dir, exist_ok=True)
purified1 = os.path.join(args.out_dir, 'result1.wav')
# purified2 = os.path.join(args.out_dir, 'result2.wav')
# purified3 = os.path.join(args.out_dir, 'result3.wav')
eliminated_path = os.path.join(args.out_dir, 'eliminated.wav')
expected_hidden_path = os.path.join(args.out_dir, 'expected_hidden.wav')
mixed_path = os.path.join(args.out_dir, 'mixed.wav')
# original mixed wav and eliminated wav are not PCM, cannot be read by google cloud
wavio.write(purified1, recorded_wav1, 16000, sampwidth=2) # frequency +
# wavio.write(purified2, shadow_wav, 16000, sampwidth=2) # est noise
# wavio.write(purified3, hidden_wav, 16000, sampwidth=2) # mix + est noise
wavio.write(eliminated_path, eliminated_wav, 16000, sampwidth=2)
wavio.write(expected_hidden_path, expected_hidden_wav, 16000, sampwidth=2)
wavio.write(mixed_path, mixed_wav, 16000, sampwidth=2)
# librosa.output.write_wav(out_path, enhanced_wav, sr=16000)
dvec = dvec.cpu().detach().numpy() # the reference embedding
mixed_dvec = wav_to_embedding(mixed_wav, embedder)
purified1_dvec = wav_to_embedding(recorded_wav1, embedder)
# purified2_dvec = wav_to_embedding(shadow_wav, embedder)
# purified3_dvec = wav_to_embedding(hidden_wav, embedder)
purified1_conf = cosine_similarity(dvec, purified1_dvec)[0][0]
# purified2_conf = cosine_similarity(dvec, purified2_dvec)[0][0]
# purified3_conf = cosine_similarity(dvec, purified3_dvec)[0][0]
mixed_conf = cosine_similarity(dvec, mixed_dvec)[0][0]
try:
[wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(eliminated_path, purified1, 'google')
except TypeError:
wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
# print(
# "Spectrogram: wer: {0}, mer: {1}, wil: {2}, pesq: {3}, sdr: {4}".format(wer, mer, wil, pesq_value, sdr))
r1 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
"wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
"confidence": purified1_conf}
result_purified1.append(r1)
# try:
# [wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(eliminated_path, purified2, 'google')
# except TypeError:
# wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
#
# r2 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
# "wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
# "confidence": purified2_conf}
# result_purified2.append(r2)
#
# try:
# [wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(eliminated_path, purified3, 'google')
# except TypeError:
# wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
# r3 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
# "wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
# "confidence": purified3_conf}
# result_purified3.append(r3)
try:
[wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(eliminated_path, mixed_path, 'google')
except TypeError:
wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
r4 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
"wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
"confidence": mixed_conf}
result_mixed.append(r4)
# Measurement for new eliminated
if dir.split('/')[-3] != 'noise':
try:
[wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(expected_hidden_path, purified1, 'google')
except TypeError:
wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
# print(
# "Spectrogram: wer: {0}, mer: {1}, wil: {2}, pesq: {3}, sdr: {4}".format(wer, mer, wil, pesq_value, sdr))
r1 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
"wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
"confidence": purified1_conf}
result_purified1_new.append(r1)
# try:
# [wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(expected_hidden_path, purified2, 'google')
# except TypeError:
# wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
#
# r2 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
# "wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
# "confidence": purified2_conf}
# result_purified2_new.append(r2)
#
# try:
# [wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(expected_hidden_path, purified3, 'google')
# except TypeError:
# wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
# r3 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
# "wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
# "confidence": purified3_conf}
# result_purified3_new.append(r3)
try:
[wer, mer, wil], pesq_value, sdr, sir, sar = speech_2_text(expected_hidden_path, mixed_path, 'google')
except TypeError:
wer, mer, wil, pesq_value, sdr, sir, sar = init_badwav()
r4 = {"mixed_path": mixed_wav_path, "eliminated_path": eliminated_wav_path,
"wer": wer, "mer": mer, "wil": wil, "pesq": pesq_value, "sdr": sdr[0], "sir": sir[0], "sar": sar[0],
"confidence": mixed_conf}
result_mixed_new.append(r4)
else:
print("Expect hidden is a constant noise, skip it! ")
hide_result_eliminated = os.path.join(dir, 'hide_eliminated.xlsx')
writer = pd.ExcelWriter(hide_result_eliminated, engine='xlsxwriter', options={'strings_to_urls': False})
df1 = pd.DataFrame(data=result_purified1)
# df2 = pd.DataFrame(data=result_purified2)
# df3 = pd.DataFrame(data=result_purified3)
df4 = pd.DataFrame(data=result_mixed)
df1.to_excel(writer, sheet_name='purified1')
# df2.to_excel(writer, sheet_name='purified2')
# df3.to_excel(writer, sheet_name='purified3')
df4.to_excel(writer, sheet_name='mixed')
hide_result_expect_hidden = os.path.join(dir, 'hide_expect_hidden.xlsx')
writer1 = pd.ExcelWriter(hide_result_expect_hidden, engine='xlsxwriter', options={'strings_to_urls': False})
df1 = pd.DataFrame(data=result_purified1_new)
# df2 = pd.DataFrame(data=result_purified2_new)
# df3 = pd.DataFrame(data=result_purified3_new)
df4 = pd.DataFrame(data=result_mixed_new)
df1.to_excel(writer1, sheet_name='purified1')
# df2.to_excel(writer1, sheet_name='purified2')
# df3.to_excel(writer1, sheet_name='purified3')
df4.to_excel(writer1, sheet_name='mixed')
writer.close()
writer1.close()