diff --git a/bob/bio/gmm/algorithm/GMM.py b/bob/bio/gmm/algorithm/GMM.py index 675910c9322bdde1d2a78a367b6df4d81856bfda..77c832ffd2ab0eea0332b7d280cb3c1060e1ff0a 100644 --- a/bob/bio/gmm/algorithm/GMM.py +++ b/bob/bio/gmm/algorithm/GMM.py @@ -216,8 +216,6 @@ class GMM (Algorithm): """Reads the model, which is a GMM machine""" return bob.learn.em.GMMMachine(bob.io.base.HDF5File(model_file)) - read_probe = read_feature - def score(self, model, probe): """Computes the score for the given model and the given probe using the scoring function from the config file""" assert isinstance(model, bob.learn.em.GMMMachine) @@ -267,11 +265,6 @@ class GMMRegular (GMM): ###################################################### ################ Feature comparison ################## - def read_probe(self, probe_file): - """Reads a feature from file, which is supposed to be a simple 2D array""" - return bob.bio.base.load(probe_file) - - def score(self, model, probe): """Computes the score for the given model and the given probe. The score are Log-Likelihood. diff --git a/bob/bio/gmm/algorithm/ISV.py b/bob/bio/gmm/algorithm/ISV.py index c2fbe409ea980c9a9817e5c85801139848d2ad75..1c5cba4670271ad48dd6eaff9dc0817790062dfa 100644 --- a/bob/bio/gmm/algorithm/ISV.py +++ b/bob/bio/gmm/algorithm/ISV.py @@ -151,15 +151,25 @@ class ISV (GMM): hdf5file = bob.io.base.HDF5File(feature_file) hdf5file.cd('gmmstats') gmmstats = bob.learn.em.GMMStats(hdf5file) - return gmmstats + hdf5file.cd('..') + Ux = hdf5file.read('Ux') + return [gmmstats, Ux] + + + def _check_projected(self, probe): + """Checks that the probe is of the desired type""" + assert isinstance(probe, (tuple, list)) + assert len(probe) == 2 + assert isinstance(probe[0], bob.learn.em.GMMStats) + assert isinstance(probe[1], numpy.ndarray) and probe[1].ndim == 1 and probe[1].dtype == numpy.float64 def enroll(self, enroll_features): """Performs ISV enrollment""" for feature in enroll_features: - assert isinstance(feature, bob.learn.em.GMMStats) + self._check_projected(feature) machine = bob.learn.em.ISVMachine(self.isvbase) - self.isv_trainer.enroll(machine, enroll_features, self.isv_enroll_iterations) + self.isv_trainer.enroll(machine, [f[0] for f in enroll_features], self.isv_enroll_iterations) # return the resulting gmm return machine @@ -172,27 +182,12 @@ class ISV (GMM): machine.isv_base = self.isvbase return machine - def read_probe(self, probe_file): - """Read the type of features that we require, namely GMMStats""" - hdf5file = bob.io.base.HDF5File(probe_file) - hdf5file.cd('gmmstats') - gmmstats = bob.learn.em.GMMStats(hdf5file) - hdf5file.cd('..') - Ux = hdf5file.read('Ux') - return [gmmstats, Ux] - - def _check_probe(self, probe): - """Checks that the probe is of the desired type""" - assert isinstance(probe, (tuple, list)) - assert len(probe) == 2 - assert isinstance(probe[0], bob.learn.em.GMMStats) - assert isinstance(probe[1], numpy.ndarray) and probe[1].ndim == 1 and probe[1].dtype == numpy.float64 def score(self, model, probe): """Computes the score for the given model and the given probe.""" assert isinstance(model, bob.learn.em.ISVMachine) - self._check_probe(probe) + self._check_projected(probe) gmmstats = probe[0] Ux = probe[1] @@ -201,7 +196,7 @@ class ISV (GMM): def score_for_multiple_probes(self, model, probes): """This function computes the score between the given model and several given probe files.""" assert isinstance(model, bob.learn.em.ISVMachine) - [self._check_probe(probe) for probe in probes] + [self._check_projected(probe) for probe in probes] if self.probe_fusion_function is not None: # When a multiple probe fusion function is selected, use it return Algorithm.score_for_multiple_probes(self, model, probes) diff --git a/bob/bio/gmm/algorithm/IVector.py b/bob/bio/gmm/algorithm/IVector.py index d6c4f0e51ae293cd2b0ddec42da2058ec7ef4889..9e21ca100d1847478c3587fe3cdefafac8bc69aa 100644 --- a/bob/bio/gmm/algorithm/IVector.py +++ b/bob/bio/gmm/algorithm/IVector.py @@ -71,10 +71,10 @@ class IVector (GMM): self.use_plda = use_plda self.subspace_dimension_of_t = subspace_dimension_of_t self.tv_training_iterations = tv_training_iterations - + self.ivector_trainer = bob.learn.em.IVectorTrainer(update_sigma=update_sigma) self.whitening_trainer = bob.learn.linear.WhiteningTrainer() - + self.lda_dim = lda_dim self.lda_trainer = bob.learn.linear.FisherLDATrainer(strip_to_rank=False) self.wccn_trainer = bob.learn.linear.WCCNTrainer() @@ -82,7 +82,7 @@ class IVector (GMM): self.plda_dim_F = plda_dim_F self.plda_dim_G = plda_dim_G self.plda_training_iterations = plda_training_iterations - + def _check_ivector(self, feature): @@ -131,7 +131,7 @@ class IVector (GMM): def train_projector(self, train_features, projector_file): """Train Projector and Enroller at the same time""" - + [self._check_feature(feature) for client in train_features for feature in client] train_features_flatten = [feature for client in train_features for feature in client] @@ -158,15 +158,15 @@ class IVector (GMM): self.train_whitener(train_ivectors_flatten) # whitening and length-normalizing i-vectors train_ivectors = [[self.project_whitening(ivec) for ivec in client] for client in train_ivectors] - + if self.use_lda: self.train_lda(train_ivectors) train_ivectors = [[self.project_lda(ivec) for ivec in client] for client in train_ivectors] - + if self.use_wccn: self.train_wccn(train_ivectors) train_ivectors = [[self.project_wccn(ivec) for ivec in client] for client in train_ivectors] - + if self.use_plda: self.train_plda(train_ivectors) @@ -191,7 +191,7 @@ class IVector (GMM): hdf5file.create_group('Whitener') hdf5file.cd('Whitener') self.whitener.save(hdf5file) - + if self.use_lda: hdf5file.cd('/') hdf5file.create_group('LDA') @@ -203,13 +203,13 @@ class IVector (GMM): hdf5file.create_group('WCCN') hdf5file.cd('WCCN') self.wccn.save(hdf5file) - + if self.use_plda: hdf5file.cd('/') hdf5file.create_group('PLDA') hdf5file.cd('PLDA') self.plda_base.save(hdf5file) - + def load_tv(self, tv_file): hdf5file = bob.io.base.HDF5File(tv_file) @@ -228,12 +228,12 @@ class IVector (GMM): def load_wccn(self, wccn_file): hdf5file = bob.io.base.HDF5File(wccn_file) self.wccn = bob.learn.linear.Machine(hdf5file) - + def load_plda(self, plda_file): hdf5file = bob.io.base.HDF5File(plda_file) self.plda_base = bob.learn.em.PLDABase(hdf5file) self.plda_machine = bob.learn.em.PLDAMachine(self.plda_base) - + def load_projector(self, projector_file): """Load the GMM and the ISV model from the same HDF5 file""" hdf5file = bob.io.base.HDF5File(projector_file) @@ -250,18 +250,18 @@ class IVector (GMM): # Load Whitening hdf5file.cd('/Whitener') self.load_whitener(hdf5file) - + if self.use_lda: # Load LDA hdf5file.cd('/LDA') self.load_lda(hdf5file) - - if self.use_wccn: + + if self.use_wccn: # Load WCCN hdf5file.cd('/WCCN') self.load_wccn(hdf5file) - if self.use_plda: + if self.use_plda: # Load PLDA hdf5file.cd('/PLDA') self.load_plda(hdf5file) @@ -338,9 +338,6 @@ class IVector (GMM): else: return bob.bio.base.load(model_file) - def read_probe(self, probe_file): - """read probe file which is an i-vector""" - return bob.bio.base.load(probe_file) def score(self, model, probe): """Computes the score for the given model and the given probe.""" diff --git a/bob/bio/gmm/algorithm/JFA.py b/bob/bio/gmm/algorithm/JFA.py index 2a5de18bc6870bb4596fe729fa00cfe25a790d8f..d610fbf848892411e1327d4b644df1764748fe71 100644 --- a/bob/bio/gmm/algorithm/JFA.py +++ b/bob/bio/gmm/algorithm/JFA.py @@ -114,8 +114,6 @@ class JFA (GMM): machine.jfa_base = self.jfa_base return machine - read_probe = read_feature - def score(self, model, probe): """Computes the score for the given model and the given probe""" assert isinstance(model, bob.learn.em.JFAMachine) diff --git a/bob/bio/gmm/test/test_algorithms.py b/bob/bio/gmm/test/test_algorithms.py index 9624cba6ba694164d353c1f657ed3b74e59eb12d..4eba483fb38e84a9c6cb7d371b72ad641c741541 100644 --- a/bob/bio/gmm/test/test_algorithms.py +++ b/bob/bio/gmm/test/test_algorithms.py @@ -119,7 +119,7 @@ def test_gmm(): _compare(model, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/gmm_model.hdf5'), gmm1.write_model, gmm1.read_model) # compare model with probe - probe = gmm1.read_probe(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/gmm_projected.hdf5')) + probe = gmm1.read_feature(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/gmm_projected.hdf5')) reference_score = -0.01676570 assert abs(gmm1.score(model, probe) - reference_score) < 1e-5, "The scores differ: %3.8f, %3.8f" % (gmm1.score(model, probe), reference_score) assert abs(gmm1.score_for_multiple_probes(model, [probe, probe]) - reference_score) < 1e-5 @@ -227,17 +227,17 @@ def test_isv(): assert len(projected) == 2 assert isinstance(projected[0], bob.learn.em.GMMStats) assert isinstance(projected[1], numpy.ndarray) - _compare_complex(projected, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/isv_projected.hdf5'), isv1.write_feature, isv1.read_probe) + _compare_complex(projected, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/isv_projected.hdf5'), isv1.write_feature, isv1.read_feature) # enroll model from random features random_features = utils.random_training_set((20,45), count=5, minimum=-5., maximum=5.) - enroll_features = [isv1.project(feature)[0] for feature in random_features] + enroll_features = [isv1.project(feature) for feature in random_features] model = isv1.enroll(enroll_features) assert isinstance(model, bob.learn.em.ISVMachine) _compare(model, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/isv_model.hdf5'), isv1.write_model, isv1.read_model) # compare model with probe - probe = isv1.read_probe(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/isv_projected.hdf5')) + probe = isv1.read_feature(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/isv_projected.hdf5')) reference_score = 0.02136784 assert abs(isv1.score(model, probe) - reference_score) < 1e-5, "The scores differ: %3.8f, %3.8f" % (isv1.score(model, probe), reference_score) # assert abs(isv1.score_for_multiple_probes(model, [probe]*4) - reference_score) < 1e-5, isv1.score_for_multiple_probes(model, [probe, probe]) @@ -321,7 +321,7 @@ def test_jfa(): _compare(model, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/jfa_model.hdf5'), jfa1.write_model, jfa1.read_model) # compare model with probe - probe = jfa1.read_probe(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/gmm_projected.hdf5')) + probe = jfa1.read_feature(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/gmm_projected.hdf5')) reference_score = 0.02225812 assert abs(jfa1.score(model, probe) - reference_score) < 1e-5, "The scores differ: %3.8f, %3.8f" % (jfa1.score(model, probe), reference_score) # TODO: implement that @@ -386,7 +386,7 @@ def test_ivector_cosine(): _compare(model, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector_model.hdf5'), ivec1.write_model, ivec1.read_model) # compare model with probe - probe = ivec1.read_probe(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector_projected.hdf5')) + probe = ivec1.read_feature(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector_projected.hdf5')) reference_score = -0.00187151 assert abs(ivec1.score(model, probe) - reference_score) < 1e-5, "The scores differ: %3.8f, %3.8f" % (ivec1.score(model, probe), reference_score) # TODO: implement that @@ -449,7 +449,7 @@ def test_ivector_plda(): _compare(model, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector2_model.hdf5'), ivec1.write_model, ivec1.read_model) # compare model with probe - probe = ivec1.read_probe(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector2_projected.hdf5')) + probe = ivec1.read_feature(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector2_projected.hdf5')) logger.info("%f" %ivec1.score(model, probe)) reference_score = 1.21879822 assert abs(ivec1.score(model, probe) - reference_score) < 1e-5, "The scores differ: %3.8f, %3.8f" % (ivec1.score(model, probe), reference_score) @@ -514,7 +514,7 @@ def test_ivector_lda_wccn_plda(): _compare(model, pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector3_model.hdf5'), ivec1.write_model, ivec1.read_model) # compare model with probe - probe = ivec1.read_probe(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector3_projected.hdf5')) + probe = ivec1.read_feature(pkg_resources.resource_filename('bob.bio.gmm.test', 'data/ivector3_projected.hdf5')) reference_score = 0.338051 assert abs(ivec1.score(model, probe) - reference_score) < 1e-5, "The scores differ: %3.8f, %3.8f" % (ivec1.score(model, probe), reference_score) assert abs(ivec1.score_for_multiple_probes(model, [probe, probe]) - reference_score) < 1e-5