Create train_vits-0.py
Browse files- train_vits-0.py +144 -0
train_vits-0.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from trainer import Trainer, TrainerArgs
|
4 |
+
|
5 |
+
from TTS.tts.configs.shared_configs import BaseDatasetConfig , CharactersConfig
|
6 |
+
from TTS.config.shared_configs import BaseAudioConfig
|
7 |
+
from TTS.tts.configs.vits_config import VitsConfig
|
8 |
+
from TTS.tts.datasets import load_tts_samples
|
9 |
+
from TTS.tts.models.vits import Vits, VitsAudioConfig
|
10 |
+
from TTS.tts.utils.text.tokenizer import TTSTokenizer
|
11 |
+
from TTS.utils.audio import AudioProcessor
|
12 |
+
from TTS.tts.utils.speakers import SpeakerManager
|
13 |
+
|
14 |
+
|
15 |
+
output_path = os.path.dirname(os.path.abspath(__file__))
|
16 |
+
|
17 |
+
|
18 |
+
dataset_names={
|
19 |
+
"persian-tts-dataset-famale":"dilara",
|
20 |
+
"persian-tts-dataset":"changiz",
|
21 |
+
"persian-tts-dataset-male":"farid"
|
22 |
+
}
|
23 |
+
def mozilla_with_speaker(root_path, meta_file, **kwargs): # pylint: disable=unused-argument
|
24 |
+
"""Normalizes Mozilla meta data files to TTS format"""
|
25 |
+
txt_file = os.path.join(root_path, meta_file)
|
26 |
+
items = []
|
27 |
+
speaker_name = dataset_names[os.path.basename(root_path)]
|
28 |
+
with open(txt_file, "r", encoding="utf-8") as ttf:
|
29 |
+
for line in ttf:
|
30 |
+
cols = line.split("|")
|
31 |
+
wav_file = cols[1].strip()
|
32 |
+
text = cols[0].strip()
|
33 |
+
wav_file = os.path.join(root_path, "wavs", wav_file)
|
34 |
+
items.append({"text": text, "audio_file": wav_file, "speaker_name": speaker_name, "root_path": root_path})
|
35 |
+
return items
|
36 |
+
|
37 |
+
|
38 |
+
dataset_config1 = BaseDatasetConfig(
|
39 |
+
meta_file_train="metadata.csv", path="/kaggle/input/persian-tts-dataset-famale"
|
40 |
+
)
|
41 |
+
|
42 |
+
dataset_config2 = BaseDatasetConfig(
|
43 |
+
meta_file_train="metadata.csv", path="/kaggle/input/persian-tts-dataset"
|
44 |
+
)
|
45 |
+
|
46 |
+
dataset_config3 = BaseDatasetConfig(
|
47 |
+
meta_file_train="metadata.csv", path="/kaggle/input/persian-tts-dataset-male"
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
audio_config = BaseAudioConfig(
|
53 |
+
sample_rate=22050,
|
54 |
+
do_trim_silence=False,
|
55 |
+
resample=False,
|
56 |
+
mel_fmin=0,
|
57 |
+
mel_fmax=None
|
58 |
+
)
|
59 |
+
character_config=CharactersConfig(
|
60 |
+
characters='ءابتثجحخدذرزسشصضطظعغفقلمنهويِپچژکگیآأؤإئًَُّ',
|
61 |
+
punctuations='!(),-.:;? ̠،؛؟<>',
|
62 |
+
phonemes='ˈˌːˑpbtdʈɖcɟkɡqɢʔɴŋɲɳnɱmʙrʀⱱɾɽɸβfvθðszʃʒʂʐçʝxɣχʁħʕhɦɬɮʋɹɻjɰlɭʎʟaegiouwyɪʊ̩æɑɔəɚɛɝɨ̃ʉʌʍ0123456789"#$%*+/=ABCDEFGHIJKLMNOPRSTUVWXYZ[]^_{}',
|
63 |
+
pad="<PAD>",
|
64 |
+
eos="<EOS>",
|
65 |
+
bos="<BOS>",
|
66 |
+
blank="<BLNK>",
|
67 |
+
characters_class="TTS.tts.utils.text.characters.IPAPhonemes",
|
68 |
+
)
|
69 |
+
config = VitsConfig(
|
70 |
+
audio=audio_config,
|
71 |
+
run_name="vits_fa_female",
|
72 |
+
batch_size=16,
|
73 |
+
eval_batch_size=8,
|
74 |
+
batch_group_size=5,
|
75 |
+
num_loader_workers=0,
|
76 |
+
num_eval_loader_workers=2,
|
77 |
+
run_eval=True,
|
78 |
+
test_delay_epochs=-1,
|
79 |
+
epochs=1000,
|
80 |
+
save_step=1000,
|
81 |
+
text_cleaner="basic_cleaners",
|
82 |
+
use_phonemes=True,
|
83 |
+
phoneme_language="fa",
|
84 |
+
characters=character_config,
|
85 |
+
phoneme_cache_path=os.path.join(output_path, "phoneme_cache"),
|
86 |
+
compute_input_seq_cache=True,
|
87 |
+
print_step=25,
|
88 |
+
print_eval=True,
|
89 |
+
mixed_precision=False,
|
90 |
+
test_sentences=[
|
91 |
+
["سلطان محمود در زمستانی سخت به طلخک گفت که: با این جامه ی یک لا در این سرما چه می کنی "],
|
92 |
+
["مردی نزد بقالی آمد و گفت پیاز هم ده تا دهان بدان خو شبوی سازم."],
|
93 |
+
["از مال خود پاره ای گوشت بستان و زیره بایی معطّر بساز"],
|
94 |
+
["یک بار هم از جهنم بگویید."],
|
95 |
+
["یکی اسبی به عاریت خواست"]
|
96 |
+
],
|
97 |
+
output_path=output_path,
|
98 |
+
datasets=[dataset_config1,dataset_config2,dataset_config3],
|
99 |
+
)
|
100 |
+
|
101 |
+
# INITIALIZE THE AUDIO PROCESSOR
|
102 |
+
# Audio processor is used for feature extraction and audio I/O.
|
103 |
+
# It mainly serves to the dataloader and the training loggers.
|
104 |
+
ap = AudioProcessor.init_from_config(config)
|
105 |
+
|
106 |
+
# INITIALIZE THE TOKENIZER
|
107 |
+
# Tokenizer is used to convert text to sequences of token IDs.
|
108 |
+
# config is updated with the default characters if not defined in the config.
|
109 |
+
tokenizer, config = TTSTokenizer.init_from_config(config)
|
110 |
+
|
111 |
+
# LOAD DATA SAMPLES
|
112 |
+
# Each sample is a list of ```[text, audio_file_path, speaker_name]```
|
113 |
+
# You can define your custom sample loader returning the list of samples.
|
114 |
+
# Or define your custom formatter and pass it to the `load_tts_samples`.
|
115 |
+
# Check `TTS.tts.datasets.load_tts_samples` for more details.
|
116 |
+
train_samples, eval_samples = load_tts_samples(
|
117 |
+
config.datasets,
|
118 |
+
formatter=mozilla_with_speaker,
|
119 |
+
eval_split=True,
|
120 |
+
eval_split_max_size=config.eval_split_max_size,
|
121 |
+
eval_split_size=config.eval_split_size,
|
122 |
+
)
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
speaker_manager = SpeakerManager()
|
127 |
+
speaker_manager.set_ids_from_data(train_samples + eval_samples, parse_key="speaker_name")
|
128 |
+
config.num_speakers = speaker_manager.num_speakers
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
# init model
|
133 |
+
model = Vits(config, ap, tokenizer, speaker_manager=speaker_manager)
|
134 |
+
|
135 |
+
# init the trainer and 🚀
|
136 |
+
trainer = Trainer(
|
137 |
+
TrainerArgs(),
|
138 |
+
config,
|
139 |
+
output_path,
|
140 |
+
model=model,
|
141 |
+
train_samples=train_samples,
|
142 |
+
eval_samples=eval_samples,
|
143 |
+
)
|
144 |
+
trainer.fit()
|