Beispiel #1
0
def test_gmm_MAP_2():

  # Train a GMMMachine with MAP_GMMTrainer and compare with matlab reference

  data = bob.io.base.load(datafile('data.hdf5', __name__, path="../data/"))
  data = data.reshape((1, data.shape[0])) # make a 2D array out of it
  means = bob.io.base.load(datafile('means.hdf5', __name__, path="../data/"))
  variances = bob.io.base.load(datafile('variances.hdf5', __name__, path="../data/"))
  weights = bob.io.base.load(datafile('weights.hdf5', __name__, path="../data/"))

  gmm = GMMMachine(2,50)
  gmm.means = means
  gmm.variances = variances
  gmm.weights = weights

  map_adapt = MAP_GMMTrainer(update_means=True, update_variances=False, update_weights=False, mean_var_update_responsibilities_threshold=0.,prior_gmm=gmm, relevance_factor=4.)

  gmm_adapted = GMMMachine(2,50)
  gmm_adapted.means = means
  gmm_adapted.variances = variances
  gmm_adapted.weights = weights

  #map_adapt.max_iterations = 1
  #map_adapt.train(gmm_adapted, data)
  bob.learn.em.train(map_adapt, gmm_adapted, data, max_iterations = 1)

  new_means = bob.io.base.load(datafile('new_adapted_mean.hdf5', __name__, path="../data/"))

 # print new_means[0,:]
 # print gmm_adapted.means[:,0]

  # Compare to matlab reference
  assert equals(new_means[0,:], gmm_adapted.means[:,0], 1e-4)
  assert equals(new_means[1,:], gmm_adapted.means[:,1], 1e-4)
Beispiel #2
0
def test_gmm_test():
    # Tests a GMMMachine by computing scores against a model and comparing to a reference

    ar = load_array(resource_filename("bob.learn.em", "data/dataforMAP.hdf5"))

    # Initialize GMMMachine
    n_gaussians = 5
    gmm = GMMMachine(n_gaussians)
    gmm.means = load_array(
        resource_filename("bob.learn.em", "data/meansAfterML.hdf5"))
    gmm.variances = load_array(
        resource_filename("bob.learn.em", "data/variancesAfterML.hdf5"))
    gmm.weights = load_array(
        resource_filename("bob.learn.em", "data/weightsAfterML.hdf5"))

    threshold = 0.001
    gmm.variance_thresholds = threshold

    # Test against the model
    score_mean_ref = -1.50379e06
    for transform in (to_numpy, to_dask_array):
        ar = transform(ar)
        score = gmm.log_likelihood(ar).sum()
        score /= len(ar)

        # Compare current results to torch3vision
        assert abs(score - score_mean_ref) / score_mean_ref < 1e-4
Beispiel #3
0
def test_ISVBase():

  # Creates a UBM
  weights = numpy.array([0.4, 0.6], 'float64')
  means = numpy.array([[1, 6, 2], [4, 3, 2]], 'float64')
  variances = numpy.array([[1, 2, 1], [2, 1, 2]], 'float64')
  ubm           = GMMMachine(2,3)
  ubm.weights   = weights
  ubm.means     = means
  ubm.variances = variances

  # Creates a ISVBase
  U = numpy.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], 'float64')
  d = numpy.array([0, 1, 0, 1, 0, 1], 'float64')
  m = ISVBase(ubm, ru=1)
  _,_,ru = m.shape
  assert ru == 1

  # Checks for correctness
  m.resize(2)
  m.u = U
  m.d = d
  n_gaussians,dim,ru = m.shape
  supervector_length = m.supervector_length
  assert (m.u == U).all()
  assert (m.d == d).all()
  assert n_gaussians        == 2
  assert dim                == 3
  assert supervector_length == 6
  assert ru                 == 2

  # Saves and loads
  filename = str(tempfile.mkstemp(".hdf5")[1])
  m.save(bob.io.base.HDF5File(filename, 'w'))
  m_loaded = ISVBase(bob.io.base.HDF5File(filename))
  m_loaded.ubm = ubm
  assert m == m_loaded
  assert (m != m_loaded) is False
  assert m.is_similar_to(m_loaded)

  # Copy constructor
  mc = ISVBase(m)
  assert m == mc

  # Variant
  #mv = ISVBase()
  # Checks for correctness
  #mv.ubm = ubm
  #mv.resize(2)
  #mv.u = U
  #mv.d = d
  #assert (m.u == U).all()
  #assert (m.d == d).all()
  #ssert m.dim_c == 2
  #assert m.dim_d == 3
  #assert m.dim_cd == 6
  #assert m.dim_ru == 2

  # Clean-up
  os.unlink(filename)
Beispiel #4
0
def test_GMMMachine_stats():
    """Tests a GMMMachine (statistics)"""

    arrayset = load_array(
        resource_filename("bob.learn.em", "data/faithful.torch3_f64.hdf5"))
    gmm = GMMMachine(n_gaussians=2)
    gmm.weights = np.array([0.5, 0.5], "float64")
    gmm.means = np.array([[3, 70], [4, 72]], "float64")
    gmm.variances = np.array([[1, 10], [2, 5]], "float64")
    gmm.variance_thresholds = np.array([[0, 0], [0, 0]], "float64")

    stats = gmm_module.e_step(
        arrayset,
        gmm,
    )

    stats_ref = GMMStats(n_gaussians=2, n_features=2)
    stats_ref.load(
        HDF5File(resource_filename("bob.learn.em", "data/stats.hdf5"), "r"))

    np.testing.assert_equal(stats.t, stats_ref.t)
    np.testing.assert_almost_equal(stats.n, stats_ref.n, decimal=10)
    # np.testing.assert_equal(stats.sum_px, stats_ref.sum_px)
    # Note AA: precision error above
    np.testing.assert_almost_equal(stats.sum_px, stats_ref.sum_px, decimal=10)
    np.testing.assert_almost_equal(stats.sum_pxx,
                                   stats_ref.sum_pxx,
                                   decimal=10)
Beispiel #5
0
def test_gmm_test():

  # Tests a GMMMachine by computing scores against a model and compare to
  # an old reference

  ar = bob.io.base.load(datafile('dataforMAP.hdf5', __name__, path="../data/"))

  # Initialize GMMMachine
  n_gaussians = 5
  n_inputs = 45
  gmm = GMMMachine(n_gaussians, n_inputs)
  gmm.means = bob.io.base.load(datafile('meansAfterML.hdf5', __name__, path="../data/"))
  gmm.variances = bob.io.base.load(datafile('variancesAfterML.hdf5', __name__, path="../data/"))
  gmm.weights = bob.io.base.load(datafile('weightsAfterML.hdf5', __name__, path="../data/"))

  threshold = 0.001
  gmm.set_variance_thresholds(threshold)

  # Test against the model
  score_mean_ref = -1.50379e+06
  score = 0.
  for v in ar: score += gmm(v)
  score /= len(ar)

  # Compare current results to torch3vision
  assert abs(score-score_mean_ref)/score_mean_ref<1e-4
Beispiel #6
0
def test_gmm_test():

    # Tests a GMMMachine by computing scores against a model and compare to
    # an old reference

    ar = bob.io.base.load(
        datafile('dataforMAP.hdf5', __name__, path="../data/"))

    # Initialize GMMMachine
    n_gaussians = 5
    n_inputs = 45
    gmm = GMMMachine(n_gaussians, n_inputs)
    gmm.means = bob.io.base.load(
        datafile('meansAfterML.hdf5', __name__, path="../data/"))
    gmm.variances = bob.io.base.load(
        datafile('variancesAfterML.hdf5', __name__, path="../data/"))
    gmm.weights = bob.io.base.load(
        datafile('weightsAfterML.hdf5', __name__, path="../data/"))

    threshold = 0.001
    gmm.set_variance_thresholds(threshold)

    # Test against the model
    score_mean_ref = -1.50379e+06
    score = 0.
    for v in ar:
        score += gmm(v)
    score /= len(ar)

    # Compare current results to torch3vision
    assert abs(score - score_mean_ref) / score_mean_ref < 1e-4
Beispiel #7
0
def _create_ubm_prior(means):
    # Creating a fake prior with 2 gaussians
    prior_gmm = GMMMachine(2)
    prior_gmm.means = means.copy()
    # All nice and round diagonal covariance
    prior_gmm.variances = np.ones((2, 3)) * 0.5
    prior_gmm.weights = np.array([0.3, 0.7])
    return prior_gmm
Beispiel #8
0
def loadGMM():
  gmm = GMMMachine(2, 2)

  gmm.weights = bob.io.base.load(datafile('gmm.init_weights.hdf5', __name__, path="../data/"))
  gmm.means = bob.io.base.load(datafile('gmm.init_means.hdf5', __name__, path="../data/"))
  gmm.variances = bob.io.base.load(datafile('gmm.init_variances.hdf5', __name__, path="../data/"))
  #gmm.variance_thresholds = numpy.array([[0.001, 0.001],[0.001, 0.001]], 'float64')

  return gmm
Beispiel #9
0
def test_gmm_MAP_3():
    # Train a GMMMachine with MAP_GMMTrainer; compares to old reference
    ar = load_array(resource_filename("bob.learn.em", "data/dataforMAP.hdf5"))

    # Initialize GMMMachine
    n_gaussians = 5
    prior_gmm = GMMMachine(n_gaussians)
    prior_gmm.means = load_array(
        resource_filename("bob.learn.em", "data/meansAfterML.hdf5"))
    prior_gmm.variances = load_array(
        resource_filename("bob.learn.em", "data/variancesAfterML.hdf5"))
    prior_gmm.weights = load_array(
        resource_filename("bob.learn.em", "data/weightsAfterML.hdf5"))

    threshold = 0.001
    prior_gmm.variance_thresholds = threshold

    # Initialize MAP Trainer
    prior = 0.001
    accuracy = 0.00001
    gmm = GMMMachine(
        n_gaussians,
        trainer="map",
        ubm=prior_gmm,
        convergence_threshold=prior,
        max_fitting_steps=1,
        update_means=True,
        update_variances=False,
        update_weights=False,
        mean_var_update_threshold=accuracy,
        map_relevance_factor=None,
    )
    gmm.variance_thresholds = threshold

    # Test results
    # Load torch3vision reference
    meansMAP_ref = load_array(
        resource_filename("bob.learn.em", "data/meansAfterMAP.hdf5"))
    variancesMAP_ref = load_array(
        resource_filename("bob.learn.em", "data/variancesAfterMAP.hdf5"))
    weightsMAP_ref = load_array(
        resource_filename("bob.learn.em", "data/weightsAfterMAP.hdf5"))

    for transform in (to_numpy, to_dask_array):
        ar = transform(ar)
        # Train
        gmm = gmm.fit(ar)

        # Compare to current results
        # Gaps are quite large. This might be explained by the fact that there is no
        # adaptation of a given Gaussian in torch3 when the corresponding responsibilities
        # are below the responsibilities threshold
        np.testing.assert_allclose(gmm.means, meansMAP_ref, atol=2e-1)
        np.testing.assert_allclose(gmm.variances, variancesMAP_ref, atol=1e-4)
        np.testing.assert_allclose(gmm.weights, weightsMAP_ref, atol=1e-4)
Beispiel #10
0
def test_gmm_MAP_2():

    # Train a GMMMachine with MAP_GMMTrainer and compare with matlab reference

    data = bob.io.base.load(datafile('data.hdf5', __name__, path="../data/"))
    data = data.reshape((1, data.shape[0]))  # make a 2D array out of it
    means = bob.io.base.load(datafile('means.hdf5', __name__, path="../data/"))
    variances = bob.io.base.load(
        datafile('variances.hdf5', __name__, path="../data/"))
    weights = bob.io.base.load(
        datafile('weights.hdf5', __name__, path="../data/"))

    gmm = GMMMachine(2, 50)
    gmm.means = means
    gmm.variances = variances
    gmm.weights = weights

    map_adapt = MAP_GMMTrainer(update_means=True,
                               update_variances=False,
                               update_weights=False,
                               mean_var_update_responsibilities_threshold=0.,
                               prior_gmm=gmm,
                               relevance_factor=4.)

    gmm_adapted = GMMMachine(2, 50)
    gmm_adapted.means = means
    gmm_adapted.variances = variances
    gmm_adapted.weights = weights

    #map_adapt.max_iterations = 1
    #map_adapt.train(gmm_adapted, data)
    bob.learn.em.train(map_adapt, gmm_adapted, data, max_iterations=1)

    new_means = bob.io.base.load(
        datafile('new_adapted_mean.hdf5', __name__, path="../data/"))

    # print new_means[0,:]
    # print gmm_adapted.means[:,0]

    # Compare to matlab reference
    assert equals(new_means[0, :], gmm_adapted.means[:, 0], 1e-4)
    assert equals(new_means[1, :], gmm_adapted.means[:, 1], 1e-4)
Beispiel #11
0
def loadGMM():
    gmm = GMMMachine(n_gaussians=2)

    gmm.weights = load_array(
        resource_filename("bob.learn.em", "data/gmm.init_weights.hdf5"))
    gmm.means = load_array(
        resource_filename("bob.learn.em", "data/gmm.init_means.hdf5"))
    gmm.variances = load_array(
        resource_filename("bob.learn.em", "data/gmm.init_variances.hdf5"))

    return gmm
Beispiel #12
0
def loadGMM():
    gmm = GMMMachine(2, 2)

    gmm.weights = bob.io.base.load(
        datafile('gmm.init_weights.hdf5', __name__, path="../data/"))
    gmm.means = bob.io.base.load(
        datafile('gmm.init_means.hdf5', __name__, path="../data/"))
    gmm.variances = bob.io.base.load(
        datafile('gmm.init_variances.hdf5', __name__, path="../data/"))
    #gmm.variance_thresholds = numpy.array([[0.001, 0.001],[0.001, 0.001]], 'float64')

    return gmm
Beispiel #13
0
def test_GMMMachine_3():
  # Test a GMMMachine (log-likelihood computation)

  data = bob.io.base.load(datafile('data.hdf5', __name__, path="../data/"))
  gmm = GMMMachine(2, 50)
  gmm.weights   = bob.io.base.load(datafile('weights.hdf5', __name__, path="../data/"))
  gmm.means     = bob.io.base.load(datafile('means.hdf5', __name__, path="../data/"))
  gmm.variances = bob.io.base.load(datafile('variances.hdf5', __name__, path="../data/"))

  # Compare the log-likelihood with the one obtained using Chris Matlab
  # implementation
  matlab_ll_ref = -2.361583051672024e+02
  assert abs(gmm(data) - matlab_ll_ref) < 1e-10
Beispiel #14
0
def test_gmm_MAP_2():
    # Train a GMMMachine with MAP_GMMTrainer and compare with matlab reference

    data = load_array(resource_filename("bob.learn.em", "data/data.hdf5"))
    data = data.reshape((1, -1))  # make a 2D array out of it
    means = load_array(resource_filename("bob.learn.em", "data/means.hdf5"))
    variances = load_array(
        resource_filename("bob.learn.em", "data/variances.hdf5"))
    weights = load_array(resource_filename("bob.learn.em",
                                           "data/weights.hdf5"))

    gmm = GMMMachine(n_gaussians=2)
    gmm.means = means
    gmm.variances = variances
    gmm.weights = weights

    gmm_adapted = GMMMachine(
        n_gaussians=2,
        trainer="map",
        ubm=gmm,
        max_fitting_steps=1,
        update_means=True,
        update_variances=False,
        update_weights=False,
        mean_var_update_threshold=0.0,
    )
    gmm_adapted.means = means
    gmm_adapted.variances = variances
    gmm_adapted.weights = weights

    new_means = load_array(
        resource_filename("bob.learn.em", "data/new_adapted_mean.hdf5"))

    for transform in (to_numpy, to_dask_array):
        data = transform(data)
        gmm_adapted = gmm_adapted.fit(data)

        # Compare to matlab reference
        np.testing.assert_allclose(new_means.T, gmm_adapted.means, rtol=1e-4)
Beispiel #15
0
def test_gmm_ML_2():

    # Trains a GMMMachine with ML_GMMTrainer; compares to an old reference

    ar = bob.io.base.load(
        datafile('dataNormalized.hdf5', __name__, path="../data/"))

    # Initialize GMMMachine
    gmm = GMMMachine(5, 45)
    gmm.means = bob.io.base.load(
        datafile('meansAfterKMeans.hdf5', __name__,
                 path="../data/")).astype('float64')
    gmm.variances = bob.io.base.load(
        datafile('variancesAfterKMeans.hdf5', __name__,
                 path="../data/")).astype('float64')
    gmm.weights = numpy.exp(
        bob.io.base.load(
            datafile('weightsAfterKMeans.hdf5', __name__,
                     path="../data/")).astype('float64'))

    threshold = 0.001
    gmm.set_variance_thresholds(threshold)

    # Initialize ML Trainer
    prior = 0.001
    max_iter_gmm = 25
    accuracy = 0.00001
    ml_gmmtrainer = ML_GMMTrainer(True, True, True, prior)

    # Run ML
    #ml_gmmtrainer.train(gmm, ar)
    bob.learn.em.train(ml_gmmtrainer,
                       gmm,
                       ar,
                       max_iterations=max_iter_gmm,
                       convergence_threshold=accuracy)

    # Test results
    # Load torch3vision reference
    meansML_ref = bob.io.base.load(
        datafile('meansAfterML.hdf5', __name__, path="../data/"))
    variancesML_ref = bob.io.base.load(
        datafile('variancesAfterML.hdf5', __name__, path="../data/"))
    weightsML_ref = bob.io.base.load(
        datafile('weightsAfterML.hdf5', __name__, path="../data/"))

    # Compare to current results
    assert equals(gmm.means, meansML_ref, 3e-3)
    assert equals(gmm.variances, variancesML_ref, 3e-3)
    assert equals(gmm.weights, weightsML_ref, 1e-4)
Beispiel #16
0
def test_gmm_ML_2():
    # Trains a GMMMachine with ML_GMMTrainer; compares to a reference
    ar = load_array(
        resource_filename("bob.learn.em", "data/dataNormalized.hdf5"))

    # Test results
    # Load torch3vision reference
    meansML_ref = load_array(
        resource_filename("bob.learn.em", "data/meansAfterML.hdf5"))
    variancesML_ref = load_array(
        resource_filename("bob.learn.em", "data/variancesAfterML.hdf5"))
    weightsML_ref = load_array(
        resource_filename("bob.learn.em", "data/weightsAfterML.hdf5"))

    for transform in (to_numpy, to_dask_array):
        ar = transform(ar)
        # Initialize GMMMachine
        gmm = GMMMachine(n_gaussians=5)
        gmm.means = load_array(
            resource_filename("bob.learn.em",
                              "data/meansAfterKMeans.hdf5")).astype("float64")
        gmm.variances = load_array(
            resource_filename(
                "bob.learn.em",
                "data/variancesAfterKMeans.hdf5")).astype("float64")
        gmm.weights = np.exp(
            load_array(
                resource_filename(
                    "bob.learn.em",
                    "data/weightsAfterKMeans.hdf5")).astype("float64"))

        threshold = 0.001
        gmm.variance_thresholds = threshold

        # Initialize ML Trainer
        gmm.mean_var_update_threshold = 0.001
        gmm.max_fitting_steps = 25
        gmm.convergence_threshold = 0.000001
        gmm.update_means = True
        gmm.update_variances = True
        gmm.update_weights = True

        # Run ML
        gmm = gmm.fit(ar)

        # Compare to current results
        np.testing.assert_allclose(gmm.means, meansML_ref, atol=3e-3)
        np.testing.assert_allclose(gmm.variances, variancesML_ref, atol=3e-3)
        np.testing.assert_allclose(gmm.weights, weightsML_ref, atol=1e-4)
Beispiel #17
0
def test_GMMMachine_3():
    # Test a GMMMachine (log-likelihood computation)

    data = bob.io.base.load(datafile('data.hdf5', __name__, path="../data/"))
    gmm = GMMMachine(2, 50)
    gmm.weights = bob.io.base.load(
        datafile('weights.hdf5', __name__, path="../data/"))
    gmm.means = bob.io.base.load(
        datafile('means.hdf5', __name__, path="../data/"))
    gmm.variances = bob.io.base.load(
        datafile('variances.hdf5', __name__, path="../data/"))

    # Compare the log-likelihood with the one obtained using Chris Matlab
    # implementation
    matlab_ll_ref = -2.361583051672024e+02
    assert abs(gmm(data) - matlab_ll_ref) < 1e-10
Beispiel #18
0
def test_gmm_MAP_3():

  # Train a GMMMachine with MAP_GMMTrainer; compares to old reference

  ar = bob.io.base.load(datafile('dataforMAP.hdf5', __name__, path="../data/"))

  # Initialize GMMMachine
  n_gaussians = 5
  n_inputs = 45
  prior_gmm = GMMMachine(n_gaussians, n_inputs)
  prior_gmm.means = bob.io.base.load(datafile('meansAfterML.hdf5', __name__, path="../data/"))
  prior_gmm.variances = bob.io.base.load(datafile('variancesAfterML.hdf5', __name__, path="../data/"))
  prior_gmm.weights = bob.io.base.load(datafile('weightsAfterML.hdf5', __name__, path="../data/"))

  threshold = 0.001
  prior_gmm.set_variance_thresholds(threshold)

  # Initialize MAP Trainer
  relevance_factor = 0.1
  prior = 0.001
  max_iter_gmm = 1
  accuracy = 0.00001
  map_factor = 0.5
  map_gmmtrainer = MAP_GMMTrainer(prior_gmm, alpha=map_factor, update_means=True, update_variances=False, update_weights=False, mean_var_update_responsibilities_threshold=accuracy)
  #map_gmmtrainer.max_iterations = max_iter_gmm
  #map_gmmtrainer.convergence_threshold = accuracy

  gmm = GMMMachine(n_gaussians, n_inputs)
  gmm.set_variance_thresholds(threshold)

  # Train
  #map_gmmtrainer.train(gmm, ar)
  bob.learn.em.train(map_gmmtrainer, gmm, ar, max_iterations = max_iter_gmm, convergence_threshold=prior)

  # Test results
  # Load torch3vision reference
  meansMAP_ref = bob.io.base.load(datafile('meansAfterMAP.hdf5', __name__, path="../data/"))
  variancesMAP_ref = bob.io.base.load(datafile('variancesAfterMAP.hdf5', __name__, path="../data/"))
  weightsMAP_ref = bob.io.base.load(datafile('weightsAfterMAP.hdf5', __name__, path="../data/"))

  # Compare to current results
  # Gaps are quite large. This might be explained by the fact that there is no
  # adaptation of a given Gaussian in torch3 when the corresponding responsibilities
  # are below the responsibilities threshold
  assert equals(gmm.means, meansMAP_ref, 2e-1)
  assert equals(gmm.variances, variancesMAP_ref, 1e-4)
  assert equals(gmm.weights, weightsMAP_ref, 1e-4)
Beispiel #19
0
def test_likelihood_weight():
    data = np.array([[1, 1, 1], [-1, 0, 0], [0, 0, 1], [2, 2, 2]])
    n_gaussians = 3
    machine = GMMMachine(n_gaussians)
    machine.means = np.repeat([[0], [1], [-1]], 3, 1)
    machine.variances = np.ones_like(machine.means)
    machine.weights = [0.6, 0.1, 0.3]
    for transform in (to_numpy, to_dask_array):
        data = transform(data)
        log_likelihood = machine.log_likelihood(data)
        expected_ll = np.array([
            -4.206596356117164,
            -3.492325679996329,
            -3.634745457950943,
            -6.49485678536014,
        ])
        np.testing.assert_almost_equal(log_likelihood, expected_ll)
Beispiel #20
0
def test_GMMMachine_ll_computation():
    """Test a GMMMachine (log-likelihood computation)"""

    data = load_array(resource_filename("bob.learn.em", "data/data.hdf5"))
    gmm = GMMMachine(n_gaussians=2)
    gmm.weights = load_array(
        resource_filename("bob.learn.em", "data/weights.hdf5"))
    gmm.means = load_array(resource_filename("bob.learn.em",
                                             "data/means.hdf5"))
    gmm.variances = load_array(
        resource_filename("bob.learn.em", "data/variances.hdf5"))

    # Compare the log-likelihood with the one obtained using Chris Matlab implementation
    matlab_ll_ref = -2.361583051672024e02
    np.testing.assert_almost_equal(gmm.log_likelihood(data),
                                   matlab_ll_ref,
                                   decimal=10)
Beispiel #21
0
def test_map_transformer():
    post_data = np.array([[1, 2, 2], [2, 1, 2], [7, 8, 9], [7, 7, 8],
                          [7, 9, 7]])
    test_data = np.array([[1, 1, 1], [1, 1, 2], [8, 9, 9], [8, 8, 8]])
    n_gaussians = 2
    n_features = 3
    prior_machine = GMMMachine(n_gaussians)
    prior_machine.means = np.array([[2, 2, 2], [8, 8, 8]])
    prior_machine.variances = np.ones_like(prior_machine.means)
    prior_machine.weights = np.array([0.5, 0.5])

    machine = GMMMachine(
        n_gaussians,
        trainer="map",
        ubm=prior_machine,
        update_means=True,
        update_variances=True,
        update_weights=True,
    )

    for transform in (to_numpy, to_dask_array):
        post_data = transform(post_data)
        machine = machine.fit(post_data)

        expected_means = np.array([[1.83333333, 1.83333333, 2.0],
                                   [7.57142857, 8, 8]])
        np.testing.assert_almost_equal(machine.means, expected_means)
        eps = np.finfo(float).eps
        expected_vars = np.array([[eps, eps, eps], [eps, eps, eps]])
        np.testing.assert_almost_equal(machine.variances, expected_vars)
        expected_weights = np.array([0.46226415, 0.53773585])
        np.testing.assert_almost_equal(machine.weights, expected_weights)

        stats = machine.acc_stats(test_data)

        expected_stats = GMMStats(n_gaussians, n_features)
        expected_stats.init_fields(
            log_likelihood=-1.3837590691807108e16,
            t=test_data.shape[0],
            n=np.array([2, 2], dtype=float),
            sum_px=np.array([[2, 2, 3], [16, 17, 17]], dtype=float),
            sum_pxx=np.array([[2, 2, 5], [128, 145, 145]], dtype=float),
        )
        assert stats.is_similar_to(expected_stats)
Beispiel #22
0
def test_GMMMachine_4():

  import numpy
  numpy.random.seed(3) # FIXING A SEED

  data = numpy.random.rand(100,50) #Doesn't matter if it is ramdom. The average of 1D array (in python) MUST output the same result for the 2D array (in C++)
  
  gmm = GMMMachine(2, 50)
  gmm.weights   = bob.io.base.load(datafile('weights.hdf5', __name__, path="../data/"))
  gmm.means     = bob.io.base.load(datafile('means.hdf5', __name__, path="../data/"))
  gmm.variances = bob.io.base.load(datafile('variances.hdf5', __name__, path="../data/"))


  ll = 0
  for i in range(data.shape[0]):
    ll += gmm(data[i,:])
  ll /= data.shape[0]
  
  assert ll==gmm(data)
Beispiel #23
0
def test_map_em():
    n_gaussians = 2
    prior_machine = GMMMachine(n_gaussians)
    prior_machine.means = np.array([[2, 2, 2], [8, 8, 8]])
    prior_machine.variances = np.ones_like(prior_machine.means)
    prior_machine.weights = np.array([0.5, 0.5])

    machine = GMMMachine(
        n_gaussians,
        trainer="map",
        ubm=prior_machine,
        update_means=True,
        update_variances=True,
        update_weights=True,
    )

    post_data = np.array([[1, 2, 2], [2, 1, 2], [7, 8, 9], [7, 7, 8],
                          [7, 9, 7]])

    machine.initialize_gaussians(None)

    # Machine equals to priors before fitting
    np.testing.assert_equal(machine.means, prior_machine.means)
    np.testing.assert_equal(machine.variances, prior_machine.variances)
    np.testing.assert_equal(machine.weights, prior_machine.weights)

    stats = gmm_module.e_step(
        post_data,
        machine,
    )
    gmm_module.m_step(
        [stats],
        machine,
    )

    expected_means = np.array([[1.83333333, 1.83333333, 2.0],
                               [7.57142857, 8, 8]])
    np.testing.assert_almost_equal(machine.means, expected_means)
    eps = np.finfo(float).eps
    expected_vars = np.array([[eps, eps, eps], [eps, eps, eps]])
    np.testing.assert_almost_equal(machine.variances, expected_vars)
    expected_weights = np.array([0.46226415, 0.53773585])
    np.testing.assert_almost_equal(machine.weights, expected_weights)
def test_machine():

    # Ubm
    ubm = GMMMachine(2, 3)
    ubm.weights = numpy.array([0.4, 0.6])
    ubm.means = numpy.array([[1., 7, 4], [4, 5, 3]])
    ubm.variances = numpy.array([[0.5, 1., 1.5], [1., 1.5, 2.]])

    # Defines GMMStats
    gs = GMMStats(2, 3)
    log_likelihood = -3.
    T = 1
    n = numpy.array([0.4, 0.6], numpy.float64)
    sumpx = numpy.array([[1., 2., 3.], [2., 4., 3.]], numpy.float64)
    sumpxx = numpy.array([[10., 20., 30.], [40., 50., 60.]], numpy.float64)
    gs.log_likelihood = log_likelihood
    gs.t = T
    gs.n = n
    gs.sum_px = sumpx
    gs.sum_pxx = sumpxx

    # IVector (Python)
    m = IVectorMachinePy(ubm, 2)
    t = numpy.array([[1., 2], [4, 1], [0, 3], [5, 8], [7, 10], [11, 1]])
    m.set_t(t)
    sigma = numpy.array([1., 2., 1., 3., 2., 4.])
    m.set_sigma(sigma)

    wij_ref = numpy.array([-0.04213415, 0.21463343
                           ])  # Reference from original Chris implementation
    wij = m.project(gs)
    assert numpy.allclose(wij_ref, wij, 1e-5)

    # IVector (C++)
    mc = IVectorMachine(ubm, 2)
    mc.t = t
    mc.sigma = sigma

    wij_ref = numpy.array([-0.04213415, 0.21463343
                           ])  # Reference from original Chris implementation
    wij = mc.project(gs)
    assert numpy.allclose(wij_ref, wij, 1e-5)
Beispiel #25
0
def test_GMMMachine_2():
  # Test a GMMMachine (statistics)

  arrayset = bob.io.base.load(datafile("faithful.torch3_f64.hdf5", __name__, path="../data/"))
  gmm = GMMMachine(2, 2)
  gmm.weights   = numpy.array([0.5, 0.5], 'float64')
  gmm.means     = numpy.array([[3, 70], [4, 72]], 'float64')
  gmm.variances = numpy.array([[1, 10], [2, 5]], 'float64')
  gmm.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

  stats = GMMStats(2, 2)
  gmm.acc_statistics(arrayset, stats)

  stats_ref = GMMStats(bob.io.base.HDF5File(datafile("stats.hdf5",__name__, path="../data/")))

  assert stats.t == stats_ref.t
  assert numpy.allclose(stats.n, stats_ref.n, atol=1e-10)
  #assert numpy.array_equal(stats.sumPx, stats_ref.sumPx)
  #Note AA: precision error above
  assert numpy.allclose(stats.sum_px, stats_ref.sum_px, atol=1e-10)
  assert numpy.allclose(stats.sum_pxx, stats_ref.sum_pxx, atol=1e-10)
Beispiel #26
0
def test_JFAMachine():

    eps = 1e-10

    # Creates a UBM
    ubm = GMMMachine(2, 3)
    ubm.weights = np.array([0.4, 0.6], "float64")
    ubm.means = np.array([[1, 6, 2], [4, 3, 2]], "float64")
    ubm.variances = np.array([[1, 2, 1], [2, 1, 2]], "float64")

    # Defines GMMStats
    gs = GMMStats(2, 3)
    gs.log_likelihood = -3.0
    gs.t = 1
    gs.n = np.array([0.4, 0.6], "float64")
    gs.sum_px = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "float64")
    gs.sum_pxx = np.array([[10.0, 20.0, 30.0], [40.0, 50.0, 60.0]], "float64")

    # Creates a JFAMachine
    m = JFAMachine(2, 2, em_iterations=10, ubm=ubm)
    m.U = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]],
                   "float64")
    m.V = np.array([[6, 5], [4, 3], [2, 1], [1, 2], [3, 4], [5, 6]], "float64")
    m.D = np.array([0, 1, 0, 1, 0, 1], "float64")

    # Preparing the model
    y = np.array([1, 2], "float64")
    z = np.array([3, 4, 1, 2, 0, 1], "float64")
    model = [y, z]

    score_ref = -2.111577181208289
    score = m.score(model, gs)
    np.testing.assert_allclose(score, score_ref, atol=eps)

    # Scoring with numpy array
    np.random.seed(0)
    X = np.random.normal(loc=0.0, scale=1.0, size=(50, 3))
    score_ref = 2.028009315286946
    score = m.score_using_array(model, X)
    np.testing.assert_allclose(score, score_ref, atol=eps)
Beispiel #27
0
def test_GMMMachine_single_ll_vs_multiple():

    np.random.seed(3)  # FIXING A SEED

    data = np.random.rand(
        100, 50
    )  # Doesn't matter if it is random. The average of 1D array (in python) MUST output the same result for the 2D array (in C++)

    gmm = GMMMachine(n_gaussians=2)
    gmm.weights = load_array(
        resource_filename("bob.learn.em", "data/weights.hdf5"))
    gmm.means = load_array(resource_filename("bob.learn.em",
                                             "data/means.hdf5"))
    gmm.variances = load_array(
        resource_filename("bob.learn.em", "data/variances.hdf5"))

    ll = 0
    for i in range(data.shape[0]):
        ll += gmm.log_likelihood(data[i, :])
    ll /= data.shape[0]

    assert np.isclose(ll, gmm.log_likelihood(data).mean())
Beispiel #28
0
def test_GMMMachine_4():

    import numpy
    numpy.random.seed(3)  # FIXING A SEED

    data = numpy.random.rand(
        100, 50
    )  #Doesn't matter if it is ramdom. The average of 1D array (in python) MUST output the same result for the 2D array (in C++)

    gmm = GMMMachine(2, 50)
    gmm.weights = bob.io.base.load(
        datafile('weights.hdf5', __name__, path="../data/"))
    gmm.means = bob.io.base.load(
        datafile('means.hdf5', __name__, path="../data/"))
    gmm.variances = bob.io.base.load(
        datafile('variances.hdf5', __name__, path="../data/"))

    ll = 0
    for i in range(data.shape[0]):
        ll += gmm(data[i, :])
    ll /= data.shape[0]

    assert ll == gmm(data)
Beispiel #29
0
def test_GMMMachine_object():
    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))
Beispiel #30
0
def test_ISVMachine():

    eps = 1e-10

    # Creates a UBM
    ubm = GMMMachine(2, 3)
    ubm.weights = np.array([0.4, 0.6], "float64")
    ubm.means = np.array([[1, 6, 2], [4, 3, 2]], "float64")
    ubm.variances = np.array([[1, 2, 1], [2, 1, 2]], "float64")

    # Creates a ISVMachine
    isv_machine = ISVMachine(ubm=ubm, r_U=2, em_iterations=10)
    isv_machine.U = np.array(
        [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], "float64")
    # base.v = np.array([[0], [0], [0], [0], [0], [0]], 'float64')
    isv_machine.D = np.array([0, 1, 0, 1, 0, 1], "float64")

    # Defines GMMStats
    gs = GMMStats(2, 3)
    gs.log_likelihood = -3.0
    gs.t = 1
    gs.n = np.array([0.4, 0.6], "float64")
    gs.sum_px = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "float64")
    gs.sum_pxx = np.array([[10.0, 20.0, 30.0], [40.0, 50.0, 60.0]], "float64")

    # Enrolled model
    latent_z = np.array([3, 4, 1, 2, 0, 1], "float64")
    score = isv_machine.score(latent_z, gs)
    score_ref = -3.280498193082100
    np.testing.assert_allclose(score, score_ref, atol=eps)

    # Scoring with numpy array
    np.random.seed(0)
    X = np.random.normal(loc=0.0, scale=1.0, size=(50, 3))
    score_ref = -1.2343813195374242
    score = isv_machine.score_using_array(latent_z, X)
    np.testing.assert_allclose(score, score_ref, atol=eps)
Beispiel #31
0
def test_gmm_ML_2():

  # Trains a GMMMachine with ML_GMMTrainer; compares to an old reference

  ar = bob.io.base.load(datafile('dataNormalized.hdf5', __name__, path="../data/"))

  # Initialize GMMMachine
  gmm = GMMMachine(5, 45)
  gmm.means = bob.io.base.load(datafile('meansAfterKMeans.hdf5', __name__, path="../data/")).astype('float64')
  gmm.variances = bob.io.base.load(datafile('variancesAfterKMeans.hdf5', __name__, path="../data/")).astype('float64')
  gmm.weights = numpy.exp(bob.io.base.load(datafile('weightsAfterKMeans.hdf5', __name__, path="../data/")).astype('float64'))

  threshold = 0.001
  gmm.set_variance_thresholds(threshold)

  # Initialize ML Trainer
  prior = 0.001
  max_iter_gmm = 25
  accuracy = 0.00001
  ml_gmmtrainer = ML_GMMTrainer(True, True, True, prior)

  # Run ML
  #ml_gmmtrainer.train(gmm, ar)
  bob.learn.em.train(ml_gmmtrainer, gmm, ar, max_iterations = max_iter_gmm, convergence_threshold=accuracy)

  # Test results
  # Load torch3vision reference
  meansML_ref = bob.io.base.load(datafile('meansAfterML.hdf5', __name__, path="../data/"))
  variancesML_ref = bob.io.base.load(datafile('variancesAfterML.hdf5', __name__, path="../data/"))
  weightsML_ref = bob.io.base.load(datafile('weightsAfterML.hdf5', __name__, path="../data/"))


  # Compare to current results
  assert equals(gmm.means, meansML_ref, 3e-3)
  assert equals(gmm.variances, variancesML_ref, 3e-3)
  assert equals(gmm.weights, weightsML_ref, 1e-4)
Beispiel #32
0
def test_GMMMachine_2():
    # Test a GMMMachine (statistics)

    arrayset = bob.io.base.load(
        datafile("faithful.torch3_f64.hdf5", __name__, path="../data/"))
    gmm = GMMMachine(2, 2)
    gmm.weights = numpy.array([0.5, 0.5], 'float64')
    gmm.means = numpy.array([[3, 70], [4, 72]], 'float64')
    gmm.variances = numpy.array([[1, 10], [2, 5]], 'float64')
    gmm.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

    stats = GMMStats(2, 2)
    gmm.acc_statistics(arrayset, stats)

    stats_ref = GMMStats(
        bob.io.base.HDF5File(datafile("stats.hdf5", __name__,
                                      path="../data/")))

    assert stats.t == stats_ref.t
    assert numpy.allclose(stats.n, stats_ref.n, atol=1e-10)
    #assert numpy.array_equal(stats.sumPx, stats_ref.sumPx)
    #Note AA: precision error above
    assert numpy.allclose(stats.sum_px, stats_ref.sum_px, atol=1e-10)
    assert numpy.allclose(stats.sum_pxx, stats_ref.sum_pxx, atol=1e-10)
Beispiel #33
0
def test_gmm_MAP_3():

    # Train a GMMMachine with MAP_GMMTrainer; compares to old reference

    ar = bob.io.base.load(
        datafile('dataforMAP.hdf5', __name__, path="../data/"))

    # Initialize GMMMachine
    n_gaussians = 5
    n_inputs = 45
    prior_gmm = GMMMachine(n_gaussians, n_inputs)
    prior_gmm.means = bob.io.base.load(
        datafile('meansAfterML.hdf5', __name__, path="../data/"))
    prior_gmm.variances = bob.io.base.load(
        datafile('variancesAfterML.hdf5', __name__, path="../data/"))
    prior_gmm.weights = bob.io.base.load(
        datafile('weightsAfterML.hdf5', __name__, path="../data/"))

    threshold = 0.001
    prior_gmm.set_variance_thresholds(threshold)

    # Initialize MAP Trainer
    relevance_factor = 0.1
    prior = 0.001
    max_iter_gmm = 1
    accuracy = 0.00001
    map_factor = 0.5
    map_gmmtrainer = MAP_GMMTrainer(
        prior_gmm,
        alpha=map_factor,
        update_means=True,
        update_variances=False,
        update_weights=False,
        mean_var_update_responsibilities_threshold=accuracy)
    #map_gmmtrainer.max_iterations = max_iter_gmm
    #map_gmmtrainer.convergence_threshold = accuracy

    gmm = GMMMachine(n_gaussians, n_inputs)
    gmm.set_variance_thresholds(threshold)

    # Train
    #map_gmmtrainer.train(gmm, ar)
    bob.learn.em.train(map_gmmtrainer,
                       gmm,
                       ar,
                       max_iterations=max_iter_gmm,
                       convergence_threshold=prior)

    # Test results
    # Load torch3vision reference
    meansMAP_ref = bob.io.base.load(
        datafile('meansAfterMAP.hdf5', __name__, path="../data/"))
    variancesMAP_ref = bob.io.base.load(
        datafile('variancesAfterMAP.hdf5', __name__, path="../data/"))
    weightsMAP_ref = bob.io.base.load(
        datafile('weightsAfterMAP.hdf5', __name__, path="../data/"))

    # Compare to current results
    # Gaps are quite large. This might be explained by the fact that there is no
    # adaptation of a given Gaussian in torch3 when the corresponding responsibilities
    # are below the responsibilities threshold
    assert equals(gmm.means, meansMAP_ref, 2e-1)
    assert equals(gmm.variances, variancesMAP_ref, 1e-4)
    assert equals(gmm.weights, weightsMAP_ref, 1e-4)
Beispiel #34
0
def test_ISVMachine():

  # Creates a UBM
  weights = numpy.array([0.4, 0.6], 'float64')
  means = numpy.array([[1, 6, 2], [4, 3, 2]], 'float64')
  variances = numpy.array([[1, 2, 1], [2, 1, 2]], 'float64')
  ubm = GMMMachine(2,3)
  ubm.weights = weights
  ubm.means = means
  ubm.variances = variances

  # Creates a ISVBaseMachine
  U = numpy.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], 'float64')
  #V = numpy.array([[0], [0], [0], [0], [0], [0]], 'float64')
  d = numpy.array([0, 1, 0, 1, 0, 1], 'float64')
  base = ISVBase(ubm,2)
  base.u = U
  #base.v = V
  base.d = d

  # Creates a JFAMachine
  z = numpy.array([3,4,1,2,0,1], 'float64')
  m = ISVMachine(base)
  m.z = z

  n_gaussians,dim,ru    = m.shape
  supervector_length    = m.supervector_length
  assert n_gaussians          == 2
  assert dim                  == 3
  assert supervector_length   == 6
  assert ru                   == 2
  assert (m.z == z).all()

  # Saves and loads
  filename = str(tempfile.mkstemp(".hdf5")[1])
  m.save(bob.io.base.HDF5File(filename, 'w'))
  m_loaded = ISVMachine(bob.io.base.HDF5File(filename))
  m_loaded.isv_base = base
  assert m == m_loaded
  assert (m != m_loaded) is False
  assert m.is_similar_to(m_loaded)

  # Copy constructor
  mc = ISVMachine(m)
  assert m == mc

  # Variant
  mv = ISVMachine(base)
  # Checks for correctness
  #mv.isv_base = base
  m.z = z

  n_gaussians,dim,ru    = m.shape
  supervector_length    = m.supervector_length
  assert n_gaussians        == 2
  assert dim                == 3
  assert supervector_length == 6
  assert ru                 == 2
  assert (m.z == z).all()

  # Defines GMMStats
  gs = GMMStats(2,3)
  log_likelihood = -3.
  T = 1
  n = numpy.array([0.4, 0.6], 'float64')
  sumpx = numpy.array([[1., 2., 3.], [4., 5., 6.]], 'float64')
  sumpxx = numpy.array([[10., 20., 30.], [40., 50., 60.]], 'float64')
  gs.log_likelihood = log_likelihood
  gs.t = T
  gs.n = n
  gs.sum_px = sumpx
  gs.sum_pxx = sumpxx

  # Forward GMMStats and check estimated value of the x speaker factor
  eps = 1e-10
  x_ref = numpy.array([0.291042849767692, 0.310273618998444], 'float64')
  score_ref = -3.280498193082100

  score = m(gs)
  assert numpy.allclose(m.x, x_ref, eps)
  assert abs(score_ref-score) < eps

  # Check using alternate forward() method
  supervector_length = m.supervector_length
  Ux = numpy.ndarray(shape=(supervector_length,), dtype=numpy.float64)
  m.estimate_ux(gs, Ux)
  score = m.forward_ux(gs, Ux)
  assert abs(score_ref-score) < eps

  # x and Ux
  x = numpy.ndarray((2,), numpy.float64)
  m.estimate_x(gs, x)
  n_gaussians,dim,_    = m.shape
  x_py = estimate_x(n_gaussians, dim, ubm.mean_supervector, ubm.variance_supervector, U, n, sumpx)
  assert numpy.allclose(x, x_py, eps)

  ux = numpy.ndarray((6,), numpy.float64)
  m.estimate_ux(gs, ux)
  n_gaussians,dim,_    = m.shape
  ux_py = estimate_ux(n_gaussians, dim, ubm.mean_supervector, ubm.variance_supervector, U, n, sumpx)
  assert numpy.allclose(ux, ux_py, eps)
  assert numpy.allclose(m.x, x, eps)

  score = m.forward_ux(gs, ux)
  assert abs(score_ref-score) < eps

  # Clean-up
  os.unlink(filename)
def test_LinearScoring():

  ubm = GMMMachine(2, 2)
  ubm.weights   = numpy.array([0.5, 0.5], 'float64')
  ubm.means     = numpy.array([[3, 70], [4, 72]], 'float64')
  ubm.variances = numpy.array([[1, 10], [2, 5]], 'float64')
  ubm.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

  model1 = GMMMachine(2, 2)
  model1.weights   = numpy.array([0.5, 0.5], 'float64')
  model1.means     = numpy.array([[1, 2], [3, 4]], 'float64')
  model1.variances = numpy.array([[9, 10], [11, 12]], 'float64')
  model1.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

  model2 = GMMMachine(2, 2)
  model2.weights   = numpy.array([0.5, 0.5], 'float64')
  model2.means     = numpy.array([[5, 6], [7, 8]], 'float64')
  model2.variances = numpy.array([[13, 14], [15, 16]], 'float64')
  model2.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

  stats1 = GMMStats(2, 2)
  stats1.sum_px = numpy.array([[1, 2], [3, 4]], 'float64')
  stats1.n = numpy.array([1, 2], 'float64')
  stats1.t = 1+2

  stats2 = GMMStats(2, 2)
  stats2.sum_px = numpy.array([[5, 6], [7, 8]], 'float64')
  stats2.n = numpy.array([3, 4], 'float64')
  stats2.t = 3+4

  stats3 = GMMStats(2, 2)
  stats3.sum_px = numpy.array([[5, 6], [7, 3]], 'float64')
  stats3.n = numpy.array([3, 4], 'float64')
  stats3.t = 3+4

  test_channeloffset = [numpy.array([9, 8, 7, 6], 'float64'), numpy.array([5, 4, 3, 2], 'float64'), numpy.array([1, 0, 1, 2], 'float64')]

  # Reference scores (from Idiap internal matlab implementation)
  ref_scores_00 = numpy.array([[2372.9, 5207.7, 5275.7], [2215.7, 4868.1, 4932.1]], 'float64')
  ref_scores_01 = numpy.array( [[790.9666666666667, 743.9571428571428, 753.6714285714285], [738.5666666666667, 695.4428571428572, 704.5857142857144]], 'float64')
  ref_scores_10 = numpy.array([[2615.5, 5434.1, 5392.5], [2381.5, 4999.3, 5022.5]], 'float64')
  ref_scores_11 = numpy.array([[871.8333333333332, 776.3000000000001, 770.3571428571427], [793.8333333333333, 714.1857142857143, 717.5000000000000]], 'float64')


  # 1/ Use GMMMachines
  # 1/a/ Without test_channelOffset, without frame-length normalisation
  scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3])
  assert (abs(scores - ref_scores_00) < 1e-7).all()

  # 1/b/ Without test_channelOffset, with frame-length normalisation
  scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], [], True)
  assert (abs(scores - ref_scores_01) < 1e-7).all()
  #scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], (), True)
  #assert (abs(scores - ref_scores_01) < 1e-7).all()
  #scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], None, True)
  #assert (abs(scores - ref_scores_01) < 1e-7).all()

  # 1/c/ With test_channelOffset, without frame-length normalisation
  scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], test_channeloffset)
  assert (abs(scores - ref_scores_10) < 1e-7).all()

  # 1/d/ With test_channelOffset, with frame-length normalisation
  scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], test_channeloffset, True)
  assert (abs(scores - ref_scores_11) < 1e-7).all()


  # 2/ Use mean/variance supervectors
  # 2/a/ Without test_channelOffset, without frame-length normalisation
  scores = linear_scoring([model1.mean_supervector, model2.mean_supervector], ubm.mean_supervector, ubm.variance_supervector, [stats1, stats2, stats3])
  assert (abs(scores - ref_scores_00) < 1e-7).all()

  # 2/b/ Without test_channelOffset, with frame-length normalisation
  scores = linear_scoring([model1.mean_supervector, model2.mean_supervector], ubm.mean_supervector, ubm.variance_supervector, [stats1, stats2, stats3], [], True)
  assert (abs(scores - ref_scores_01) < 1e-7).all()

  # 2/c/ With test_channelOffset, without frame-length normalisation
  scores = linear_scoring([model1.mean_supervector, model2.mean_supervector], ubm.mean_supervector, ubm.variance_supervector, [stats1, stats2, stats3], test_channeloffset)
  assert (abs(scores - ref_scores_10) < 1e-7).all()

  # 2/d/ With test_channelOffset, with frame-length normalisation
  scores = linear_scoring([model1.mean_supervector, model2.mean_supervector], ubm.mean_supervector, ubm.variance_supervector, [stats1, stats2, stats3], test_channeloffset, True)
  assert (abs(scores - ref_scores_11) < 1e-7).all()


  # 3/ Using single model/sample
  # 3/a/ without frame-length normalisation
  score = linear_scoring(model1.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats1, test_channeloffset[0])
  assert abs(score - ref_scores_10[0,0]) < 1e-7
  score = linear_scoring(model1.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats2, test_channeloffset[1])
  assert abs(score - ref_scores_10[0,1]) < 1e-7
  score = linear_scoring(model1.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats3, test_channeloffset[2])
  assert abs(score - ref_scores_10[0,2]) < 1e-7
  score = linear_scoring(model2.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats1, test_channeloffset[0])
  assert abs(score - ref_scores_10[1,0]) < 1e-7
  score = linear_scoring(model2.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats2, test_channeloffset[1])
  assert abs(score - ref_scores_10[1,1]) < 1e-7
  score = linear_scoring(model2.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats3, test_channeloffset[2])
  assert abs(score - ref_scores_10[1,2]) < 1e-7


  # 3/b/ without frame-length normalisation
  score = linear_scoring(model1.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats1, test_channeloffset[0], True)
  assert abs(score - ref_scores_11[0,0]) < 1e-7
  score = linear_scoring(model1.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats2, test_channeloffset[1], True)
  assert abs(score - ref_scores_11[0,1]) < 1e-7
  score = linear_scoring(model1.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats3, test_channeloffset[2], True)
  assert abs(score - ref_scores_11[0,2]) < 1e-7
  score = linear_scoring(model2.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats1, test_channeloffset[0], True)
  assert abs(score - ref_scores_11[1,0]) < 1e-7
  score = linear_scoring(model2.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats2, test_channeloffset[1], True)
  assert abs(score - ref_scores_11[1,1]) < 1e-7
  score = linear_scoring(model2.mean_supervector, ubm.mean_supervector, ubm.variance_supervector, stats3, test_channeloffset[2], True)
  assert abs(score - ref_scores_11[1,2]) < 1e-7
Beispiel #36
0
def test_GMMMachine_1():
    # Test a GMMMachine basic features

    weights = numpy.array([0.5, 0.5], 'float64')
    weights2 = numpy.array([0.6, 0.4], 'float64')
    means = numpy.array([[3, 70, 0], [4, 72, 0]], 'float64')
    means2 = numpy.array([[3, 7, 0], [4, 72, 0]], 'float64')
    variances = numpy.array([[1, 10, 1], [2, 5, 2]], 'float64')
    variances2 = numpy.array([[10, 10, 1], [2, 5, 2]], 'float64')
    varianceThresholds = numpy.array([[0, 0, 0], [0, 0, 0]], 'float64')
    varianceThresholds2 = numpy.array([[0.0005, 0.0005, 0.0005], [0, 0, 0]],
                                      'float64')

    # Initializes a GMMMachine
    gmm = GMMMachine(2, 3)
    # 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)
    assert (gmm.weights == weights).all()
    assert (gmm.means == means).all()
    assert (gmm.variances == variances).all()
    assert (gmm.variance_thresholds == varianceThresholds).all()

    # Checks supervector-like accesses
    assert (gmm.mean_supervector == means.reshape(means.size)).all()
    assert (gmm.variance_supervector == variances.reshape(
        variances.size)).all()
    newMeans = numpy.array([[3, 70, 2], [4, 72, 2]], 'float64')
    newVariances = numpy.array([[1, 1, 1], [2, 2, 2]], 'float64')

    # Checks particular varianceThresholds-related methods
    varianceThresholds1D = numpy.array([0.3, 1, 0.5], 'float64')
    gmm.set_variance_thresholds(varianceThresholds1D)
    assert (gmm.variance_thresholds[0, :] == varianceThresholds1D).all()
    assert (gmm.variance_thresholds[1, :] == varianceThresholds1D).all()

    gmm.set_variance_thresholds(0.005)
    assert (gmm.variance_thresholds == 0.005).all()

    # Checks Gaussians access
    gmm.means = newMeans
    gmm.variances = newVariances
    assert (gmm.get_gaussian(0).mean == newMeans[0, :]).all()
    assert (gmm.get_gaussian(1).mean == newMeans[1, :]).all()
    assert (gmm.get_gaussian(0).variance == newVariances[0, :]).all()
    assert (gmm.get_gaussian(1).variance == newVariances[1, :]).all()

    # Checks resize
    gmm.resize(4, 5)
    assert gmm.shape == (4, 5)

    # Checks comparison
    gmm2 = GMMMachine(gmm)
    gmm3 = GMMMachine(2, 3)
    gmm3.weights = weights2
    gmm3.means = means
    gmm3.variances = variances
    #gmm3.varianceThresholds = varianceThresholds
    gmm4 = GMMMachine(2, 3)
    gmm4.weights = weights
    gmm4.means = means2
    gmm4.variances = variances
    #gmm4.varianceThresholds = varianceThresholds
    gmm5 = GMMMachine(2, 3)
    gmm5.weights = weights
    gmm5.means = means
    gmm5.variances = variances2
    #gmm5.varianceThresholds = varianceThresholds
    gmm6 = GMMMachine(2, 3)
    gmm6.weights = weights
    gmm6.means = means
    gmm6.variances = variances
    #gmm6.varianceThresholds = varianceThresholds2

    assert gmm == gmm2
    assert (gmm != gmm2) is False
    assert gmm.is_similar_to(gmm2)
    assert gmm != gmm3
    assert (gmm == gmm3) is False
    assert gmm.is_similar_to(gmm3) is False
    assert gmm != gmm4
    assert (gmm == gmm4) is False
    assert gmm.is_similar_to(gmm4) is False
    assert gmm != gmm5
    assert (gmm == gmm5) is False
    assert gmm.is_similar_to(gmm5) is False
    assert gmm != gmm6
    assert (gmm == gmm6) is False
    assert gmm.is_similar_to(gmm6) is False
Beispiel #37
0
def test_JFAMachine():

  # Creates a UBM
  weights   = numpy.array([0.4, 0.6], 'float64')
  means     = numpy.array([[1, 6, 2], [4, 3, 2]], 'float64')
  variances = numpy.array([[1, 2, 1], [2, 1, 2]], 'float64')
  ubm           = GMMMachine(2,3)
  ubm.weights   = weights
  ubm.means     = means
  ubm.variances = variances

  # Creates a JFABase
  U = numpy.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], 'float64')
  V = numpy.array([[6, 5], [4, 3], [2, 1], [1, 2], [3, 4], [5, 6]], 'float64')
  d = numpy.array([0, 1, 0, 1, 0, 1], 'float64')
  base = JFABase(ubm,2,2)
  base.u = U
  base.v = V
  base.d = d

  # Creates a JFAMachine
  y = numpy.array([1,2], 'float64')
  z = numpy.array([3,4,1,2,0,1], 'float64')
  m = JFAMachine(base)
  m.y = y
  m.z = z
  n_gaussians,dim,ru,rv = m.shape
  supervector_length    = m.supervector_length

  assert n_gaussians        == 2
  assert dim                == 3
  assert supervector_length == 6
  assert ru                 == 2
  assert rv                 == 2
  assert (m.y == y).all()
  assert (m.z == z).all()

  # Saves and loads
  filename = str(tempfile.mkstemp(".hdf5")[1])
  m.save(bob.io.base.HDF5File(filename, 'w'))
  m_loaded = JFAMachine(bob.io.base.HDF5File(filename))
  m_loaded.jfa_base = base
  assert m == m_loaded
  assert (m != m_loaded) is False
  assert m.is_similar_to(m_loaded)

  # Copy constructor
  mc = JFAMachine(m)
  assert m == mc

  # Variant
  #mv = JFAMachine()
  # Checks for correctness
  #mv.jfa_base = base
  #m.y = y
  #m.z = z
  #assert m.dim_c == 2
  #assert m.dim_d == 3
  #assert m.dim_cd == 6
  #assert m.dim_ru == 2
  #assert m.dim_rv == 2
  #assert (m.y == y).all()
  #assert (m.z == z).all()

  # Defines GMMStats
  gs = GMMStats(2,3)
  log_likelihood = -3.
  T = 1
  n = numpy.array([0.4, 0.6], 'float64')
  sumpx = numpy.array([[1., 2., 3.], [4., 5., 6.]], 'float64')
  sumpxx = numpy.array([[10., 20., 30.], [40., 50., 60.]], 'float64')
  gs.log_likelihood = log_likelihood
  gs.t = T
  gs.n = n
  gs.sum_px = sumpx
  gs.sum_pxx = sumpxx

  # Forward GMMStats and check estimated value of the x speaker factor
  eps = 1e-10
  x_ref = numpy.array([0.291042849767692, 0.310273618998444], 'float64')
  score_ref = -2.111577181208289
  score = m.log_likelihood(gs)
  assert numpy.allclose(m.x, x_ref, eps)
  assert abs(score_ref-score) < eps

  # x and Ux
  x = numpy.ndarray((2,), numpy.float64)
  m.estimate_x(gs, x)
  n_gaussians, dim,_,_ = m.shape
  x_py = estimate_x(n_gaussians, dim, ubm.mean_supervector, ubm.variance_supervector, U, n, sumpx)
  assert numpy.allclose(x, x_py, eps)

  ux = numpy.ndarray((6,), numpy.float64)
  m.estimate_ux(gs, ux)
  n_gaussians, dim,_,_ = m.shape
  ux_py = estimate_ux(n_gaussians, dim, ubm.mean_supervector, ubm.variance_supervector, U, n, sumpx)
  assert numpy.allclose(ux, ux_py, eps)
  assert numpy.allclose(m.x, x, eps)

  score = m.forward_ux(gs, ux)

  assert abs(score_ref-score) < eps

  # Clean-up
  os.unlink(filename)
def test_LinearScoring():

    ubm = GMMMachine(n_gaussians=2)
    ubm.weights = np.array([0.5, 0.5], "float64")
    ubm.means = np.array([[3, 70], [4, 72]], "float64")
    ubm.variances = np.array([[1, 10], [2, 5]], "float64")
    ubm.variance_thresholds = np.array([[0, 0], [0, 0]], "float64")

    model1 = GMMMachine(n_gaussians=2)
    model1.weights = np.array([0.5, 0.5], "float64")
    model1.means = np.array([[1, 2], [3, 4]], "float64")
    model1.variances = np.array([[9, 10], [11, 12]], "float64")
    model1.variance_thresholds = np.array([[0, 0], [0, 0]], "float64")

    model2 = GMMMachine(n_gaussians=2)
    model2.weights = np.array([0.5, 0.5], "float64")
    model2.means = np.array([[5, 6], [7, 8]], "float64")
    model2.variances = np.array([[13, 14], [15, 16]], "float64")
    model2.variance_thresholds = np.array([[0, 0], [0, 0]], "float64")

    stats1 = GMMStats(2, 2)
    stats1.sum_px = np.array([[1, 2], [3, 4]], "float64")
    stats1.n = np.array([1, 2], "float64")
    stats1.t = 1 + 2

    stats2 = GMMStats(2, 2)
    stats2.sum_px = np.array([[5, 6], [7, 8]], "float64")
    stats2.n = np.array([3, 4], "float64")
    stats2.t = 3 + 4

    stats3 = GMMStats(2, 2)
    stats3.sum_px = np.array([[5, 6], [7, 3]], "float64")
    stats3.n = np.array([3, 4], "float64")
    stats3.t = 3 + 4

    test_channeloffset = [
        np.array([[9, 8], [7, 6]], "float64"),
        np.array([[5, 4], [3, 2]], "float64"),
        np.array([[1, 0], [1, 2]], "float64"),
    ]

    # Reference scores (from Idiap internal matlab implementation)
    ref_scores_00 = np.array(
        [[2372.9, 5207.7, 5275.7], [2215.7, 4868.1, 4932.1]], "float64")
    ref_scores_01 = np.array(
        [
            [790.9666666666667, 743.9571428571428, 753.6714285714285],
            [738.5666666666667, 695.4428571428572, 704.5857142857144],
        ],
        "float64",
    )
    ref_scores_10 = np.array(
        [[2615.5, 5434.1, 5392.5], [2381.5, 4999.3, 5022.5]], "float64")
    ref_scores_11 = np.array(
        [
            [871.8333333333332, 776.3000000000001, 770.3571428571427],
            [793.8333333333333, 714.1857142857143, 717.5000000000000],
        ],
        "float64",
    )

    # 1/ Use GMMMachines
    # 1/a/ Without test_channelOffset, without frame-length normalisation
    scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3])
    np.testing.assert_almost_equal(scores, ref_scores_00, decimal=7)

    # 1/b/ Without test_channelOffset, with frame-length normalisation
    scores = linear_scoring(
        [model1, model2],
        ubm,
        [stats1, stats2, stats3],
        frame_length_normalization=True,
    )
    np.testing.assert_almost_equal(scores, ref_scores_01, decimal=7)
    scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], 0,
                            True)
    np.testing.assert_almost_equal(scores, ref_scores_01, decimal=7)

    # 1/c/ With test_channelOffset, without frame-length normalisation
    scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3],
                            test_channeloffset)
    np.testing.assert_almost_equal(scores, ref_scores_10, decimal=7)

    # 1/d/ With test_channelOffset, with frame-length normalisation
    scores = linear_scoring(
        [model1, model2],
        ubm,
        [stats1, stats2, stats3],
        test_channeloffset,
        frame_length_normalization=True,
    )
    np.testing.assert_almost_equal(scores, ref_scores_11, decimal=7)

    # 2/ Use means instead of models
    # 2/a/ Without test_channelOffset, without frame-length normalisation
    scores = linear_scoring([model1.means, model2.means], ubm,
                            [stats1, stats2, stats3])
    assert (abs(scores - ref_scores_00) < 1e-7).all()

    # 2/b/ Without test_channelOffset, with frame-length normalisation
    scores = linear_scoring(
        [model1.means, model2.means],
        ubm,
        [stats1, stats2, stats3],
        frame_length_normalization=True,
    )
    assert (abs(scores - ref_scores_01) < 1e-7).all()

    # 2/c/ With test_channelOffset, without frame-length normalisation
    scores = linear_scoring(
        [model1.means, model2.means],
        ubm,
        [stats1, stats2, stats3],
        test_channeloffset,
    )
    assert (abs(scores - ref_scores_10) < 1e-7).all()

    # 2/d/ With test_channelOffset, with frame-length normalisation
    scores = linear_scoring(
        [model1.means, model2.means],
        ubm,
        [stats1, stats2, stats3],
        test_channeloffset,
        frame_length_normalization=True,
    )
    assert (abs(scores - ref_scores_11) < 1e-7).all()

    # 3/ Using single model/sample
    # 3/a/ without frame-length normalisation
    score = linear_scoring(model1.means, ubm, stats1, test_channeloffset[0])
    np.testing.assert_almost_equal(score, ref_scores_10[0, 0], decimal=7)
    score = linear_scoring(model1.means, ubm, stats2, test_channeloffset[1])
    np.testing.assert_almost_equal(score, ref_scores_10[0, 1], decimal=7)
    score = linear_scoring(model1.means, ubm, stats3, test_channeloffset[2])
    np.testing.assert_almost_equal(score, ref_scores_10[0, 2], decimal=7)
    score = linear_scoring(model2.means, ubm, stats1, test_channeloffset[0])
    np.testing.assert_almost_equal(score, ref_scores_10[1, 0], decimal=7)
    score = linear_scoring(model2.means, ubm, stats2, test_channeloffset[1])
    np.testing.assert_almost_equal(score, ref_scores_10[1, 1], decimal=7)
    score = linear_scoring(model2.means, ubm, stats3, test_channeloffset[2])
    np.testing.assert_almost_equal(score, ref_scores_10[1, 2], decimal=7)

    # 3/b/ with frame-length normalisation
    score = linear_scoring(model1.means, ubm, stats1, test_channeloffset[0],
                           True)
    np.testing.assert_almost_equal(score, ref_scores_11[0, 0], decimal=7)
    score = linear_scoring(model1.means, ubm, stats2, test_channeloffset[1],
                           True)
    np.testing.assert_almost_equal(score, ref_scores_11[0, 1], decimal=7)
    score = linear_scoring(model1.means, ubm, stats3, test_channeloffset[2],
                           True)
    np.testing.assert_almost_equal(score, ref_scores_11[0, 2], decimal=7)
    score = linear_scoring(model2.means, ubm, stats1, test_channeloffset[0],
                           True)
    np.testing.assert_almost_equal(score, ref_scores_11[1, 0], decimal=7)
    score = linear_scoring(model2.means, ubm, stats2, test_channeloffset[1],
                           True)
    np.testing.assert_almost_equal(score, ref_scores_11[1, 1], decimal=7)
    score = linear_scoring(model2.means, ubm, stats3, test_channeloffset[2],
                           True)
    np.testing.assert_almost_equal(score, ref_scores_11[1, 2], decimal=7)
Beispiel #39
0
def test_LinearScoring():

    ubm = GMMMachine(2, 2)
    ubm.weights = numpy.array([0.5, 0.5], 'float64')
    ubm.means = numpy.array([[3, 70], [4, 72]], 'float64')
    ubm.variances = numpy.array([[1, 10], [2, 5]], 'float64')
    ubm.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

    model1 = GMMMachine(2, 2)
    model1.weights = numpy.array([0.5, 0.5], 'float64')
    model1.means = numpy.array([[1, 2], [3, 4]], 'float64')
    model1.variances = numpy.array([[9, 10], [11, 12]], 'float64')
    model1.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

    model2 = GMMMachine(2, 2)
    model2.weights = numpy.array([0.5, 0.5], 'float64')
    model2.means = numpy.array([[5, 6], [7, 8]], 'float64')
    model2.variances = numpy.array([[13, 14], [15, 16]], 'float64')
    model2.variance_thresholds = numpy.array([[0, 0], [0, 0]], 'float64')

    stats1 = GMMStats(2, 2)
    stats1.sum_px = numpy.array([[1, 2], [3, 4]], 'float64')
    stats1.n = numpy.array([1, 2], 'float64')
    stats1.t = 1 + 2

    stats2 = GMMStats(2, 2)
    stats2.sum_px = numpy.array([[5, 6], [7, 8]], 'float64')
    stats2.n = numpy.array([3, 4], 'float64')
    stats2.t = 3 + 4

    stats3 = GMMStats(2, 2)
    stats3.sum_px = numpy.array([[5, 6], [7, 3]], 'float64')
    stats3.n = numpy.array([3, 4], 'float64')
    stats3.t = 3 + 4

    test_channeloffset = [
        numpy.array([9, 8, 7, 6], 'float64'),
        numpy.array([5, 4, 3, 2], 'float64'),
        numpy.array([1, 0, 1, 2], 'float64')
    ]

    # Reference scores (from Idiap internal matlab implementation)
    ref_scores_00 = numpy.array(
        [[2372.9, 5207.7, 5275.7], [2215.7, 4868.1, 4932.1]], 'float64')
    ref_scores_01 = numpy.array(
        [[790.9666666666667, 743.9571428571428, 753.6714285714285],
         [738.5666666666667, 695.4428571428572, 704.5857142857144]], 'float64')
    ref_scores_10 = numpy.array(
        [[2615.5, 5434.1, 5392.5], [2381.5, 4999.3, 5022.5]], 'float64')
    ref_scores_11 = numpy.array(
        [[871.8333333333332, 776.3000000000001, 770.3571428571427],
         [793.8333333333333, 714.1857142857143, 717.5000000000000]], 'float64')

    # 1/ Use GMMMachines
    # 1/a/ Without test_channelOffset, without frame-length normalisation
    scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3])
    assert (abs(scores - ref_scores_00) < 1e-7).all()

    # 1/b/ Without test_channelOffset, with frame-length normalisation
    scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3],
                            [], True)
    assert (abs(scores - ref_scores_01) < 1e-7).all()
    #scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], (), True)
    #assert (abs(scores - ref_scores_01) < 1e-7).all()
    #scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3], None, True)
    #assert (abs(scores - ref_scores_01) < 1e-7).all()

    # 1/c/ With test_channelOffset, without frame-length normalisation
    scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3],
                            test_channeloffset)
    assert (abs(scores - ref_scores_10) < 1e-7).all()

    # 1/d/ With test_channelOffset, with frame-length normalisation
    scores = linear_scoring([model1, model2], ubm, [stats1, stats2, stats3],
                            test_channeloffset, True)
    assert (abs(scores - ref_scores_11) < 1e-7).all()

    # 2/ Use mean/variance supervectors
    # 2/a/ Without test_channelOffset, without frame-length normalisation
    scores = linear_scoring([model1.mean_supervector, model2.mean_supervector],
                            ubm.mean_supervector, ubm.variance_supervector,
                            [stats1, stats2, stats3])
    assert (abs(scores - ref_scores_00) < 1e-7).all()

    # 2/b/ Without test_channelOffset, with frame-length normalisation
    scores = linear_scoring([model1.mean_supervector, model2.mean_supervector],
                            ubm.mean_supervector, ubm.variance_supervector,
                            [stats1, stats2, stats3], [], True)
    assert (abs(scores - ref_scores_01) < 1e-7).all()

    # 2/c/ With test_channelOffset, without frame-length normalisation
    scores = linear_scoring([model1.mean_supervector, model2.mean_supervector],
                            ubm.mean_supervector, ubm.variance_supervector,
                            [stats1, stats2, stats3], test_channeloffset)
    assert (abs(scores - ref_scores_10) < 1e-7).all()

    # 2/d/ With test_channelOffset, with frame-length normalisation
    scores = linear_scoring([model1.mean_supervector, model2.mean_supervector],
                            ubm.mean_supervector, ubm.variance_supervector,
                            [stats1, stats2, stats3], test_channeloffset, True)
    assert (abs(scores - ref_scores_11) < 1e-7).all()

    # 3/ Using single model/sample
    # 3/a/ without frame-length normalisation
    score = linear_scoring(model1.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats1,
                           test_channeloffset[0])
    assert abs(score - ref_scores_10[0, 0]) < 1e-7
    score = linear_scoring(model1.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats2,
                           test_channeloffset[1])
    assert abs(score - ref_scores_10[0, 1]) < 1e-7
    score = linear_scoring(model1.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats3,
                           test_channeloffset[2])
    assert abs(score - ref_scores_10[0, 2]) < 1e-7
    score = linear_scoring(model2.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats1,
                           test_channeloffset[0])
    assert abs(score - ref_scores_10[1, 0]) < 1e-7
    score = linear_scoring(model2.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats2,
                           test_channeloffset[1])
    assert abs(score - ref_scores_10[1, 1]) < 1e-7
    score = linear_scoring(model2.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats3,
                           test_channeloffset[2])
    assert abs(score - ref_scores_10[1, 2]) < 1e-7

    # 3/b/ without frame-length normalisation
    score = linear_scoring(model1.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats1,
                           test_channeloffset[0], True)
    assert abs(score - ref_scores_11[0, 0]) < 1e-7
    score = linear_scoring(model1.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats2,
                           test_channeloffset[1], True)
    assert abs(score - ref_scores_11[0, 1]) < 1e-7
    score = linear_scoring(model1.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats3,
                           test_channeloffset[2], True)
    assert abs(score - ref_scores_11[0, 2]) < 1e-7
    score = linear_scoring(model2.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats1,
                           test_channeloffset[0], True)
    assert abs(score - ref_scores_11[1, 0]) < 1e-7
    score = linear_scoring(model2.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats2,
                           test_channeloffset[1], True)
    assert abs(score - ref_scores_11[1, 1]) < 1e-7
    score = linear_scoring(model2.mean_supervector, ubm.mean_supervector,
                           ubm.variance_supervector, stats3,
                           test_channeloffset[2], True)
    assert abs(score - ref_scores_11[1, 2]) < 1e-7
Beispiel #40
0
def test_GMMMachine_1():
  # Test a GMMMachine basic features

  weights   = numpy.array([0.5, 0.5], 'float64')
  weights2   = numpy.array([0.6, 0.4], 'float64')
  means     = numpy.array([[3, 70, 0], [4, 72, 0]], 'float64')
  means2     = numpy.array([[3, 7, 0], [4, 72, 0]], 'float64')
  variances = numpy.array([[1, 10, 1], [2, 5, 2]], 'float64')
  variances2 = numpy.array([[10, 10, 1], [2, 5, 2]], 'float64')
  varianceThresholds = numpy.array([[0, 0, 0], [0, 0, 0]], 'float64')
  varianceThresholds2 = numpy.array([[0.0005, 0.0005, 0.0005], [0, 0, 0]], 'float64')

  # Initializes a GMMMachine
  gmm = GMMMachine(2,3)
  # 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)
  assert (gmm.weights == weights).all()
  assert (gmm.means == means).all()
  assert (gmm.variances == variances).all()
  assert (gmm.variance_thresholds == varianceThresholds).all()

  # Checks supervector-like accesses
  assert (gmm.mean_supervector == means.reshape(means.size)).all()
  assert (gmm.variance_supervector == variances.reshape(variances.size)).all()
  newMeans = numpy.array([[3, 70, 2], [4, 72, 2]], 'float64')
  newVariances = numpy.array([[1, 1, 1], [2, 2, 2]], 'float64')


  # Checks particular varianceThresholds-related methods
  varianceThresholds1D = numpy.array([0.3, 1, 0.5], 'float64')
  gmm.set_variance_thresholds(varianceThresholds1D)
  assert (gmm.variance_thresholds[0,:] == varianceThresholds1D).all()
  assert (gmm.variance_thresholds[1,:] == varianceThresholds1D).all()

  gmm.set_variance_thresholds(0.005)
  assert (gmm.variance_thresholds == 0.005).all()

  # Checks Gaussians access
  gmm.means     = newMeans
  gmm.variances = newVariances
  assert (gmm.get_gaussian(0).mean == newMeans[0,:]).all()
  assert (gmm.get_gaussian(1).mean == newMeans[1,:]).all()
  assert (gmm.get_gaussian(0).variance == newVariances[0,:]).all()
  assert (gmm.get_gaussian(1).variance == newVariances[1,:]).all()

  # Checks resize
  gmm.resize(4,5)
  assert gmm.shape == (4,5)

  # Checks comparison
  gmm2 = GMMMachine(gmm)
  gmm3 = GMMMachine(2,3)
  gmm3.weights = weights2
  gmm3.means = means
  gmm3.variances = variances
  #gmm3.varianceThresholds = varianceThresholds
  gmm4 = GMMMachine(2,3)
  gmm4.weights = weights
  gmm4.means = means2
  gmm4.variances = variances
  #gmm4.varianceThresholds = varianceThresholds
  gmm5 = GMMMachine(2,3)
  gmm5.weights = weights
  gmm5.means = means
  gmm5.variances = variances2
  #gmm5.varianceThresholds = varianceThresholds
  gmm6 = GMMMachine(2,3)
  gmm6.weights = weights
  gmm6.means = means
  gmm6.variances = variances
  #gmm6.varianceThresholds = varianceThresholds2

  assert gmm == gmm2
  assert (gmm != gmm2) is False
  assert gmm.is_similar_to(gmm2)
  assert gmm != gmm3
  assert (gmm == gmm3) is False
  assert gmm.is_similar_to(gmm3) is False
  assert gmm != gmm4
  assert (gmm == gmm4) is False
  assert gmm.is_similar_to(gmm4) is False
  assert gmm != gmm5
  assert (gmm == gmm5) is False
  assert gmm.is_similar_to(gmm5) is False
  assert gmm != gmm6
  assert (gmm == gmm6) is False
  assert gmm.is_similar_to(gmm6) is False