模型描述
本 Notebook 演示了 HiFi-GAN 模型在 PyTorch 中的实现,该模型在论文中有所描述:HiFi-GAN:用于高效高保真语音合成的生成对抗网络。HiFi-GAN 模型实现了频谱图反演模型,可从梅尔频谱图合成语音波形。它遵循生成对抗网络 (GAN) 范式,由一个生成器和一个判别器组成。训练完成后,生成器用于合成,而判别器被丢弃。
我们的实现基于论文作者发布的版本。我们修改了原始超参数并提供了一种替代训练方案,该方案支持更大批量训练并加快收敛。HiFi-GAN 在公开的 LJ Speech 数据集上进行训练。样本展示了使用我们公开的 FastPitch 和 HiFi-GAN 检查点合成的语音。
模型架构
示例
在下面的示例中
- 从 torch.hub 加载预训练的 FastPitch 和 HiFiGAN 模型
- 给定输入文本 (“Say this smoothly to prove you are not a robot.”) 的张量表示,FastPitch 生成梅尔频谱图
- HiFiGAN 根据梅尔频谱图生成声音
- 输出声音保存到 ‘audio.wav’ 文件中
要运行此示例,你需要安装一些额外的 Python 包。这些包用于文本和音频的预处理,以及显示和输入/输出处理。最后,为了获得更好的 FastPitch 模型性能,我们下载了 CMU 发音词典。
pip install numpy scipy librosa unidecode inflect librosa matplotlib==3.6.3
apt-get update
apt-get install -y libsndfile1 wget
wget https://raw.githubusercontent.com/NVIDIA/NeMo/263a30be71e859cee330e5925332009da3e5efbc/scripts/tts_dataset_files/heteronyms-052722 -qO heteronyms
wget https://raw.githubusercontent.com/NVIDIA/NeMo/263a30be71e859cee330e5925332009da3e5efbc/scripts/tts_dataset_files/cmudict-0.7b_nv22.08 -qO cmudict-0.7b
import torch
import matplotlib.pyplot as plt
from IPython.display import Audio
import warnings
warnings.filterwarnings('ignore')
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
print(f'Using {device} for inference')
下载并设置 FastPitch 生成器模型。
fastpitch, generator_train_setup = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_fastpitch')
下载并设置声码器和去噪器模型。
hifigan, vocoder_train_setup, denoiser = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_hifigan')
验证生成器和声码器模型在输入参数上一致。
CHECKPOINT_SPECIFIC_ARGS = [
'sampling_rate', 'hop_length', 'win_length', 'p_arpabet', 'text_cleaners',
'symbol_set', 'max_wav_value', 'prepend_space_to_text',
'append_space_to_text']
for k in CHECKPOINT_SPECIFIC_ARGS:
v1 = generator_train_setup.get(k, None)
v2 = vocoder_train_setup.get(k, None)
assert v1 is None or v2 is None or v1 == v2, \
f'{k} mismatch in spectrogram generator and vocoder'
将所有模型放到可用设备上。
fastpitch.to(device)
hifigan.to(device)
denoiser.to(device)
加载文本处理器。
tp = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_textprocessing_utils', cmudict_path="cmudict-0.7b", heteronyms_path="heteronyms")
设置要合成的文本,准备输入并设置额外的生成参数。
text = "Say this smoothly, to prove you are not a robot."
batches = tp.prepare_input_sequence([text], batch_size=1)
gen_kw = {'pace': 1.0,
'speaker': 0,
'pitch_tgt': None,
'pitch_transform': None}
denoising_strength = 0.005
for batch in batches:
with torch.no_grad():
mel, mel_lens, *_ = fastpitch(batch['text'].to(device), **gen_kw)
audios = hifigan(mel).float()
audios = denoiser(audios.squeeze(1), denoising_strength)
audios = audios.squeeze(1) * vocoder_train_setup['max_wav_value']
绘制中间频谱图。
plt.figure(figsize=(10,12))
res_mel = mel[0].detach().cpu().numpy()
plt.imshow(res_mel, origin='lower')
plt.xlabel('time')
plt.ylabel('frequency')
_=plt.title('Spectrogram')
合成音频。
audio_numpy = audios[0].cpu().numpy()
Audio(audio_numpy, rate=22050)
将音频写入 wav 文件。
from scipy.io.wavfile import write
write("audio.wav", vocoder_train_setup['sampling_rate'], audio_numpy)
详细信息
要获取模型输入和输出、训练方案、推理和性能的详细信息,请访问:github 和/或 NGC