Beispiel #1
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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