Sesame CSM (1B) TTS —— 文本转语音微调


文档摘要

源文件:chapter7/sesame/README.md Sesame CSM (1B) TTS —— 文本转语音微调 本目录包含基于 Unsloth 微调并运行 Sesame CSM 文本转语音模型推理的脚本。 文件 —— 用 LoRA 微调模型的训练脚本 —— 从单段文本生成语音的单条推理脚本 —— 批量处理多段文本的推理脚本 —— 批量推理的示例输入文件 —— Python 依赖 安装 训练 用自己的数据集训练模型: 这会: 加载基础模型 添加 LoRA 适配器 在你的数据集上微调 把 LoRA 适配器保存到 推理 单段文本推理 从单段文本生成语音: 批量推理 一次性处理多段文本: 输入文件格式 JSON 格式( ): 不带上下文: 带上下文(用数据集索引保持音色一致): 注意:

源文件:chapter7/sesame/README.md

Sesame CSM (1B) TTS —— 文本转语音微调

本目录包含基于 Unsloth 微调并运行 Sesame CSM 文本转语音模型推理的脚本。

文件

  • sesame_csm_sft_unsloth.py —— 用 LoRA 微调模型的训练脚本
  • inference.py —— 从单段文本生成语音的单条推理脚本
  • batch_inference.py —— 批量处理多段文本的推理脚本
  • example_inputs.json —— 批量推理的示例输入文件
  • requirements.txt —— Python 依赖

安装

# Install dependencies pip install -r requirements.txt # For Conda users, install ffmpeg conda install -c conda-forge "ffmpeg>=6.0" -y conda install -c conda-forge libiconv -y

训练

用自己的数据集训练模型:

python sesame_csm_sft_unsloth.py

这会:

  1. 加载基础模型 unsloth/csm-1b
  2. 添加 LoRA 适配器
  3. 在你的数据集上微调
  4. 把 LoRA 适配器保存到 lora_model/

推理

单段文本推理

从单段文本生成语音:

# Without context (simple generation) python inference.py \ --lora-path lora_model \ --text "We just finished fine tuning a text to speech model... and it's pretty good!" \ --output example_without_context_1.wav # With voice context (for voice consistency) using dataset index 3 python inference.py \ --lora-path lora_model \ --text "Sesame is a super cool TTS model which can be fine tuned with Unsloth." \ --dataset-context-idx 3 \ --output example_with_context_1.wav # Using base model only (without LoRA) python inference.py \ --text "Hello world, this is a test of the text to speech system." # With custom speaker ID and longer generation python inference.py \ --lora-path lora_model \ --text "This is a longer sentence that needs more tokens." \ --speaker-id 0 \ --max-tokens 250 \ --output long_speech.wav # With 4-bit quantization (lower memory) python inference.py \ --lora-path lora_model \ --text "Memory efficient inference." \ --load-in-4bit

批量推理

一次性处理多段文本:

# From JSON file python batch_inference.py \ --lora-path lora_model \ --input-file example_inputs.json \ --output-dir batch_outputs # From plain text file (one text per line) python batch_inference.py \ --lora-path lora_model \ --input-file texts.txt \ --output-dir batch_outputs

输入文件格式

JSON 格式example_inputs.json):

不带上下文:

[ { "text": "We just finished fine tuning a text to speech model... and it's pretty good!", "speaker_id": 0, "output": "example_without_context_1.wav" }, { "text": "Sesame is a super cool TTS model which can be fine tuned with Unsloth.", "speaker_id": 0, "output": "example_without_context_2.wav" } ]

带上下文(用数据集索引保持音色一致):

[ { "text": "Sesame is a super cool TTS model which can be fine tuned with Unsloth.", "speaker_id": 0, "dataset_context_idx": 3, "output": "example_with_context_1.wav" }, { "text": "We just finished fine tuning a text to speech model... and it's pretty good!", "speaker_id": 0, "dataset_context_idx": 4, "output": "example_with_context_2.wav" } ]

注意dataset_context_idx 指训练数据集(例如 MrDragonFox/Elise)中的索引。训练脚本示例中用的是索引 3 和 4。

纯文本格式(每行一句,不支持上下文):

Hello world, this is the first sentence. This is the second sentence. This is the third sentence.

参数

通用参数

  • --base-model:基础模型名称或路径(默认:unsloth/csm-1b
  • --lora-path:已保存 LoRA 适配器的路径(可选)
  • --load-in-4bit:以 4-bit 量化加载模型以降低显存占用

推理参数

  • --text:要转为语音的文本
  • --speaker-id:多说话人模型的说话人 ID(默认:0)
  • --output:输出音频文件路径(默认:output.wav
  • --max-tokens:最多生成的 token 数(125 ≈ 10 秒音频)
  • --dataset-context-idx:用于音色一致性的数据集索引(例如训练示例中的 3 或 4)
  • --dataset-name:从中加载上下文的数据集名(默认:MrDragonFox/Elise

批量推理参数

  • --input-file:输入文件(JSON 或纯文本)
  • --output-dir:音频文件输出目录(默认:outputs

模型信息

  • 基础模型:Sesame CSM (1B) —— 一个紧凑的文本转语音模型
  • 输出格式:24kHz WAV 音频
  • token 与时长比例:约 125 token = 10 秒音频
  • 多说话人:通过说话人 ID 支持多个说话人

进阶用法

用上下文保持音色一致

为获得更好的音色一致性,可从数据集提供音频上下文(示例见 sesame_csm_sft_unsloth.py 第 320-390 行):

from inference import load_model, generate_speech model, processor = load_model("unsloth/csm-1b", "lora_model") # Use dataset index 3 for voice consistency (same as training script) generate_speech( model=model, processor=processor, text="Sesame is a super cool TTS model which can be fine tuned with Unsloth.", dataset_context_idx=3, dataset_name="MrDragonFox/Elise", output_path="output_with_context.wav" )

推理脚本会自动从指定的数据集索引加载音频与文本,确保与训练方式保持一致。

显存需求

  • 基础模型:约 4-6GB 显存
  • 基础模型 + LoRA 训练:约 8-12GB 显存
  • 4-bit 量化:约 2-3GB 显存
  • 仅推理:约 2-4GB 显存

若显存有限,请使用 --load-in-4bit 标志。

故障排查

生成时出现 AssertionError

若在 model.generate() 时遇到 AssertionError,请确保正确传入 tensor:

# ❌ Wrong audio_values = model.generate(**inputs, max_new_tokens=125) # ✅ Correct audio_values = model.generate( input_ids=inputs["input_ids"], attention_mask=inputs.get("attention_mask"), max_new_tokens=125, output_audio=True )

显存不足

  • 使用 --load-in-4bit 标志
  • 减小 max_new_tokens
  • 逐条处理文本而非批量
  • 训练时使用更小的 batch size

资源

许可证

本项目使用 Unsloth 库与 Sesame CSM 模型,请参阅它们各自的许可证。


发布者: 作者: bojieli 转发
评论区 (0)
U