Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
def test_GMMStats():
    # Test a GMMStats
    # Initializes a GMMStats
    n_gaussians = 2
    n_features = 3
    gs = GMMStats(n_gaussians, n_features)
    log_likelihood = -3.0
    T = 57
    n = np.array([4.37, 5.31], "float64")
    sumpx = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "float64")
    sumpxx = np.array([[10.0, 20.0, 30.0], [40.0, 50.0, 60.0]], "float64")
    gs.log_likelihood = log_likelihood
    gs.t = T
    gs.n = n
    gs.sum_px = sumpx
    gs.sum_pxx = sumpxx
    np.testing.assert_equal(gs.log_likelihood, log_likelihood)
    np.testing.assert_equal(gs.t, T)
    np.testing.assert_equal(gs.n, n)
    np.testing.assert_equal(gs.sum_px, sumpx)
    np.testing.assert_equal(gs.sum_pxx, sumpxx)
    np.testing.assert_equal(gs.shape, (n_gaussians, n_features))

    # Saves and reads from file using `from_hdf5`
    filename = str(tempfile.mkstemp(".hdf5")[1])
    gs.save(HDF5File(filename, "w"))
    gs_loaded = GMMStats.from_hdf5(HDF5File(filename, "r"))
    assert gs == gs_loaded
    assert (gs != gs_loaded) is False
    assert gs.is_similar_to(gs_loaded)

    assert type(gs_loaded.n_gaussians) is np.int64
    assert type(gs_loaded.n_features) is np.int64
    assert type(gs_loaded.log_likelihood) is np.float64

    # Saves and load from file using `load`
    filename = str(tempfile.mkstemp(".hdf5")[1])
    gs.save(hdf5=HDF5File(filename, "w"))
    gs_loaded = GMMStats(n_gaussians, n_features)
    gs_loaded.load(HDF5File(filename, "r"))
    assert gs == gs_loaded
    assert (gs != gs_loaded) is False
    assert gs.is_similar_to(gs_loaded)

    # Makes them different
    gs_loaded.t = 58
    assert (gs == gs_loaded) is False
    assert gs != gs_loaded
    assert not (gs.is_similar_to(gs_loaded))

    # Accumulates from another GMMStats
    gs2 = GMMStats(n_gaussians, n_features)
    gs2.log_likelihood = log_likelihood
    gs2.t = T
    gs2.n = n.copy()
    gs2.sum_px = sumpx.copy()
    gs2.sum_pxx = sumpxx.copy()
    gs2 += gs
    np.testing.assert_equal(gs2.log_likelihood, 2 * log_likelihood)
    np.testing.assert_equal(gs2.t, 2 * T)
    np.testing.assert_almost_equal(gs2.n, 2 * n, decimal=8)
    np.testing.assert_almost_equal(gs2.sum_px, 2 * sumpx, decimal=8)
    np.testing.assert_almost_equal(gs2.sum_pxx, 2 * sumpxx, decimal=8)

    # Re-init and checks for zeros
    gs_loaded.init_fields()
    np.testing.assert_equal(gs_loaded.log_likelihood, 0)
    np.testing.assert_equal(gs_loaded.t, 0)
    np.testing.assert_equal(gs_loaded.n, np.zeros((n_gaussians, )))
    np.testing.assert_equal(gs_loaded.sum_px,
                            np.zeros((n_gaussians, n_features)))
    np.testing.assert_equal(gs_loaded.sum_pxx,
                            np.zeros((n_gaussians, n_features)))
    # Resize and checks size
    assert gs_loaded.shape == (n_gaussians, n_features)
    gs_loaded.resize(4, 5)
    assert gs_loaded.shape == (4, 5)
    assert gs_loaded.sum_px.shape[0] == 4
    assert gs_loaded.sum_px.shape[1] == 5

    # Clean-up
    os.unlink(filename)