Peng Wei commited on
Commit
2743281
1 Parent(s): bc69a87

setup the exp for both langs"

Browse files
Files changed (1) hide show
  1. fine_tune_tianet_tr_pt.ipynb +89 -1153
fine_tune_tianet_tr_pt.ipynb CHANGED
@@ -7432,7 +7432,7 @@
7432
  }
7433
  ],
7434
  "source": [
7435
- "# Convert the mp3 files to wav files\n",
7436
  "import os\n",
7437
  "NEMO_ROOT = os.getcwd()\n",
7438
  "print(NEMO_ROOT)\n",
@@ -8419,7 +8419,7 @@
8419
  },
8420
  {
8421
  "cell_type": "code",
8422
- "execution_count": 33,
8423
  "metadata": {
8424
  "colab": {},
8425
  "colab_type": "code",
@@ -8443,7 +8443,7 @@
8443
  },
8444
  {
8445
  "cell_type": "code",
8446
- "execution_count": 34,
8447
  "metadata": {},
8448
  "outputs": [],
8449
  "source": [
@@ -8466,7 +8466,7 @@
8466
  },
8467
  {
8468
  "cell_type": "code",
8469
- "execution_count": 35,
8470
  "metadata": {},
8471
  "outputs": [
8472
  {
@@ -8597,7 +8597,7 @@
8597
  "[21057 rows x 3 columns]"
8598
  ]
8599
  },
8600
- "execution_count": 35,
8601
  "metadata": {},
8602
  "output_type": "execute_result"
8603
  }
@@ -8667,7 +8667,7 @@
8667
  },
8668
  {
8669
  "cell_type": "code",
8670
- "execution_count": 44,
8671
  "metadata": {},
8672
  "outputs": [
8673
  {
@@ -8707,6 +8707,13 @@
8707
  " labels: null\n",
8708
  " batch_size: 128\n",
8709
  " shuffle: false\n",
 
 
 
 
 
 
 
8710
  " model_defaults:\n",
8711
  " filters: 1024\n",
8712
  " repeat: 3\n",
@@ -8849,21 +8856,9 @@
8849
  },
8850
  {
8851
  "cell_type": "code",
8852
- "execution_count": 1,
8853
  "metadata": {},
8854
- "outputs": [
8855
- {
8856
- "ename": "ModuleNotFoundError",
8857
- "evalue": "No module named 'nemo'",
8858
- "output_type": "error",
8859
- "traceback": [
8860
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
8861
- "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
8862
- "\u001b[1;32m/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb Cell 11\u001b[0m in \u001b[0;36m<cell line: 5>\u001b[0;34m()\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb#Y161sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39m# Fine-tune the model with Portuguese language\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb#Y161sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m \n\u001b[1;32m <a href='vscode-notebook-cell:/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb#Y161sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m \u001b[39m#import torch\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb#Y161sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m \u001b[39m#import pytorch_lightning as pl\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb#Y161sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mnemo\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb#Y161sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mnemo\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mcollections\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39masr\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mnemo_asr\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/fine_tune_tianet_tr_pt.ipynb#Y161sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39momegaconf\u001b[39;00m \u001b[39mimport\u001b[39;00m OmegaConf\n",
8863
- "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'nemo'"
8864
- ]
8865
- }
8866
- ],
8867
  "source": [
8868
  "# Fine-tune the model with Portuguese language\n",
8869
  "\n",
@@ -8871,1187 +8866,128 @@
8871
  "import pytorch_lightning as pl\n",
8872
  "import nemo\n",
8873
  "import nemo.collections.asr as nemo_asr\n",
8874
- "from omegaconf import OmegaConf"
8875
- ]
8876
- },
8877
- {
8878
- "cell_type": "code",
8879
- "execution_count": 2,
8880
- "metadata": {},
8881
- "outputs": [
8882
- {
8883
- "name": "stdout",
8884
- "output_type": "stream",
8885
- "text": [
8886
- "Collecting torch\n",
8887
- " Downloading torch-2.0.1-cp310-none-macosx_10_9_x86_64.whl (143.4 MB)\n",
8888
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.4/143.4 MB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
8889
- "\u001b[?25hCollecting torchvision\n",
8890
- " Downloading torchvision-0.15.2-cp310-cp310-macosx_10_9_x86_64.whl (1.5 MB)\n",
8891
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.5/1.5 MB\u001b[0m \u001b[31m16.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
8892
- "\u001b[?25hCollecting torchaudio\n",
8893
- " Downloading torchaudio-2.0.2-cp310-cp310-macosx_10_9_x86_64.whl (3.9 MB)\n",
8894
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.9/3.9 MB\u001b[0m \u001b[31m15.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
8895
- "\u001b[?25hRequirement already satisfied: filelock in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from torch) (3.7.1)\n",
8896
- "Requirement already satisfied: typing-extensions in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from torch) (4.3.0)\n",
8897
- "Collecting sympy (from torch)\n",
8898
- " Using cached sympy-1.12-py3-none-any.whl (5.7 MB)\n",
8899
- "Collecting networkx (from torch)\n",
8900
- " Using cached networkx-3.1-py3-none-any.whl (2.1 MB)\n",
8901
- "Requirement already satisfied: jinja2 in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from torch) (3.1.2)\n",
8902
- "Requirement already satisfied: numpy in /Users/Peng_Wei/miniconda3/envs/transcribe2/lib/python3.10/site-packages (from torchvision) (1.21.6)\n",
8903
- "Requirement already satisfied: requests in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from torchvision) (2.28.1)\n",
8904
- "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from torchvision) (9.2.0)\n",
8905
- "Requirement already satisfied: MarkupSafe>=2.0 in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from jinja2->torch) (2.1.1)\n",
8906
- "Requirement already satisfied: charset-normalizer<3,>=2 in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from requests->torchvision) (2.1.0)\n",
8907
- "Requirement already satisfied: idna<4,>=2.5 in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from requests->torchvision) (3.3)\n",
8908
- "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/Peng_Wei/.local/lib/python3.10/site-packages (from requests->torchvision) (1.26.11)\n",
8909
- "Collecting certifi>=2017.4.17 (from requests->torchvision)\n",
8910
- " Obtaining dependency information for certifi>=2017.4.17 from https://files.pythonhosted.org/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl.metadata\n",
8911
- " Downloading certifi-2023.7.22-py3-none-any.whl.metadata (2.2 kB)\n",
8912
- "Collecting mpmath>=0.19 (from sympy->torch)\n",
8913
- " Using cached mpmath-1.3.0-py3-none-any.whl (536 kB)\n",
8914
- "Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)\n",
8915
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.3/158.3 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
8916
- "\u001b[?25hInstalling collected packages: mpmath, sympy, networkx, certifi, torch, torchvision, torchaudio\n",
8917
- "Successfully installed certifi-2023.7.22 mpmath-1.3.0 networkx-3.1 sympy-1.12 torch-2.0.1 torchaudio-2.0.2 torchvision-0.15.2\n",
8918
- "Note: you may need to restart the kernel to use updated packages.\n"
8919
- ]
8920
- }
8921
- ],
8922
- "source": [
8923
- "%pip install torch torchvision torchaudio"
8924
- ]
8925
- },
8926
- {
8927
- "cell_type": "markdown",
8928
- "metadata": {
8929
- "colab_type": "text",
8930
- "id": "_LYwHAr1G8hp"
8931
- },
8932
- "source": [
8933
- "After the download and conversion, your `data` folder should contain directories with manifest files as:\n",
8934
- "\n",
8935
- "* `data/<path>/train.json`\n",
8936
- "* `data/<path>/dev.json` \n",
8937
- "* `data/<path>/train_all.json` \n",
8938
- "\n",
8939
- "Each line in the manifest file describes a training sample - `audio_filepath` contains the path to the wav file, `duration` it's duration in seconds, and `label` is the speaker class label:\n",
8940
- "\n",
8941
- "`{\"audio_filepath\": \"<absolute path to dataset>data/an4/wav/an4test_clstk/menk/cen4-menk-b.wav\", \"duration\": 3.9, \"label\": \"menk\"}` "
8942
- ]
8943
- },
8944
- {
8945
- "cell_type": "code",
8946
- "execution_count": 11,
8947
- "metadata": {
8948
- "colab": {},
8949
- "colab_type": "code",
8950
- "id": "mpAv77JoD98c"
8951
- },
8952
- "outputs": [
8953
- {
8954
- "name": "stdout",
8955
- "output_type": "stream",
8956
- "text": [
8957
- "Collecting librosa\n",
8958
- " Obtaining dependency information for librosa from https://files.pythonhosted.org/packages/e2/a2/4f639c1168d7aada749a896afb4892a831e2041bebdcf636aebfe9e86556/librosa-0.10.1-py3-none-any.whl.metadata\n",
8959
- " Using cached librosa-0.10.1-py3-none-any.whl.metadata (8.3 kB)\n",
8960
- "Collecting audioread>=2.1.9 (from librosa)\n",
8961
- " Using cached audioread-3.0.0.tar.gz (377 kB)\n",
8962
- " Preparing metadata (setup.py) ... \u001b[?25ldone\n",
8963
- "\u001b[?25hRequirement already satisfied: numpy!=1.22.0,!=1.22.1,!=1.22.2,>=1.20.3 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from librosa) (1.25.1)\n",
8964
- "Collecting scipy>=1.2.0 (from librosa)\n",
8965
- " Obtaining dependency information for scipy>=1.2.0 from https://files.pythonhosted.org/packages/1d/77/5e660d211906becd9f8e13e00d828f5e68b5e66d9b956f4646bb4882c68e/scipy-1.11.2-cp311-cp311-macosx_10_9_x86_64.whl.metadata\n",
8966
- " Using cached scipy-1.11.2-cp311-cp311-macosx_10_9_x86_64.whl.metadata (54 kB)\n",
8967
- "Collecting scikit-learn>=0.20.0 (from librosa)\n",
8968
- " Obtaining dependency information for scikit-learn>=0.20.0 from https://files.pythonhosted.org/packages/e8/dd/41bc4dfa519bc1a0617b68496120c472f1a1a5db264132d1530c43f059a8/scikit_learn-1.3.0-cp311-cp311-macosx_10_9_x86_64.whl.metadata\n",
8969
- " Using cached scikit_learn-1.3.0-cp311-cp311-macosx_10_9_x86_64.whl.metadata (11 kB)\n",
8970
- "Collecting joblib>=0.14 (from librosa)\n",
8971
- " Obtaining dependency information for joblib>=0.14 from https://files.pythonhosted.org/packages/10/40/d551139c85db202f1f384ba8bcf96aca2f329440a844f924c8a0040b6d02/joblib-1.3.2-py3-none-any.whl.metadata\n",
8972
- " Downloading joblib-1.3.2-py3-none-any.whl.metadata (5.4 kB)\n",
8973
- "Requirement already satisfied: decorator>=4.3.0 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from librosa) (5.1.1)\n",
8974
- "Collecting numba>=0.51.0 (from librosa)\n",
8975
- " Obtaining dependency information for numba>=0.51.0 from https://files.pythonhosted.org/packages/6c/80/4378109514d72efe552a8899392fa6526b48eeca43c12eac314bf0819bff/numba-0.57.1-cp311-cp311-macosx_10_9_x86_64.whl.metadata\n",
8976
- " Using cached numba-0.57.1-cp311-cp311-macosx_10_9_x86_64.whl.metadata (2.7 kB)\n",
8977
- "Collecting soundfile>=0.12.1 (from librosa)\n",
8978
- " Using cached soundfile-0.12.1-py2.py3-none-macosx_10_9_x86_64.whl (1.2 MB)\n",
8979
- "Collecting pooch>=1.0 (from librosa)\n",
8980
- " Downloading pooch-1.7.0-py3-none-any.whl (60 kB)\n",
8981
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.9/60.9 kB\u001b[0m \u001b[31m1.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
8982
- "\u001b[?25hCollecting soxr>=0.3.2 (from librosa)\n",
8983
- " Obtaining dependency information for soxr>=0.3.2 from https://files.pythonhosted.org/packages/59/46/cb9965a341570c697af1e8df305f4fd9b9a1b528abafc6d4abfbd43164df/soxr-0.3.6-cp311-cp311-macosx_10_9_x86_64.whl.metadata\n",
8984
- " Downloading soxr-0.3.6-cp311-cp311-macosx_10_9_x86_64.whl.metadata (5.4 kB)\n",
8985
- "Requirement already satisfied: typing-extensions>=4.1.1 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from librosa) (4.7.1)\n",
8986
- "Collecting lazy-loader>=0.1 (from librosa)\n",
8987
- " Obtaining dependency information for lazy-loader>=0.1 from https://files.pythonhosted.org/packages/a1/c3/65b3814e155836acacf720e5be3b5757130346670ac454fee29d3eda1381/lazy_loader-0.3-py3-none-any.whl.metadata\n",
8988
- " Using cached lazy_loader-0.3-py3-none-any.whl.metadata (4.3 kB)\n",
8989
- "Collecting msgpack>=1.0 (from librosa)\n",
8990
- " Using cached msgpack-1.0.5-cp311-cp311-macosx_10_9_x86_64.whl (73 kB)\n",
8991
- "Collecting llvmlite<0.41,>=0.40.0dev0 (from numba>=0.51.0->librosa)\n",
8992
- " Obtaining dependency information for llvmlite<0.41,>=0.40.0dev0 from https://files.pythonhosted.org/packages/2b/da/9de67270696d43ab28eba38c9a248f646e6b9a3fb2c7115504a2a986f55f/llvmlite-0.40.1-cp311-cp311-macosx_10_9_x86_64.whl.metadata\n",
8993
- " Downloading llvmlite-0.40.1-cp311-cp311-macosx_10_9_x86_64.whl.metadata (4.7 kB)\n",
8994
- "Collecting numpy!=1.22.0,!=1.22.1,!=1.22.2,>=1.20.3 (from librosa)\n",
8995
- " Obtaining dependency information for numpy!=1.22.0,!=1.22.1,!=1.22.2,>=1.20.3 from https://files.pythonhosted.org/packages/a9/cc/5ed2280a27e5dab12994c884f1f4d8c3bd4d885d02ae9e52a9d213a6a5e2/numpy-1.24.4-cp311-cp311-macosx_10_9_x86_64.whl.metadata\n",
8996
- " Downloading numpy-1.24.4-cp311-cp311-macosx_10_9_x86_64.whl.metadata (5.6 kB)\n",
8997
- "Requirement already satisfied: platformdirs>=2.5.0 in /Users/Peng_Wei/miniconda3/envs/transcribe/lib/python3.11/site-packages (from pooch>=1.0->librosa) (3.10.0)\n",
8998
- "Requirement already satisfied: packaging>=20.0 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from pooch>=1.0->librosa) (23.1)\n",
8999
- "Requirement already satisfied: requests>=2.19.0 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from pooch>=1.0->librosa) (2.31.0)\n",
9000
- "Collecting threadpoolctl>=2.0.0 (from scikit-learn>=0.20.0->librosa)\n",
9001
- " Obtaining dependency information for threadpoolctl>=2.0.0 from https://files.pythonhosted.org/packages/81/12/fd4dea011af9d69e1cad05c75f3f7202cdcbeac9b712eea58ca779a72865/threadpoolctl-3.2.0-py3-none-any.whl.metadata\n",
9002
- " Downloading threadpoolctl-3.2.0-py3-none-any.whl.metadata (10.0 kB)\n",
9003
- "Collecting cffi>=1.0 (from soundfile>=0.12.1->librosa)\n",
9004
- " Using cached cffi-1.15.1-cp311-cp311-macosx_10_9_x86_64.whl (179 kB)\n",
9005
- "Collecting pycparser (from cffi>=1.0->soundfile>=0.12.1->librosa)\n",
9006
- " Using cached pycparser-2.21-py2.py3-none-any.whl (118 kB)\n",
9007
- "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (3.2.0)\n",
9008
- "Requirement already satisfied: idna<4,>=2.5 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (3.4)\n",
9009
- "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (2.0.3)\n",
9010
- "Requirement already satisfied: certifi>=2017.4.17 in /Users/Peng_Wei/.local/lib/python3.11/site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (2023.5.7)\n",
9011
- "Downloading librosa-0.10.1-py3-none-any.whl (253 kB)\n",
9012
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m253.7/253.7 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
9013
- "\u001b[?25hDownloading joblib-1.3.2-py3-none-any.whl (302 kB)\n",
9014
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m302.2/302.2 kB\u001b[0m \u001b[31m9.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
9015
- "\u001b[?25hUsing cached lazy_loader-0.3-py3-none-any.whl (9.1 kB)\n",
9016
- "Downloading numba-0.57.1-cp311-cp311-macosx_10_9_x86_64.whl (2.5 MB)\n",
9017
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m19.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
9018
- "\u001b[?25hDownloading numpy-1.24.4-cp311-cp311-macosx_10_9_x86_64.whl (19.8 MB)\n",
9019
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.8/19.8 MB\u001b[0m \u001b[31m9.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m0:01\u001b[0mm\n",
9020
- "\u001b[?25hUsing cached scikit_learn-1.3.0-cp311-cp311-macosx_10_9_x86_64.whl (10.1 MB)\n",
9021
- "Downloading scipy-1.11.2-cp311-cp311-macosx_10_9_x86_64.whl (37.0 MB)\n",
9022
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m37.0/37.0 MB\u001b[0m \u001b[31m8.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0mm\n",
9023
- "\u001b[?25hDownloading soxr-0.3.6-cp311-cp311-macosx_10_9_x86_64.whl (409 kB)\n",
9024
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m409.2/409.2 kB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n",
9025
- "\u001b[?25hDownloading llvmlite-0.40.1-cp311-cp311-macosx_10_9_x86_64.whl (30.4 MB)\n",
9026
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m30.4/30.4 MB\u001b[0m \u001b[31m10.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
9027
- "\u001b[?25hDownloading threadpoolctl-3.2.0-py3-none-any.whl (15 kB)\n",
9028
- "Building wheels for collected packages: audioread\n",
9029
- " Building wheel for audioread (setup.py) ... \u001b[?25ldone\n",
9030
- "\u001b[?25h Created wheel for audioread: filename=audioread-3.0.0-py3-none-any.whl size=23703 sha256=80250ff87c2643525d550b8226f4be3c824d18270fb426ad2b2a580d61e9e8ff\n",
9031
- " Stored in directory: /Users/Peng_Wei/Library/Caches/pip/wheels/34/6d/02/40eff8045c8287806e7d83333301c1c7bab6109045424e8558\n",
9032
- "Successfully built audioread\n",
9033
- "Installing collected packages: msgpack, threadpoolctl, pycparser, numpy, llvmlite, lazy-loader, joblib, audioread, soxr, scipy, pooch, numba, cffi, soundfile, scikit-learn, librosa\n",
9034
- " Attempting uninstall: numpy\n",
9035
- " Found existing installation: numpy 1.25.1\n",
9036
- " Uninstalling numpy-1.25.1:\n",
9037
- " Successfully uninstalled numpy-1.25.1\n",
9038
- "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
9039
- "presidio-analyzer 2.2.33 requires phonenumbers>=8.12, which is not installed.\u001b[0m\u001b[31m\n",
9040
- "\u001b[0mSuccessfully installed audioread-3.0.0 cffi-1.15.1 joblib-1.3.2 lazy-loader-0.3 librosa-0.10.1 llvmlite-0.40.1 msgpack-1.0.5 numba-0.57.1 numpy-1.24.4 pooch-1.7.0 pycparser-2.21 scikit-learn-1.3.0 scipy-1.11.2 soundfile-0.12.1 soxr-0.3.6 threadpoolctl-3.2.0\n",
9041
- "Traceback (most recent call last):\n",
9042
- " File \"/Users/Peng_Wei/work/mlrun_related/en_tr_pt_titanet_large/scripts/speaker_tasks/filelist_to_manifest.py\", line 43, in <module>\n",
9043
- " import sox\n",
9044
- "ModuleNotFoundError: No module named 'sox'\n"
9045
- ]
9046
- }
9047
- ],
9048
- "source": [
9049
- "!pip install librosa\n",
9050
- "\n",
9051
- "if not os.path.exists('scripts'):\n",
9052
- " print(\"Downloading necessary scripts\")\n",
9053
- " !mkdir -p scripts/speaker_tasks\n",
9054
- " !wget -P scripts/speaker_tasks/ https://raw.githubusercontent.com/NVIDIA/NeMo/$BRANCH/scripts/speaker_tasks/filelist_to_manifest.py\n",
9055
- "!python {NEMO_ROOT}/scripts/speaker_tasks/filelist_to_manifest.py --filelist {data_dir}/an4/wav/an4_clstk/train_all.txt --id -2 --out {data_dir}/an4/wav/an4_clstk/all_manifest.json --split"
9056
- ]
9057
- },
9058
- {
9059
- "cell_type": "markdown",
9060
- "metadata": {
9061
- "colab_type": "text",
9062
- "id": "5kPCmx5DHvY5"
9063
- },
9064
- "source": [
9065
- "Generate the list text file for the test folder and then convert it to a manifest."
9066
- ]
9067
- },
9068
- {
9069
- "cell_type": "code",
9070
- "execution_count": null,
9071
- "metadata": {
9072
- "colab": {},
9073
- "colab_type": "code",
9074
- "id": "nMd24GVaFBwr"
9075
- },
9076
- "outputs": [],
9077
- "source": [
9078
- "!find {data_dir}/an4/wav/an4test_clstk -iname \"*.wav\" > {data_dir}/an4/wav/an4test_clstk/test_all.txt\n",
9079
- "!python {NEMO_ROOT}/scripts/speaker_tasks/filelist_to_manifest.py --filelist {data_dir}/an4/wav/an4test_clstk/test_all.txt --id -2 --out {data_dir}/an4/wav/an4test_clstk/test.json"
9080
- ]
9081
- },
9082
- {
9083
- "cell_type": "markdown",
9084
- "metadata": {
9085
- "colab_type": "text",
9086
- "id": "H5FPmxUkGakD"
9087
- },
9088
- "source": [
9089
- "## Path to manifest files\n"
9090
  ]
9091
  },
9092
  {
9093
  "cell_type": "code",
9094
- "execution_count": null,
9095
- "metadata": {
9096
- "colab": {},
9097
- "colab_type": "code",
9098
- "id": "vo-VnYPtJO_v"
9099
- },
9100
- "outputs": [],
9101
- "source": [
9102
- "train_manifest = os.path.join(data_dir,'an4/wav/an4_clstk/train.json')\n",
9103
- "validation_manifest = os.path.join(data_dir,'an4/wav/an4_clstk/dev.json')\n",
9104
- "test_manifest = os.path.join(data_dir,'an4/wav/an4_clstk/dev.json')"
9105
- ]
9106
- },
9107
- {
9108
- "cell_type": "markdown",
9109
- "metadata": {
9110
- "colab_type": "text",
9111
- "id": "KyDVdtjAL2__"
9112
- },
9113
- "source": [
9114
- "As the goal of most speaker-related systems is to get good speaker level embeddings that could help distinguish from\n",
9115
- "other speakers, we shall first train these embeddings in an end-to-end\n",
9116
- "manner optimizing the [TitaNet](https://arxiv.org/pdf/2110.04410.pdf) model.\n",
9117
- "We modify the decoder to get these fixed-size embeddings irrespective of the length of the input audio."
9118
- ]
9119
- },
9120
- {
9121
- "cell_type": "markdown",
9122
- "metadata": {
9123
- "colab_type": "text",
9124
- "id": "OJtU_GEdMUUo"
9125
- },
9126
- "source": [
9127
- "# Training\n",
9128
- "Import necessary packages"
9129
- ]
9130
- },
9131
- {
9132
- "cell_type": "markdown",
9133
  "metadata": {},
9134
- "source": [
9135
- "Note: All the following steps are just for explanation of each section, but one can use the provided [training script](https://github.com/NVIDIA/NeMo/blob/main/examples/speaker_tasks/recognition/speaker_reco.py) to launch training in the command line."
9136
- ]
9137
- },
9138
- {
9139
- "cell_type": "code",
9140
- "execution_count": null,
9141
- "metadata": {
9142
- "colab": {},
9143
- "colab_type": "code",
9144
- "id": "o1ojB0cZMSmv"
9145
- },
9146
  "outputs": [],
9147
  "source": [
9148
- "import nemo\n",
9149
- "# NeMo's ASR collection - This collection contains complete ASR models and\n",
9150
- "# building blocks (modules) for ASR\n",
9151
- "import nemo.collections.asr as nemo_asr\n",
9152
- "from omegaconf import OmegaConf"
9153
- ]
9154
- },
9155
- {
9156
- "cell_type": "markdown",
9157
- "metadata": {
9158
- "colab_type": "text",
9159
- "id": "m5Zho11LNAFJ"
9160
- },
9161
- "source": [
9162
- "## Model Configuration \n",
9163
- "The TitaNet model is defined in a config file which declares multiple important sections.\n",
9164
- "\n",
9165
- "They are:\n",
9166
- "\n",
9167
- "1) model: All arguments that will relate to the Model - preprocessors, encoder, decoder, optimizer and schedulers, datasets, and any other related information\n",
9168
  "\n",
9169
- "2) trainer: Any argument to be passed to PyTorch Lightning"
 
 
 
 
 
 
 
 
 
 
 
 
 
9170
  ]
9171
  },
9172
  {
9173
  "cell_type": "code",
9174
  "execution_count": null,
9175
- "metadata": {
9176
- "colab": {},
9177
- "colab_type": "code",
9178
- "id": "6HQtZfKnMhpI"
9179
- },
9180
  "outputs": [],
9181
  "source": [
9182
- "# This line will print the entire config of sample TitaNet model\n",
9183
- "!mkdir conf \n",
9184
- "!wget -P conf https://raw.githubusercontent.com/NVIDIA/NeMo/$BRANCH/examples/speaker_tasks/recognition/conf/titanet-large.yaml\n",
9185
- "MODEL_CONFIG = os.path.join(NEMO_ROOT,'conf/titanet-large.yaml')\n",
9186
- "config = OmegaConf.load(MODEL_CONFIG)\n",
9187
- "print(OmegaConf.to_yaml(config))"
9188
- ]
9189
- },
9190
- {
9191
- "cell_type": "markdown",
9192
- "metadata": {
9193
- "colab_type": "text",
9194
- "id": "HtbXN-cFOwxi"
9195
- },
9196
- "source": [
9197
- "## Setting up the datasets within the config\n",
9198
- "If you'll notice, there are a few config dictionaries called train_ds, validation_ds and test_ds. These are configurations used to setup the Dataset and DataLoaders of the corresponding config."
9199
  ]
9200
  },
9201
  {
9202
  "cell_type": "code",
9203
- "execution_count": null,
9204
- "metadata": {
9205
- "colab": {},
9206
- "colab_type": "code",
9207
- "id": "NPBIf1jmNgjn"
9208
- },
9209
  "outputs": [],
9210
  "source": [
9211
- "print(OmegaConf.to_yaml(config.model.train_ds))\n",
9212
- "print(OmegaConf.to_yaml(config.model.validation_ds))"
9213
- ]
9214
- },
9215
- {
9216
- "cell_type": "markdown",
9217
- "metadata": {
9218
- "colab_type": "text",
9219
- "id": "PLIjKOMUP0YE"
9220
- },
9221
- "source": [
9222
- "You will often notice that some configs have ??? in place of paths. This is used as a placeholder so that the user can change the value at a later time.\n",
9223
- "\n",
9224
- "Let's add the paths to the manifests to the config above\n",
9225
- "Also, since an4 dataset doesn't have a test set of the same speakers used in training, we will use validation manifest as test manifest for demonstration purposes"
9226
  ]
9227
  },
9228
  {
9229
  "cell_type": "code",
9230
  "execution_count": null,
9231
- "metadata": {
9232
- "colab": {},
9233
- "colab_type": "code",
9234
- "id": "TSotpjL_O2BN"
9235
- },
9236
- "outputs": [],
9237
- "source": [
9238
- "config.model.train_ds.manifest_filepath = train_manifest\n",
9239
- "config.model.validation_ds.manifest_filepath = validation_manifest"
9240
- ]
9241
- },
9242
- {
9243
- "cell_type": "markdown",
9244
  "metadata": {},
9245
- "source": [
9246
- "Note: Since we are training speaker embedding extractor model for verification we do not add test_ds dataset. To include it add it to config and replace manifest file as \n",
9247
- "`config.model.test_ds.manifest_filepath = test_manifest`"
9248
- ]
9249
- },
9250
- {
9251
- "cell_type": "markdown",
9252
- "metadata": {
9253
- "colab_type": "text",
9254
- "id": "xy6_Lf6fW9aJ"
9255
- },
9256
- "source": [
9257
- "Also as we are training on an4 dataset, there are 74 speaker labels in training, and we need to set this in the decoder config"
9258
- ]
9259
- },
9260
- {
9261
- "cell_type": "code",
9262
- "execution_count": null,
9263
- "metadata": {
9264
- "colab": {},
9265
- "colab_type": "code",
9266
- "id": "-B96tFTnW8Yh"
9267
- },
9268
  "outputs": [],
9269
  "source": [
9270
- "config.model.decoder.num_classes = 74"
9271
- ]
9272
- },
9273
- {
9274
- "cell_type": "markdown",
9275
- "metadata": {
9276
- "colab_type": "text",
9277
- "id": "83pHBRDpQTF0"
9278
- },
9279
- "source": [
9280
- "## Building the PyTorch Lightning Trainer\n",
9281
- "NeMo models are primarily PyTorch Lightning modules - and therefore are entirely compatible with the PyTorch Lightning ecosystem!\n",
9282
  "\n",
9283
- "Let us first instantiate a Trainer object!"
9284
- ]
9285
- },
9286
- {
9287
- "cell_type": "code",
9288
- "execution_count": null,
9289
- "metadata": {
9290
- "colab": {},
9291
- "colab_type": "code",
9292
- "id": "GWzGJoHMQQnG"
9293
- },
9294
- "outputs": [],
9295
- "source": [
9296
- "import torch\n",
9297
- "import pytorch_lightning as pl"
9298
  ]
9299
  },
9300
  {
9301
  "cell_type": "code",
9302
  "execution_count": null,
9303
- "metadata": {
9304
- "colab": {},
9305
- "colab_type": "code",
9306
- "id": "WIYf4-KFQYHl"
9307
- },
9308
  "outputs": [],
9309
  "source": [
9310
- "print(\"Trainer config - \\n\")\n",
9311
- "print(OmegaConf.to_yaml(config.trainer))"
9312
  ]
9313
  },
9314
  {
9315
  "cell_type": "code",
9316
  "execution_count": null,
9317
- "metadata": {
9318
- "colab": {},
9319
- "colab_type": "code",
9320
- "id": "aXuSMYMNQeW7"
9321
- },
9322
  "outputs": [],
9323
  "source": [
9324
- "# Let us modify some trainer configs for this demo\n",
9325
- "# Checks if we have GPU available and uses it\n",
 
9326
  "accelerator = 'gpu' if torch.cuda.is_available() else 'cpu'\n",
9327
- "config.trainer.devices = 1\n",
9328
- "config.trainer.accelerator = accelerator\n",
9329
  "\n",
9330
- "# Reduces maximum number of epochs to 5 for quick demonstration\n",
9331
- "config.trainer.max_epochs = 10\n",
 
 
 
 
 
 
 
 
 
 
 
 
9332
  "\n",
9333
- "# Remove distributed training flags\n",
9334
- "config.trainer.strategy = None\n",
9335
  "\n",
9336
- "# Remove augmentations\n",
9337
- "config.model.train_ds.augmentor=None"
9338
- ]
9339
- },
9340
- {
9341
- "cell_type": "code",
9342
- "execution_count": null,
9343
- "metadata": {
9344
- "colab": {},
9345
- "colab_type": "code",
9346
- "id": "pBq3eCLwQhCy"
9347
- },
9348
- "outputs": [],
9349
- "source": [
9350
- "trainer = pl.Trainer(**config.trainer)"
9351
- ]
9352
- },
9353
- {
9354
- "cell_type": "markdown",
9355
- "metadata": {
9356
- "colab_type": "text",
9357
- "id": "-xHq_rcmQiry"
9358
- },
9359
- "source": [
9360
- "## Setting up a NeMo Experiment\n",
9361
- "NeMo has an experiment manager that handles logging and checkpointing for us, so let's use it !"
9362
- ]
9363
- },
9364
- {
9365
- "cell_type": "code",
9366
- "execution_count": null,
9367
- "metadata": {
9368
- "colab": {},
9369
- "colab_type": "code",
9370
- "id": "DMm8MPYfQsCS"
9371
- },
9372
- "outputs": [],
9373
- "source": [
9374
- "from nemo.utils.exp_manager import exp_manager\n",
9375
- "log_dir = exp_manager(trainer, config.get(\"exp_manager\", None))\n",
9376
- "# The log_dir provides a path to the current logging directory for easy access\n",
9377
- "print(log_dir)"
9378
  ]
9379
- },
9380
- {
9381
- "cell_type": "markdown",
9382
- "metadata": {
9383
- "colab_type": "text",
9384
- "id": "nQQMlXmLQ7h1"
9385
- },
9386
- "source": [
9387
- "## Building the TitaNet Model\n",
9388
- "TitaNet is a speaker embedding extractor model that can be used for speaker identification tasks - it generates one label for the entire provided audio stream. Therefore we encapsulate it inside the EncDecSpeakerLabelModel as follows."
9389
- ]
9390
- },
9391
- {
9392
- "cell_type": "code",
9393
- "execution_count": null,
9394
- "metadata": {
9395
- "colab": {},
9396
- "colab_type": "code",
9397
- "id": "E_KY_s5LROYf"
9398
- },
9399
- "outputs": [],
9400
- "source": [
9401
- "speaker_model = nemo_asr.models.EncDecSpeakerLabelModel(cfg=config.model, trainer=trainer)"
9402
- ]
9403
- },
9404
- {
9405
- "cell_type": "markdown",
9406
- "metadata": {
9407
- "colab_type": "text",
9408
- "id": "_AphpMhkSVdU"
9409
- },
9410
- "source": [
9411
- "Before we begin training, let us first create a Tensorboard visualization to monitor progress"
9412
- ]
9413
- },
9414
- {
9415
- "cell_type": "code",
9416
- "execution_count": null,
9417
- "metadata": {
9418
- "colab": {},
9419
- "colab_type": "code",
9420
- "id": "BUnDpe_5SbDR"
9421
- },
9422
- "outputs": [],
9423
- "source": [
9424
- "try:\n",
9425
- " from google import colab\n",
9426
- " COLAB_ENV = True\n",
9427
- "except (ImportError, ModuleNotFoundError):\n",
9428
- " COLAB_ENV = False\n",
9429
- "\n",
9430
- "# Load the TensorBoard notebook extension\n",
9431
- "if COLAB_ENV:\n",
9432
- " %load_ext tensorboard\n",
9433
- " %tensorboard --logdir {exp_dir}\n",
9434
- "else:\n",
9435
- " print(\"To use tensorboard, please use this notebook in a Google Colab environment.\")"
9436
- ]
9437
- },
9438
- {
9439
- "cell_type": "markdown",
9440
- "metadata": {
9441
- "colab_type": "text",
9442
- "id": "Or8g1cksSf8C"
9443
- },
9444
- "source": [
9445
- "As any NeMo model is inherently a PyTorch Lightning Model, it can easily be trained in a single line - trainer.fit(model)!\n",
9446
- "Below we see that the model begins to get modest scores on the validation set after just 5 epochs of training"
9447
- ]
9448
- },
9449
- {
9450
- "cell_type": "code",
9451
- "execution_count": null,
9452
- "metadata": {
9453
- "colab": {},
9454
- "colab_type": "code",
9455
- "id": "HvYhsOWuSpL_",
9456
- "scrolled": false
9457
- },
9458
- "outputs": [],
9459
- "source": [
9460
- "trainer.fit(speaker_model)"
9461
- ]
9462
- },
9463
- {
9464
- "cell_type": "markdown",
9465
- "metadata": {
9466
- "colab_type": "text",
9467
- "id": "lSRACGt3UAYn"
9468
- },
9469
- "source": [
9470
- "This config is not suited and designed for an4 so you may observe unstable val_loss"
9471
- ]
9472
- },
9473
- {
9474
- "cell_type": "markdown",
9475
- "metadata": {
9476
- "colab_type": "text",
9477
- "id": "jvtVKO8FZsoe"
9478
- },
9479
- "source": [
9480
- "If you have a test manifest file, we can easily compute test accuracy by running\n",
9481
- "<pre><code>trainer.test(speaker_model, ckpt_path=None)\n",
9482
- "</code></pre>\n"
9483
- ]
9484
- },
9485
- {
9486
- "cell_type": "markdown",
9487
- "metadata": {
9488
- "colab_type": "text",
9489
- "id": "FlBwMsRdZfqg"
9490
- },
9491
- "source": [
9492
- "## For Faster Training\n",
9493
- "We can dramatically improve the time taken to train this model by using Multi GPU training along with Mixed Precision.\n",
9494
- "\n",
9495
- "### Trainer with a distributed backend:\n",
9496
- "<pre><code>trainer = Trainer(devices=2, num_nodes=2, accelerator='gpu', strategy='dp')\n",
9497
- "</code></pre>\n",
9498
- "\n",
9499
- "### Mixed precision:\n",
9500
- "<pre><code>trainer = Trainer(amp_level='O1', precision=16)\n",
9501
- "</code></pre>\n",
9502
- "\n",
9503
- "Of course, you can combine these flags as well."
9504
- ]
9505
- },
9506
- {
9507
- "cell_type": "markdown",
9508
- "metadata": {
9509
- "colab_type": "text",
9510
- "id": "XcnWub9-0TW2"
9511
- },
9512
- "source": [
9513
- "## Saving/Restoring a checkpoint\n",
9514
- "There are multiple ways to save and load models in NeMo. Since all NeMo models are inherently Lightning Modules, we can use the standard way that PyTorch Lightning saves and restores models.\n",
9515
- "\n",
9516
- "NeMo also provides a more advanced model save/restore format, which encapsulates all the parts of the model that are required to restore that model for immediate use.\n",
9517
- "\n",
9518
- "In this example, we will explore both ways of saving and restoring models, but we will focus on the PyTorch Lightning method.\n",
9519
- "\n",
9520
- "## Saving and Restoring via PyTorch Lightning Checkpoints\n",
9521
- "When using NeMo for training, it is advisable to utilize the exp_manager framework. It is tasked with handling checkpointing and logging (Tensorboard as well as WandB optionally!), as well as dealing with multi-node and multi-GPU logging.\n",
9522
- "\n",
9523
- "Since we utilized the exp_manager framework above, we have access to the directory where the checkpoints exist.\n",
9524
- "\n",
9525
- "exp_manager with the default settings will save multiple checkpoints for us -\n",
9526
- "\n",
9527
- "1) A few checkpoints from certain steps of training. They will have --val_loss= tags\n",
9528
- "\n",
9529
- "2) Checkpoints at the last epoch of training are denoted by --last.\n",
9530
- "\n",
9531
- "3) If the model finishes training, it will also have a --last checkpoint."
9532
- ]
9533
- },
9534
- {
9535
- "cell_type": "code",
9536
- "execution_count": null,
9537
- "metadata": {
9538
- "colab": {},
9539
- "colab_type": "code",
9540
- "id": "QSLjq-edaPt_"
9541
- },
9542
- "outputs": [],
9543
- "source": [
9544
- "# Let us list all the checkpoints we have\n",
9545
- "checkpoint_dir = os.path.join(log_dir, 'checkpoints')\n",
9546
- "checkpoint_paths = list(glob.glob(os.path.join(checkpoint_dir, \"*.ckpt\")))\n",
9547
- "checkpoint_paths"
9548
- ]
9549
- },
9550
- {
9551
- "cell_type": "code",
9552
- "execution_count": null,
9553
- "metadata": {
9554
- "colab": {},
9555
- "colab_type": "code",
9556
- "id": "BwltdVWXaroa"
9557
- },
9558
- "outputs": [],
9559
- "source": [
9560
- "final_checkpoint = list(filter(lambda x: \"-last.ckpt\" in x, checkpoint_paths))[0]\n",
9561
- "print(final_checkpoint)"
9562
- ]
9563
- },
9564
- {
9565
- "cell_type": "markdown",
9566
- "metadata": {
9567
- "colab_type": "text",
9568
- "id": "1tGKKojs0fEh"
9569
- },
9570
- "source": [
9571
- "\n",
9572
- "## Restoring from a PyTorch Lightning checkpoint\n",
9573
- "To restore a model using the LightningModule.load_from_checkpoint() class method."
9574
- ]
9575
- },
9576
- {
9577
- "cell_type": "code",
9578
- "execution_count": null,
9579
- "metadata": {
9580
- "colab": {},
9581
- "colab_type": "code",
9582
- "id": "EgyP9cYVbFc8"
9583
- },
9584
- "outputs": [],
9585
- "source": [
9586
- "restored_model = nemo_asr.models.EncDecSpeakerLabelModel.load_from_checkpoint(final_checkpoint)"
9587
- ]
9588
- },
9589
- {
9590
- "cell_type": "markdown",
9591
- "metadata": {
9592
- "colab_type": "text",
9593
- "id": "AnZVMKZpbI_M"
9594
- },
9595
- "source": [
9596
- "# Finetuning\n",
9597
- "Since we don't have any new manifest file to finetune, I will demonstrate here by using the test manifest file we created earlier. \n",
9598
- "an4 test dataset has a different set of speakers from the train set (total number: 10). And as we didn't split this dataset for validation I will use the same for validation. "
9599
- ]
9600
- },
9601
- {
9602
- "cell_type": "markdown",
9603
- "metadata": {
9604
- "colab_type": "text",
9605
- "id": "kV9gInFwQ2F5"
9606
- },
9607
- "source": [
9608
- "There are a couple of ways we can finetune a speaker recognition model. \n",
9609
- "1. Finetuning using a pretrained model published on NGC. \n",
9610
- "2. Finetuning from a PTL checkpoint. \n",
9611
- "\n",
9612
- "Since finetuning from a large pretrained model is more common, I shall use it to demonstrate finetuning procedure. In order to make finetuning step independent from training from scratch, we use another config. Here we shall use `titanet-finetune.yaml` config, that is created to show finetuning on pretrained titanet-large model. "
9613
- ]
9614
- },
9615
- {
9616
- "cell_type": "markdown",
9617
- "metadata": {},
9618
- "source": [
9619
- "Note: You may use [finetune-script](https://github.com/NVIDIA/NeMo/blob/main/examples/speaker_tasks/recognition/speaker_reco_finetune.py) to launch training in the command line. Following is just a demonstration of the script"
9620
- ]
9621
- },
9622
- {
9623
- "cell_type": "code",
9624
- "execution_count": null,
9625
- "metadata": {},
9626
- "outputs": [],
9627
- "source": [
9628
- "!wget -P conf https://raw.githubusercontent.com/NVIDIA/NeMo/$BRANCH/examples/speaker_tasks/recognition/conf/titanet-finetune.yaml\n",
9629
- "MODEL_CONFIG = os.path.join(NEMO_ROOT,'conf/titanet-finetune.yaml')\n",
9630
- "finetune_config = OmegaConf.load(MODEL_CONFIG)\n",
9631
- "print(OmegaConf.to_yaml(finetune_config))"
9632
- ]
9633
- },
9634
- {
9635
- "cell_type": "markdown",
9636
- "metadata": {},
9637
- "source": [
9638
- "For step 2, if one would like to finetune from a PTL checkpoint, `init_from_pretrained_model` in config should be replaced with `init_from_nemo_model` and need to provide the path to checkpoint. "
9639
- ]
9640
- },
9641
- {
9642
- "cell_type": "code",
9643
- "execution_count": null,
9644
- "metadata": {
9645
- "colab": {},
9646
- "colab_type": "code",
9647
- "id": "HtXUWmYLQ0PJ"
9648
- },
9649
- "outputs": [],
9650
- "source": [
9651
- "test_manifest = os.path.join(data_dir,'an4/wav/an4test_clstk/test.json')\n",
9652
- "finetune_config.model.train_ds.manifest_filepath = test_manifest\n",
9653
- "finetune_config.model.validation_ds.manifest_filepath = test_manifest\n",
9654
- "finetune_config.model.decoder.num_classes = 10"
9655
- ]
9656
- },
9657
- {
9658
- "cell_type": "markdown",
9659
- "metadata": {
9660
- "colab_type": "text",
9661
- "id": "IHy1zE1cTDZn"
9662
- },
9663
- "source": [
9664
- "So we have set up the data and changed the decoder required for finetune, we now just need to create a trainer and start training with a smaller learning rate for fewer epochs"
9665
- ]
9666
- },
9667
- {
9668
- "cell_type": "code",
9669
- "execution_count": null,
9670
- "metadata": {
9671
- "colab": {},
9672
- "colab_type": "code",
9673
- "id": "nBmF6tQITSRl"
9674
- },
9675
- "outputs": [],
9676
- "source": [
9677
- "# Setup the new trainer object\n",
9678
- "# Let us modify some trainer configs for this demo\n",
9679
- "# Checks if we have GPU available and uses it\n",
9680
- "accelerator = 'gpu' if torch.cuda.is_available() else 'cpu'\n",
9681
- "\n",
9682
- "trainer_config = OmegaConf.create(dict(\n",
9683
- " devices=1,\n",
9684
- " accelerator=accelerator,\n",
9685
- " max_epochs=5,\n",
9686
- " max_steps=-1, # computed at runtime if not set\n",
9687
- " num_nodes=1,\n",
9688
- " accumulate_grad_batches=1,\n",
9689
- " enable_checkpointing=False, # Provided by exp_manager\n",
9690
- " logger=False, # Provided by exp_manager\n",
9691
- " log_every_n_steps=1, # Interval of logging.\n",
9692
- " val_check_interval=1.0, # Set to 0.25 to check 4 times per epoch, or an int for number of iterations\n",
9693
- "))\n",
9694
- "print(OmegaConf.to_yaml(trainer_config))"
9695
- ]
9696
- },
9697
- {
9698
- "cell_type": "code",
9699
- "execution_count": null,
9700
- "metadata": {
9701
- "colab": {},
9702
- "colab_type": "code",
9703
- "id": "bRz-8-xzUHKZ"
9704
- },
9705
- "outputs": [],
9706
- "source": [
9707
- "trainer_finetune = pl.Trainer(**trainer_config)"
9708
- ]
9709
- },
9710
- {
9711
- "cell_type": "markdown",
9712
- "metadata": {
9713
- "colab_type": "text",
9714
- "id": "EOwHTkW-UUy8"
9715
- },
9716
- "source": [
9717
- "## Setting the trainer to the restored model\n",
9718
- "Setting the trainer to the restored model"
9719
- ]
9720
- },
9721
- {
9722
- "cell_type": "code",
9723
- "execution_count": null,
9724
- "metadata": {
9725
- "colab": {},
9726
- "colab_type": "code",
9727
- "id": "0FhYQQQOUPIk"
9728
- },
9729
- "outputs": [],
9730
- "source": [
9731
- "log_dir_finetune = exp_manager(trainer_finetune, config.get(\"exp_manager\", None))\n",
9732
- "print(log_dir_finetune)"
9733
- ]
9734
- },
9735
- {
9736
- "cell_type": "markdown",
9737
- "metadata": {
9738
- "colab_type": "text",
9739
- "id": "lc3fzGYVVTyi"
9740
- },
9741
- "source": [
9742
- "## Fine-tune training step\n",
9743
- "\n",
9744
- "When fine-tuning on a truly new dataset, we will not see such a dramatic improvement in performance. However, it should still converge a little faster than if it was trained from scratch."
9745
- ]
9746
- },
9747
- {
9748
- "cell_type": "code",
9749
- "execution_count": null,
9750
- "metadata": {},
9751
- "outputs": [],
9752
- "source": [
9753
- "speaker_model = nemo_asr.models.EncDecSpeakerLabelModel(cfg=finetune_config.model, trainer=trainer_finetune)\n",
9754
- "speaker_model.maybe_init_from_pretrained_checkpoint(finetune_config)"
9755
- ]
9756
- },
9757
- {
9758
- "cell_type": "markdown",
9759
- "metadata": {},
9760
- "source": [
9761
- "In the config, we keep weights of preprocessor and encoder, and attach a new decoder as mentioned in above section to match num of classes of new data"
9762
- ]
9763
- },
9764
- {
9765
- "cell_type": "code",
9766
- "execution_count": null,
9767
- "metadata": {
9768
- "colab": {},
9769
- "colab_type": "code",
9770
- "id": "uFIOsuFYVLzr"
9771
- },
9772
- "outputs": [],
9773
- "source": [
9774
- "## Fine-tuning for 5 epochs¶\n",
9775
- "trainer_finetune.fit(speaker_model)"
9776
- ]
9777
- },
9778
- {
9779
- "cell_type": "markdown",
9780
- "metadata": {},
9781
- "source": [
9782
- "Tip: Add more data augmentation and dropout while finetuning on your data"
9783
- ]
9784
- },
9785
- {
9786
- "cell_type": "markdown",
9787
- "metadata": {
9788
- "colab_type": "text",
9789
- "id": "5DNidtl4VplU"
9790
- },
9791
- "source": [
9792
- "# Saving .nemo file\n",
9793
- "Now we can save the whole config and model parameters in a single .nemo and we can anytime restore from it"
9794
- ]
9795
- },
9796
- {
9797
- "cell_type": "code",
9798
- "execution_count": null,
9799
- "metadata": {
9800
- "colab": {},
9801
- "colab_type": "code",
9802
- "id": "am5wej6-VdZW"
9803
- },
9804
- "outputs": [],
9805
- "source": [
9806
- "restored_model.save_to(os.path.join(log_dir_finetune, '..',\"titanet-large-finetune.nemo\"))"
9807
- ]
9808
- },
9809
- {
9810
- "cell_type": "code",
9811
- "execution_count": null,
9812
- "metadata": {
9813
- "colab": {},
9814
- "colab_type": "code",
9815
- "id": "WnBhFJefV-Pf"
9816
- },
9817
- "outputs": [],
9818
- "source": [
9819
- "!ls {log_dir_finetune}/.."
9820
- ]
9821
- },
9822
- {
9823
- "cell_type": "code",
9824
- "execution_count": null,
9825
- "metadata": {
9826
- "colab": {},
9827
- "colab_type": "code",
9828
- "id": "kVx1hNP_V_iz"
9829
- },
9830
- "outputs": [],
9831
- "source": [
9832
- "# restore from a save model\n",
9833
- "restored_model = nemo_asr.models.EncDecSpeakerLabelModel.restore_from(os.path.join(log_dir_finetune, '..', \"titanet-large-finetune.nemo\"))\n"
9834
- ]
9835
- },
9836
- {
9837
- "cell_type": "markdown",
9838
- "metadata": {
9839
- "colab_type": "text",
9840
- "id": "80tLWTN40uaB"
9841
- },
9842
- "source": [
9843
- "# Speaker Verification"
9844
- ]
9845
- },
9846
- {
9847
- "cell_type": "markdown",
9848
- "metadata": {
9849
- "colab_type": "text",
9850
- "id": "VciRUIRz0y6P"
9851
- },
9852
- "source": [
9853
- "Training for a speaker verification model is almost the same as the speaker recognition model with a change in the loss function. Angular Loss is a better function to train for a speaker verification model as the model is trained in an end-to-end manner with loss optimizing for embeddings cluster to be far from each other for different speaker by maximizing the angle between these clusters"
9854
- ]
9855
- },
9856
- {
9857
- "cell_type": "markdown",
9858
- "metadata": {
9859
- "colab_type": "text",
9860
- "id": "ULTjBuFI19Js"
9861
- },
9862
- "source": [
9863
- "To train for verification we just need to toggle `angular` flag in `config.model.decoder.params.angular = True` else set it to `False` to train with cross-entropy loss for identification purposes. \n",
9864
- "Once we set this, the loss will be changed to angular loss and we can follow the above steps to the model.\n",
9865
- "Note the scale and margin values to be set for the loss function are present at `config.model.loss.scale` and `config.model.loss.margin`"
9866
- ]
9867
- },
9868
- {
9869
- "cell_type": "markdown",
9870
- "metadata": {
9871
- "colab_type": "text",
9872
- "id": "LcKiNEY032-t"
9873
- },
9874
- "source": [
9875
- "## Extract Speaker Embeddings\n",
9876
- "Once you have a trained model or use one of our pretrained nemo checkpoints to get speaker embeddings for any speaker.\n",
9877
- "\n",
9878
- "To demonstrate this we shall use `nemo_asr.models.EncDecSpeakerLabelModel` with say 5 audio_samples from our dev manifest set. This model is specifically for inference purposes to extract embeddings from a trained `.nemo` model"
9879
- ]
9880
- },
9881
- {
9882
- "cell_type": "code",
9883
- "execution_count": null,
9884
- "metadata": {
9885
- "colab": {},
9886
- "colab_type": "code",
9887
- "id": "uXEzKMHf3r6-"
9888
- },
9889
- "outputs": [],
9890
- "source": [
9891
- "verification_model = nemo_asr.models.EncDecSpeakerLabelModel.restore_from(os.path.join(log_dir_finetune, '..', 'titanet-large-finetune.nemo'))"
9892
- ]
9893
- },
9894
- {
9895
- "cell_type": "markdown",
9896
- "metadata": {
9897
- "colab_type": "text",
9898
- "id": "Y-XiLHMQ8BIk"
9899
- },
9900
- "source": [
9901
- "Now, we need to pass the necessary manifest_filepath and params to set up the data loader for extracting embeddings"
9902
- ]
9903
- },
9904
- {
9905
- "cell_type": "code",
9906
- "execution_count": null,
9907
- "metadata": {
9908
- "colab": {},
9909
- "colab_type": "code",
9910
- "id": "lk2vsDJk9PS8"
9911
- },
9912
- "outputs": [],
9913
- "source": [
9914
- "!head -5 {validation_manifest} > embeddings_manifest.json"
9915
- ]
9916
- },
9917
- {
9918
- "cell_type": "code",
9919
- "execution_count": null,
9920
- "metadata": {
9921
- "colab": {},
9922
- "colab_type": "code",
9923
- "id": "DEd5poCr9yrP"
9924
- },
9925
- "outputs": [],
9926
- "source": [
9927
- "config.model.train_ds"
9928
- ]
9929
- },
9930
- {
9931
- "cell_type": "code",
9932
- "execution_count": null,
9933
- "metadata": {},
9934
- "outputs": [],
9935
- "source": [
9936
- "from nemo.collections.asr.parts.utils.speaker_utils import embedding_normalize\n",
9937
- "from tqdm import tqdm\n",
9938
- "try:\n",
9939
- " from torch.cuda.amp import autocast\n",
9940
- "except ImportError:\n",
9941
- " from contextlib import contextmanager\n",
9942
- "\n",
9943
- " @contextmanager\n",
9944
- " def autocast(enabled=None):\n",
9945
- " yield\n",
9946
- "import numpy as np\n",
9947
- "import json\n",
9948
- "import pickle as pkl"
9949
- ]
9950
- },
9951
- {
9952
- "cell_type": "code",
9953
- "execution_count": null,
9954
- "metadata": {
9955
- "colab": {},
9956
- "colab_type": "code",
9957
- "id": "JIHok6LD8g0F"
9958
- },
9959
- "outputs": [],
9960
- "source": [
9961
- "def get_embeddings(speaker_model, manifest_file, batch_size=1, embedding_dir='./', device='cuda'):\n",
9962
- " test_config = OmegaConf.create(\n",
9963
- " dict(\n",
9964
- " manifest_filepath=manifest_file,\n",
9965
- " sample_rate=16000,\n",
9966
- " labels=None,\n",
9967
- " batch_size=batch_size,\n",
9968
- " shuffle=False,\n",
9969
- " time_length=20,\n",
9970
- " )\n",
9971
- " )\n",
9972
- "\n",
9973
- " speaker_model.setup_test_data(test_config)\n",
9974
- " speaker_model = speaker_model.to(device)\n",
9975
- " speaker_model.eval()\n",
9976
- "\n",
9977
- " all_embs=[]\n",
9978
- " out_embeddings = {}\n",
9979
- " \n",
9980
- " for test_batch in tqdm(speaker_model.test_dataloader()):\n",
9981
- " test_batch = [x.to(device) for x in test_batch]\n",
9982
- " audio_signal, audio_signal_len, labels, slices = test_batch\n",
9983
- " with autocast():\n",
9984
- " _, embs = speaker_model.forward(input_signal=audio_signal, input_signal_length=audio_signal_len)\n",
9985
- " emb_shape = embs.shape[-1]\n",
9986
- " embs = embs.view(-1, emb_shape)\n",
9987
- " all_embs.extend(embs.cpu().detach().numpy())\n",
9988
- " del test_batch\n",
9989
- "\n",
9990
- " all_embs = np.asarray(all_embs)\n",
9991
- " all_embs = embedding_normalize(all_embs)\n",
9992
- " with open(manifest_file, 'r') as manifest:\n",
9993
- " for i, line in enumerate(manifest.readlines()):\n",
9994
- " line = line.strip()\n",
9995
- " dic = json.loads(line)\n",
9996
- " uniq_name = '@'.join(dic['audio_filepath'].split('/')[-3:])\n",
9997
- " out_embeddings[uniq_name] = all_embs[i]\n",
9998
- "\n",
9999
- " embedding_dir = os.path.join(embedding_dir, 'embeddings')\n",
10000
- " if not os.path.exists(embedding_dir):\n",
10001
- " os.makedirs(embedding_dir, exist_ok=True)\n",
10002
- "\n",
10003
- " prefix = manifest_file.split('/')[-1].rsplit('.', 1)[-2]\n",
10004
- "\n",
10005
- " name = os.path.join(embedding_dir, prefix)\n",
10006
- " embeddings_file = name + '_embeddings.pkl'\n",
10007
- " pkl.dump(out_embeddings, open(embeddings_file, 'wb'))\n",
10008
- " print(\"Saved embedding files to {}\".format(embedding_dir))"
10009
- ]
10010
- },
10011
- {
10012
- "cell_type": "code",
10013
- "execution_count": null,
10014
- "metadata": {
10015
- "colab": {},
10016
- "colab_type": "code",
10017
- "id": "u2FRecqD-ln5"
10018
- },
10019
- "outputs": [],
10020
- "source": [
10021
- "manifest_filepath = os.path.join(NEMO_ROOT,'embeddings_manifest.json')\n",
10022
- "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
10023
- "get_embeddings(verification_model, manifest_filepath, batch_size=64,embedding_dir='./', device=device)"
10024
- ]
10025
- },
10026
- {
10027
- "cell_type": "markdown",
10028
- "metadata": {
10029
- "colab_type": "text",
10030
- "id": "zfjXPsjzDOgr"
10031
- },
10032
- "source": [
10033
- "Embeddings are stored in dict structure with key-value pair, key being uniq_name generated based on audio_filepath of the sample present in manifest_file in `embedding_dir`"
10034
- ]
10035
- },
10036
- {
10037
- "cell_type": "code",
10038
- "execution_count": null,
10039
- "metadata": {
10040
- "colab": {},
10041
- "colab_type": "code",
10042
- "id": "hmTeSR6jD28k"
10043
- },
10044
- "outputs": [],
10045
- "source": [
10046
- "ls ./embeddings/"
10047
- ]
10048
- },
10049
- {
10050
- "cell_type": "code",
10051
- "execution_count": null,
10052
- "metadata": {},
10053
- "outputs": [],
10054
- "source": []
10055
  }
10056
  ],
10057
  "metadata": {
@@ -10077,7 +9013,7 @@
10077
  "name": "python",
10078
  "nbconvert_exporter": "python",
10079
  "pygments_lexer": "ipython3",
10080
- "version": "3.10.12"
10081
  }
10082
  },
10083
  "nbformat": 4,
 
7432
  }
7433
  ],
7434
  "source": [
7435
+ "# Convert the mp3 files to wav files with 16kHz sampling rate and 16 bits, 1 channel\n",
7436
  "import os\n",
7437
  "NEMO_ROOT = os.getcwd()\n",
7438
  "print(NEMO_ROOT)\n",
 
8419
  },
8420
  {
8421
  "cell_type": "code",
8422
+ "execution_count": 5,
8423
  "metadata": {
8424
  "colab": {},
8425
  "colab_type": "code",
 
8443
  },
8444
  {
8445
  "cell_type": "code",
8446
+ "execution_count": 6,
8447
  "metadata": {},
8448
  "outputs": [],
8449
  "source": [
 
8466
  },
8467
  {
8468
  "cell_type": "code",
8469
+ "execution_count": 7,
8470
  "metadata": {},
8471
  "outputs": [
8472
  {
 
8597
  "[21057 rows x 3 columns]"
8598
  ]
8599
  },
8600
+ "execution_count": 7,
8601
  "metadata": {},
8602
  "output_type": "execute_result"
8603
  }
 
8667
  },
8668
  {
8669
  "cell_type": "code",
8670
+ "execution_count": 1,
8671
  "metadata": {},
8672
  "outputs": [
8673
  {
 
8707
  " labels: null\n",
8708
  " batch_size: 128\n",
8709
  " shuffle: false\n",
8710
+ " test_ds:\n",
8711
+ " manifest_filepath: ???\n",
8712
+ " sample_rate: 16000\n",
8713
+ " labels: null\n",
8714
+ " batch_size: 1\n",
8715
+ " shuffle: false\n",
8716
+ " embedding_dir: ./embeddings\n",
8717
  " model_defaults:\n",
8718
  " filters: 1024\n",
8719
  " repeat: 3\n",
 
8856
  },
8857
  {
8858
  "cell_type": "code",
8859
+ "execution_count": 2,
8860
  "metadata": {},
8861
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
8862
  "source": [
8863
  "# Fine-tune the model with Portuguese language\n",
8864
  "\n",
 
8866
  "import pytorch_lightning as pl\n",
8867
  "import nemo\n",
8868
  "import nemo.collections.asr as nemo_asr\n",
8869
+ "from omegaconf import OmegaConf\n",
8870
+ "from nemo.utils.exp_manager import exp_manager\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8871
  ]
8872
  },
8873
  {
8874
  "cell_type": "code",
8875
+ "execution_count": 4,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8876
  "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
 
8877
  "outputs": [],
8878
  "source": [
8879
+ "pt_config = OmegaConf.load(\"conf/titanet-finetune.yaml\")\n",
8880
+ "## set up the trainer\n",
8881
+ "accelerator = 'gpu' if torch.cuda.is_available() else 'cpu'\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8882
  "\n",
8883
+ "pt_trainer_config = OmegaConf.create(dict(\n",
8884
+ " devices=1,\n",
8885
+ " accelerator=accelerator,\n",
8886
+ " max_epochs=5,\n",
8887
+ " max_steps=-1, # computed at runtime if not set\n",
8888
+ " num_nodes=1,\n",
8889
+ " accumulate_grad_batches=1,\n",
8890
+ " enable_checkpointing=False, # Provided by exp_manager\n",
8891
+ " logger=False, # Provided by exp_manager\n",
8892
+ " log_every_n_steps=1, # Interval of logging.\n",
8893
+ " val_check_interval=1.0, # Set to 0.25 to check 4 times per epoch, or an int for number of iterations\n",
8894
+ "))\n",
8895
+ "print(OmegaConf.to_yaml(pt_trainer_config))\n",
8896
+ "pt_trainer_finetune = pl.Trainer(**pt_trainer_config)"
8897
  ]
8898
  },
8899
  {
8900
  "cell_type": "code",
8901
  "execution_count": null,
8902
+ "metadata": {},
 
 
 
 
8903
  "outputs": [],
8904
  "source": [
8905
+ "#set up the nemo experiment for logging and monitoring purpose\n",
8906
+ "log_dir_finetune = exp_manager(trainer=pt_trainer_finetune, config=pt_config, name='titanet_finetune_pt').get_save_dir()"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8907
  ]
8908
  },
8909
  {
8910
  "cell_type": "code",
8911
+ "execution_count": 8,
8912
+ "metadata": {},
 
 
 
 
8913
  "outputs": [],
8914
  "source": [
8915
+ "# set up the manifest file for Portuguese language\n",
8916
+ "pt_config.model.train_ds.manifest_filepath = 'data/cv-corpus-15.0-2023-09-08/pt/train.json'\n",
8917
+ "pt_config.model.validation_ds.manifest_filepath = 'data/cv-corpus-15.0-2023-09-08/pt/dev.json'\n",
8918
+ "pt_config.model.test_ds.manifest_filepath = 'data/cv-corpus-15.0-2023-09-08/pt/test.json'\n",
8919
+ "pt_config.model.decoder.num_classes = merged_pt_train_df['label'].nunique()"
 
 
 
 
 
 
 
 
 
 
8920
  ]
8921
  },
8922
  {
8923
  "cell_type": "code",
8924
  "execution_count": null,
 
 
 
 
 
 
 
 
 
 
 
 
 
8925
  "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8926
  "outputs": [],
8927
  "source": [
8928
+ "# set up the model for Portuguese language and train the model\n",
8929
+ "speaker_model = nemo_asr.models.EncDecSpeakerLabelModel(cfg=pt_config.model, trainer=trainer_finetune)\n",
8930
+ "speaker_model.maybe_init_from_pretrained_checkpoint(pt_config)\n",
 
 
 
 
 
 
 
 
 
8931
  "\n",
8932
+ "pt_trainer_finetune.fit(speaker_model)\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8933
  ]
8934
  },
8935
  {
8936
  "cell_type": "code",
8937
  "execution_count": null,
8938
+ "metadata": {},
 
 
 
 
8939
  "outputs": [],
8940
  "source": [
8941
+ "# Save the model after fine-tuning with Portuguese language\n",
8942
+ "speaker_model.save_to('titanet_finetune_pt.nemo')"
8943
  ]
8944
  },
8945
  {
8946
  "cell_type": "code",
8947
  "execution_count": null,
8948
+ "metadata": {},
 
 
 
 
8949
  "outputs": [],
8950
  "source": [
8951
+ "# Fine-tune the model with Turkish language\n",
8952
+ "tr_config = OmegaConf.load(\"conf/titanet-finetune.yaml\")\n",
8953
+ "## set up the trainer\n",
8954
  "accelerator = 'gpu' if torch.cuda.is_available() else 'cpu'\n",
 
 
8955
  "\n",
8956
+ "tr_trainer_config = OmegaConf.create(dict(\n",
8957
+ " devices=1,\n",
8958
+ " accelerator=accelerator,\n",
8959
+ " max_epochs=5,\n",
8960
+ " max_steps=-1, # computed at runtime if not set\n",
8961
+ " num_nodes=1,\n",
8962
+ " accumulate_grad_batches=1,\n",
8963
+ " enable_checkpointing=False, # Provided by exp_manager\n",
8964
+ " logger=False, # Provided by exp_manager\n",
8965
+ " log_every_n_steps=1, # Interval of logging.\n",
8966
+ " val_check_interval=1.0, # Set to 0.25 to check 4 times per epoch, or an int for number of iterations\n",
8967
+ "))\n",
8968
+ "print(OmegaConf.to_yaml(tr_trainer_config))\n",
8969
+ "tr_trainer_finetune = pl.Trainer(**tr_trainer_config)\n",
8970
  "\n",
 
 
8971
  "\n",
8972
+ "#set up the nemo experiment for logging and monitoring purpose\n",
8973
+ "log_dir_finetune = exp_manager(trainer=pt_trainer_finetune, config=pt_config, name='titanet_finetune_tr').get_save_dir()\n",
8974
+ "\n",
8975
+ "\n",
8976
+ "# set up the manifest file for Turkish language\n",
8977
+ "tr_config.model.train_ds.manifest_filepath = 'data/cv-corpus-15.0-2023-09-08/tr/train.json'\n",
8978
+ "tr_config.model.validation_ds.manifest_filepath = 'data/cv-corpus-15.0-2023-09-08/tr/dev.json'\n",
8979
+ "tr_config.model.test_ds.manifest_filepath = 'data/cv-corpus-15.0-2023-09-08/tr/test.json'\n",
8980
+ "tr_config.model.decoder.num_classes = merged_tr_train_df['label'].nunique()\n",
8981
+ "\n",
8982
+ "\n",
8983
+ "# set up the model for Turkish language and train the model\n",
8984
+ "speaker_model.maybe_init_from_pretrained_checkpoint(tr_config)\n",
8985
+ "tr_trainer_finetune.fit(speaker_model)\n",
8986
+ "\n",
8987
+ "# Save the model after fine-tuning with Turkish language\n",
8988
+ "\n",
8989
+ "speaker_model.save_to('titanet_finetune_pt_tr.nemo')"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8990
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8991
  }
8992
  ],
8993
  "metadata": {
 
9013
  "name": "python",
9014
  "nbconvert_exporter": "python",
9015
  "pygments_lexer": "ipython3",
9016
+ "version": "3.11.4"
9017
  }
9018
  },
9019
  "nbformat": 4,