-
Notifications
You must be signed in to change notification settings - Fork 2
/
object_metrics.py
451 lines (364 loc) · 15.2 KB
/
object_metrics.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
from model import PreDefinedEmbedder
# from encoder.model import SpeakerEncoder
from encoder import inference as encoder
from encoder.params_model import model_embedding_size as speaker_embedding_size
import librosa
import numpy as np
import yaml
import os
from tqdm import tqdm
import argparse
import pathlib
import scipy
from sklearn.metrics.pairwise import cosine_similarity
import math
import glob
import pyworld
import json
import pysptk
import matplotlib.pyplot as plot
import torch
import zipfile
import torchaudio
import jiwer
import argparse
from Metrics.f0_frame_error import FFE
from utils.tools import pad_1D
# from binary_io import BinaryIOCollection
def compute_mcd(args):
ref_wav_path = args.ref_wav_dir
synth_wav_path = args.synth_wav_dir
# SAMPLING_RATE = 22050
FRAME_PERIOD = 5.0
alpha = 0.65 # commonly used at 22050 Hz
fft_size = 512
mcep_size = 25
# Compute mcep for reference audio
ref_mcep_dir = "output/mcep_numpy/ref"
# compute mcep for synthesied audio
synth_mcep_dir = "output/mcep_numpy/synth"
if os.path.exists(f"{ref_wav_path}/{ref_mcep_dir}"):
pass
else:
os.makedirs(f"{ref_wav_path}/{ref_mcep_dir}")
if os.path.exists(f"{synth_wav_path}/{synth_mcep_dir}"):
pass
else:
os.makedirs(f"{synth_wav_path}/{synth_mcep_dir}")
def load_wav(wav_file, sr):
"""
Load a wav file with librosa.
:param wav_file: path to wav file
:param sr: sampling rate
:return: audio time series numpy array
"""
wav, _ = librosa.load(wav_file, sr=sr, mono=True)
return wav
def log_spec_dB_dist(x, y):
log_spec_dB_const = 10.0 / math.log(10.0) * math.sqrt(2.0)
diff = x - y
return log_spec_dB_const * math.sqrt(np.inner(diff, diff))
def wav2mcep_numpy(wavfile, target_directory, alpha=0.65, fft_size=512, mcep_size=34,type=None):
# make relevant directories
if not os.path.exists(target_directory):
os.makedirs(target_directory)
loaded_wav = load_wav(wavfile, sr=args.sampling_rate)
# Use WORLD vocoder to spectral envelope
_, sp, _ = pyworld.wav2world(loaded_wav.astype(np.double), fs=args.sampling_rate,
frame_period=FRAME_PERIOD, fft_size=fft_size)
# Extract MCEP features
mgc = pysptk.sptk.mcep(sp, order=mcep_size, alpha=alpha, maxiter=0,
etype=1, eps=1.0E-8, min_det=0.0, itype=3)
fname = os.path.basename(wavfile).split('.')[0]
# if type == "ref":
# basename = wavfile.split("/")[-3]
# else:
# basename = wavfile.split("/")[-2]
# np.save(os.path.join(target_directory, basename + '-' + fname + '.npy'),
# mgc,
# allow_pickle=False)
np.save(os.path.join(target_directory, fname + '.npy'),
mgc,
allow_pickle=False)
# computer average mcd using mcep files
def average_mcd(ref_mcep_files, synth_mcep_files, cost_function):
"""
Calculate the average MCD.
:param ref_mcep_files: list of strings, paths to MCEP target reference files
:param synth_mcep_files: list of strings, paths to MCEP converted synthesised files
:param cost_function: distance metric used
:returns: average MCD, total frames processed
"""
min_cost_tot = 0.0
frames_tot = 0
for num, ref in enumerate(ref_mcep_files):
for synth in synth_mcep_files:
# get the trg_ref and conv_synth speaker name and sample id
ref_fsplit, synth_fsplit = os.path.basename(ref).split('_'), os.path.basename(synth).split('_')
ref_spk, ref_id = ref_fsplit[0], ref_fsplit[-1].split('.')[0]
synth_spk, synth_id = synth_fsplit[0], synth_fsplit[-1].split('.')[0]
# print(synth_id)
# breakpoint()
# if the speaker name is the same and sample id is the same, do MCD
if ref_spk == synth_spk and ref_id == synth_id:
# load MCEP vectors
ref_vec = np.load(ref)
ref_frame_no = len(ref_vec)
synth_vec = np.load(synth)
# dynamic time warping using librosa
min_cost, _ = librosa.sequence.dtw(ref_vec[:, 1:].T, synth_vec[:, 1:].T,
metric=cost_function)
min_cost_tot += np.mean(min_cost)
frames_tot += ref_frame_no
# breakpoint()
print("----------", num, "------------")
mean_mcd = min_cost_tot / frames_tot
return mean_mcd, frames_tot
# ref_wav_files = glob.glob(f"{ref_wav_path}/*/*.wav",recursive=True)
# synth_wav_files = glob.glob(f"{synth_wav_path}/*/*.wav",recursive=True)
#changed
#'''
ref_wav_files = glob.glob(f"{ref_wav_path}/*.wav",recursive=True)
synth_wav_files = glob.glob(f"{synth_wav_path}/*.wav",recursive=True)
#'''
# for wav in ref_wav_files:
# wav2mcep_numpy(wav, ref_mcep_dir, fft_size=fft_size, mcep_size=mcep_size)
# for wav in synth_wav_files:
# wav2mcep_numpy(wav, synth_mcep_dir, fft_size=fft_size, mcep_size=mcep_size)
# alpha = 0.65 # commonly used at 22050 Hz
# fft_size = 512
# mcep_size = 25
# Compute mcep for reference audio
# ref_mcep_dir = "./output/mcep_numpy/ref"
# # compute mcep for synthesied audio
# synth_mcep_dir = "./output/mcep_numpy/synth"
for wav in tqdm(ref_wav_files):
wav2mcep_numpy(wav, f"{ref_wav_path}/{ref_mcep_dir}", fft_size=fft_size, mcep_size=mcep_size)
for wav in tqdm(synth_wav_files):
wav2mcep_numpy(wav, f"{synth_wav_path}/{synth_mcep_dir}", fft_size=fft_size, mcep_size=mcep_size)
trg_refs = glob.glob(f"{ref_wav_path}/{ref_mcep_dir}/*")
conv_synths = glob.glob(f"{synth_wav_path}/{synth_mcep_dir}/*")
cost_function = log_spec_dB_dist
mcd, tot_frames_used = average_mcd(trg_refs, conv_synths, cost_function)
return mcd, tot_frames_used
def computer_wer(args):
ref_wav_path = args.ref_wav_dir
synth_wav_path = args.synth_wav_dir
device = torch.device('cpu') # gpu also works, but our models are fast enough for CPU
model, decoder, utils = torch.hub.load(repo_or_dir='snakers4/silero-models',
model='silero_stt',
jit_model='jit_xlarge',
language='en', # also available 'de', 'es'
device=device)
(read_batch, split_into_batches,read_audio, prepare_model_input) = utils # see function signature for details
ref_txt_files = glob.glob(f"{ref_wav_path}/*.lab",recursive=True)
batches = split_into_batches(ref_txt_files, batch_size=10)
# print(len(batches))
# breakpoint()
ground_truth = []
hypothesis = []
for batch in tqdm(batches):
# print(batch)
# input = prepare_model_input(read_batch(batch),
# device=device)
# output = model(input)
for example in batch:
# print(decoder(example.cpu()))
with open(example) as f:
lines = f.readlines()
ground_truth.append(lines[0])
file_list = str(example).split("/")
file_name = file_list[-1].split(".")[0]
# basename = file_list[-3]
# synth_wav_file = synth_wav_path + "/" + basename + "/" +file_name + ".wav"
synth_wav_file = synth_wav_path + "/" + file_name + ".wav"
input = prepare_model_input(read_batch([synth_wav_file]), device=device)
output = model(input)
hypothesis.append(decoder(output[0].cpu()))
# synth_wav_files = glob.glob(f"{synth_wav_path}/*/*.wav",recursive=True)
# batches = split_into_batches(synth_wav_files, batch_size=10)
# for batch in tqdm(batches):
# input = prepare_model_input(read_batch(batch),
# # device=device)
# output = model(input)
# for example in output:
# # print(decoder(example.cpu()))
# hypothesis.append(decoder(example.cpu()))
transformation = jiwer.Compose([
jiwer.ToLowerCase(),
jiwer.RemoveWhiteSpace(replace_by_space=True),
jiwer.RemoveMultipleSpaces(),
jiwer.ReduceToListOfListOfWords(word_delimiter=" ")
])
wer = jiwer.wer(
ground_truth,
hypothesis,
truth_transform=transformation,
hypothesis_transform=transformation)
cer = jiwer.cer(
ground_truth,
hypothesis,
truth_transform=transformation,
hypothesis_transform=transformation)
# breakpoint()
return wer,cer
##changed
#'''
# SAMPLING_RATE = 22050
# trim_top_db = 23
# filter_length = 1024
# hop_length = 256
# config_dir = "./config/VCTK"
#'''
def average_cosine_similarity(args):
ref_wav_dir = args.ref_wav_dir
ref_wav_dir = pathlib.Path(ref_wav_dir)
synth_wav_dir = args.synth_wav_dir
# synth_wav_dir = pathlib.Path(synth_wav_dir)
def load_audio(wav_path):
wav_raw, _ = librosa.load(wav_path, args.sampling_rate)
_, index = librosa.effects.trim(wav_raw, top_db= args.trim_top_db, frame_length= args.filter_length, hop_length= args.hop_length)
wav = wav_raw[index[0]:index[1]]
duration = (index[1] - index[0]) / args.hop_length
return wav_raw.astype(np.float32), wav.astype(np.float32), int(duration)
preprocess_config = yaml.load(open(
os.path.join(args.config_dir, "preprocess.yaml"), "r"), Loader=yaml.FullLoader)
if preprocess_config["preprocessing"]["speaker_embedder"] == "DeepSpeaker":
speaker_emb = PreDefinedEmbedder(preprocess_config)
elif preprocess_config["preprocessing"]["speaker_embedder"] == "GE2E":
encoder.load_model(Path(preprocess_config["preprocessing"]["speaker_embedder_path"]))
cosine_score = []
for ref_wav_path in tqdm(ref_wav_dir.rglob("*.wav")):
ref_wav_raw, ref_wav, ref_duration = load_audio(ref_wav_path)
# ref_spker_embed = speaker_emb(ref_wav)
#added
#'''
ref_spker_embed = encoder.embed_utterance(ref_wav).reshape(1, -1)
#'''
wav_name = str(ref_wav_path).split("/")[-1]
# print("----->>>>", wav_name)
synth_wav_path = synth_wav_dir + "/" + wav_name
synth_wav_raw, synth_wav, synth_duration = load_audio(synth_wav_path)
# synth_spker_embed = speaker_emb(synth_wav)
#added
#'''
synth_spker_embed = encoder.embed_utterance(synth_wav).reshape(1,-1)
#'''
# breakpoint()
score = cosine_similarity(ref_spker_embed, synth_spker_embed)
cosine_score.append(score)
return np.mean(cosine_score),np.var(cosine_score), cosine_score
def average_frame_error_rate(args):
ref_wav_dir = args.ref_wav_dir
ref_wav_dir = pathlib.Path(ref_wav_dir)
synth_wav_dir = args.synth_wav_dir
def load_audio(wav_path):
wav_raw, _ = librosa.load(wav_path, args.sampling_rate)
_, index = librosa.effects.trim(wav_raw, top_db= args.trim_top_db, frame_length= args.filter_length, hop_length= args.hop_length)
wav = wav_raw[index[0]:index[1]]
duration = (index[1] - index[0]) / args.hop_length
return wav_raw.astype(np.float32), wav.astype(np.float32), int(duration)
ffe = FFE(args.sampling_rate)
ffe_score = []
for ref_wav_path in tqdm(ref_wav_dir.rglob("*.wav")):
ref_wav_raw, ref_wav, ref_duration = load_audio(ref_wav_path)
wav_name = str(ref_wav_path).split("/")[-1]
synth_wav_path = synth_wav_dir + "/" + wav_name
synth_wav_raw, synth_wav, synth_duration = load_audio(synth_wav_path)
data = [ref_wav,synth_wav]
data = pad_1D(data)
ref_wav,synth_wav = data
score = ffe.calculate_ffe(torch.tensor(ref_wav),torch.tensor(synth_wav))
ffe_score.append(score)
return np.mean(ffe_score), np.var(ffe_score), ffe_score
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="LTS100",
help="Dataset",
)
parser.add_argument(
"--ref_wav_dir",
type=str,
required=True,
help="Path to orignal wav files",
)
parser.add_argument(
"--synth_wav_dir",
type=str,
required=True,
help="Path to denoised wav files",
)
parser.add_argument(
"--config_dir",
type=str,
default="./config/LTS",
help="config file",
)
parser.add_argument(
"--trim_top_db",
type=int,
default=23,
help="trim_top_db",
)
parser.add_argument(
"--filter_length",
type=int,
default=1024,
help="filter_length",
)
parser.add_argument(
"--hop_length",
type=int,
default=256,
help="hop_length",
)
parser.add_argument(
"--sampling_rate",
type=int,
default=16000,
help="sampling_rate",
)
args = parser.parse_args()
return args
'''
#LTS
python object_metrics.py --ref_wav_dir /data/Dataset/raw_data/LTS100_16k --synth_wav_dir /data/tts_0_output/result/LTS100_hyperx/392500
#VCTK
python object_metrics.py --ref_wav_dir /data/Dataset/raw_data/VCTK --synth_wav_dir /data/tts_0_output/result/VCTK_hyperx/105000
'''
from pathlib import Path
def fetch_ref_from_synth(args):
synth_wav_dir = Path(args.synth_wav_dir)
dataset = args.dataset
ref_wav_dir = os.path.join("/".join(args.synth_wav_dir.split("/")[:-2]), f"{dataset}_GT")
if not os.path.exists(ref_wav_dir):
os.makedirs(ref_wav_dir)
for synth_wav_path in tqdm(synth_wav_dir.rglob("*.wav")):
synth_wav_path = str(synth_wav_path)
base_name = synth_wav_path.split("/")[-1]
if dataset == "VCTK":
spk = base_name.split("-")[0]
else:
spk = base_name.split("_")[0]
ref_wav_path = os.path.join(args.ref_wav_dir, spk, base_name)
ref_lab_path = os.path.join(args.ref_wav_dir, spk, base_name.replace(".wav", ".lab"))
os.system(f"cp {ref_wav_path} {ref_wav_dir}")
os.system(f"cp {ref_lab_path} {ref_wav_dir}")
'''
python object_metrics.py --ref_wav_dir /data/tts_0_output/result/LTS100_GT --synth_wav_dir /data/tts_0_output/result/LTS100_concat_wo_hyperx/330000
'''
if __name__ == "__main__":
args = get_args()
if False:
fetch_ref_from_synth(args)
cos_mean, cos_var, cosine_scores = average_cosine_similarity(args)
ffe_mean, ffe_var, ffe_scores = average_frame_error_rate(args)
wer, cer = computer_wer(args)
mcd,tot_frames_used = compute_mcd(args)
print(f'COS mean={cos_mean}, var={cos_var}')
print(f'FFE mean={ffe_mean}, var={ffe_var}')
print(f"Word Error Rate = {wer}, Character Error Rate = {cer}")
print(f'MCD = {mcd} dB, calculated over a total of {tot_frames_used} frames')