尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

如何用 TTS Model API 手动加载 XTTS v2 模型并做推理?

如何用 TTS Model API 手动加载 XTTS v2 模型并做推理? 如何用 TTS Model API 手动加载 XTTS v2 模型并做推理【免费下载链接】TTS - a deep learning toolkit for Text-to-Speech, battle-tested in research and production项目地址: https://gitcode.com/GitHub_Trending/tt/TTS当你想绕过高层的TTS()封装、直接用自己的模型文件加载 TTSa deep learning toolkit for Text-to-Speech中的 XTTS v2 并调用底层推理接口时需要走 Model API 这条路径自己下载模型文件手动传入config.json和 checkpoint 目录通过XttsConfigXtts.init_from_config构建模型再用get_conditioning_latents提取参考音频的说话人条件、用model.inference合成语音并保存为 wav 文件。本文的适用前提是你已经在本地准备好了 XTTS v2 的模型文件目录和一段参考音频环境满足 TTS 的安装要求Python 3.7 3.11支持 Ubuntu 18.10/19.10/20.10见 安装文档并且代码示例中的model.cuda()表明示例按 CUDA 环境编写。准备条件先安装 TTS。文档推荐推理场景用 pip 安装pip install TTS # from PyPI手动推理代码依赖torch和torchaudio示例中用torchaudio.save保存结果。此外XTTS 文档在 Manual Inference 一节明确说明如果希望load_checkpoint时传入use_deepspeedTrue并享受加速需要先安装 deepspeedpip install deepspeed0.10.3不想安装 deepspeed 也可以把load_checkpoint的use_deepspeed设为False即可同文档“Advanced training”一节给出的推理示例就是这样调用的。模型文件从哪里来文档对 Model API 的说明只有一句话但很关键To use the model API, you need to download the model files and pass config and model file paths manually.使用 Model API 需要你下载模型文件并手动传入 config 和模型文件路径。也就是说与TTS(tts_models/multilingual/multi-dataset/xtts_v2)自动下载不同Model API 不会替你拉取文件模型目录必须已存在于本地。从load_checkpoint的源码实现TTS/tts/models/xtts.py可以看到当你只传checkpoint_dir时它默认在该目录下查找config.json—— 模型配置由你自己先用XttsConfig.load_json读取model.pth—— 模型权重vocab.json—— tokenizer 词表speakers_xtts.pth—— 可选Coqui 内置说话人的 speaker 文件存在时才会初始化SpeakerManager。所以下文中所有代码里的/path/to/xtts/都指你自己下载的、包含上述文件的目录config.json也在这个目录内。文档还提示使用特定版本时需要注意分支版本部分版本可能与 API 不兼容。手动加载模型并做推理完整流程分四步加载配置、初始化并加载 checkpoint、计算说话人条件、调用inference并保存音频。以下代码来自 docs/source/models/xtts.md 的 “Manual Inference” 一节执行前把/path/to/xtts/换成你的模型目录把reference.wav换成你的参考音频路径建议使用 3 秒左右的清晰语音import torch import torchaudio from TTS.tts.configs.xtts_config import XttsConfig from TTS.tts.models.xtts import Xtts print(Loading model...) config XttsConfig() config.load_json(/path/to/xtts/config.json) model Xtts.init_from_config(config) model.load_checkpoint(config, checkpoint_dir/path/to/xtts/, use_deepspeedTrue) model.cuda() print(Computing speaker latents...) gpt_cond_latent, speaker_embedding model.get_conditioning_latents(audio_path[reference.wav]) print(Inference...) out model.inference( It took me quite a long time to develop a voice and now that I have it I am not going to be silent., en, gpt_cond_latent, speaker_embedding, temperature0.7, # Add custom parameters here ) torchaudio.save(xtts.wav, torch.tensor(out[wav]).unsqueeze(0), 24000)各步骤的作用XttsConfig().load_json(...)读取模型配置Xtts.init_from_config(config)仅根据配置构建空模型等价于Xtts(config)见 TTS/tts/models/xtts.py 中的init_from_config。load_checkpoint(config, checkpoint_dir..., use_deepspeedTrue)从checkpoint_dir加载model.pth、初始化 tokenizer并在evalTrue时把 GPT 置为推理模式use_deepspeedTrue只有在安装了 deepspeed 时使用。get_conditioning_latents(audio_path[reference.wav])从参考音频提取两个张量GPT 条件 latentgpt_cond_latent和说话人 embeddingspeaker_embedding。文档建议同一个说话人做多次推理时可以把这两个结果缓存下来以加快推理。model.inference(text, language, gpt_cond_latent, speaker_embedding, ...)返回一个 dict其中out[wav]是合成波形numpy示例按 24000 Hz 采样率保存为xtts.wav。推理参数说明model.inference的可选参数在 XTTS 文档 中列出了用途和默认值其中几个会影响听感参数用途文档原文描述默认值文档temperature自回归模型的 softmax 温度0.65length_penalty长度惩罚越高输出越短促1.0repetition_penalty防止解码器自我重复可降低长静音或“uhhhhhh”出现的概率2.0top_k取值越低输出越“likely”即越保守50top_p同上基于概率阈值的采样截断0.8speed生成音频的语速倍率远离 1.0 可能产生伪影1.0enable_text_splitting是否按句子切分文本逐句生成允许无限输入长度但句间上下文可能丢失Truelanguage参数需要与文本语言一致且要在 XTTS v2 支持的语言范围内English (en)、Spanish (es)、French (fr)、German (de)、Italian (it)、Portuguese (pt)、Polish (pl)、Turkish (tr)、Russian (ru)、Dutch (nl)、Czech (cs)、Arabic (ar)、Chinese (zh-cn)、Japanese (ja)、Hungarian (hu)、Korean (ko)共 16 种。结果验证按上面代码跑完后验证方式就是文档展示的产物本身当前目录生成xtts.wav采样率 24000 Hz。播放该文件确认能听到合成语音即完成一次成功的手动推理。如果model.inference抛异常常见于前置步骤没做对模型文件目录里缺model.pth/vocab.json、reference.wav不可读、或没有安装 deepspeed 却传了use_deepspeedTrue。可选分支流式推理如果做实时应用、需要在音频生成过程中就拿到第一个片段可以用inference_stream代替inference。文档说明流式推理通常比常规推理慢但能更快拿到第一个音频块。示例同样来自 docs/source/models/xtts.md 的 “Streaming manually” 一节路径替换规则同上import time import torch import torchaudio from TTS.tts.configs.xtts_config import XttsConfig from TTS.tts.models.xtts import Xtts print(Loading model...) config XttsConfig() config.load_json(/path/to/xtts/config.json) model Xtts.init_from_config(config) model.load_checkpoint(config, checkpoint_dir/path/to/xtts/, use_deepspeedTrue) model.cuda() print(Computing speaker latents...) gpt_cond_latent, speaker_embedding model.get_conditioning_latents(audio_path[reference.wav]) print(Inference...) t0 time.time() chunks model.inference_stream( It took me quite a long time to develop a voice and now that I have it I am not going to be silent., en, gpt_cond_latent, speaker_embedding ) wav_chuncks [] for i, chunk in enumerate(chunks): if i 0: print(fTime to first chunck: {time.time() - t0}) print(fReceived chunk {i} of audio length {chunk.shape[-1]}) wav_chuncks.append(chunk) wav torch.cat(wav_chuncks, dim0) torchaudio.save(xtts_streaming.wav, wav.squeeze().unsqueeze(0).cpu(), 24000)运行时的判断依据终端会打印 “Time to first chunck: ...”首个音频块的到达耗时这是文档示例的运行方式不是固定预期值随后逐块打印Received chunk {i} of audio length ...结束后生成xtts_streaming.wav24000 Hz。限制与说明XTTS v2 文档明确“Current implementation only supports inference and GPT encoder training”即当前实现只支持推理和 GPT encoder 训练Xtts的forward/train_step均抛出NotImplementedError见 TTS/tts/models/xtts.py。单句文本 token 数有限源码中inference对每条文本断言 token 数小于gpt_max_text_tokens超过会报 “❗ XTTS can only generate text with a maximum of 400 tokens”长文本建议开启enable_text_splitting按句切分。get_conditioning_latents支持传多个参考音频list内部会拼接后统一提取 GPT 条件 latent、并对各文件的 speaker embedding 求平均。如果只是想快速验证效果而不做手动加载同文档还给出了 CLI 路径tts --model_name tts_models/multilingual/multi-dataset/xtts_v2 ...和高层 API 路径from TTS.api import TTS它们会自动处理模型下载但不提供 Model API 的手动控制能力。XTTS v2 的模型配置字段定义在 TTS/tts/configs/xtts_config.py如需了解XttsConfig各字段的含义如gpt_cond_len、max_ref_len等可以直接查看该文件的 docstring。【免费下载链接】TTS - a deep learning toolkit for Text-to-Speech, battle-tested in research and production项目地址: https://gitcode.com/GitHub_Trending/tt/TTS创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表