【PaddleSpeech】语音合成-男声

环境安装

系统:Ubuntu >= 16.04

源码下载

  • 克隆 PaddleSpeech 仓库
bash 复制代码
# github下载
git clone https://github.com/PaddlePaddle/PaddleSpeech.git
# 也可以从gitee下载
git clone https://gitee.com/paddlepaddle/PaddleSpeech.git

# 进入PaddleSpeech目录
cd PaddleSpeech

安装 Conda

bash 复制代码
使用apt安装 build-essential
sudo apt install build-essential


# 下载 miniconda
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -P tools/
# 安装 miniconda
bash tools/Miniconda3-latest-Linux-x86_64.sh -b
# conda 初始化
$HOME/miniconda3/bin/conda init
# 激活 conda
bash
# 创建 Conda 虚拟环境
conda create -y -p tools/venv python=3.8
# 激活 Conda 虚拟环境:
conda activate tools/venv
# 安装 Conda 包
conda install -y -c conda-forge sox libsndfile swig bzip2 libflac bc

安装 PaddlePaddle

bash 复制代码
#CPU版本安装
python3 -m pip install paddlepaddle- -i https://mirror.baidu.com/pypi/simple

#GPU版本安装,注意:2.4.1 只是一个示例,请按照对paddlepaddle的最小依赖进行选择。
python3 -m pip install paddlepaddle-gpu==2.4.1 -i https://mirror.baidu.com/pypi/simple

用开发者模式安装 PaddleSpeech

bash 复制代码
pip install pytest-runner -i https://pypi.tuna.tsinghua.edu.cn/simple 

pip install -e .[develop] -i https://pypi.tuna.tsinghua.edu.cn/simple

下载预训练模型

bash 复制代码
#下载预训练模型:声学模型、声码器
!mkdir download

#中文男声学模型
!wget -P download https://paddlespeech.bj.bcebos.com/Parakeet/released_models/fastspeech2/fastspeech2_male_zh_ckpt_1.4.0.zip
!unzip -d download download/fastspeech2_male_zh_ckpt_1.4.0.zip

#声码器
!wget -P download https://paddlespeech.bj.bcebos.com/Parakeet/released_models/hifigan/hifigan_male_ckpt_1.4.0.zip
!unzip -d download download/hifigan_male_ckpt_1.4.0.zip

语音合成

脚本命名为:FastSpeech2-hifigan.py

python 复制代码
import argparse
import os
from pathlib import Path
import IPython.display as dp
import matplotlib.pyplot as plt
import numpy as np
import paddle
import soundfile as sf
import yaml
from paddlespeech.t2s.frontend.zh_frontend import Frontend
from paddlespeech.t2s.models.fastspeech2 import FastSpeech2
from paddlespeech.t2s.models.fastspeech2 import FastSpeech2Inference
from paddlespeech.t2s.models.hifigan import HiFiGANGenerator
from paddlespeech.t2s.models.hifigan import HiFiGANInference
from paddlespeech.t2s.modules.normalizer import ZScore
from yacs.config import CfgNode

# 配置预训练模型
fastspeech2_config = "download/fastspeech2_male_zh_ckpt_1.4.0/default.yaml"
fastspeech2_checkpoint = "download/fastspeech2_male_zh_ckpt_1.4.0/snapshot_iter_76000.pdz"
fastspeech2_stat = "download/fastspeech2_male_zh_ckpt_1.4.0/speech_stats.npy"
hifigan_config = "download/hifigan_male_ckpt_1.4.0/default.yaml"
hifigan_checkpoint = "download/hifigan_male_ckpt_1.4.0/snapshot_iter_630000.pdz"
hifigan_stat = "download/hifigan_male_ckpt_1.4.0/feats_stats.npy"
phones_dict = "download/fastspeech2_male_zh_ckpt_1.4.0/phone_id_map.txt"
# 读取 conf 配置文件并结构化
with open(fastspeech2_config) as f:
    fastspeech2_config = CfgNode(yaml.safe_load(f))
with open(hifigan_config) as f:
    hifigan_config = CfgNode(yaml.safe_load(f))
print("========Config========")
print(fastspeech2_config)
print("---------------------")
print(hifigan_config)

# 构造文本前端对象
# 传入 phones_dict 会把相应的 phones 转换成 phone_ids
frontend = Frontend(phone_vocab_path=phones_dict)
print("Frontend done!")

# 调用文本前端
# input = "我每天中午12:00起床"
# input = "我出生于2005/11/08,那天的最低气温达到-10°C"
input = "先生您好,欢迎使用百度飞桨框架进行深度学习!"
input_ids = frontend.get_input_ids(input, merge_sentences=True, print_info=True)
phone_ids = input_ids["phone_ids"][0]
print("phone_ids:%s"%phone_ids)

# 初始化声学模型
with open(phones_dict, "r") as f:
    phn_id = [line.strip().split() for line in f.readlines()]
vocab_size = len(phn_id)
print("vocab_size:", vocab_size)
odim = fastspeech2_config.n_mels
model = FastSpeech2(
    idim=vocab_size, odim=odim, **fastspeech2_config["model"])
# 加载预训练模型参数
model.set_state_dict(paddle.load(fastspeech2_checkpoint)["main_params"])
# 推理阶段不启用 batch norm 和 dropout
model.eval()
stat = np.load(fastspeech2_stat)
# 读取数据预处理阶段数据集的均值和标准差
mu, std = stat
mu, std = paddle.to_tensor(mu), paddle.to_tensor(std)
# 构造归一化的新模型
fastspeech2_normalizer = ZScore(mu, std)
fastspeech2_inference = FastSpeech2Inference(fastspeech2_normalizer, model)
fastspeech2_inference.eval()
print("FastSpeech2 done!")

# 调用声学模型
with paddle.no_grad():
    mel = fastspeech2_inference(phone_ids)
print("shepe of mel (n_frames x n_mels):")
print(mel.shape)
# 绘制声学模型输出的 mel 频谱
#fig, ax = plt.subplots(figsize=(16, 6))
#im = ax.imshow(mel.T, aspect='auto',origin='lower')
#plt.title('Mel Spectrogram')
#plt.xlabel('Time')
#plt.ylabel('Frequency')
#plt.tight_layout()

# 初始化声码器
vocoder = HiFiGANGenerator(**hifigan_config["generator_params"])
# 模型加载预训练参数
vocoder.set_state_dict(paddle.load(hifigan_checkpoint)["generator_params"])
vocoder.remove_weight_norm()
# 推理阶段不启用 batch norm 和 dropout
vocoder.eval()
# 读取数据预处理阶段数据集的均值和标准差
stat = np.load(hifigan_stat)
mu, std = stat
mu, std = paddle.to_tensor(mu), paddle.to_tensor(std)
hifigan_normalizer = ZScore(mu, std)
# 构建归一化的模型
hifigan_inference = HiFiGANInference(hifigan_normalizer, vocoder)
hifigan_inference.eval()
print("HiFiGan done!")

# 调用声码器
with paddle.no_grad():
    wav = hifigan_inference(mel)
print("shepe of wav (time x n_channels):%s"%wav.shape)

# 绘制声码器输出的波形图
wave_data = wav.numpy().T
time = np.arange(0, wave_data.shape[1]) * (1.0 / fastspeech2_config.fs)
fig, ax = plt.subplots(figsize=(16, 6))
plt.plot(time, wave_data[0])
plt.title('Waveform')
plt.xlabel('Time (seconds)')
plt.ylabel('Amplitude (normed)')
plt.tight_layout()

#保存音频
sf.write(
    "output/output-male-hifigan.wav",
    wav.numpy(),
    samplerate=fastspeech2_config.fs)

运行脚本

bash 复制代码
#运行脚本前,确保有output目录,没有就手动创建一下

python3 FastSpeech2-hifigan.py

#运行成功后在output/output-male-hifigan.wav目录可以找到生成的音频文件
  1. 环境安装参考官网:https://github.com/PaddlePaddle/PaddleSpeech/blob/develop/docs/source/install_cn.mdhttps://github.com/PaddlePaddle/PaddleSpeech/blob/develop/docs/source/install_cn.md

2. 飞桨PaddleSpeech语音技术课程 - 飞桨AI Studio星河社区-人工智能学习与实训社区 (baidu.com)

  1. 更多模型下载

Released Models --- paddle speech 2.1 documentationhttps://paddlespeech.readthedocs.io/en/latest/released_model.html

相关推荐
羑悻的小杀马特37 分钟前
谁主沉浮:人工智能对未来信息技术发展路径的影响研究
ai
我爱一条柴ya1 小时前
【AI大模型】神经网络反向传播:核心原理与完整实现
人工智能·深度学习·神经网络·ai·ai编程
y_y_liang1 小时前
图生生AI商品换背景,高效商拍!
大数据·人工智能·ai·ai作画
敖行客 Allthinker3 小时前
云原生安全观察:零信任架构与动态防御的下一代免疫体系
安全·ai·云原生·架构·kubernetes·ebpf
带刺的坐椅6 小时前
Java MCP 实战:构建跨进程与远程的工具服务
java·ai·solon·mcp
小付爱coding6 小时前
SpringAIAlibaba正式版发布!
ai
令狐少侠20119 小时前
ai之对接电信ds后端服务,通过nginx代理转发https为http,对外请求,保持到达第三方后请求头不变
nginx·ai·https
产品经理独孤虾17 小时前
人工智能大模型如何助力电商产品经理打造高效的商品工业属性画像
人工智能·机器学习·ai·大模型·产品经理·商品画像·商品工业属性
海豚调度1 天前
Linux 基金会报告解读:开源 AI 重塑经济格局,有人失业,有人涨薪!
大数据·人工智能·ai·开源
令狐少侠20111 天前
ai之RAG本地知识库--基于OCR和文本解析器的新一代RAG引擎:RAGFlow 认识和源码剖析
人工智能·ai