|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": { |
| 7 | + "collapsed": true |
| 8 | + }, |
| 9 | + "outputs": [], |
| 10 | + "source": [ |
| 11 | + "from tensorflow.contrib.learn.python.learn.datasets import base\n", |
| 12 | + "\n", |
| 13 | + "import tensorflow as tf\n", |
| 14 | + "import numpy as np\n", |
| 15 | + "import os,sys\n", |
| 16 | + "sys.path.insert(0, './scripts')\n", |
| 17 | + "dataDir ='./data'\n", |
| 18 | + "\n", |
| 19 | + "\n", |
| 20 | + "import py_compile\n", |
| 21 | + "py_compile.compile('scripts/ivector_dataset.py')\n", |
| 22 | + "py_compile.compile('scripts/ivector_tools.py')\n", |
| 23 | + "py_compile.compile('scripts/siamese_model.py')\n", |
| 24 | + "import ivector_dataset\n", |
| 25 | + "import siamese_model\n", |
| 26 | + "import ivector_tools as it" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 2, |
| 32 | + "metadata": { |
| 33 | + "collapsed": true |
| 34 | + }, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "# write prototxt for siamese network\n", |
| 38 | + "\n", |
| 39 | + "languages = ['EGY','GLF','LAV','MSA','NOR']\n", |
| 40 | + "trn_labels = []\n", |
| 41 | + "trn_names = []\n", |
| 42 | + "trn_ivectors = np.empty((0,400))\n", |
| 43 | + "dev_labels = []\n", |
| 44 | + "dev_names = []\n", |
| 45 | + "dev_ivectors = np.empty((0,400))\n", |
| 46 | + "\n", |
| 47 | + "\n", |
| 48 | + "for i,lang in enumerate(languages):\n", |
| 49 | + " #load train.vardial2017\n", |
| 50 | + " filename = dataDir+'/train.vardial2017/%s.ivec' % lang\n", |
| 51 | + " name = np.loadtxt(filename,usecols=[0],dtype='string')\n", |
| 52 | + " ivector = np.loadtxt(filename,usecols=range(1,401),dtype='float32')\n", |
| 53 | + " trn_labels = np.append(trn_labels, np.ones(np.size(name))*(i+1))\n", |
| 54 | + " trn_names=np.append(trn_names,name)\n", |
| 55 | + " trn_ivectors = np.append(trn_ivectors, ivector,axis=0)\n", |
| 56 | + "\n", |
| 57 | + " #load dev.vardial2017\n", |
| 58 | + " filename = dataDir+'/dev.vardial2017/%s.ivec' % lang\n", |
| 59 | + " name = np.loadtxt(filename,usecols=[0],dtype='string')\n", |
| 60 | + " ivector = np.loadtxt(filename,usecols=range(1,401),dtype='float32')\n", |
| 61 | + " dev_names=np.append(dev_names,name)\n", |
| 62 | + " dev_ivectors = np.append(dev_ivectors, ivector,axis=0)\n", |
| 63 | + " dev_labels = np.append(dev_labels, np.ones(np.size(name))*(i+1))\n", |
| 64 | + " \n", |
| 65 | + "# load test.MGB3\n", |
| 66 | + "filename = dataDir+'/test.MGB3/ivec_features'\n", |
| 67 | + "tst_names = np.loadtxt(filename,usecols=[0],dtype='string')\n", |
| 68 | + "tst_ivectors = np.loadtxt(filename,usecols=range(1,401),dtype='float32')\n", |
| 69 | + "\n", |
| 70 | + "# merge trn+dev\n", |
| 71 | + "trndev_ivectors = np.append(trn_ivectors, dev_ivectors,axis=0)\n", |
| 72 | + "trndev_labels = np.append(trn_labels,dev_labels)\n", |
| 73 | + "trndev_name = np.append(trn_names,dev_names)\n", |
| 74 | + "\n", |
| 75 | + "\n", |
| 76 | + "# load tst.MGB3 labels\n", |
| 77 | + "filename = 'data/test.MGB3/reference'\n", |
| 78 | + "tst_ref_names = np.loadtxt(filename,usecols=[0],dtype='string')\n", |
| 79 | + "tst_ref_labels = np.loadtxt(filename,usecols=[1],dtype='int')\n", |
| 80 | + "\n", |
| 81 | + "tst_ref_labels_index = []\n", |
| 82 | + "for i, name_ref in enumerate(tst_ref_names):\n", |
| 83 | + " for j, name in enumerate(tst_names):\n", |
| 84 | + " if name == name_ref:\n", |
| 85 | + " tst_ref_labels_index = np.append(tst_ref_labels_index,int(j))\n", |
| 86 | + "\n", |
| 87 | + "tst_labels = tst_ref_labels\n", |
| 88 | + "tst_ivectors = tst_ivectors[ map(int,tst_ref_labels_index),:]" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 3, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [ |
| 96 | + { |
| 97 | + "name": "stdout", |
| 98 | + "output_type": "stream", |
| 99 | + "text": [ |
| 100 | + "((13825, 400), (1524, 400), (5, 400), (1492, 400))\n", |
| 101 | + "Final accurary on test dataset : 0.603\n", |
| 102 | + "Confusion matrix\n", |
| 103 | + "[[ 192. 14. 40. 10. 46.]\n", |
| 104 | + " [ 15. 118. 34. 8. 20.]\n", |
| 105 | + " [ 65. 83. 221. 16. 102.]\n", |
| 106 | + " [ 23. 28. 24. 225. 32.]\n", |
| 107 | + " [ 7. 7. 15. 3. 144.]]\n", |
| 108 | + "Precision\n", |
| 109 | + "[ 0.63576159 0.60512821 0.45379877 0.67771084 0.81818182]\n", |
| 110 | + "Recall\n", |
| 111 | + "[ 0.63576159 0.472 0.66167665 0.85877863 0.41860465]\n", |
| 112 | + "\n", |
| 113 | + "\n", |
| 114 | + "<Performance evaluation on Test dataset : CDS (baseline) >\n", |
| 115 | + "Accurary : 0.603\n", |
| 116 | + "Precision : 0.638\n", |
| 117 | + "Recall : 0.609\n" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "#center and length norm.\n", |
| 123 | + "m=np.mean(trn_ivectors,axis=0)\n", |
| 124 | + "A = np.cov(trn_ivectors.transpose())\n", |
| 125 | + "[a,D,V] = np.linalg.svd(A)\n", |
| 126 | + "V= V.transpose()\n", |
| 127 | + "W= np.dot(V, np.diag(1./( np.sqrt(D) + 0.0000000001 )))\n", |
| 128 | + "\n", |
| 129 | + "trn_ivectors = np.dot( np.subtract( trn_ivectors, m), W)\n", |
| 130 | + "trndev_ivectors = np.dot( np.subtract( trndev_ivectors, m), W)\n", |
| 131 | + "dev_ivectors = np.dot( np.subtract( dev_ivectors, m), W)\n", |
| 132 | + "tst_ivectors = np.dot( np.subtract( tst_ivectors, m), W)\n", |
| 133 | + "\n", |
| 134 | + "trn_ivectors = it.length_norm(trn_ivectors)\n", |
| 135 | + "trndev_ivectors = it.length_norm(trndev_ivectors)\n", |
| 136 | + "dev_ivectors = it.length_norm(dev_ivectors)\n", |
| 137 | + "tst_ivectors = it.length_norm(tst_ivectors)\n", |
| 138 | + "\n", |
| 139 | + "#language modeling\n", |
| 140 | + "lang_mean=[]\n", |
| 141 | + "for i, lang in enumerate(languages):\n", |
| 142 | + " lang_mean.append(np.mean(np.append(trn_ivectors[np.nonzero(trn_labels == i+1)] ,dev_ivectors[np.nonzero(dev_labels == i+1)],axis=0),axis=0))\n", |
| 143 | + "# lang_mean.append(np.mean(trn_ivectors[np.nonzero(trn_labels == i+1)],axis=0))\n", |
| 144 | + "\n", |
| 145 | + "lang_mean = np.array(lang_mean)\n", |
| 146 | + "lang_mean = it.length_norm(lang_mean)\n", |
| 147 | + "\n", |
| 148 | + "print( np.shape(trn_ivectors), np.shape(dev_ivectors), np.shape(lang_mean),np.shape(tst_ivectors) )\n", |
| 149 | + "\n", |
| 150 | + "\n", |
| 151 | + "tst_scores = lang_mean.dot(tst_ivectors.transpose() )\n", |
| 152 | + "# print(tst_scores.shape)\n", |
| 153 | + "hypo_lang = np.argmax(tst_scores,axis = 0)\n", |
| 154 | + "temp = ((tst_labels-1) - hypo_lang)\n", |
| 155 | + "acc =1- np.size(np.nonzero(temp)) / float(np.size(tst_labels))\n", |
| 156 | + "print 'Final accurary on test dataset : %0.3f' %(acc)\n", |
| 157 | + "\n", |
| 158 | + "confusionmat = np.zeros((5,5))\n", |
| 159 | + "for i,lang in enumerate(languages):\n", |
| 160 | + " hypo_bylang = hypo_lang[ tst_labels == i+1]\n", |
| 161 | + " hist_bylang = np.histogram(hypo_bylang,5)\n", |
| 162 | + " confusionmat[:,i] = hist_bylang[0]\n", |
| 163 | + "\n", |
| 164 | + "precision = np.diag(confusionmat) / np.sum(confusionmat,axis=1) #precision\n", |
| 165 | + "recall = np.diag(confusionmat) / np.sum(confusionmat,axis=0) # recall\n", |
| 166 | + " \n", |
| 167 | + "print 'Confusion matrix'\n", |
| 168 | + "print confusionmat\n", |
| 169 | + "print 'Precision'\n", |
| 170 | + "print precision\n", |
| 171 | + "print 'Recall'\n", |
| 172 | + "print recall\n", |
| 173 | + "\n", |
| 174 | + "print '\\n\\n<Performance evaluation on Test dataset : CDS (baseline) >'\n", |
| 175 | + "print 'Accurary : %0.3f' %(acc)\n", |
| 176 | + "print 'Precision : %0.3f' %(np.mean(precision))\n", |
| 177 | + "print 'Recall : %0.3f' %(np.mean(recall))" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": 4, |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [ |
| 185 | + { |
| 186 | + "name": "stdout", |
| 187 | + "output_type": "stream", |
| 188 | + "text": [ |
| 189 | + "((13825, 4), (1524, 4), (5, 4), (1492, 4))\n", |
| 190 | + "Final accurary on test dataset : 0.628\n", |
| 191 | + "Confusion matrix\n", |
| 192 | + "[[ 200. 22. 46. 13. 40.]\n", |
| 193 | + " [ 17. 145. 62. 10. 27.]\n", |
| 194 | + " [ 47. 49. 172. 9. 54.]\n", |
| 195 | + " [ 22. 23. 26. 224. 27.]\n", |
| 196 | + " [ 16. 11. 28. 6. 196.]]\n", |
| 197 | + "Precision\n", |
| 198 | + "[ 0.62305296 0.55555556 0.51963746 0.69565217 0.76264591]\n", |
| 199 | + "Recall\n", |
| 200 | + "[ 0.66225166 0.58 0.51497006 0.85496183 0.56976744]\n", |
| 201 | + "\n", |
| 202 | + "\n", |
| 203 | + "<Performance evaluation on Test dataset : LDA+CDS>\n", |
| 204 | + "Accurary : 0.628\n", |
| 205 | + "Precision : 0.631\n", |
| 206 | + "Recall : 0.636\n" |
| 207 | + ] |
| 208 | + } |
| 209 | + ], |
| 210 | + "source": [ |
| 211 | + "#LDA\n", |
| 212 | + "[languages,train_languages_num] = np.unique(trndev_labels,return_inverse=True)\n", |
| 213 | + "V = it.lda2(trndev_ivectors,train_languages_num)\n", |
| 214 | + "V = np.real(V[:,0:4])\n", |
| 215 | + "trn_ivectors = np.matmul(trn_ivectors,V)\n", |
| 216 | + "dev_ivectors = np.matmul(dev_ivectors,V)\n", |
| 217 | + "tst_ivectors = np.matmul(tst_ivectors,V)\n", |
| 218 | + "trndev_ivectors = np.matmul(trndev_ivectors,V)\n", |
| 219 | + "\n", |
| 220 | + "\n", |
| 221 | + "\n", |
| 222 | + "trn_ivectors = it.length_norm(trn_ivectors)\n", |
| 223 | + "trndev_ivectors = it.length_norm(trndev_ivectors)\n", |
| 224 | + "dev_ivectors = it.length_norm(dev_ivectors)\n", |
| 225 | + "tst_ivectors = it.length_norm(tst_ivectors)\n", |
| 226 | + "\n", |
| 227 | + "\n", |
| 228 | + "#language modeling\n", |
| 229 | + "lang_mean=[]\n", |
| 230 | + "for i, lang in enumerate(languages):\n", |
| 231 | + " lang_mean.append(np.mean(np.append(trn_ivectors[np.nonzero(trn_labels == i+1)] ,dev_ivectors[np.nonzero(dev_labels == i+1)],axis=0),axis=0))\n", |
| 232 | + "# lang_mean.append(np.mean(trn_ivectors[np.nonzero(trn_labels == i+1)],axis=0))\n", |
| 233 | + "\n", |
| 234 | + "lang_mean = np.array(lang_mean)\n", |
| 235 | + "lang_mean = it.length_norm(lang_mean)\n", |
| 236 | + "\n", |
| 237 | + "print( np.shape(trn_ivectors), np.shape(dev_ivectors), np.shape(lang_mean),np.shape(tst_ivectors) )\n", |
| 238 | + "\n", |
| 239 | + "\n", |
| 240 | + "tst_scores = lang_mean.dot(tst_ivectors.transpose() )\n", |
| 241 | + "# print(tst_scores.shape)\n", |
| 242 | + "hypo_lang = np.argmax(tst_scores,axis = 0)\n", |
| 243 | + "temp = ((tst_labels-1) - hypo_lang)\n", |
| 244 | + "acc =1- np.size(np.nonzero(temp)) / float(np.size(tst_labels))\n", |
| 245 | + "print 'Final accurary on test dataset : %0.3f' %(acc)\n", |
| 246 | + "\n", |
| 247 | + "confusionmat = np.zeros((5,5))\n", |
| 248 | + "for i,lang in enumerate(languages):\n", |
| 249 | + " hypo_bylang = hypo_lang[ tst_labels == i+1]\n", |
| 250 | + " hist_bylang = np.histogram(hypo_bylang,5)\n", |
| 251 | + " confusionmat[:,i] = hist_bylang[0]\n", |
| 252 | + "\n", |
| 253 | + "precision = np.diag(confusionmat) / np.sum(confusionmat,axis=1) #precision\n", |
| 254 | + "recall = np.diag(confusionmat) / np.sum(confusionmat,axis=0) # recall\n", |
| 255 | + " \n", |
| 256 | + "print 'Confusion matrix'\n", |
| 257 | + "print confusionmat\n", |
| 258 | + "print 'Precision'\n", |
| 259 | + "print precision\n", |
| 260 | + "print 'Recall'\n", |
| 261 | + "print recall\n", |
| 262 | + "\n", |
| 263 | + "print '\\n\\n<Performance evaluation on Test dataset : LDA+CDS>'\n", |
| 264 | + "print 'Accurary : %0.3f' %(acc)\n", |
| 265 | + "print 'Precision : %0.3f' %(np.mean(precision))\n", |
| 266 | + "print 'Recall : %0.3f' %(np.mean(recall))" |
| 267 | + ] |
| 268 | + } |
| 269 | + ], |
| 270 | + "metadata": { |
| 271 | + "kernelspec": { |
| 272 | + "display_name": "Python 2", |
| 273 | + "language": "python", |
| 274 | + "name": "python2" |
| 275 | + }, |
| 276 | + "language_info": { |
| 277 | + "codemirror_mode": { |
| 278 | + "name": "ipython", |
| 279 | + "version": 2 |
| 280 | + }, |
| 281 | + "file_extension": ".py", |
| 282 | + "mimetype": "text/x-python", |
| 283 | + "name": "python", |
| 284 | + "nbconvert_exporter": "python", |
| 285 | + "pygments_lexer": "ipython2", |
| 286 | + "version": "2.7.6" |
| 287 | + } |
| 288 | + }, |
| 289 | + "nbformat": 4, |
| 290 | + "nbformat_minor": 2 |
| 291 | +} |
0 commit comments