File size: 13,259 Bytes
7934b29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "htbJiaJjYQAD"
   },
   "source": [
    "# Tacotron 2 Training\n",
    "\n",
    "This notebook is designed to provide a guide on how to train Tacotron2 as part of the TTS pipeline. It contains the following sections\n",
    "\n",
    "  1. Tacotron2 and NeMo - An introduction to the Tacotron2 model\n",
    "  2. LJSpeech - How to train Tacotron2 on LJSpeech\n",
    "  3. Custom Datasets - How to collect audio data to train Tacotron2 for difference voices and languages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "wqPMTEXXYUP4"
   },
   "source": [
    "# License\n",
    "\n",
    "> Copyright 2020 NVIDIA. All Rights Reserved.\n",
    "> \n",
    "> Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "> you may not use this file except in compliance with the License.\n",
    "> You may obtain a copy of the License at\n",
    "> \n",
    ">     http://www.apache.org/licenses/LICENSE-2.0\n",
    "> \n",
    "> Unless required by applicable law or agreed to in writing, software\n",
    "> distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "> See the License for the specific language governing permissions and\n",
    "> limitations under the License."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "SUkq9HAvYU7T"
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "You can either run this notebook locally (if you have all the dependencies and a GPU) or on Google Colab.\n",
    "Instructions for setting up Colab are as follows:\n",
    "1. Open a new Python 3 notebook.\n",
    "2. Import this notebook from GitHub (File -> Upload Notebook -> \"GITHUB\" tab -> copy/paste GitHub URL)\n",
    "3. Connect to an instance with a GPU (Runtime -> Change runtime type -> select \"GPU\" for hardware accelerator)\n",
    "4. Run this cell to set up dependencies# .\n",
    "\"\"\"\n",
    "BRANCH = 'r1.17.0'\n",
    "# # If you're using Colab and not running locally, uncomment and run this cell.\n",
    "# !apt-get install sox libsndfile1 ffmpeg\n",
    "# !pip install wget text-unidecode\n",
    "# !python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[all]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ZivXzmq0YYLj"
   },
   "source": [
    "# Tacotron2 and NeMo\n",
    "\n",
    "Tacotron2 is a neural network that converts text characters into a mel spectrogram. For more details on the model, please refer to Nvidia's [Tacotron2 Model Card](https://ngc.nvidia.com/catalog/models/nvidia:nemo:tts_en_tacotron2), or the original [paper](https://arxiv.org/abs/1712.05884).\n",
    "\n",
    "Tacotron2 like most NeMo models are defined as a LightningModule, allowing for easy training via PyTorch Lightning, and parameterized by a configuration, currently defined via a yaml file and loading using Hydra.\n",
    "\n",
    "Let's take a look using NeMo's pretrained model and how to use it to generate spectrograms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "HEvdSU5WYZbj"
   },
   "outputs": [],
   "source": [
    "# Load the Tacotron2Model\n",
    "from nemo.collections.tts.models import Tacotron2Model\n",
    "from nemo.collections.tts.models.base import SpectrogramGenerator\n",
    "\n",
    "# Let's see what pretrained models are available\n",
    "print(Tacotron2Model.list_available_models())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "3W8unatgYbUp"
   },
   "outputs": [],
   "source": [
    "# We can load the pre-trained model as follows\n",
    "model = Tacotron2Model.from_pretrained(\"tts_en_tacotron2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "xsyBa9tIdHp4"
   },
   "outputs": [],
   "source": [
    "# Tacotron2 is a SpectrogramGenerator\n",
    "assert isinstance(model, SpectrogramGenerator)\n",
    "\n",
    "# SpectrogramGenerators in NeMo have two helper functions:\n",
    "#   1. parse(self, text: str, normalize=True) which takes an English string and produces a token tensor\n",
    "#   2. generate_spectrogram(self, *, tokens) which takes the token tensor and generates a spectrogram\n",
    "# Let's try it out\n",
    "tokens = model.parse(text = \"Hey, this produces speech!\")\n",
    "spectrogram = model.generate_spectrogram(tokens = tokens)\n",
    "\n",
    "# Now we can visualize the generated spectrogram\n",
    "# If we want to generate speech, we have to use a vocoder in conjunction to a spectrogram generator.\n",
    "# Refer to the TTS Inference notebook on how to convert spectrograms to speech.\n",
    "from matplotlib.pyplot import imshow\n",
    "from matplotlib import pyplot as plt\n",
    "%matplotlib inline\n",
    "imshow(spectrogram.cpu().detach().numpy()[0,...], origin=\"lower\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zZ90eCfdrNIf"
   },
   "source": [
    "# Training\n",
    "\n",
    "Now that we looked at the Tacotron2 model, let's see how to train a Tacotron2 Model\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "7rHG-LERrPRY"
   },
   "outputs": [],
   "source": [
    "# NeMo's training scripts are stored inside the examples/ folder. Let's grab the tacotron2.py file\n",
    "# as well as the tacotron2.yaml file\n",
    "!wget https://raw.githubusercontent.com/NVIDIA/NeMo/$BRANCH/examples/tts/tacotron2.py\n",
    "!(mkdir -p conf \\\n",
    "  && cd conf \\\n",
    "  && wget https://raw.githubusercontent.com/NVIDIA/NeMo/$BRANCH/examples/tts/conf/tacotron2.yaml \\\n",
    "  && cd ..)\n",
    "\n",
    "# We will also need a few extra files for handling text.\n",
    "!(mkdir -p scripts/tts_dataset_files \\\n",
    "  && cd scripts/tts_dataset_files \\\n",
    "  && wget https://raw.githubusercontent.com/NVIDIA/NeMo/$BRANCH/scripts/tts_dataset_files/cmudict-0.7b_nv22.10 \\\n",
    "  && wget https://raw.githubusercontent.com/NVIDIA/NeMo/$BRANCH/scripts/tts_dataset_files/heteronyms-052722 \\\n",
    "  && cd ..)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Upv_LxBIsC51"
   },
   "source": [
    "Let's take a look at the tacotron2.py file\n",
    "\n",
    "```python\n",
    "import pytorch_lightning as pl\n",
    "\n",
    "from nemo.collections.common.callbacks import LogEpochTimeCallback\n",
    "from nemo.collections.tts.models import Tacotron2Model\n",
    "from nemo.core.config import hydra_runner\n",
    "from nemo.utils.exp_manager import exp_manager\n",
    "\n",
    "\n",
    "# hydra_runner is a thin NeMo wrapper around Hydra\n",
    "# It looks for a config named tacotron2.yaml inside the conf folder\n",
    "# Hydra parses the yaml and returns it as a Omegaconf DictConfig\n",
    "@hydra_runner(config_path=\"conf\", config_name=\"tacotron2\")\n",
    "def main(cfg):\n",
    "    # Define the Lightning trainer\n",
    "    trainer = pl.Trainer(**cfg.trainer)\n",
    "    # exp_manager is a NeMo construct that helps with logging and checkpointing\n",
    "    exp_manager(trainer, cfg.get(\"exp_manager\", None))\n",
    "    # Define the Tacotron 2 model, this will construct the model as well as\n",
    "    # define the training and validation dataloaders\n",
    "    model = Tacotron2Model(cfg=cfg.model, trainer=trainer)\n",
    "    # Let's add a few more callbacks\n",
    "    lr_logger = pl.callbacks.LearningRateMonitor()\n",
    "    epoch_time_logger = LogEpochTimeCallback()\n",
    "    trainer.callbacks.extend([lr_logger, epoch_time_logger])\n",
    "    # Call lightning trainer's fit() to train the model\n",
    "    trainer.fit(model)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()  # noqa pylint: disable=no-value-for-parameter\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6nM-fZO-s75u"
   },
   "source": [
    "Let's take a look at the yaml config\n",
    "\n",
    "```yaml\n",
    "name: &name Tacotron2\n",
    "\n",
    "train_dataset: ???\n",
    "validation_datasets: ???\n",
    "sup_data_path: null\n",
    "sup_data_types: null\n",
    "\n",
    "phoneme_dict_path: \"scripts/tts_dataset_files/cmudict-0.7b_nv22.10\"\n",
    "heteronyms_path: \"scripts/tts_dataset_files/heteronyms-052722\"\n",
    "```\n",
    "\n",
    "The first part of the yaml defines dataset parameters used by Tacotron. Then in the head of 'model' section there are processing - related parameters. You can see\n",
    "that the sample rate is set to 22050 for LJSpeech. \n",
    "\n",
    "Looking at the yaml, there is `train_dataset: ???` and `validation_datasets: ???`. The ??? indicates to hydra that these values must be passed via the command line or the script will fail.\n",
    "\n",
    "Looking further down the yaml, we get to the pytorch lightning trainer parameters.\n",
    "\n",
    "```yaml\n",
    "trainer:\n",
    "  devices: 1 # number of gpus\n",
    "  accelerator: 'gpu' \n",
    "  max_epochs: ???\n",
    "  num_nodes: 1\n",
    "  accelerator: 'gpu'\n",
    "  strategy: 'ddp'\n",
    "  accumulate_grad_batches: 1\n",
    "  enable_checkpointing: False  # Provided by exp_manager\n",
    "  logger: False  # Provided by exp_manager\n",
    "  gradient_clip_val: 1.0\n",
    "  log_every_n_steps: 200\n",
    "  check_val_every_n_epoch: 25\n",
    "```\n",
    "\n",
    "These values can be changed either by editing the yaml or through the command line.\n",
    "\n",
    "Let's grab some simple audio data and test Tacotron2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "GnEzODcorugt"
   },
   "outputs": [],
   "source": [
    "!wget https://github.com/NVIDIA/NeMo/releases/download/v0.11.0/test_data.tar.gz \\\n",
    "&& mkdir -p tests/data \\\n",
    "&& tar xzf test_data.tar.gz -C tests/data\n",
    "\n",
    "# Just like ASR, the Tacotron2 require .json files to define the training and validation data.\n",
    "!cat tests/data/asr/an4_val.json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have some sample data, we can try training Tacotron 2!\n",
    "\n",
    "Note that the sample data is not enough data to fully train a Tacotron 2 model. The following code uses a toy dataset to illustrate how the pipeline for training would work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!(python tacotron2.py \\\n",
    "  model.sample_rate=16000 \\\n",
    "  train_dataset=tests/data/asr/an4_train.json \\\n",
    "  validation_datasets=tests/data/asr/an4_val.json \\\n",
    "  trainer.max_epochs=3 \\\n",
    "  trainer.accelerator=null \\\n",
    "  trainer.check_val_every_n_epoch=1 \\\n",
    " +trainer.gpus=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "9erGDGZJ1H_p"
   },
   "source": [
    "# Training Data\n",
    "\n",
    "In order to train Tacotron2, it is highly recommended to obtain high quality speech data with the following properties:\n",
    "  - Sampling rate of 22050Hz or higher\n",
    "  - Single speaker\n",
    "  - Speech should contain a variety of speech phonemes\n",
    "  - Audio split into segments of 1-10 seconds\n",
    "  - Audio segments should not have silence at the beginning and end\n",
    "  - Audio segments should not contain long silences inside\n",
    "\n",
    "After obtaining the speech data and splitting into training, validation, and test sections, it is required to construct .json files to tell NeMo where to find these audio files.\n",
    "\n",
    "The .json files should adhere to the format required by the `nemo.collections.tts.data.tts_dataset.TTSDataset` class. For example, here is a sample .json file\n",
    "\n",
    "```json\n",
    "{\"audio_filepath\": \"/path/to/audio1.wav\", \"text\": \"the transcription\", \"duration\": 0.82}\n",
    "{\"audio_filepath\": \"/path/to/audio2.wav\", \"text\": \"the other transcription\", \"duration\": 2.1}\n",
    "...\n",
    "```\n",
    "Please note that the duration is in seconds.\n",
    "\n",
    "\n",
    "Then you are ready to run your training script:\n",
    "```bash\n",
    "python tacotron2.py train_dataset=YOUR_TRAIN.json validation_datasets=YOUR_VAL.json trainer.devices=-1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "Taco2.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}