Kamtera commited on
Commit
11caf7d
1 Parent(s): d1a87bf

Upload train_tacotron2.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_tacotron2.py +122 -0
train_tacotron2.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # Trainer: Where the ✨️ happens.
4
+ # TrainingArgs: Defines the set of arguments of the Trainer.
5
+ from trainer import Trainer, TrainerArgs
6
+
7
+ # GlowTTSConfig: all model related values for training, validating and testing.
8
+ # from TTS.tts.configs.glow_tts_config import GlowTTSConfig
9
+ from TTS.tts.configs.tacotron2_config import Tacotron2Config
10
+
11
+
12
+ # BaseDatasetConfig: defines name, formatter and path of the dataset.
13
+ from TTS.tts.configs.shared_configs import BaseDatasetConfig , CharactersConfig
14
+ from TTS.config.shared_configs import BaseAudioConfig
15
+ from TTS.tts.datasets import load_tts_samples
16
+
17
+
18
+ # from TTS.tts.models.glow_tts import GlowTTS
19
+ from TTS.tts.models.tacotron2 import Tacotron2
20
+
21
+
22
+ from TTS.tts.utils.text.tokenizer import TTSTokenizer
23
+ from TTS.utils.audio import AudioProcessor
24
+
25
+ # we use the same path as this script as our training folder.
26
+ output_path = os.path.dirname(os.path.abspath(__file__))
27
+
28
+ # DEFINE DATASET CONFIG
29
+ # Set LJSpeech as our target dataset and define its path.
30
+ # You can also use a simple Dict to define the dataset and pass it to your custom formatter.
31
+
32
+
33
+ dataset_config = BaseDatasetConfig(
34
+ formatter="mozilla", meta_file_train="metadata.csv", path="/kaggle/input/persian-tts-dataset-famale"
35
+ )
36
+
37
+ audio_config = BaseAudioConfig(
38
+ sample_rate=24000,
39
+ do_trim_silence=True,
40
+ resample=False
41
+
42
+ )
43
+
44
+ character_config=CharactersConfig(
45
+ characters='ءابتثجحخدذرزسشصضطظعغفقلمنهويِپچژکگیآأؤإئًَُّ',
46
+ punctuations='!(),-.:;? ̠،؛؟‌<>',
47
+ phonemes='ˈˌːˑpbtdʈɖcɟkɡqɢʔɴŋɲɳnɱmʙrʀⱱɾɽɸβfvθðszʃʒʂʐçʝxɣχʁħʕhɦɬɮʋɹɻjɰlɭʎʟaegiouwyɪʊ̩æɑɔəɚɛɝɨ̃ʉʌʍ0123456789"#$%*+/=ABCDEFGHIJKLMNOPRSTUVWXYZ[]^_{}',
48
+ pad="<PAD>",
49
+ eos="<EOS>",
50
+ bos="<BOS>",
51
+ blank="<BLNK>",
52
+ characters_class="TTS.tts.utils.text.characters.IPAPhonemes",
53
+ )
54
+ # INITIALIZE THE TRAINING CONFIGURATION
55
+ # Configure the model. Every config class inherits the BaseTTSConfig.
56
+ config = Tacotron2Config(
57
+ save_step=1000,
58
+ batch_size=8,#
59
+ eval_batch_size=4,#
60
+ model='tacotron2',
61
+ num_loader_workers=0,
62
+ num_eval_loader_workers=0,
63
+ output_path=output_path,
64
+ audio=audio_config,
65
+ use_phonemes=True,
66
+ phoneme_language="fa",
67
+ text_cleaner="basic_cleaners",
68
+ phoneme_cache_path=os.path.join(output_path, "phoneme_cache"),
69
+ characters=character_config,
70
+ print_step=25,
71
+ print_eval=False,
72
+ mixed_precision=True,
73
+ datasets=[dataset_config],
74
+ test_sentences=[
75
+ "سلطان محمود در زمستانی سخت به طلخک گفت که: با این جامه ی یک لا در این سرما چه می کنی ",
76
+ "مردی نزد بقالی آمد و گفت پیاز هم ده تا دهان بدان خو شبوی سازم.",
77
+ "از مال خود پاره ای گوشت بستان و زیره بایی معطّر بساز",
78
+ "یک بار هم از جهنم بگویید.",
79
+ "یکی اسبی به عاریت خواست"
80
+ ],
81
+ num_chars=len('ˈˌːˑpbtdʈɖcɟkɡqɢʔɴŋɲɳnɱmʙrʀⱱɾɽɸβfvθðszʃʒʂʐçʝxɣχʁħʕhɦɬɮʋɹɻjɰlɭʎʟaegiouwyɪʊ̩æɑɔəɚɛɝɨ̃ʉʌʍ0123456789"#$%*+/=ABCDEFGHIJKLMNOPRSTUVWXYZ[]^_{}'),
82
+
83
+ )
84
+
85
+ # INITIALIZE THE AUDIO PROCESSOR
86
+ # Audio processor is used for feature extraction and audio I/O.
87
+ # It mainly serves to the dataloader and the training loggers.
88
+ ap = AudioProcessor.init_from_config(config)
89
+
90
+ # INITIALIZE THE TOKENIZER
91
+ # Tokenizer is used to convert text to sequences of token IDs.
92
+ # If characters are not defined in the config, default characters are passed to the config
93
+ tokenizer, config = TTSTokenizer.init_from_config(config)
94
+
95
+ # LOAD DATA SAMPLES
96
+ # Each sample is a list of ```[text, audio_file_path, speaker_name]```
97
+ # You can define your custom sample loader returning the list of samples.
98
+ # Or define your custom formatter and pass it to the `load_tts_samples`.
99
+ # Check `TTS.tts.datasets.load_tts_samples` for more details.
100
+ train_samples, eval_samples = load_tts_samples(
101
+ dataset_config,
102
+ eval_split=True,
103
+ eval_split_max_size=config.eval_split_max_size,
104
+ eval_split_size=config.eval_split_size,
105
+ #formatter=changizer
106
+ )
107
+
108
+ # INITIALIZE THE MODEL
109
+ # Models take a config object and a speaker manager as input
110
+ # Config defines the details of the model like the number of layers, the size of the embedding, etc.
111
+ # Speaker manager is used by multi-speaker models.
112
+ model = Tacotron2(config, ap, tokenizer, speaker_manager=None)
113
+
114
+ # INITIALIZE THE TRAINER
115
+ # Trainer provides a generic API to train all the 🐸TTS models with all its perks like mixed-precision training,
116
+ # distributed training, etc.
117
+ trainer = Trainer(
118
+ TrainerArgs(), config, output_path, model=model, train_samples=train_samples, eval_samples=eval_samples
119
+ )
120
+
121
+ # AND... 3,2,1... 🚀
122
+ trainer.fit()