-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluate.py
264 lines (231 loc) · 12.3 KB
/
evaluate.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
import argparse
import os
import glob
import torch
import yaml
import torch.nn as nn
from torch.utils.data import DataLoader
from utils.tools import log, synth_one_sample
# from style_dubber import Style_dubber_model_loss, Style_dubber_model_loss_change, Style_dubber_model_loss_noACL, Style_dubber_model_loss_Dia, Style_dubber_model_loss_Dia_1, Style_dubber_model_loss_Dia_LipACL, Style_dubber_model_loss_Dia_P1, Style_dubber_model_loss_Dia_P2, Style_dubber_model_loss_Dia_P1_ACL, Style_dubber_model_loss_Dia_P1_ACL_E1, Style_dubber_model_loss_13, Style_dubber_model_loss_15, Style_dubber_model_loss_15_Emo, Style_dubber_model_loss_15_Emo_AB0
from style_dubber import Style_dubber_model_loss_15_Emo
from dataset import Dataset_denoise2_Setting1_Run, Dataset_GRIDdataset
from joblib import Parallel, delayed
from scipy.io.wavfile import write
from tqdm import tqdm
from pymcd.mcd import Calculate_MCD
import matplotlib.pyplot as plt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def save_plot(tensor, savepath):
plt.style.use('default')
fig, ax = plt.subplots(figsize=(12, 3))
im = ax.imshow(tensor, aspect="auto", origin="lower", interpolation='none')
plt.colorbar(im, ax=ax)
plt.tight_layout()
fig.canvas.draw()
plt.savefig(savepath)
plt.close()
return
def Test_more_MCD_with_GT(i, audio_path, mcd_toolbox, mcd_toolbox_dtw, mcd_toolbox_dtw_sl):
#
name_i = i.split("/")[-1].split("_pred_")[-1].split(".wav")[0]
base_name = name_i.split("-")[0]
# Target
target_wav = os.path.join(audio_path, base_name, "{}.wav".format(name_i))
# # Predict_wav
Predict_wav = i
plain_value = mcd_toolbox.calculate_mcd(Predict_wav, target_wav)
dtw_value = mcd_toolbox_dtw.calculate_mcd(Predict_wav, target_wav)
dtw_value_sl = mcd_toolbox_dtw_sl.calculate_mcd(Predict_wav, target_wav)
return plain_value, dtw_value, dtw_value_sl
def Test_more_MCD_with_GT_V2C(i, audio_path, mcd_toolbox, mcd_toolbox_dtw, mcd_toolbox_dtw_sl):
#
name_i = i.split("/")[-1].split("_pred_")[-1].split(".wav")[0]
base_name = name_i.split("_00")[0]
# Target
target_wav = os.path.join(audio_path, base_name, "{}.wav".format(name_i))
# # Predict_wav
Predict_wav = i
plain_value = mcd_toolbox.calculate_mcd(Predict_wav, target_wav)
dtw_value = mcd_toolbox_dtw.calculate_mcd(Predict_wav, target_wav)
dtw_value_sl = mcd_toolbox_dtw_sl.calculate_mcd(Predict_wav, target_wav)
return plain_value, dtw_value, dtw_value_sl
def save_wav(sampling_rate, samples_path,
wav_predictions_batch, tags_batch):
for i in range(len(wav_predictions_batch)):
generated_path = os.path.join(samples_path)
os.makedirs(generated_path, exist_ok=True)
pred_fpath = os.path.join(generated_path, "wav_pred_{}.wav".format(tags_batch[i]))
write(pred_fpath, sampling_rate, wav_predictions_batch[i])
def synth_multi_samples_predonly(ids, Post_Mel, mel_len_preout, vocoder, model_config, preprocess_config):
wav_reconstructions = []
wav_predictions = []
for i in range(len(Post_Mel)):
if vocoder is not None:
from utils.model import vocoder_infer
wav_prediction = vocoder_infer(
Post_Mel[i, :(mel_len_preout[i].item())].detach().transpose(0, 1).unsqueeze(0),
vocoder,
model_config,
preprocess_config,
)[0]
else:
wav_reconstruction = wav_prediction = None
wav_predictions.append(wav_prediction)
return wav_predictions, ids
# need V2C when train
def evaluate_Denoise2_ID20Emo_Setting1(model, fusion_model, step, configs, val_log_path, logger=None, vocoder=None):
preprocess_config, model_config, train_config = configs
val_set = "val.txt"
dataset = Dataset_denoise2_Setting1_Run(
val_set, preprocess_config, train_config, sort=False, drop_last=False, inference_mode=False)
print("Watch which Dataset:", preprocess_config["dataset"], "Dataset_denoise2 len(test): ", len(dataset))
batch_size = train_config["optimizer"]["batch_size"]
loader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
collate_fn=dataset.collate_fn,
)
Total_Loss_Val = []
Mel_Loss1_Val = []
Mel_Loss_Post_Val = []
duration_loss_Val = []
PhnCls_Loss_Val = []
SpkCls_Loss_Val = []
Phone_acc_Val = []
Dialoss_Val = []
EMOClass_Val = []
wav_reconstructions_batch = []
wav_predictions_batch = []
tags_batch =[]
speakers_batch = []
emotions_batch = []
cofs_batch = []
# Get loss function
# Loss = Style_dubber_model_loss_15_Emo(preprocess_config, model_config).to(device)
# Evaluation
# loss_sums = [0 for _ in range(9)]
for batchs in loader:
for batch in batchs:
with torch.no_grad():
id_basename, text, src_len, max_src_len, speakers, ref_mels, ref_mel_lens, mel_target, mel_lens, max_mel_len, pitches, energies, durations, ref_linguistics, face_lens, MaxfaceL, lip_embedding, spk_embedding, face_embedding, emos_embedding, emotion_id = model.parse_batch(batch)
feature, src_masks, speaker_predicts, emotion_id_embedding, text_encoder = fusion_model(text, face_embedding, src_len, max_src_len, ref_mels, ref_mel_lens, face_lens, MaxfaceL)
Ture_postnet_mel_predictions, mel_lens_pred = model(feature, text_encoder, src_masks, ref_mels, ref_mel_lens, face_lens, MaxfaceL, lip_embedding, spk_embedding, mel_lens=mel_lens, max_mel_len=max_mel_len)
# Total_Loss_Val.append(item_losses[0])
# Mel_Loss1_Val.append(item_losses[1])
# Mel_Loss_Post_Val.append(item_losses[2])
# duration_loss_Val.append(item_losses[5])
# SpkCls_Loss_Val.append(item_losses[8])
# Dialoss_Val.append(item_losses[9])
# EMOClass_Val.append(item_losses[10])
wav_predictions, tags= synth_multi_samples_predonly(
id_basename, Ture_postnet_mel_predictions, mel_lens_pred,
vocoder,
model_config,
preprocess_config,
)
wav_predictions_batch.extend(wav_predictions)
tags_batch.extend(tags)
AV_attn_path = os.path.join(val_log_path, "AV_attn_image_Step{}".format(step))
os.makedirs(AV_attn_path, exist_ok=True)
print("len(dataset):", len(dataset))
val_samples_path = os.path.join(train_config["path"]["file"], train_config["path"]["result_path"])
sampling_rate = preprocess_config["preprocessing"]["audio"]["sampling_rate"]
save_wav(sampling_rate, val_samples_path, wav_predictions_batch, tags_batch)
print("============MCD=================")
audio_path = "/data/conggaoxiang/dataset/wav_22050_chenqi_clean_Denoise_version2_all" # change you path; We use the processed GT audio (e.g., Abs) as the target, which is used to convert mel-spectrograms.
generated_path = os.path.join(val_samples_path, "*")
mcd_toolbox = Calculate_MCD(MCD_mode="plain")
mcd_toolbox_dtw = Calculate_MCD(MCD_mode="dtw")
mcd_toolbox_dtw_sl = Calculate_MCD(MCD_mode="dtw_sl")
all = glob.glob(generated_path)
print("test all:", len(all))
results = Parallel(n_jobs=40, verbose=1)(
delayed(Test_more_MCD_with_GT_V2C)(i, audio_path, mcd_toolbox, mcd_toolbox_dtw, mcd_toolbox_dtw_sl) for i in all
)
avg_mcd_plain = sum(result[0] for result in results)/len(all)
avg_mcd_dtw = sum(result[1] for result in results)/len(all)
dtw_value_sl = sum(result[2] for result in results)/len(all)
log(logger, step, MCD_Value=[avg_mcd_plain, avg_mcd_dtw, dtw_value_sl])
return [avg_mcd_plain, avg_mcd_dtw, dtw_value_sl]
# need GRID when train
def evaluate_GRID(model, fusion_model, step, configs, val_log_path, logger=None, vocoder=None):
preprocess_config, model_config, train_config = configs
# Get dataset
val_set = "val.txt"
dataset = Dataset_GRIDdataset(
val_set, preprocess_config, train_config, sort=False, drop_last=False, inference_mode=False
)
print("Watch which Dataset:", preprocess_config["dataset"], "Dataset_denoise2 len(test): ", len(dataset))
batch_size = train_config["optimizer"]["batch_size"]
loader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
collate_fn=dataset.collate_fn,
)
Total_Loss_Val = []
Mel_Loss1_Val = []
Mel_Loss_Post_Val = []
duration_loss_Val = []
PhnCls_Loss_Val = []
SpkCls_Loss_Val = []
Phone_acc_Val = []
Dialoss_Val = []
EMOClass_Val = []
#
wav_reconstructions_batch = []
wav_predictions_batch = []
tags_batch =[]
speakers_batch = []
emotions_batch = []
cofs_batch = []
# Get loss function
# Loss = Style_dubber_model_loss_15_Emo(preprocess_config, model_config).to(device)
# Evaluation
# loss_sums = [0 for _ in range(9)]
for batchs in loader:
for batch in batchs:
# batch = to_device(batch, device)
with torch.no_grad():
id_basename, text, src_len, max_src_len, speakers, ref_mels, ref_mel_lens, mel_target, mel_lens, max_mel_len, pitches, energies, durations, ref_linguistics, face_lens, MaxfaceL, lip_embedding, spk_embedding, face_embedding = model.parse_batch_GRID(batch)
feature, src_masks, speaker_predicts, emotion_id_embedding, text_encoder = fusion_model(text, face_embedding, src_len, max_src_len, ref_mels, ref_mel_lens, face_lens, MaxfaceL)
Ture_postnet_mel_predictions, mel_lens_pred = model(feature, text_encoder, src_masks, ref_mels, ref_mel_lens, face_lens, MaxfaceL, lip_embedding, spk_embedding, mel_lens=mel_lens, max_mel_len=max_mel_len)
# Total_Loss_Val.append(item_losses[0])
# Mel_Loss1_Val.append(item_losses[1])
# Mel_Loss_Post_Val.append(item_losses[2])
# duration_loss_Val.append(item_losses[5])
# SpkCls_Loss_Val.append(item_losses[8])
# Dialoss_Val.append(item_losses[9])
# EMOClass_Val.append(item_losses[10])
wav_predictions, tags= synth_multi_samples_predonly(
id_basename, Ture_postnet_mel_predictions, mel_lens_pred,
vocoder,
model_config,
preprocess_config,
)
wav_predictions_batch.extend(wav_predictions)
tags_batch.extend(tags)
AV_attn_path = os.path.join(val_log_path, "AV_attn_image_Step{}".format(step))
os.makedirs(AV_attn_path, exist_ok=True)
print("len(dataset):", len(dataset))
val_samples_path = os.path.join(train_config["path"]["file"], train_config["path"]["result_path"], 'Test_when_train')
sampling_rate = preprocess_config["preprocessing"]["audio"]["sampling_rate"]
save_wav(sampling_rate, val_samples_path, wav_predictions_batch, tags_batch)
print("============MCD=================")
audio_path = "/data/conggaoxiang/GRID/GRID_dataset/Grid_Wav_22050_Abs" # change you path; We use the processed GT audio (e.g., Abs) as the target, which is used to convert mel-spectrograms.
print("GT_wav:", audio_path)
generated_path = os.path.join(val_samples_path, "*")
mcd_toolbox = Calculate_MCD(MCD_mode="plain")
mcd_toolbox_dtw = Calculate_MCD(MCD_mode="dtw")
mcd_toolbox_dtw_sl = Calculate_MCD(MCD_mode="dtw_sl")
all = glob.glob(generated_path)
print("test all:", len(all))
results = Parallel(n_jobs=40, verbose=1)(
delayed(Test_more_MCD_with_GT)(i, audio_path, mcd_toolbox, mcd_toolbox_dtw, mcd_toolbox_dtw_sl) for i in all
)
avg_mcd_plain = sum(result[0] for result in results)/len(all)
avg_mcd_dtw = sum(result[1] for result in results)/len(all)
dtw_value_sl = sum(result[2] for result in results)/len(all)
log(logger, step, MCD_Value=[avg_mcd_plain, avg_mcd_dtw, dtw_value_sl])
return [avg_mcd_plain, avg_mcd_dtw, dtw_value_sl]