tuto_1_iv_model.ipynb 20.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Train model for Diarization\n",
    "====\n",
    "\n",
    "This script trains UBM, TV and PLDA models for a diarization system.\n",
    "\n",
    "Initialization\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
18
   "execution_count": 9,
19
20
21
   "metadata": {},
   "outputs": [],
   "source": [
22
23
    "%matplotlib inline\n",
    "\n",
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    "from s4d.diar import Diar\n",
    "from s4d.utils import *\n",
    "\n",
    "from sidekit import Mixture, FactorAnalyser, StatServer, IdMap\n",
    "import numpy\n",
    "import logging\n",
    "import re\n",
    "import sidekit\n",
    "from sidekit.sidekit_io import *\n",
    "try:\n",
    "    from sortedcontainers import SortedDict as dict\n",
    "except ImportError:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
41
   "execution_count": 10,
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
   "metadata": {},
   "outputs": [],
   "source": [
    "init_logging(level=logging.INFO)\n",
    "num_thread = 4\n",
    "audio_dir = '../data/train/{}.wav'\n",
    "\n",
    "\n",
    "ubm_seg_fn = './data/seg/ubm_ester.seg'\n",
    "nb_gauss = 1024\n",
    "mfcc_ubm_fn = './data/mfcc/ubm.h5'\n",
    "ubm_idmap_fn = './data/mfcc/ubm_idmap.txt'\n",
    "ubm_fn = './data/model/ester_ubm_'+str(nb_gauss)+'.h5'\n",
    "\n",
    "\n",
    "tv_seg_fn = './data/seg/train.tv.seg'\n",
    "rank_tv = 300\n",
    "it_max_tv = 10\n",
    "mfcc_tv_fn = './data/mfcc/tv.h5'\n",
    "tv_idmap_fn = './data/mfcc/tv_idmap.h5'\n",
    "tv_stat_fn  = './data/model/tv.stat.h5'\n",
    "tv_fn = './data/model/tv_'+str(rank_tv)+'.h5'\n",
    "\n",
    "\n",
    "plda_seg_fn = './data/seg/train.plda.seg'\n",
    "rank_plda = 150\n",
    "it_max_plda = 10\n",
    "mfcc_plda_fn = './data/mfcc/norm_plda.h5'\n",
    "plda_idmap_fn = './data/mfcc/plda_idmap.h5'\n",
    "plda_fn = './data/model/plda_'+str(rank_tv)+'_'+str(rank_plda)+'.h5'\n",
    "norm_stat_fn = './data/model/norm.stat.h5'\n",
    "norm_fn = './data/model/norm.h5'\n",
    "norm_iv_fn = './data/model/norm.iv.h5'\n",
    "\n",
    "\n",
    "matrices_fn = './data/model/matrices.h5'\n",
    "model_fn = './data/model/ester_model_{}_{}_{}.h5'.format(nb_gauss, rank_tv, rank_plda)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step 1: UBM\n",
    "---\n",
    "Extract MFCC for the UBM"
   ]
  },
  {
   "cell_type": "code",
92
   "execution_count": 11,
93
   "metadata": {},
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
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-09-26 11:50:10,393 - INFO - Computing MFCC for UBM\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '../data/train/19981207_0700_0800_inter_fm_dga.wav'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-11-b9765c5346e8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mdiar_ubm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDiar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_seg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mubm_seg_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnormalize_cluster\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mfe\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_feature_extractor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maudio_dir\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'sid'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mubm_idmap\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfe\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave_multispeakers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdiar_ubm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mid_map\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput_feature_filename\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmfcc_ubm_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeep_all\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m \u001b[0mubm_idmap\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite_txt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mubm_idmap_fn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/Desktop/Doctorat/Tools/Environments/miniconda/Python3/lib/python3.6/site-packages/sidekit/features_extractor.py\u001b[0m in \u001b[0;36msave_multispeakers\u001b[0;34m(self, idmap, channel, input_audio_filename, output_feature_filename, keep_all, skip_existing_file)\u001b[0m\n\u001b[1;32m    460\u001b[0m             \u001b[0;31m# logging.info('tmp file name: '+temp_file_name)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    461\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvad\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 462\u001b[0;31m             \u001b[0mh5f\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextract\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mchannel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_audio_filename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbacking_store\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    463\u001b[0m             \u001b[0menergy\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mh5f\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshow\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'/energy'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    464\u001b[0m             \u001b[0mlabel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mh5f\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshow\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'/vad'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/Desktop/Doctorat/Tools/Environments/miniconda/Python3/lib/python3.6/site-packages/sidekit/features_extractor.py\u001b[0m in \u001b[0;36mextract\u001b[0;34m(self, show, channel, input_audio_filename, output_feature_filename, backing_store)\u001b[0m\n\u001b[1;32m    215\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    216\u001b[0m         \u001b[0;31m# Open audio file, get the signal and possibly the sampling frequency\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 217\u001b[0;31m         \u001b[0msignal\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msample_rate\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mread_audio\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maudio_filename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msampling_frequency\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    218\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0msignal\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    219\u001b[0m             \u001b[0msignal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msignal\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/Desktop/Doctorat/Tools/Environments/miniconda/Python3/lib/python3.6/site-packages/sidekit/frontend/io.py\u001b[0m in \u001b[0;36mread_audio\u001b[0;34m(input_file_name, framerate)\u001b[0m\n\u001b[1;32m    420\u001b[0m         \u001b[0msig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mread_framerate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msampwidth\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mread_sph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_file_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'p'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    421\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0mext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'.wav'\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'.wave'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 422\u001b[0;31m         \u001b[0msig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mread_framerate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msampwidth\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mread_wav\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_file_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    423\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0mext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'.pcm'\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'.raw'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    424\u001b[0m         \u001b[0msig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mread_framerate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msampwidth\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mread_pcm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_file_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/Desktop/Doctorat/Tools/Environments/miniconda/Python3/lib/python3.6/site-packages/sidekit/frontend/io.py\u001b[0m in \u001b[0;36mread_wav\u001b[0;34m(input_file_name)\u001b[0m\n\u001b[1;32m    117\u001b[0m     \u001b[0;34m:\u001b[0m\u001b[0;32mreturn\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    118\u001b[0m     \"\"\"\n\u001b[0;32m--> 119\u001b[0;31m     \u001b[0;32mwith\u001b[0m \u001b[0mwave\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_file_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mwfh\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    120\u001b[0m         \u001b[0;34m(\u001b[0m\u001b[0mnchannels\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msampwidth\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mframerate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnframes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcomptype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcompname\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwfh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetparams\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    121\u001b[0m         \u001b[0mraw\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwfh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadframes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnframes\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mnchannels\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/Desktop/Doctorat/Tools/Environments/miniconda/Python3/lib/python3.6/wave.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(f, mode)\u001b[0m\n\u001b[1;32m    497\u001b[0m             \u001b[0mmode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'rb'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    498\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mmode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'r'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rb'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 499\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mWave_read\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    500\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0mmode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'w'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'wb'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    501\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mWave_write\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/Desktop/Doctorat/Tools/Environments/miniconda/Python3/lib/python3.6/wave.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, f)\u001b[0m\n\u001b[1;32m    157\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_i_opened_the_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    158\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 159\u001b[0;31m             \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbuiltins\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rb'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    160\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_i_opened_the_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    161\u001b[0m         \u001b[0;31m# else, assume it is an open file object already\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '../data/train/19981207_0700_0800_inter_fm_dga.wav'"
     ]
    }
   ],
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
372
373
374
375
   "source": [
    "logging.info('Computing MFCC for UBM')\n",
    "diar_ubm = Diar.read_seg(ubm_seg_fn, normalize_cluster=True)\n",
    "fe = get_feature_extractor(audio_dir, 'sid')\n",
    "ubm_idmap = fe.save_multispeakers(diar_ubm.id_map(), output_feature_filename=mfcc_ubm_fn, keep_all=False)\n",
    "ubm_idmap.write_txt(ubm_idmap_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Train the UBM by EM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ubm_idmap = IdMap.read_txt(ubm_idmap_fn)\n",
    "\n",
    "fs = get_feature_server(mfcc_ubm_fn, 'sid')\n",
    "\n",
    "spk_lst = ubm_idmap.rightids\n",
    "ubm = Mixture()\n",
    "ubm.EM_split(fs, spk_lst, nb_gauss,\n",
    "             iterations=(1, 2, 2, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8), num_thread=num_thread,\n",
    "             llk_gain=0.01)\n",
    "ubm.write(ubm_fn, prefix='ubm/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step 2: TV\n",
    "---\n",
    "Extract MFCC for TV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.info('Computing MFCC for TV')\n",
    "diar_tv = Diar.read_seg(tv_seg_fn, normalize_cluster=True)\n",
    "fe = get_feature_extractor(audio_dir, 'sid')\n",
    "tv_idmap = fe.save_multispeakers(diar_tv.id_map(), output_feature_filename=mfcc_tv_fn, keep_all=False)\n",
    "tv_idmap.write(tv_idmap_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Train a Total Variability model using the FactorAnalyser class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tv_idmap = IdMap.read(tv_idmap_fn)\n",
    "\n",
    "ubm = Mixture()\n",
    "ubm.read(ubm_fn, prefix='ubm/')\n",
    "\n",
    "fs = get_feature_server(mfcc_tv_fn, 'sid')\n",
    "\n",
    "tv_idmap.leftids = numpy.copy(tv_idmap.rightids)\n",
    "\n",
    "tv_stat = StatServer(tv_idmap, ubm.get_distrib_nb(), ubm.dim())\n",
    "tv_stat.accumulate_stat(ubm=ubm, feature_server=fs, seg_indices=range(tv_stat.segset.shape[0]), num_thread=num_thread)\n",
    "tv_stat.write(tv_stat_fn)\n",
    "fa = FactorAnalyser()\n",
    "fa.total_variability(tv_stat_fn, ubm, rank_tv, nb_iter=it_max_tv, batch_size=1000, num_thread=num_thread)\n",
    "\n",
    "write_tv_hdf5([fa.F, fa.mean, fa.Sigma], tv_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step 3: PLDA\n",
    "---\n",
    "Extract the MFCC for the PLDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.info('Computing MFCC for PLDA')\n",
    "diar_plda = Diar.read_seg(plda_seg_fn, normalize_cluster=True)\n",
    "fe = get_feature_extractor(audio_dir, 'sid')\n",
    "plda_idmap = fe.save_multispeakers(diar_plda.id_map(), output_feature_filename=mfcc_plda_fn, keep_all=False)\n",
    "plda_idmap.write(plda_idmap_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Accumulate statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plda_idmap = IdMap.read(plda_idmap_fn)\n",
    "\n",
    "ubm = Mixture()\n",
    "ubm.read(ubm_fn, prefix='ubm/')\n",
    "tv, tv_mean, tv_sigma = read_tv_hdf5(tv_fn)\n",
    "\n",
    "fs = get_feature_server(mfcc_plda_fn, 'sid')\n",
    "\n",
    "plda_norm_stat = StatServer(plda_idmap, ubm.get_distrib_nb(), ubm.dim())\n",
    "plda_norm_stat.accumulate_stat(ubm=ubm, feature_server=fs, \n",
    "                               seg_indices=range(plda_norm_stat.segset.shape[0]), num_thread=num_thread)\n",
    "plda_norm_stat.write(norm_stat_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Extract i-vectors and compute norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fa = FactorAnalyser(F=tv, mean=tv_mean, Sigma=tv_sigma)\n",
    "norm_iv = fa.extract_ivectors(ubm, norm_stat_fn, num_thread=num_thread)\n",
    "norm_iv.write(norm_iv_fn)\n",
    "\n",
    "norm_mean, norm_cov = norm_iv.estimate_spectral_norm_stat1(1, 'sphNorm')\n",
    "\n",
    "write_norm_hdf5([norm_mean, norm_cov], norm_fn)\n",
    "\n",
    "norm_iv.spectral_norm_stat1(norm_mean[:1], norm_cov[:1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Train the PLDA model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fa = FactorAnalyser()\n",
    "fa.plda(norm_iv, rank_plda, nb_iter=it_max_plda)\n",
    "write_plda_hdf5([fa.mean, fa.F, numpy.zeros((rank_tv, 0)), fa.Sigma], plda_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step 4: Compute additional data (optional)\n",
    "---\n",
    "Adding matrices for additional scoring methods: \n",
    "* Mahalonobis matrix\n",
    "* Lower Choleski decomposition of the WCCN matrix\n",
    "* Within- and Between-class Covariance matrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iv = StatServer(norm_iv_fn)\n",
    "matrix_dict = {}\n",
    "\n",
    "logging.info('compute mahalanobis_matrix')\n",
    "mahalanobis_matrix = iv.get_mahalanobis_matrix_stat1()\n",
    "matrix_dict['mahalanobis_matrix'] = mahalanobis_matrix\n",
    "\n",
    "logging.info('compute wccn_choleski')\n",
    "wccn_choleski = iv.get_wccn_choleski_stat1()\n",
    "matrix_dict['wccn_choleski'] = wccn_choleski\n",
    "\n",
    "logging.info('compute two_covariance')\n",
    "within_covariance = iv.get_within_covariance_stat1()\n",
    "matrix_dict['two_covariance/within_covariance'] = within_covariance\n",
    "between_covariance = iv.get_between_covariance_stat1()\n",
    "matrix_dict['two_covariance/between_covariance'] = between_covariance\n",
    "\n",
    "write_dict_hdf5(matrix_dict, matrices_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step 5: Merge in one model\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with h5py.File(model_fn, 'w') as model:\n",
    "    for fn in [ubm_fn, tv_fn, norm_fn, plda_fn, matrices_fn]:\n",
    "        if not os.path.exists(fn):\n",
    "            continue\n",
    "        with h5py.File(fn, 'r') as fh:\n",
    "            for group in fh:\n",
    "                logging.info(group)\n",
    "                fh.copy(group, model)"
   ]
  }
 ],
 "metadata": {
  "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",
376
   "version": "3.6.6"
377
378
379
380
381
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}