텍스트-음성 변환 모델 미세 조정

Hugging Face로 배우는 멀티모달 모델

James Chapman

Curriculum Manager, DataCamp

텍스트-음성 변환 미세 조정의 목적

 

  • 새로운 언어·방언의 음소 학습
  • 새 문맥에 적용

예: 일반 영어 사전학습 모델 ⇏ 실제 같은 이탈리아어 음성

영어·이탈리아어 말풍선 만화

Hugging Face로 배우는 멀티모달 모델

텍스트-음성 변환 미세 조정의 목적

음성 임베딩이 음성 생성 파이프라인에 들어가는 방식 다이어그램

  • 화자 임베딩 + TTS 모델 특성 → 생성 모델
  • 미세 조정 없이 새로운 화자 임베딩만으로는 불충분
Hugging Face로 배우는 멀티모달 모델

오디오 데이터셋 준비

VoxPopuli 데이터셋: EU 의회 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...