Kamtera commited on
Commit
c48cf7d
1 Parent(s): f3073f4

Create train_vits-2.py

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