def test_enroll(): # Load the UBM ubm = GMMMachine.from_hdf5( pkg_resources.resource_filename("bob.bio.gmm.test", "data/gmm_ubm.hdf5")) # Create a GMM object with that UBM gmm1 = GMM( number_of_gaussians=2, enroll_update_means=True, enroll_update_variances=True, ) gmm1.ubm = ubm # Enroll the biometric reference from random features enroll = utils.random_training_set((20, 45), 5, -5.0, 5.0, seed=seed_value) biometric_reference = gmm1.enroll(enroll) assert not biometric_reference.is_similar_to(biometric_reference.ubm) assert isinstance(biometric_reference, GMMMachine) reference_file = pkg_resources.resource_filename("bob.bio.gmm.test", "data/gmm_enrolled.hdf5") if regenerate_refs: gmm1.write_biometric_reference(biometric_reference, reference_file) # Compare to pre-generated file gmm2 = gmm1.read_biometric_reference(reference_file) assert biometric_reference.is_similar_to(gmm2) with tempfile.NamedTemporaryFile(prefix="bob_", suffix="_bioref.hdf5") as fd: temp_file = fd.name gmm1.write_biometric_reference(biometric_reference, temp_file) assert GMMMachine.from_hdf5(temp_file, ubm).is_similar_to(gmm2)
def test_training(): """Tests the generation of the UBM.""" # Set a small training iteration count gmm1 = GMM( number_of_gaussians=2, kmeans_training_iterations=5, ubm_training_iterations=5, init_seed=seed_value, kmeans_oversampling_factor=2, ) train_data = utils.random_training_set((100, 45), count=5, minimum=-5.0, maximum=5.0) train_data = numpy.vstack(train_data) # Train the UBM (projector) gmm1.fit(train_data) # Test saving and loading of projector with tempfile.NamedTemporaryFile(prefix="bob_", suffix="_model.hdf5") as fd: temp_file = fd.name gmm1.save_model(temp_file) reference_file = pkg_resources.resource_filename( "bob.bio.gmm.test", "data/gmm_ubm.hdf5") if regenerate_refs: gmm1.save_model(reference_file) gmm2 = GMM(number_of_gaussians=2) gmm2.load_model(temp_file) ubm_reference = GMMMachine.from_hdf5(reference_file) assert gmm2.ubm.is_similar_to(ubm_reference)
def test_score(): gmm1 = GMM(number_of_gaussians=2) gmm1.load_model( pkg_resources.resource_filename("bob.bio.gmm.test", "data/gmm_ubm.hdf5")) biometric_reference = GMMMachine.from_hdf5( pkg_resources.resource_filename("bob.bio.gmm.test", "data/gmm_enrolled.hdf5"), ubm=gmm1.ubm, ) probe = GMMStats.from_hdf5( pkg_resources.resource_filename("bob.bio.gmm.test", "data/gmm_projected.hdf5")) probe_data = utils.random_array((20, 45), -5.0, 5.0, seed=seed_value) reference_score = 0.6509 numpy.testing.assert_almost_equal(gmm1.score(biometric_reference, probe), reference_score, decimal=5) multi_refs = gmm1.score_multiple_biometric_references( [biometric_reference, biometric_reference, biometric_reference], probe) assert multi_refs.shape == (3, 1), multi_refs.shape numpy.testing.assert_almost_equal(multi_refs, reference_score, decimal=5) # With not projected data numpy.testing.assert_almost_equal(gmm1.score(biometric_reference, probe_data), reference_score, decimal=5)
def test_gmm_ML_1(): """Trains a GMMMachine with ML_GMMTrainer""" ar = load_array( resource_filename("bob.learn.em", "data/faithful.torch3_f64.hdf5")) gmm_ref = GMMMachine.from_hdf5( HDF5File(resource_filename("bob.learn.em", "data/gmm_ML.hdf5"), "r")) for transform in (to_numpy, to_dask_array): ar = transform(ar) gmm = loadGMM() # test rng handling gmm.convergence_threshold = 0.001 gmm.update_means = True gmm.update_variances = True gmm.update_weights = True gmm.random_state = np.random.RandomState(seed=12345) gmm = gmm.fit(ar) gmm = loadGMM() gmm.convergence_threshold = 0.001 gmm.update_means = True gmm.update_variances = True gmm.update_weights = True # Generate reference # gmm.save(HDF5File(resource_filename("bob.learn.em", "data/gmm_ML.hdf5"), "w")) gmm = gmm.fit(ar) assert_gmm_equal(gmm, gmm_ref)
def load_model(self, ubm_file): """Loads the projector (UBM) from a file.""" hdf5file = HDF5File(ubm_file, "r") logger.debug("Loading model from file '%s'", ubm_file) # Read the UBM self.ubm = GMMMachine.from_hdf5(hdf5file) self.ubm.variance_thresholds = self.variance_threshold
def test_gmm_MAP_1(): # Train a GMMMachine with MAP_GMMTrainer ar = load_array( resource_filename("bob.learn.em", "data/faithful.torch3_f64.hdf5")) # test with rng gmmprior = GMMMachine.from_hdf5( HDF5File(resource_filename("bob.learn.em", "data/gmm_ML.hdf5"), "r")) gmm = GMMMachine.from_hdf5( HDF5File(resource_filename("bob.learn.em", "data/gmm_ML.hdf5"), "r"), ubm=gmmprior, ) gmm.update_means = True gmm.update_variances = False gmm.update_weights = False rng = np.random.RandomState(seed=12345) gmm.random_state = rng gmm = gmm.fit(ar) gmmprior = GMMMachine.from_hdf5( HDF5File(resource_filename("bob.learn.em", "data/gmm_ML.hdf5"), "r")) gmm = GMMMachine.from_hdf5( HDF5File(resource_filename("bob.learn.em", "data/gmm_ML.hdf5"), "r"), ubm=gmmprior, ) gmm.update_means = True gmm.update_variances = False gmm.update_weights = False # Generate reference # gmm.save(HDF5File(resource_filename("bob.learn.em", "data/gmm_MAP.hdf5"), "w")) gmm_ref = GMMMachine.from_hdf5( HDF5File(resource_filename("bob.learn.em", "data/gmm_MAP.hdf5"), "r")) for transform in (to_numpy, to_dask_array): ar = transform(ar) gmm = gmm.fit(ar) np.testing.assert_almost_equal(gmm.means, gmm_ref.means, decimal=3) np.testing.assert_almost_equal(gmm.variances, gmm_ref.variances, decimal=3) np.testing.assert_almost_equal(gmm.weights, gmm_ref.weights, decimal=3)
def test_projector(): """Tests the projector.""" # Load the UBM gmm1 = GMM(number_of_gaussians=2) gmm1.ubm = GMMMachine.from_hdf5( pkg_resources.resource_filename("bob.bio.gmm.test", "data/gmm_ubm.hdf5")) # Generate and project random feature feature = utils.random_array((20, 45), -5.0, 5.0, seed=seed_value) projected = gmm1.project(feature) assert isinstance(projected, GMMStats) reference_file = pkg_resources.resource_filename( "bob.bio.gmm.test", "data/gmm_projected.hdf5") if regenerate_refs: projected.save(reference_file) reference = GMMStats.from_hdf5(reference_file) assert projected.is_similar_to(reference)
def custom_enrolled_load_fn(self, path): return GMMMachine.from_hdf5(path, ubm=self)
def read_biometric_reference(self, model_file): """Reads an enrolled reference model, which is a MAP GMMMachine.""" return GMMMachine.from_hdf5(HDF5File(model_file, "r"), ubm=self)
def test_GMMMachine(): # Test a GMMMachine basic features weights = np.array([0.5, 0.5], "float64") weights2 = np.array([0.6, 0.4], "float64") means = np.array([[3, 70, 0], [4, 72, 0]], "float64") means2 = np.array([[3, 7, 0], [4, 72, 0]], "float64") variances = np.array([[1, 10, 1], [2, 5, 2]], "float64") variances2 = np.array([[10, 10, 1], [2, 5, 2]], "float64") varianceThresholds = np.array([[0, 0, 0], [0, 0, 0]], "float64") varianceThresholds2 = np.array([[0.0005, 0.0005, 0.0005], [0, 0, 0]], "float64") # Initializes a GMMMachine gmm = GMMMachine(n_gaussians=2) # Sets the weights, means, variances and varianceThresholds and # Checks correctness gmm.weights = weights gmm.means = means gmm.variances = variances gmm.variance_thresholds = varianceThresholds assert gmm.shape == (2, 3) np.testing.assert_equal(gmm.weights, weights) np.testing.assert_equal(gmm.means, means) np.testing.assert_equal(gmm.variances, variances) np.testing.assert_equal(gmm.variance_thresholds, varianceThresholds) newMeans = np.array([[3, 70, 2], [4, 72, 2]], "float64") newVariances = np.array([[1, 1, 1], [2, 2, 2]], "float64") # Checks particular varianceThresholds-related methods varianceThresholds1D = np.array([0.3, 1, 0.5], "float64") gmm.variance_thresholds = varianceThresholds1D np.testing.assert_equal(gmm.variance_thresholds, varianceThresholds1D) gmm.variance_thresholds = 0.005 np.testing.assert_equal(gmm.variance_thresholds, 0.005) gmm.means = newMeans gmm.variances = newVariances np.testing.assert_equal(gmm.means, newMeans) np.testing.assert_equal(gmm.variances, newVariances) # Checks comparison gmm2 = deepcopy(gmm) gmm3 = GMMMachine(n_gaussians=2) gmm3.weights = weights2 gmm3.means = means gmm3.variances = variances gmm3.variance_thresholds = varianceThresholds gmm4 = GMMMachine(n_gaussians=2) gmm4.weights = weights gmm4.means = means2 gmm4.variances = variances gmm4.variance_thresholds = varianceThresholds gmm5 = GMMMachine(n_gaussians=2) gmm5.weights = weights gmm5.means = means gmm5.variances = variances2 gmm5.variance_thresholds = varianceThresholds gmm6 = GMMMachine(n_gaussians=2) gmm6.weights = weights gmm6.means = means gmm6.variances = variances gmm6.variance_thresholds = varianceThresholds2 assert_gmm_equal(gmm, gmm2) assert (gmm != gmm2) is False assert gmm.is_similar_to(gmm2) assert gmm != gmm3 assert gmm.is_similar_to(gmm3) is False assert gmm != gmm4 assert gmm.is_similar_to(gmm4) is False assert gmm != gmm5 assert gmm.is_similar_to(gmm5) is False assert gmm != gmm6 assert gmm.is_similar_to(gmm6) is False # Saving and loading with tempfile.NamedTemporaryFile(suffix=".hdf5") as f: filename = f.name gmm.save(HDF5File(filename, "w")) # Using from_hdf5 gmm1 = GMMMachine.from_hdf5(HDF5File(filename, "r")) assert type(gmm1.n_gaussians) is np.int64 assert type(gmm1.update_means) is np.bool_ assert type(gmm1.update_variances) is np.bool_ assert type(gmm1.update_weights) is np.bool_ assert type(gmm1.trainer) is str assert gmm1.ubm is None assert_gmm_equal(gmm, gmm1) # Using load gmm1 = GMMMachine(n_gaussians=gmm.n_gaussians) gmm1.load(HDF5File(filename, "r")) assert type(gmm1.n_gaussians) is np.int64 assert type(gmm1.update_means) is np.bool_ assert type(gmm1.update_variances) is np.bool_ assert type(gmm1.update_weights) is np.bool_ assert type(gmm1.trainer) is str assert gmm1.ubm is None assert_gmm_equal(gmm, gmm1) with tempfile.NamedTemporaryFile(suffix=".hdf5") as f: filename = f.name gmm.save(filename) gmm1 = GMMMachine.from_hdf5(filename) assert_gmm_equal(gmm, gmm1) # Weights n_gaussians = 5 machine = GMMMachine(n_gaussians) default_weights = np.full(shape=(n_gaussians, ), fill_value=1.0 / n_gaussians) default_log_weights = np.full(shape=(n_gaussians, ), fill_value=np.log(1.0 / n_gaussians)) # Test weights getting and setting np.testing.assert_almost_equal(machine.weights, default_weights) np.testing.assert_almost_equal(machine.log_weights, default_log_weights) modified_weights = default_weights modified_weights[:n_gaussians // 2] = (1 / n_gaussians) / 2 modified_weights[n_gaussians // 2 + n_gaussians % 2:] = (1 / n_gaussians) * 1.5 # Ensure setter works (log_weights is updated correctly) machine.weights = modified_weights np.testing.assert_almost_equal(machine.weights, modified_weights) np.testing.assert_almost_equal(machine.log_weights, np.log(modified_weights))
def read_biometric_reference(self, model_file): """Reads an enrolled reference model, which is a MAP GMMMachine.""" if self.ubm is None: raise ValueError( "You must load a UBM before reading a biometric reference.") return GMMMachine.from_hdf5(HDF5File(model_file, "r"), ubm=self.ubm)