freds0 commited on
Commit
83b4c5f
·
verified ·
1 Parent(s): 7fa5531

XTTS finetuned using CML-TTS Dataset in Brazilian Portuguese for 50k steps

Browse files
Files changed (1) hide show
  1. train_xtts_pt.py +220 -0
train_xtts_pt.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from trainer import Trainer, TrainerArgs
4
+
5
+ from TTS.config.shared_configs import BaseDatasetConfig
6
+ from TTS.tts.datasets import load_tts_samples
7
+ from TTS.tts.layers.xtts.trainer.gpt_trainer import GPTArgs, GPTTrainer, GPTTrainerConfig, XttsAudioConfig
8
+ from TTS.utils.manage import ModelManager
9
+
10
+ # Logging parameters
11
+ RUN_NAME = "GPT_XTTS_v2.0_pt"
12
+ PROJECT_NAME = "XTTS_trainer"
13
+ DASHBOARD_LOGGER = "tensorboard"
14
+ LOGGER_URI = None
15
+
16
+ # Set here the path that the checkpoints will be saved. Default: ./run/training/
17
+ OUT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "checkpoints_xtts")
18
+
19
+ # Training Parameters
20
+ OPTIMIZER_WD_ONLY_ON_WEIGHTS = True # for multi-gpu training please make it False
21
+ START_WITH_EVAL = False # if True it will star with evaluation
22
+ BATCH_SIZE = 23 # set here the batch size
23
+ GRAD_ACUMM_STEPS = 84 # set here the grad accumulation steps
24
+ # Note: we recommend that BATCH_SIZE * GRAD_ACUMM_STEPS need to be at least 252 for more efficient training. You can increase/decrease BATCH_SIZE but then set GRAD_ACUMM_STEPS accordingly.
25
+
26
+ # Define here the dataset that you want to use for the fine-tuning on.
27
+
28
+ config_dataset_brspeech = BaseDatasetConfig(
29
+ formatter="brspeech",
30
+ path="/root/DATASETS/BRSpeech_CML_TTS_v14012024_24khz/",
31
+ meta_file_train="metadata.csv",
32
+ language="pt",
33
+ )
34
+ # Add here the configs of the datasets
35
+ DATASETS_CONFIG_LIST = [
36
+ config_dataset_brspeech,
37
+ ]
38
+ # Define the path where XTTS v2.0.1 files will be downloaded
39
+ CHECKPOINTS_OUT_PATH = os.path.join(OUT_PATH, "XTTS_v2.0_original_model_files/")
40
+ os.makedirs(CHECKPOINTS_OUT_PATH, exist_ok=True)
41
+
42
+
43
+ # DVAE files
44
+ DVAE_CHECKPOINT_LINK = "https://coqui.gateway.scarf.sh/hf-coqui/XTTS-v2/main/dvae.pth"
45
+ MEL_NORM_LINK = "https://coqui.gateway.scarf.sh/hf-coqui/XTTS-v2/main/mel_stats.pth"
46
+
47
+ # Set the path to the downloaded files
48
+ DVAE_CHECKPOINT = os.path.join(CHECKPOINTS_OUT_PATH, os.path.basename(DVAE_CHECKPOINT_LINK))
49
+ MEL_NORM_FILE = os.path.join(CHECKPOINTS_OUT_PATH, os.path.basename(MEL_NORM_LINK))
50
+
51
+ # download DVAE files if needed
52
+ if not os.path.isfile(DVAE_CHECKPOINT) or not os.path.isfile(MEL_NORM_FILE):
53
+ print(" > Downloading DVAE files!")
54
+ ModelManager._download_model_files([MEL_NORM_LINK, DVAE_CHECKPOINT_LINK], CHECKPOINTS_OUT_PATH, progress_bar=True)
55
+
56
+
57
+ # Download XTTS v2.0 checkpoint if needed
58
+ TOKENIZER_FILE_LINK = "https://coqui.gateway.scarf.sh/hf-coqui/XTTS-v2/main/vocab.json"
59
+ XTTS_CHECKPOINT_LINK = "https://coqui.gateway.scarf.sh/hf-coqui/XTTS-v2/main/model.pth"
60
+
61
+ # XTTS transfer learning parameters: You we need to provide the paths of XTTS model checkpoint that you want to do the fine tuning.
62
+ TOKENIZER_FILE = os.path.join(CHECKPOINTS_OUT_PATH, os.path.basename(TOKENIZER_FILE_LINK)) # vocab.json file
63
+ #XTTS_CHECKPOINT = os.path.join(CHECKPOINTS_OUT_PATH, os.path.basename(XTTS_CHECKPOINT_LINK)) # model.pth file
64
+ #XTTS_CHECKPOINT = "/root/TTS-XTTS-decoder/checkpoints_xtts/GPT_XTTS_v2.0_pt-May-13-2024_12+37AM-3fef64e9/checkpoint_217163.pth"
65
+ XTTS_CHECKPOINT ="/root/TTS-XTTS-decoder/checkpoints_xtts/GPT_XTTS_v2.0_pt-May-17-2024_01+34AM-3fef64e9/checkpoint_39030.pth"
66
+ # download XTTS v2.0 files if needed
67
+ '''
68
+ if not os.path.isfile(TOKENIZER_FILE) or not os.path.isfile(XTTS_CHECKPOINT):
69
+ print(" > Downloading XTTS v2.0 files!")
70
+ ModelManager._download_model_files(
71
+ [TOKENIZER_FILE_LINK, XTTS_CHECKPOINT_LINK], CHECKPOINTS_OUT_PATH, progress_bar=True
72
+ )
73
+ '''
74
+
75
+ # Training sentences generations
76
+ SPEAKER_REFERENCE = [
77
+ "/root/DATASETS/BRSpeech_CML_TTS_v14012024_24khz/train/audio/12249/12765/12249_12765_000007-0003.wav" # speaker reference to be used in training test sentences
78
+ ]
79
+ LANGUAGE = config_dataset_brspeech.language
80
+
81
+
82
+ def main():
83
+ # init args and config
84
+ model_args = GPTArgs(
85
+ max_conditioning_length=132300, # 6 secs
86
+ min_conditioning_length=66150, # 3 secs
87
+ debug_loading_failures=False,
88
+ max_wav_length=255995, # ~11.6 seconds
89
+ max_text_length=200,
90
+ mel_norm_file=MEL_NORM_FILE,
91
+ dvae_checkpoint=DVAE_CHECKPOINT,
92
+ xtts_checkpoint=XTTS_CHECKPOINT, # checkpoint path of the model that you want to fine-tune
93
+ tokenizer_file=TOKENIZER_FILE,
94
+ gpt_num_audio_tokens=1026,
95
+ gpt_start_audio_token=1024,
96
+ gpt_stop_audio_token=1025,
97
+ gpt_use_masking_gt_prompt_approach=True,
98
+ gpt_use_perceiver_resampler=True,
99
+ )
100
+ # define audio config
101
+ audio_config = XttsAudioConfig(sample_rate=24000, dvae_sample_rate=24000, output_sample_rate=24000)
102
+ # training parameters config
103
+ config = GPTTrainerConfig(
104
+ output_path=OUT_PATH,
105
+ model_args=model_args,
106
+ run_name=RUN_NAME,
107
+ project_name=PROJECT_NAME,
108
+ run_description="""
109
+ GPT XTTS training
110
+ """,
111
+ dashboard_logger=DASHBOARD_LOGGER,
112
+ logger_uri=LOGGER_URI,
113
+ audio=audio_config,
114
+ batch_size=BATCH_SIZE,
115
+ batch_group_size=48,
116
+ eval_batch_size=BATCH_SIZE,
117
+ num_loader_workers=8,
118
+ eval_split_max_size=256,
119
+ eval_split_size=0.05,
120
+ print_step=50,
121
+ plot_step=100,
122
+ log_model_step=1000,
123
+ save_step=10000,
124
+ save_n_checkpoints=3,
125
+ save_checkpoints=True,
126
+ # target_loss="loss",
127
+ print_eval=True,
128
+ run_eval_steps=10000,
129
+ # Optimizer values like tortoise, pytorch implementation with modifications to not apply WD to non-weight parameters.
130
+ optimizer="AdamW",
131
+ optimizer_wd_only_on_weights=OPTIMIZER_WD_ONLY_ON_WEIGHTS,
132
+ optimizer_params={"betas": [0.9, 0.96], "eps": 1e-8, "weight_decay": 1e-2},
133
+ lr=5e-06, # learning rate
134
+ lr_scheduler="MultiStepLR",
135
+ # it was adjusted accordly for the new step scheme
136
+ lr_scheduler_params={"milestones": [50000 * 18, 150000 * 18, 300000 * 18], "gamma": 0.5, "last_epoch": -1},
137
+ test_sentences=[
138
+ {
139
+ "text": "Ouviram do ipiranga às margens plácidas de um povo heróico o brado retumbante.",
140
+ "speaker_wav": SPEAKER_REFERENCE,
141
+ "language": LANGUAGE,
142
+ },
143
+ {
144
+ "text": "Minha terra tem palmeiras onde canta o sabiá, as aves que aqui gorjeiam não gorjeiam como lá.",
145
+ "speaker_wav": SPEAKER_REFERENCE,
146
+ "language": LANGUAGE,
147
+ },
148
+ {
149
+ "text": "Ó que saudades que tenho da aurora da minha vida, da minha infância querida, Que os anos não trazem mais.",
150
+ "speaker_wav": SPEAKER_REFERENCE,
151
+ "language": LANGUAGE,
152
+ },
153
+ {
154
+ "text": "No princípio Deus criou o céu e a terra, entretanto a terra era sem forma e vazia.",
155
+ "speaker_wav": SPEAKER_REFERENCE,
156
+ "language": LANGUAGE,
157
+ },
158
+ {
159
+ "text": "Amor é fogo que arde sem se ver é ferida que dói e não se sente é um contentamento descontente é dor que desatina sem doer.",
160
+ "speaker_wav": SPEAKER_REFERENCE,
161
+ "language": LANGUAGE,
162
+ },
163
+ {
164
+ "text": "E agora José? A festa acabou, a luz apagou, o povo sumiu, a noite esfriou, e agora José?",
165
+ "speaker_wav": SPEAKER_REFERENCE,
166
+ "language": LANGUAGE,
167
+ },
168
+ {
169
+ "text": "Vou-me embora pra Pasárgada, Lá sou amigo do rei, Lá tenho a mulher que eu quero, Na cama que escolherei!",
170
+ "speaker_wav": SPEAKER_REFERENCE,
171
+ "language": LANGUAGE,
172
+ },
173
+ {
174
+ "text": "É pau, é pedra, é o fim do caminho. É um resto de toco, é um pouco sozinho.",
175
+ "speaker_wav": SPEAKER_REFERENCE,
176
+ "language": LANGUAGE,
177
+ },
178
+ {
179
+ "text": "No meio do caminho tinha uma pedra; Tinha uma pedra no meio do caminho.",
180
+ "speaker_wav": SPEAKER_REFERENCE,
181
+ "language": LANGUAGE,
182
+ },
183
+ {
184
+ "text": "Brasil, mostra tua cara; quero ver quem paga pra gente ficar assim.",
185
+ "speaker_wav": SPEAKER_REFERENCE,
186
+ "language": LANGUAGE,
187
+ }
188
+ ],
189
+ )
190
+
191
+ # init the model from config
192
+ model = GPTTrainer.init_from_config(config)
193
+
194
+ # load training samples
195
+ train_samples, eval_samples = load_tts_samples(
196
+ DATASETS_CONFIG_LIST,
197
+ eval_split=True,
198
+ eval_split_max_size=config.eval_split_max_size,
199
+ eval_split_size=config.eval_split_size,
200
+ )
201
+
202
+ # init the trainer and 🚀
203
+ trainer = Trainer(
204
+ TrainerArgs(
205
+ restore_path=None, # xtts checkpoint is restored via xtts_checkpoint key so no need of restore it using Trainer restore_path parameter
206
+ skip_train_epoch=False,
207
+ start_with_eval=START_WITH_EVAL,
208
+ grad_accum_steps=GRAD_ACUMM_STEPS,
209
+ ),
210
+ config,
211
+ output_path=OUT_PATH,
212
+ model=model,
213
+ train_samples=train_samples,
214
+ eval_samples=eval_samples,
215
+ )
216
+ trainer.fit()
217
+
218
+
219
+ if __name__ == "__main__":
220
+ main()