微調文字轉語音模型

使用 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...