Spaces:
Sleeping
Sleeping
File size: 12,008 Bytes
22257c4 075b64e 22257c4 |
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 |
import os
import numpy as np
import pickle
import torch
from math import ceil
from src.autovc.retrain_version.model_vc_37_1 import Generator
from pydub import AudioSegment
import pynormalize.pynormalize
from scipy.io import wavfile as wav
from scipy.signal import stft
def match_target_amplitude(sound, target_dBFS):
change_in_dBFS = target_dBFS - sound.dBFS
return sound.apply_gain(change_in_dBFS)
class AutoVC_mel_Convertor():
def __init__(self, src_dir, proportion=(0., 1.), seed=0):
self.src_dir = src_dir
if(not os.path.exists(os.path.join(src_dir, 'filename_index.txt'))):
self.filenames = []
else:
with open(os.path.join(src_dir, 'filename_index.txt'), 'r') as f:
lines = f.readlines()
self.filenames = [(int(line.split(' ')[0]), line.split(' ')[1][:-1]) for line in lines]
np.random.seed(seed)
rand_perm = np.random.permutation(len(self.filenames))
proportion_idx = (int(proportion[0] * len(rand_perm)), int(proportion[1] * len(rand_perm)))
selected_index = rand_perm[proportion_idx[0] : proportion_idx[1]]
self.selected_filenames = [self.filenames[i] for i in selected_index]
print('{} out of {} are in this portion'.format(len(self.selected_filenames), len(self.filenames)))
def __convert_single_only_au_AutoVC_format_to_dataset__(self, filename, build_train_dataset=True):
"""
Convert a single file (only audio in AutoVC embedding format) to numpy arrays
:param filename:
:param is_map_to_std_face:
:return:
"""
global_clip_index, video_name = filename
# audio_file = os.path.join(self.src_dir, 'raw_wav', '{}.wav'.
# format(video_name[:-4]))
audio_file = os.path.join(self.src_dir, 'raw_wav', '{:05d}_{}_audio.wav'.
format(global_clip_index, video_name[:-4]))
if(not build_train_dataset):
import shutil
audio_file = os.path.join(self.src_dir, 'raw_wav', '{:05d}_{}_audio.wav'.
format(global_clip_index, video_name[:-4]))
shutil.copy(os.path.join(self.src_dir, 'test_wav_files', video_name), audio_file)
sound = AudioSegment.from_file(audio_file, "wav")
normalized_sound = match_target_amplitude(sound, -20.0)
normalized_sound.export(audio_file, format='wav')
from src.autovc.retrain_version.vocoder_spec.extract_f0_func import extract_f0_func_audiofile
S, f0_norm = extract_f0_func_audiofile(audio_file, 'M')
from src.autovc.utils import quantize_f0_interp
f0_onehot = quantize_f0_interp(f0_norm)
from thirdparty.resemblyer_util.speaker_emb import get_spk_emb
mean_emb, _ = get_spk_emb(audio_file)
return S, mean_emb, f0_onehot
def convert_wav_to_autovc_input(self, build_train_dataset=True, autovc_model_path=r'E:\Dataset\VCTK\stargan_vc\train_85_withpre1125000_local\360000-G.ckpt'):
def pad_seq(x, base=32):
len_out = int(base * ceil(float(x.shape[0]) / base))
len_pad = len_out - x.shape[0]
assert len_pad >= 0
return np.pad(x, ((0, len_pad), (0, 0)), 'constant'), len_pad
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
G = Generator(16, 256, 512, 16).eval().to(device)
g_checkpoint = torch.load(autovc_model_path, map_location=device)
G.load_state_dict(g_checkpoint['model'])
emb = np.loadtxt('autovc/retrain_version/obama_emb.txt')
emb_trg = torch.from_numpy(emb[np.newaxis, :].astype('float32')).to(device)
aus = []
for i, file in enumerate(self.selected_filenames):
print(i, file)
x_real_src, emb, f0_org_src = self.__convert_single_only_au_AutoVC_format_to_dataset__(filename=file, build_train_dataset=build_train_dataset)
'''# normal length #'''
# with torch.no_grad():
# x_identic, x_identic_psnt, code_real = G(x_real, emb_org, f0_org, emb_trg, f0_org)
# g_loss_id_psnt = F.mse_loss(x_real, x_identic_psnt, reduction='sum')
# print('loss:', g_loss_id_psnt / x_identic_psnt.shape[1] * 128)
''' too long split length '''
l = x_real_src.shape[0]
x_identic_psnt = []
step = 4096
for i in range(0, l, step):
x_real = x_real_src[i:i+step]
f0_org = f0_org_src[i:i+step]
x_real, len_pad = pad_seq(x_real.astype('float32'))
f0_org, _ = pad_seq(f0_org.astype('float32'))
x_real = torch.from_numpy(x_real[np.newaxis, :].astype('float32')).to(device)
emb_org = torch.from_numpy(emb[np.newaxis, :].astype('float32')).to(device)
# emb_trg = torch.from_numpy(emb[np.newaxis, :].astype('float32')).to(device)
f0_org = torch.from_numpy(f0_org[np.newaxis, :].astype('float32')).to(device)
print('source shape:', x_real.shape, emb_org.shape, emb_trg.shape, f0_org.shape)
with torch.no_grad():
x_identic, x_identic_psnt_i, code_real = G(x_real, emb_org, f0_org, emb_trg, f0_org)
x_identic_psnt.append(x_identic_psnt_i)
x_identic_psnt = torch.cat(x_identic_psnt, dim=1)
print('converted shape:', x_identic_psnt.shape, code_real.shape)
if len_pad == 0:
uttr_trg = x_identic_psnt[0, :, :].cpu().numpy()
else:
uttr_trg = x_identic_psnt[0, :-len_pad, :].cpu().numpy()
# ''' plot source and converted mel-spec figures '''
# import matplotlib.pyplot as plt
# plt.subplot(1, 2, 1)
# plt.imshow(x_real_src[0:200, :])
# plt.subplot(1, 2, 2)
# plt.imshow(uttr_trg[0:200, :])
# plt.show()
#
# exit(0)
file = (file[0], file[1], emb)
aus.append((uttr_trg, file))
return aus
def convert_single_wav_to_input(self, audio_filename):
aus = []
audio_file = os.path.join(self.src_dir, 'demo_wav', audio_filename)
# Default param
TARGET_AUDIO_DBFS = -20.0
WAV_STEP = int(0.2 * 16000) # 0.2s = 5 frames
STFT_WINDOW_SIZE = {'25': 320, '29.97': 356}
STFT_WINDOW_STEP = {'25': 4, '29.97': 3}
FPS = 25
# Step 1 : Normalize the volume
target_dbfs = TARGET_AUDIO_DBFS
pynormalize.process_files(
Files=[audio_file],
target_dbfs=target_dbfs,
directory=os.path.join(self.src_dir, 'raw_wav')
)
# Step 2 : load wav file
sample_rate, samples = wav.read(audio_file)
assert (sample_rate == 16000)
if (len(samples.shape) > 1):
samples = samples[:, 0] # pick mono
# Step 3 : STFT,
# 1 frame = 1/25 * 16k = 640 samples => windowsize=320, overlap=160
# 1 frame = 1/29.97 * 16k = 533.86 samples => windowsize=356, overlap=178, (mis-align = 4.2sample / 1s)
f, t, Zxx = stft(samples, fs=sample_rate, nperseg=STFT_WINDOW_SIZE[str(FPS)])
# stft_abs = np.abs(Zxx)
stft_abs = np.log(np.abs(Zxx) ** 2 + 1e-10)
stft_abs_max = np.max(stft_abs)
stft_abs /= stft_abs_max
# Step 4 : align AV (drop last 2 frames of V)
fl_length = stft_abs.shape[1] // STFT_WINDOW_STEP[str(FPS)]
audio_stft_length = (fl_length - 2) * STFT_WINDOW_STEP[str(FPS)]
stft_signal = Zxx[:, 0:audio_stft_length]
stft_abs = stft_abs[:, 0:audio_stft_length]
audio_wav_length = int((fl_length - 2) * sample_rate / FPS)
wav_signal = samples[0:audio_wav_length]
# # Step 6 : Save audio
# info_audio = (0, stft_signal, fl_length - 2, audio_stft_length, audio_wav_length)
# au_data = (stft_abs, wav_signal, info_audio)
aus.append((stft_abs.T, None, (0, audio_filename, 0)))
return aus
def convert_single_wav_to_autovc_input(self, audio_filename, autovc_model_path):
def pad_seq(x, base=32):
len_out = int(base * ceil(float(x.shape[0]) / base))
len_pad = len_out - x.shape[0]
assert len_pad >= 0
return np.pad(x, ((0, len_pad), (0, 0)), 'constant'), len_pad
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
G = Generator(16, 256, 512, 16).eval().to(device)
g_checkpoint = torch.load(autovc_model_path, map_location=device)
G.load_state_dict(g_checkpoint['model'])
emb = np.loadtxt('MakeItTalk/src/autovc/retrain_version/obama_emb.txt')
emb_trg = torch.from_numpy(emb[np.newaxis, :].astype('float32')).to(device)
aus = []
audio_file = audio_filename
sound = AudioSegment.from_file(audio_file, "wav")
normalized_sound = match_target_amplitude(sound, -20.0)
normalized_sound.export(audio_file, format='wav')
from src.autovc.retrain_version.vocoder_spec.extract_f0_func import extract_f0_func_audiofile
x_real_src, f0_norm = extract_f0_func_audiofile(audio_file, 'F')
from src.autovc.utils import quantize_f0_interp
f0_org_src = quantize_f0_interp(f0_norm)
from thirdparty.resemblyer_util.speaker_emb import get_spk_emb
emb, _ = get_spk_emb(audio_file)
''' normal length version '''
# x_real, len_pad = pad_seq(x_real_src.astype('float32'))
# f0_org, _ = pad_seq(f0_org_src.astype('float32'))
# x_real = torch.from_numpy(x_real[np.newaxis, :].astype('float32')).to(device)
# emb_org = torch.from_numpy(emb[np.newaxis, :].astype('float32')).to(device)
# f0_org = torch.from_numpy(f0_org[np.newaxis, :].astype('float32')).to(device)
# print('source shape:', x_real.shape, emb_org.shape, emb_trg.shape, f0_org.shape)
#
# with torch.no_grad():
# x_identic, x_identic_psnt, code_real = G(x_real, emb_org, f0_org, emb_trg, f0_org)
# print('converted shape:', x_identic_psnt.shape, code_real.shape)
''' long split version '''
l = x_real_src.shape[0]
x_identic_psnt = []
step = 4096
for i in range(0, l, step):
x_real = x_real_src[i:i + step]
f0_org = f0_org_src[i:i + step]
x_real, len_pad = pad_seq(x_real.astype('float32'))
f0_org, _ = pad_seq(f0_org.astype('float32'))
x_real = torch.from_numpy(x_real[np.newaxis, :].astype('float32')).to(device)
emb_org = torch.from_numpy(emb[np.newaxis, :].astype('float32')).to(device)
# emb_trg = torch.from_numpy(emb[np.newaxis, :].astype('float32')).to(device)
f0_org = torch.from_numpy(f0_org[np.newaxis, :].astype('float32')).to(device)
print('source shape:', x_real.shape, emb_org.shape, emb_trg.shape, f0_org.shape)
with torch.no_grad():
x_identic, x_identic_psnt_i, code_real = G(x_real, emb_org, f0_org, emb_trg, f0_org)
x_identic_psnt.append(x_identic_psnt_i)
x_identic_psnt = torch.cat(x_identic_psnt, dim=1)
print('converted shape:', x_identic_psnt.shape, code_real.shape)
if len_pad == 0:
uttr_trg = x_identic_psnt[0, :, :].cpu().numpy()
else:
uttr_trg = x_identic_psnt[0, :-len_pad, :].cpu().numpy()
aus.append((uttr_trg, (0, audio_filename, emb)))
return aus
if __name__ == '__main__':
c = AutoVC_mel_Convertor(r'E:\Dataset\TalkingToon\Obama_for_train', proportion=(0.0, 1.0))
aus = c.convert_wav_to_autovc_input()
with open(os.path.join(r'E:\Dataset\TalkingToon\Obama_for_train', 'dump', 'autovc_retrain_mel_au.pickle'), 'wb') as fp:
pickle.dump(aus, fp) |