-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproject_ivectors.py
146 lines (121 loc) · 5.59 KB
/
project_ivectors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import sys
import bob
import numpy
import shutil
import cPickle
import scipy.sparse
import facereclib.utils as utils
from sklearn import preprocessing
if len(sys.argv)!=2:
print '\nUsage: python scoring_GMM_UBM_final.py <number_of_gaussians> '
sys.exit()
# parameters
training_threshold = 5e-4
variance_threshold = 5e-4
max_iterations = 25
relevance_factor = 4 # Relevance factor as described in Reynolds paper
gmm_enroll_iterations = 1 # Number of iterations for the enrollment phase
subspace_dimension_of_t = 100
INIT_SEED = 5489
# parameters for the GMM
gaussians = sys.argv[1]
# read UBM
'''
if (gaussians == '64'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment2/model_gmm_ubm/gmm_ubm_64G.hdf5'
if (gaussians == '128'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment2/model_gmm_ubm/gmm_ubm_128G.hdf5'
if (gaussians == '256'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment2/model_gmm_ubm/gmm_ubm_256G.hdf5'
if (gaussians == '512'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment2/model_gmm_ubm/gmm_ubm_512G.hdf5'
'''
# TIMIT with FAU no label UBM 39D
if (gaussians == '64'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment3/data/giantubm_model/gmm_ubm/39D/gmm_giantubm_64G.hdf5'
if (gaussians == '128'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment3/data/giantubm_model/gmm_ubm/39D/gmm_giantubm_128G.hdf5'
if (gaussians == '256'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment3/data/giantubm_model/gmm_ubm/39D/gmm_giantubm_256G.hdf5'
if (gaussians == '512'):
ubm_file = '/mnt/alderaan/mlteam3/Assignment3/data/giantubm_model/gmm_ubm/39D/gmm_giantubm_512G.hdf5'
input_ubm_features = '/mnt/alderaan/mlteam3/Assignment2/data/Noisy_TIMIT_buckeye/ubm_noisy_mfcc_39D'
input_emotion_train_features = '/mnt/alderaan/mlteam3/Assignment3/data/FAU_mfcc/39D/mfcc_segmented/Ohm_training'
input_emotion_test_features = '/mnt/alderaan/mlteam3/Assignment3/data/FAU_mfcc/39D/mfcc_segmented/Mont_testing'
def normalize(data):
return preprocessing.normalize(data,norm='l2')
def read_mfcc_features(input_features):
with open(input_features, 'rb') as file:
features = cPickle.load(file)
features = scipy.sparse.coo_matrix((features),dtype=numpy.float64).toarray()
if features.shape[1] != 0:
features = normalize(features)
return features
def load_training_gmmstats(input_features):
gmm_stats_list = []
for root, dir, files in os.walk(input_features):
for file in files:
features_path = os.path.join(root, str(file))
features = read_mfcc_features(features_path)
stats = bob.machine.GMMStats(ubm.dim_c, ubm.dim_d)
if features.shape[1] == 39:
ubm.acc_statistics(features, stats)
gmm_stats_list.append(stats)
return gmm_stats_list
def train_enroller(input_features):
# load GMM stats from UBM training files
gmm_stats = load_training_gmmstats(input_features)
# Training IVector enroller
output_file = 'data/tv_enroller_512G_39D_25i_100.hdf5'
print "training enroller (total variability matrix) ", max_iterations, 'max_iterations'
# Perform IVector initialization with the UBM
ivector_machine = bob.machine.IVectorMachine(ubm, subspace_dimension_of_t)
ivector_machine.variance_threshold = variance_threshold
# Creates the IVectorTrainer and trains the ivector machine
ivector_trainer = bob.trainer.IVectorTrainer(update_sigma=True, convergence_threshold=variance_threshold, max_iterations=max_iterations)
# An trainer to extract i-vector (i.e. for training the Total Variability matrix)
ivector_trainer.train(ivector_machine, gmm_stats)
ivector_machine.save(bob.io.HDF5File(output_file, 'w'))
print "IVector training: saved enroller's IVector machine base to '%s'" % output_file
return ivector_machine
def lnorm_ivector(ivector):
norm = numpy.linalg.norm(ivector)
if norm != 0:
return ivector/numpy.linalg.norm(ivector)
else:
return ivector
def save_ivectors(data, feature_file):
hdf5file = bob.io.HDF5File(feature_file, "w")
hdf5file.set('ivec', data)
def project_ivectors(input_features):
"""Extract the ivectors for all files of the database"""
print "projecting ivetors"
tv_enroller = bob.machine.IVectorMachine(ubm, subspace_dimension_of_t)
tv_enroller.load(bob.io.HDF5File("/mnt/alderaan/mlteam3/Assignment3/data/tv_enroller_512G_13D_25i_100.hdf5"))
#print input_features
for root, dir, files in os.walk(input_features):
ivectors = []
for file in files:
features_path = os.path.join(root, str(file))
features = read_mfcc_features(features_path)
stats = bob.machine.GMMStats(ubm.dim_c, ubm.dim_d)
if features.shape[1] == 39:
ubm.acc_statistics(features, stats)
ivector = tv_enroller.forward(stats)
lnorm_ivector(ivector)
ivectors.append(ivector)
ivectors_path = 'ivectors_100/39D/'+ gaussians + '/' + input_features.split('/')[-1]
if not os.path.exists(ivectors_path):
os.makedirs(ivectors_path)
ivectors_path = ivectors_path + '/' + os.path.split(root)[1] + '_' + gaussians + '_100.ivec'
save_ivectors(ivectors, ivectors_path)
print "saved ivetors to '%s' " % ivectors_path
#############################################
ubm_hdf5 = bob.io.HDF5File(ubm_file)
ubm = bob.machine.GMMMachine(ubm_hdf5)
ubm.set_variance_thresholds(variance_threshold)
#train_enroller(input_ubm_features)
#project_ivectors(input_ubm_features)
#project_ivectors(input_emotion_train_features)
project_ivectors(input_emotion_test_features)