├── ivector ├── test │ ├── __init__.py │ ├── script │ │ ├── __init__.py │ │ └── ivector_trainT.py │ └── util │ │ ├── __init__.py │ │ └── readers.py └── __init__.py ├── MANIFEST.in ├── data └── M256_MOBIO_ICB_2013_full.gmm ├── .gitignore ├── setup.py ├── README.rst └── bootstrap.py /ivector/test/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ivector/test/script/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ivector/test/util/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.rst bootstrap.py *.cfg 2 | -------------------------------------------------------------------------------- /data/M256_MOBIO_ICB_2013_full.gmm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagofrepereira2012/ivector_example/HEAD/data/M256_MOBIO_ICB_2013_full.gmm -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | *.pyc 4 | bin 5 | eggs 6 | parts 7 | .installed.cfg 8 | .mr.developer.cfg 9 | *.egg-info 10 | src 11 | develop-eggs 12 | -------------------------------------------------------------------------------- /ivector/__init__.py: -------------------------------------------------------------------------------- 1 | #see http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages 2 | __import__('pkg_resources').declare_namespace(__name__) 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #Tiago de Freitas Pereira 3 | #Thu Jun 13 09:00:55 BRT 2013 4 | 5 | from setuptools import setup, find_packages 6 | 7 | # The only thing we do in this file is to call the setup() function with all 8 | # parameters that define our package. 9 | setup( 10 | 11 | name='ivector.test', 12 | version='1.0.0a', 13 | description='IVector test', 14 | url='', 15 | license='GPLv3', 16 | author='Tiago de Freitas Pereira', 17 | author_email='tiagofrepereira@gmail.com', 18 | long_description=open('README.rst').read(), 19 | 20 | # This line is required for any distutils based packaging. 21 | packages=find_packages(), 22 | include_package_data = True, 23 | 24 | install_requires=[ 25 | "bob >= 1.2.0a", # base signal proc./machine learning library 26 | ], 27 | 28 | namespace_packages = [ 29 | 'ivector', 30 | ], 31 | 32 | entry_points={ 33 | 'console_scripts': [ 34 | 'ivector_trainT.py = ivector.test.script.ivector_trainT:main' 35 | ], 36 | }, 37 | 38 | classifiers = [ 39 | 'Development Status :: 5 - Production/Stable', 40 | 'Intended Audience :: Science/Research', 41 | 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 42 | 'Natural Language :: English', 43 | 'Programming Language :: Python', 44 | 'Topic :: Scientific/Engineering :: Artificial Intelligence', 45 | ], 46 | 47 | 48 | ) 49 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | =============================================================================== 2 | IVector sample 3 | =============================================================================== 4 | 5 | This package implements an IVector based system using the toolbox Bob. 6 | 7 | If you use this package and/or its results, please cite the following publications: 8 | 9 | 1. Bob as the core framework used to run the experiments:: 10 | 11 | @inproceedings{Anjos_ACMMM_2012, 12 | author = {A. Anjos AND L. El Shafey AND R. Wallace AND M. G\"unther AND C. McCool AND S. Marcel}, 13 | title = {Bob: a free signal processing and machine learning toolbox for researchers}, 14 | year = {2012}, 15 | month = oct, 16 | booktitle = {20th ACM Conference on Multimedia Systems (ACMMM), Nara, Japan}, 17 | publisher = {ACM Press}, 18 | } 19 | 20 | 21 | 22 | Installation 23 | ------------ 24 | 25 | First, clone the package from the github:: 26 | 27 | $ git clone https://github.com/tiagofrepereira2012/ivector_example/ 28 | 29 | This code uses the bob 1.2.0 using the mr.developer recipe; please edit the file ``buildout.cfg`` and set the ``prefixes`` variable to the correct path to your Bob installation:: 30 | 31 | $ prefixes = 32 | 33 | Finally, follow the recipe bellow:: 34 | 35 | $ /bin/python bootstrap.py 36 | $ ./bin/buildout 37 | 38 | 39 | User guide 40 | ---------- 41 | 42 | Until now it is developed only the training of the Total Variability Matrix. 43 | 44 | To gerenate the total variability matrix for each iteration:: 45 | 46 | $ ./bin/ivector_trainT.py --help 47 | 48 | 49 | Problems 50 | -------- 51 | 52 | In case of problems, please contact me. 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /ivector/test/util/readers.py: -------------------------------------------------------------------------------- 1 | ################################################################################### 2 | # @file readers.py # 3 | # @date Mai 03 2013 # 4 | # @author Mario Uliani Neto # 5 | # # 6 | # Copyright (C) 2013 CPqD - Research and Development Center in Telecommunications # 7 | # Campinas, Brazil # 8 | ################################################################################### 9 | 10 | 11 | import os 12 | import numpy 13 | import array 14 | 15 | 16 | def gmmread(fileUBM, N_MIXTURES, DIMENSION): 17 | """ 18 | Reads a binary file containing a GMM. 19 | Returns a vector with the GMM parameters (weights, means and variances) 20 | """ 21 | numberOfFloats = os.path.getsize(fileUBM)/4 # each parameter is a float 22 | file = open(fileUBM,mode = 'rb') # opens GMM file 23 | parameters_GMM = array.array('f') 24 | parameters_GMM.fromfile(file,numberOfFloats) 25 | parameters_GMM = numpy.array(parameters_GMM, dtype=numpy.float64) 26 | file.close() 27 | 28 | weights = [] 29 | means = [] 30 | variances = [] 31 | weights = parameters_GMM[0:256] 32 | for i in range(N_MIXTURES): 33 | means.append(parameters_GMM[(i)*DIMENSION*2+N_MIXTURES:(i)*DIMENSION*2+DIMENSION+N_MIXTURES]) 34 | variances.append(parameters_GMM[(i)*DIMENSION*2+DIMENSION+N_MIXTURES:(i)*DIMENSION*2+DIMENSION*2+N_MIXTURES]) 35 | 36 | UBM = [] 37 | UBM.append(weights) 38 | UBM.append(means) 39 | UBM.append(variances) 40 | 41 | return UBM 42 | 43 | 44 | 45 | def paramlistread(list_in, feature_dimension): 46 | """ 47 | Receives a text file with a list of feature files as input. 48 | Returns the feature arrays in the form of a list (each element corresponding to an entry of the input file) 49 | """ 50 | 51 | #Counting the number of frames 52 | #list_file = open(list_in, 'r') 53 | #linha = list_file.readline().strip() #reads first line (feature file name) 54 | #rows = 0 55 | #while len(linha) > 0 : 56 | #parameters = paramread(linha, feature_dimension) #reads current line's features 57 | #rows = rows + parameters.shape[0] 58 | #linha = list_file.readline().strip() #reads next line (feature file name) 59 | #list_file.close() 60 | 61 | 62 | #Reading 63 | list_file = open(list_in, 'r') 64 | linha = list_file.readline().strip() #reads first line (feature file name) 65 | list_parameters = [] 66 | i = 0 67 | while len(linha) > 0 : 68 | parameters = paramread(linha, feature_dimension) #reads current line's features 69 | list_parameters.append(parameters) 70 | linha = list_file.readline().strip() #reads next line (feature file name) 71 | list_file.close() 72 | 73 | return list_parameters 74 | 75 | 76 | 77 | def paramread(arquivo, feature_dimension): 78 | """ 79 | Reads a feature file. 80 | Returns an array with the features. 81 | """ 82 | 83 | numberOfFloats = os.path.getsize(arquivo)/4 # each feature is a float 84 | file = open(arquivo,mode = 'rb') # opens feature input file 85 | parameters = array.array('f') 86 | parameters.fromfile(file,numberOfFloats) 87 | parameters = numpy.array(parameters, dtype=numpy.float64) 88 | file.close() 89 | 90 | parameters = parameters.reshape(len(parameters)/feature_dimension, feature_dimension) 91 | 92 | return parameters 93 | 94 | 95 | 96 | def Tmatrix_write_bob(T_Matrix,Tmatrix_file): 97 | """ Writes total variability matrix to file. 98 | 99 | T_Matrix: structure containing the total variability matrix 100 | Tmatrix_file: total variability matrix output file name 101 | 102 | """ 103 | import struct 104 | import bob 105 | 106 | bob.db.utils.makedirs_safe(os.path.dirname(Tmatrix_file)) 107 | out_file = open(Tmatrix_file,"wb") 108 | for j in range(T_Matrix.dim_cd): 109 | s = struct.pack('d'*T_Matrix.dim_rt, *T_Matrix.t[j]) 110 | out_file.write(s) 111 | out_file.close() 112 | 113 | return 114 | 115 | -------------------------------------------------------------------------------- /bootstrap.py: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # 3 | # Copyright (c) 2006 Zope Foundation and Contributors. 4 | # All Rights Reserved. 5 | # 6 | # This software is subject to the provisions of the Zope Public License, 7 | # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. 8 | # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED 9 | # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 10 | # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 11 | # FOR A PARTICULAR PURPOSE. 12 | # 13 | ############################################################################## 14 | """Bootstrap a buildout-based project 15 | 16 | Simply run this script in a directory containing a buildout.cfg. 17 | The script accepts buildout command-line options, so you can 18 | use the -c option to specify an alternate configuration file. 19 | """ 20 | 21 | import os, shutil, sys, tempfile 22 | from optparse import OptionParser 23 | 24 | tmpeggs = tempfile.mkdtemp() 25 | 26 | usage = '''\ 27 | [DESIRED PYTHON FOR BUILDOUT] bootstrap.py [options] 28 | 29 | Bootstraps a buildout-based project. 30 | 31 | Simply run this script in a directory containing a buildout.cfg, using the 32 | Python that you want bin/buildout to use. 33 | 34 | Note that by using --setup-source and --download-base to point to 35 | local resources, you can keep this script from going over the network. 36 | ''' 37 | 38 | parser = OptionParser(usage=usage) 39 | parser.add_option("-v", "--version", help="use a specific zc.buildout version") 40 | 41 | parser.add_option("-t", "--accept-buildout-test-releases", 42 | dest='accept_buildout_test_releases', 43 | action="store_true", default=False, 44 | help=("Normally, if you do not specify a --version, the " 45 | "bootstrap script and buildout gets the newest " 46 | "*final* versions of zc.buildout and its recipes and " 47 | "extensions for you. If you use this flag, " 48 | "bootstrap and buildout will get the newest releases " 49 | "even if they are alphas or betas.")) 50 | parser.add_option("-c", "--config-file", 51 | help=("Specify the path to the buildout configuration " 52 | "file to be used.")) 53 | parser.add_option("-f", "--find-links", 54 | help=("Specify a URL to search for buildout releases")) 55 | 56 | 57 | options, args = parser.parse_args() 58 | 59 | ###################################################################### 60 | # load/install distribute 61 | 62 | to_reload = False 63 | try: 64 | import pkg_resources, setuptools 65 | if not hasattr(pkg_resources, '_distribute'): 66 | to_reload = True 67 | raise ImportError 68 | except ImportError: 69 | ez = {} 70 | 71 | try: 72 | from urllib.request import urlopen 73 | except ImportError: 74 | from urllib2 import urlopen 75 | 76 | exec(urlopen('http://python-distribute.org/distribute_setup.py').read(), ez) 77 | setup_args = dict(to_dir=tmpeggs, download_delay=0, no_fake=True) 78 | ez['use_setuptools'](**setup_args) 79 | 80 | if to_reload: 81 | reload(pkg_resources) 82 | import pkg_resources 83 | # This does not (always?) update the default working set. We will 84 | # do it. 85 | for path in sys.path: 86 | if path not in pkg_resources.working_set.entries: 87 | pkg_resources.working_set.add_entry(path) 88 | 89 | ###################################################################### 90 | # Install buildout 91 | 92 | ws = pkg_resources.working_set 93 | 94 | cmd = [sys.executable, '-c', 95 | 'from setuptools.command.easy_install import main; main()', 96 | '-mZqNxd', tmpeggs] 97 | 98 | find_links = os.environ.get( 99 | 'bootstrap-testing-find-links', 100 | options.find_links or 101 | ('http://downloads.buildout.org/' 102 | if options.accept_buildout_test_releases else None) 103 | ) 104 | if find_links: 105 | cmd.extend(['-f', find_links]) 106 | 107 | distribute_path = ws.find( 108 | pkg_resources.Requirement.parse('distribute')).location 109 | 110 | requirement = 'zc.buildout' 111 | version = options.version 112 | if version is None and not options.accept_buildout_test_releases: 113 | # Figure out the most recent final version of zc.buildout. 114 | import setuptools.package_index 115 | _final_parts = '*final-', '*final' 116 | def _final_version(parsed_version): 117 | for part in parsed_version: 118 | if (part[:1] == '*') and (part not in _final_parts): 119 | return False 120 | return True 121 | index = setuptools.package_index.PackageIndex( 122 | search_path=[distribute_path]) 123 | if find_links: 124 | index.add_find_links((find_links,)) 125 | req = pkg_resources.Requirement.parse(requirement) 126 | if index.obtain(req) is not None: 127 | best = [] 128 | bestv = None 129 | for dist in index[req.project_name]: 130 | distv = dist.parsed_version 131 | if _final_version(distv): 132 | if bestv is None or distv > bestv: 133 | best = [dist] 134 | bestv = distv 135 | elif distv == bestv: 136 | best.append(dist) 137 | if best: 138 | best.sort() 139 | version = best[-1].version 140 | if version: 141 | requirement = '=='.join((requirement, version)) 142 | cmd.append(requirement) 143 | 144 | import subprocess 145 | if subprocess.call(cmd, env=dict(os.environ, PYTHONPATH=distribute_path)) != 0: 146 | raise Exception( 147 | "Failed to execute command:\n%s", 148 | repr(cmd)[1:-1]) 149 | 150 | ###################################################################### 151 | # Import and run buildout 152 | 153 | ws.add_entry(tmpeggs) 154 | ws.require(requirement) 155 | import zc.buildout.buildout 156 | 157 | if not [a for a in args if '=' not in a]: 158 | args.append('bootstrap') 159 | 160 | # if -c was provided, we push it back into args for buildout' main function 161 | if options.config_file is not None: 162 | args[0:0] = ['-c', options.config_file] 163 | 164 | zc.buildout.buildout.main(args) 165 | shutil.rmtree(tmpeggs) 166 | -------------------------------------------------------------------------------- /ivector/test/script/ivector_trainT.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #Tiago de Freitas Pereira 3 | #Wed Jun 12 14:30:00 BRT 2013 4 | 5 | """ 6 | """ 7 | 8 | import os, sys 9 | import argparse 10 | import bob 11 | import numpy 12 | 13 | from ivector.test.util import readers 14 | 15 | def main(): 16 | 17 | parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 18 | 19 | parser.add_argument('-o', '--output-dir', metavar='DIR', type=str, dest='output_dir', default='./EXPERIMENTS/T/', help='Output directory (defaults to "%(default)s")') 20 | 21 | parser.add_argument('-b', '--ubm_file', metavar='DIR', type=str, dest='ubm_file', default='./data/M256_MOBIO_ICB_2013_full.gmm', help='UBM directory (defaults to "%(default)s")') 22 | 23 | parser.add_argument('-l', '--training_files', metavar='DIR', type=str, dest='training_files', default='./data/MOBIO_ICB_2013_train_full_M256_Tmatrix.list', help='UBM directory (defaults to "%(default)s")') 24 | 25 | parser.add_argument('-m', '--n_mixtures', metavar='HIP', type=int, dest='n_mixtures', default=256, help='Number of gaussians (defaults to "%(default)s")') 26 | 27 | parser.add_argument('-f', '--feature_dim', metavar='HIP', type=int, dest='feature_dim', default=40, help='Features dimension (defaults to "%(default)s")') 28 | 29 | parser.add_argument('-t', '--t_dim', metavar='HIP', type=int, dest='t_dim', default=50, help='Total variability space dimension (defaults to "%(default)s")') 30 | 31 | parser.add_argument('-i', '--iterations', metavar='HIP', type=int, dest='iterations', default=10, help='Number of iterations to train the total variability matrix (defaults to "%(default)s")') 32 | 33 | parser.add_argument('-u', '--update_sigma', action='store_true', dest='update_sigma', default=True, help='Update sigma param to train the total variability matrix (T matrix) (defaults to "%(default)s")') 34 | 35 | parser.add_argument('-a', '--fake_data', action='store_true', dest='fake_data', default=False, help='Use fake data? (defaults to "%(default)s")') 36 | 37 | parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False, help="Increase some verbosity") 38 | 39 | args = parser.parse_args() 40 | 41 | ######################## 42 | # Loading Hiperparameters 43 | ######################### 44 | OUTPUT_DIR = args.output_dir # file containing UBM 45 | UBM_FILE = args.ubm_file # file containing UBM 46 | TRAINING_FILES = args.training_files 47 | 48 | N_MIXTURES = args.n_mixtures # Number of gaussian mixtures 49 | FEATURE_DIMENSION = args.feature_dim # Feature vector dimension 50 | T_MATRIX_DIM = args.t_dim # Total variability space dimension 51 | T_MATRIX_ITERA = args.iterations # Number of iterations to train the total variability matrix 52 | UPDATE_SIGMA = args.update_sigma # Update sigma param to train the total variability matrix (T matrix) - True or False 53 | FAKE_DATA = args.fake_data 54 | TOTAL_FAKE_DATA = 100 55 | 56 | VERBOSE = args.verbose 57 | 58 | ########################## 59 | # Reading the UBM 60 | ########################## 61 | if(VERBOSE): 62 | print("Reading the UBM ....") 63 | 64 | GMM_read = readers.gmmread(UBM_FILE, N_MIXTURES, FEATURE_DIMENSION) 65 | 66 | UBM = bob.machine.GMMMachine(N_MIXTURES, FEATURE_DIMENSION) # creates an object to store the UBM 67 | UBM.weights = GMM_read[0] 68 | UBM.means = GMM_read[1] 69 | UBM.variances = GMM_read[2] 70 | 71 | 72 | ########################## 73 | # Reads list of feature files used to train the total variability matrix 74 | ########################## 75 | if(not FAKE_DATA): 76 | if(VERBOSE): 77 | print("Reading the T-matrix files ....") 78 | T_Matrix_files = readers.paramlistread(TRAINING_FILES, FEATURE_DIMENSION) 79 | else: 80 | 81 | if(FAKE_DATA): 82 | print("Loading fake data ....") 83 | 84 | T_Matrix_files = [] 85 | for i in range(TOTAL_FAKE_DATA): 86 | T_Matrix_files.append( numpy.random.rand(TOTAL_FAKE_DATA,FEATURE_DIMENSION)) 87 | 88 | ########################## 89 | # Computes Baum-Welch statistics for all T-matrix training files 90 | ########################## 91 | if(VERBOSE): 92 | print("Compute statistics for T Matrix training files ....") 93 | 94 | stats = [] 95 | for i in range(len(T_Matrix_files)): 96 | quadros = T_Matrix_files[i] 97 | s = bob.machine.GMMStats(N_MIXTURES,FEATURE_DIMENSION) 98 | for j in range(len(quadros)): 99 | UBM.acc_statistics(quadros[j],s) 100 | stats.append(s) 101 | 102 | 103 | ########################## 104 | #This code does not work properly. 105 | # The "t" and the "sigma" provided to the iVectorMachine is ignored by the implementation. The initialization is always "random". 106 | ########################## 107 | """ 108 | # Training steps... 109 | print("Training T-Matrix ...") 110 | T_Matrix = bob.machine.IVectorMachine(UBM,T_MATRIX_DIM) 111 | T_Matrix.variance_threshold = 1e-5 112 | for i in range(T_MATRIX_ITERA): 113 | print " Executing iteration %d ..." % (i+1) 114 | if i > 0: 115 | T_Matrix.t = t_temp 116 | T_Matrix.sigma = sigma_temp 117 | trainer = bob.trainer.IVectorTrainer(UPDATE_SIGMA, 0.0, 1, 0) # ( update_sigma, convergence_threshold, max_iterations, compute_likelihood) 118 | trainer.train(T_Matrix,stats) 119 | # saves the total variability matrix 120 | t_temp = T_Matrix.t 121 | sigma_temp = T_Matrix.sigma 122 | output_file_Tmatrix = 'matriz_T_M{0}_L{1}_T{2}_it{3}.T'.format(N_MIXTURES,FEATURE_DIMENSION,T_MATRIX_DIM,i+1) 123 | output_file_Tmatrix = os.path.join(OUTPUT_DIR, output_file_Tmatrix) 124 | readers.Tmatrix_write_bob(T_Matrix,output_file_Tmatrix) 125 | """ 126 | 127 | ########################## 128 | # THIS CODE WORKS 129 | #Training the total variability matrix. 130 | ########################## 131 | # Training steps... 132 | if(VERBOSE): 133 | print "Training T-Matrix with %d iterations ..." % T_MATRIX_ITERA 134 | 135 | 136 | for i in range(T_MATRIX_ITERA): 137 | if(VERBOSE): 138 | print " Executing iteration %d ..." % (i+1) 139 | 140 | T_Matrix = bob.machine.IVectorMachine(UBM,T_MATRIX_DIM) 141 | trainer = bob.trainer.IVectorTrainer(UPDATE_SIGMA, 0.0, i+1, 0) # ( update_sigma, convergence_threshold, max_iterations, compute_likelihood) 142 | trainer.train(T_Matrix,stats) 143 | 144 | #saves the total variability matrix 145 | output_file_Tmatrix = 'matriz_T_M{0}_L{1}_T{2}_it{3}.T'.format(N_MIXTURES,FEATURE_DIMENSION,T_MATRIX_DIM,i+1) 146 | output_file_Tmatrix = os.path.join(OUTPUT_DIR, output_file_Tmatrix) 147 | readers.Tmatrix_write_bob(T_Matrix,output_file_Tmatrix) 148 | 149 | 150 | 151 | 152 | 153 | 154 | if __name__ == "__main__": 155 | main() 156 | --------------------------------------------------------------------------------