微调文本转语音模型

使用 Hugging Face 的多模态模型

James Chapman

Curriculum Manager, DataCamp

微调文本转语音的目的

 

  • 学习新的语言和方言的发音
  • 迁移到新场景

如:大型英文通用预训练模型 ⇏ 真实的意大利语语音

英语/意大利语对话气泡卡通

使用 Hugging Face 的多模态模型

微调文本转语音的目的

语者嵌入在语音生成流程中的位置示意图

  • 说话人嵌入 + 文本转语音模型特征 → 生成式模型
  • 仅有新说话人嵌入不足以生成效果,需微调
使用 Hugging Face 的多模态模型

准备音频数据集

VoxPopuli 数据集:来自欧盟议会的18种语言转写语音数据

from datasets import load_dataset
dataset = load_dataset("facebook/voxpopuli", "it", split="train", 
                       trust_remote_code=True)
print(dataset.features)
['audio', 'raw_text', 'normalized_text', 'gender', 'speaker_id', ... ]
  • 需要预处理音频并添加说话人嵌入:
speaker_model = EncoderClassifier.from_hparams(source="speechbrain/spkrec-xvect-voxceleb", 
                                       savedir="pretrained_models/spkrec-xvect-voxceleb")
使用 Hugging Face 的多模态模型

音频预处理

from transformers import SpeechT5Processor
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")


def prepare_dataset(example): audio = example["audio"] example = processor(text=example["normalized_text"], audio_target=audio["array"], sampling_rate=audio["sampling_rate"], return_attention_mask=False)
example["labels"] = example["labels"][0]
with torch.no_grad(): speaker_embeddings = speaker_model.encode_batch(torch.tensor(audio["array"])) speaker_embeddings = torch.nn.functional.normalize(speaker_embeddings, dim=2) example["speaker_embeddings"] = speaker_embeddings.squeeze().cpu().numpy() return example
dataset = dataset.map(prepare_dataset)
使用 Hugging Face 的多模态模型

训练参数

from transformers import Seq2SeqTrainingArguments


training_args = Seq2SeqTrainingArguments( per_device_train_batch_size=4,
gradient_accumulation_steps=8,
learning_rate=1e-5,
warmup_steps=500,
label_names=["labels"],
data_collator=data_collator )
使用 Hugging Face 的多模态模型

整合到一起

model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")

训练器:

trainer = Seq2SeqTrainer(args=training_args, model=model,
    train_dataset=dataset["train"], eval_dataset=dataset["test"],
                         tokenizer=processor)

运行训练:trainer.train()

使用 Hugging Face 的多模态模型

使用新模型

text = "se sono italiano posso cantare l'opera lirica"


speaker_embedding = torch.tensor(dataset[5]["speaker_embeddings"]).unsqueeze(0)
inputs = processor(text=text, return_tensors="pt")
speech = model.generate_speech(inputs["input_ids"], speaker_embedding, vocoder=vocoder)
make_spectrogram(speech)
使用 Hugging Face 的多模态模型

使用新模型

意大利语语音频谱图

使用 Hugging Face 的多模态模型

开始练习!

使用 Hugging Face 的多模态模型

Preparing Video For Download...