-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
executable file
·215 lines (161 loc) · 7.24 KB
/
inference.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
import matplotlib
matplotlib.use("Agg")
import matplotlib.pylab as plt
import os
import librosa
import numpy as np
import torch
from torch.utils.data import DataLoader
from reader import TextMelIDLoader, TextMelIDCollate, id2ph, id2sp
from hparams import create_hparams
from model import Parrot, lcm
from train import load_model
import scipy.io.wavfile
import torch.nn.functional as F
########### Configuration ###########
hparams = create_hparams()
#generation list
hlist = '/home/jxzhang/Documents/DataSets/VCTK/list/hold_english.list'
tlist = '/home/hk/voice_conversion/nonparaSeq2seqVC_code/pre-train/reader/vctk_ref_f2f_299to339.txt'
# use seen (tlist) or unseen list (hlist)
test_list = tlist
checkpoint_path='outdir_mean_std_trimmed/checkpoint_116000'
# TTS or VC task?
input_text= True
# number of utterances for generation
NUM=10
ISMEL=(not hparams.predict_spectrogram)
#####################################
def plot_data(data, fn, figsize=(12, 4)):
fig, axes = plt.subplots(1, len(data), figsize=figsize)
for i in range(len(data)):
if len(data) == 1:
ax = axes
else:
ax = axes[i]
g = ax.imshow(data[i], aspect='auto', origin='bottom',
interpolation='none')
plt.colorbar(g, ax=ax)
plt.savefig(fn)
model = load_model(hparams)
model.load_state_dict(torch.load(checkpoint_path)['state_dict'])
_ = model.eval()
test_set = TextMelIDLoader(test_list,hparams.mel_mean, hparams.mel_std, shuffle=True)
sample_list = test_set.file_path_list
collate_fn = TextMelIDCollate(lcm(hparams.n_frames_per_step_encoder,
hparams.n_frames_per_step_decoder))
test_loader = DataLoader(test_set, num_workers=1, shuffle=False,
sampler=None,
batch_size=1, pin_memory=False,
drop_last=True, collate_fn=collate_fn)
task = 'tts' if input_text else 'vc'
path_save = os.path.join(checkpoint_path.replace('checkpoint', 'test'), task)
path_save += '_seen' if test_list == tlist else '_unseen'
if not os.path.exists(path_save):
os.makedirs(path_save)
print(path_save)
def recover_wav(mel, wav_path, ismel=False,
n_fft=2048, win_length=800,hop_length=200):
if ismel:
mean = np.load(hparams.mel_mean)
std = np.load(hparams.mel_std)
else:
mean, std = np.load(hparams.mel_mean_std.replace('mel','spec'))
mean = mean[:,None]
std = std[:,None]
mel = 1.2 * mel * std + mean
#np.save(os.path.join("/home/hk/voice_conversion/nonparaSeq2seqVC_code/pre-train/outdir_mean_std_trimmed/test_116000/vc_seen", 'Mel.npy'), mel)
#import pdb
#pdb.set_trace()
mel = np.exp(mel)
if ismel:
filters = librosa.filters.mel(sr=22050, n_fft=2048, n_mels=80)
inv_filters = np.linalg.pinv(filters)
spec = np.dot(inv_filters, mel)
else:
spec = mel
def _griffin_lim(stftm_matrix, shape, max_iter=50):
y = np.random.random(shape)
for i in range(max_iter):
stft_matrix = librosa.core.stft(y, n_fft=n_fft, win_length=win_length, hop_length=hop_length)
stft_matrix = stftm_matrix * stft_matrix / np.abs(stft_matrix)
y = librosa.core.istft(stft_matrix, win_length=win_length, hop_length=hop_length)
return y
shape = spec.shape[1] * hop_length - hop_length + 1
y = _griffin_lim(spec, shape)
scipy.io.wavfile.write(wav_path, 22050, y)
return y
text_input, mel, speaker_id = test_set[0]
reference_mel = mel.cuda().unsqueeze(0)
ref_sp = id2sp[speaker_id.item()]
def levenshteinDistance(s1, s2):
if len(s1) > len(s2):
s1, s2 = s2, s1
distances = list(range(len(s1) + 1))
for i2, c2 in enumerate(s2):
distances_ = [i2+1]
for i1, c1 in enumerate(s1):
if c1 == c2:
distances_.append(distances[i1])
else:
distances_.append(1 + min((distances[i1], distances[i1 + 1], distances_[-1])))
distances = distances_
return distances[-1]
with torch.no_grad():
errs = 0
totalphs = 0
for i, batch in enumerate(test_loader):
if i == NUM:
break
#import pdb
#pdb.set_trace()
sample_id = sample_list[i].split('/')[-1][1:8]
print(('%d index %s, decoding ...'%(i,sample_id)))
x, y = model.parse_batch(batch)
text_input_padded, mel_padded, text_lengths, mel_lengths = x
mel_padded = mel_padded.data.cpu().numpy()[0]
predicted_mel, post_output, predicted_stop, alignments, \
text_hidden, audio_seq2seq_hidden, audio_seq2seq_phids, audio_seq2seq_alignments, \
speaker_id = model.inference(x, input_text, reference_mel, hparams.beam_width)
post_output = post_output.data.cpu().numpy()[0]
predicted_mel = predicted_mel.data.cpu().numpy()[0]
alignments = alignments.data.cpu().numpy()[0].T
audio_seq2seq_alignments = audio_seq2seq_alignments.data.cpu().numpy()[0].T
text_hidden = text_hidden.data.cpu().numpy()[0].T #-> [hidden_dim, max_text_len]
audio_seq2seq_hidden = audio_seq2seq_hidden.data.cpu().numpy()[0].T
audio_seq2seq_phids = audio_seq2seq_phids.data.cpu().numpy()[0] # [T + 1]
speaker_id = speaker_id.data.cpu().numpy()[0] # scalar
predicted_stop = F.sigmoid(predicted_stop[0]).data.cpu().numpy()
predicted_stop = np.argmax(predicted_stop)
#post_output = post_output[:,:predicted_stop]
#alignments = alignments[:,:(predicted_stop)]
# plot_data(plot_spectrogram_to_numpy(mel.data.cpu().numpy()),
# os.path.join(path_save, 'Hid_%s_ref_%s_%s.pdf'%(sample_id, ref_sp, task)))
task = 'TTS' if input_text else 'VC'
#import pdb
#pdb.set_trace()
recover_wav(post_output,
os.path.join(path_save, 'Wav_%s_ref_%s_%s.wav'%(sample_id, ref_sp, task)),
ismel=ISMEL)
post_output_path = os.path.join(path_save, 'Mel_%s_ref_%s_%s.npy'%(sample_id, ref_sp, task))
np.save(post_output_path, post_output)
plot_data([mel], os.path.join(path_save, 'mel_ref_%s.pdf'% ref_sp))
plot_data([mel_padded], os.path.join(path_save, 'mel_src_%s.pdf'% sample_id))
plot_data([predicted_mel], os.path.join(path_save, 'melconverted_src_%s_ref_%s_%s.pdf'% (sample_id, ref_sp,task)))
#import pdb
#pdb.set_trace()
plot_data([alignments, audio_seq2seq_alignments],
os.path.join(path_save, 'Ali_%s_ref_%s_%s.pdf'%(sample_id, ref_sp, task)))
plot_data([np.hstack([text_hidden, audio_seq2seq_hidden])],
os.path.join(path_save, 'Hid_%s_ref_%s_%s.pdf'%(sample_id, ref_sp, task)))
audio_seq2seq_phids = [id2ph[id] for id in audio_seq2seq_phids[:-1]]
target_text = y[0].data.cpu().numpy()[0]
target_text = [id2ph[id] for id in target_text[:]]
print('Sounds like %s, Decoded text is '%(id2sp[speaker_id]))
print(audio_seq2seq_phids)
print(target_text)
err = levenshteinDistance(audio_seq2seq_phids, target_text)
print(err, len(target_text))
errs += err
totalphs += len(target_text)
print(float(errs)/float(totalphs))