Ejemplo n.º 1
0
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
def test_GMMStats():
    # Test a GMMStats
    # Initializes a GMMStats
    gs = GMMStats(2, 3)
    log_likelihood = -3.
    T = 57
    n = numpy.array([4.37, 5.31], '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
    assert gs.log_likelihood == log_likelihood
    assert gs.t == T
    assert (gs.n == n).all()
    assert (gs.sum_px == sumpx).all()
    assert (gs.sum_pxx == sumpxx).all()
    assert gs.shape == (2, 3)

    # Saves and reads from file
    filename = str(tempfile.mkstemp(".hdf5")[1])
    gs.save(bob.io.base.HDF5File(filename, 'w'))
    gs_loaded = GMMStats(bob.io.base.HDF5File(filename))
    assert gs == gs_loaded
    assert (gs != gs_loaded) is False
    assert gs.is_similar_to(gs_loaded)

    # Saves and reads from file using the keyword argument
    filename = str(tempfile.mkstemp(".hdf5")[1])
    gs.save(hdf5=bob.io.base.HDF5File(filename, 'w'))
    gs_loaded = GMMStats(bob.io.base.HDF5File(filename))
    assert gs == gs_loaded
    assert (gs != gs_loaded) is False
    assert gs.is_similar_to(gs_loaded)

    # Saves and load from file using the keyword argument
    filename = str(tempfile.mkstemp(".hdf5")[1])
    gs.save(hdf5=bob.io.base.HDF5File(filename, 'w'))
    gs_loaded = GMMStats()
    gs_loaded.load(bob.io.base.HDF5File(filename))
    assert gs == gs_loaded
    assert (gs != gs_loaded) is False
    assert gs.is_similar_to(gs_loaded)

    # Saves and load from file using the keyword argument
    filename = str(tempfile.mkstemp(".hdf5")[1])
    gs.save(hdf5=bob.io.base.HDF5File(filename, 'w'))
    gs_loaded = GMMStats()
    gs_loaded.load(hdf5=bob.io.base.HDF5File(filename))
    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 (gs.is_similar_to(gs_loaded)) is False
    # Accumulates from another GMMStats
    gs2 = GMMStats(2, 3)
    gs2.log_likelihood = log_likelihood
    gs2.t = T
    gs2.n = n
    gs2.sum_px = sumpx
    gs2.sum_pxx = sumpxx
    gs2 += gs
    eps = 1e-8
    assert gs2.log_likelihood == 2 * log_likelihood
    assert gs2.t == 2 * T
    assert numpy.allclose(gs2.n, 2 * n, eps)
    assert numpy.allclose(gs2.sum_px, 2 * sumpx, eps)
    assert numpy.allclose(gs2.sum_pxx, 2 * sumpxx, eps)

    # Reinit and checks for zeros
    gs_loaded.init()
    assert gs_loaded.log_likelihood == 0
    assert gs_loaded.t == 0
    assert (gs_loaded.n == 0).all()
    assert (gs_loaded.sum_px == 0).all()
    assert (gs_loaded.sum_pxx == 0).all()
    # Resize and checks size
    assert gs_loaded.shape == (2, 3)
    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)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
def test_trainer_update_sigma():
  # Ubm
  dim_c = 2
  dim_d = 3
  ubm = GMMMachine(dim_c,dim_d)
  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
  gs1 = GMMStats(dim_c,dim_d)
  log_likelihood1 = -3.
  T1 = 1
  n1 = numpy.array([0.4, 0.6], numpy.float64)
  sumpx1 = numpy.array([[1., 2., 3.], [2., 4., 3.]], numpy.float64)
  sumpxx1 = numpy.array([[10., 20., 30.], [40., 50., 60.]], numpy.float64)
  gs1.log_likelihood = log_likelihood1
  gs1.t = T1
  gs1.n = n1
  gs1.sum_px = sumpx1
  gs1.sum_pxx = sumpxx1

  gs2 = GMMStats(dim_c,dim_d)
  log_likelihood2 = -4.
  T2 = 1
  n2 = numpy.array([0.2, 0.8], numpy.float64)
  sumpx2 = numpy.array([[2., 1., 3.], [3., 4.1, 3.2]], numpy.float64)
  sumpxx2 = numpy.array([[12., 15., 25.], [39., 51., 62.]], numpy.float64)
  gs2.log_likelihood = log_likelihood2
  gs2.t = T2
  gs2.n = n2
  gs2.sum_px = sumpx2
  gs2.sum_pxx = sumpxx2

  data = [gs1, gs2]

  # Reference values
  acc_Nij_Sigma_wij2_ref1  = {0: numpy.array([[ 0.03202305, -0.02947769], [-0.02947769,  0.0561132 ]]),
                              1: numpy.array([[ 0.07953279, -0.07829414], [-0.07829414,  0.13814242]])}
  acc_Fnorm_Sigma_wij_ref1 = {0: numpy.array([[-0.29622691,  0.61411796], [ 0.09391764, -0.27955961], [-0.39014455,  0.89367757]]),
                              1: numpy.array([[ 0.04695882, -0.13977981], [-0.05718673,  0.24159665], [-0.17098161,  0.47326585]])}
  acc_Snorm_ref1           = numpy.array([16.6, 22.4, 16.6, 61.4, 55., 97.4])
  N_ref1                   = numpy.array([0.6, 1.4])
  t_ref1                   = numpy.array([[  1.59543739, 11.78239235], [ -3.20130371, -6.66379081], [  4.79674111, 18.44618316],
                                          [ -0.91765407, -1.5319461 ], [  2.26805901,  3.03434944], [  2.76600031,  4.9935962 ]])
  sigma_ref1               = numpy.array([ 16.39472121, 34.72955353,  3.3108037, 43.73496916, 38.85472445, 68.22116903])

  acc_Nij_Sigma_wij2_ref2  = {0: numpy.array([[ 0.50807426, -0.11907756], [-0.11907756,  0.12336544]]),
                              1: numpy.array([[ 1.18602399, -0.2835859 ], [-0.2835859 ,  0.39440498]])}
  acc_Fnorm_Sigma_wij_ref2 = {0: numpy.array([[ 0.07221453,  1.1189786 ], [-0.08681275, -0.35396112], [ 0.15902728,  1.47293972]]),
                              1: numpy.array([[-0.04340637, -0.17698056], [ 0.10662127,  0.21484933],[ 0.13116645,  0.64474271]])}
  acc_Snorm_ref2           = numpy.array([16.6, 22.4, 16.6, 61.4, 55., 97.4])
  N_ref2                   = numpy.array([0.6, 1.4])
  t_ref2                   = numpy.array([[  2.93105054, 11.89961223], [ -1.08988119, -3.92120757], [  4.02093173, 15.82081981],
                                          [ -0.17376634, -0.57366984], [  0.26585634,  0.73589952], [  0.60557877,   2.07014704]])
  sigma_ref2               = numpy.array([5.12154025e+00, 3.48623823e+01, 1.00000000e-05, 4.37792350e+01, 3.91525332e+01, 6.85613258e+01])

  acc_Nij_Sigma_wij2_ref = [acc_Nij_Sigma_wij2_ref1, acc_Nij_Sigma_wij2_ref2]
  acc_Fnorm_Sigma_wij_ref = [acc_Fnorm_Sigma_wij_ref1, acc_Fnorm_Sigma_wij_ref2]
  acc_Snorm_ref = [acc_Snorm_ref1, acc_Snorm_ref2]
  N_ref = [N_ref1, N_ref2]
  t_ref = [t_ref1, t_ref2]
  sigma_ref = [sigma_ref1, sigma_ref2]


  # Python implementation
  # Machine
  m = IVectorMachine(ubm, 2)
  t = numpy.array([[1.,2],[4,1],[0,3],[5,8],[7,10],[11,1]])
  sigma = numpy.array([1.,2.,1.,3.,2.,4.])

  # Initialization
  trainer = IVectorTrainerPy(sigma_update=True)
  trainer.initialize(m, data)
  m.t = t
  m.sigma = sigma
  for it in range(2):
    # E-Step
    trainer.e_step(m, data)
    for k in acc_Nij_Sigma_wij2_ref[it]:
      assert numpy.allclose(acc_Nij_Sigma_wij2_ref[it][k], trainer.m_acc_Nij_Sigma_wij2[k], 1e-5)
    for k in acc_Fnorm_Sigma_wij_ref[it]:
      assert numpy.allclose(acc_Fnorm_Sigma_wij_ref[it][k], trainer.m_acc_Fnorm_Sigma_wij[k], 1e-5)
    assert numpy.allclose(acc_Snorm_ref[it], trainer.m_acc_Snorm, 1e-5)
    assert numpy.allclose(N_ref[it], trainer.m_N, 1e-5)

    # M-Step
    trainer.m_step(m, data)
    assert numpy.allclose(t_ref[it], m.t, 1e-5)
    assert numpy.allclose(sigma_ref[it], m.sigma, 1e-5)


  # C++ implementation
  # Machine
  m = IVectorMachine(ubm, 2)
  m.variance_threshold = 1e-5

  # Initialization
  trainer = IVectorTrainer(update_sigma=True)
  trainer.initialize(m)
  m.t = t
  m.sigma = sigma
  for it in range(2):
    # E-Step
    trainer.e_step(m, data)
    for k in acc_Nij_Sigma_wij2_ref[it]:
      assert numpy.allclose(acc_Nij_Sigma_wij2_ref[it][k], trainer.acc_nij_wij2[k], 1e-5)
    for k in acc_Fnorm_Sigma_wij_ref[it]:
      assert numpy.allclose(acc_Fnorm_Sigma_wij_ref[it][k], trainer.acc_fnormij_wij[k], 1e-5)
    assert numpy.allclose(acc_Snorm_ref[it].reshape(dim_c,dim_d), trainer.acc_snormij, 1e-5)
    assert numpy.allclose(N_ref[it], trainer.acc_nij, 1e-5)

    # M-Step
    trainer.m_step(m)
    assert numpy.allclose(t_ref[it], m.t, 1e-5)
    assert numpy.allclose(sigma_ref[it], m.sigma, 1e-5)
Ejemplo n.º 9
0
def test_trainer_nosigma():
  # 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
  gs1 = GMMStats(2,3)
  log_likelihood1 = -3.
  T1 = 1
  n1 = numpy.array([0.4, 0.6], numpy.float64)
  sumpx1 = numpy.array([[1., 2., 3.], [2., 4., 3.]], numpy.float64)
  sumpxx1 = numpy.array([[10., 20., 30.], [40., 50., 60.]], numpy.float64)
  gs1.log_likelihood = log_likelihood1
  gs1.t = T1
  gs1.n = n1
  gs1.sum_px = sumpx1
  gs1.sum_pxx = sumpxx1

  gs2 = GMMStats(2,3)
  log_likelihood2 = -4.
  T2 = 1
  n2 = numpy.array([0.2, 0.8], numpy.float64)
  sumpx2 = numpy.array([[2., 1., 3.], [3., 4.1, 3.2]], numpy.float64)
  sumpxx2 = numpy.array([[12., 15., 25.], [39., 51., 62.]], numpy.float64)
  gs2.log_likelihood = log_likelihood2
  gs2.t = T2
  gs2.n = n2
  gs2.sum_px = sumpx2
  gs2.sum_pxx = sumpxx2

  data = [gs1, gs2]


  acc_Nij_Sigma_wij2_ref1  = {0: numpy.array([[ 0.03202305, -0.02947769], [-0.02947769,  0.0561132 ]]),
                             1: numpy.array([[ 0.07953279, -0.07829414], [-0.07829414,  0.13814242]])}
  acc_Fnorm_Sigma_wij_ref1 = {0: numpy.array([[-0.29622691,  0.61411796], [ 0.09391764, -0.27955961], [-0.39014455,  0.89367757]]),
                             1: numpy.array([[ 0.04695882, -0.13977981], [-0.05718673,  0.24159665], [-0.17098161,  0.47326585]])}
  acc_Snorm_ref1           = numpy.array([16.6, 22.4, 16.6, 61.4, 55., 97.4])
  N_ref1                   = numpy.array([0.6, 1.4])
  t_ref1                   = numpy.array([[  1.59543739, 11.78239235], [ -3.20130371, -6.66379081], [  4.79674111, 18.44618316],
                                          [ -0.91765407, -1.5319461 ], [  2.26805901,  3.03434944], [  2.76600031,  4.9935962 ]])

  acc_Nij_Sigma_wij2_ref2  = {0: numpy.array([[ 0.37558389, -0.15405228], [-0.15405228,  0.1421269 ]]),
                             1: numpy.array([[ 1.02076081, -0.57683953], [-0.57683953,  0.53912239]])}
  acc_Fnorm_Sigma_wij_ref2 = {0: numpy.array([[-1.1261668 ,  1.46496753], [-0.03579289, -0.37875811], [-1.09037391,  1.84372565]]),
                             1: numpy.array([[-0.01789645, -0.18937906], [ 0.35221084,  0.15854126], [-0.10004552,  0.72559036]])}
  acc_Snorm_ref2           = numpy.array([16.6, 22.4, 16.6, 61.4, 55., 97.4])
  N_ref2                   = numpy.array([0.6, 1.4])
  t_ref2                   = numpy.array([[  2.2133685,  12.70654597], [ -2.13959381, -4.98404887], [  4.35296231, 17.69059484],
                                          [ -0.54644055, -0.93594252], [  1.29308324,  1.67762053], [  1.67583072,  3.13894546]])
  acc_Nij_Sigma_wij2_ref = [acc_Nij_Sigma_wij2_ref1, acc_Nij_Sigma_wij2_ref2]
  acc_Fnorm_Sigma_wij_ref = [acc_Fnorm_Sigma_wij_ref1, acc_Fnorm_Sigma_wij_ref2]
  acc_Snorm_ref = [acc_Snorm_ref1, acc_Snorm_ref2]
  N_ref = [N_ref1, N_ref2]
  t_ref = [t_ref1, t_ref2]

  # Python implementation
  # Machine
  m = IVectorMachine(ubm, 2)
  t = numpy.array([[1.,2],[4,1],[0,3],[5,8],[7,10],[11,1]])
  sigma = numpy.array([1.,2.,1.,3.,2.,4.])

  # Initialization
  trainer = IVectorTrainerPy()
  trainer.initialize(m, data)
  m.t = t
  m.sigma = sigma
  for it in range(2):
    # E-Step
    trainer.e_step(m, data)
    for k in acc_Nij_Sigma_wij2_ref[it]:
      assert numpy.allclose(acc_Nij_Sigma_wij2_ref[it][k], trainer.m_acc_Nij_Sigma_wij2[k], 1e-5)
    for k in acc_Fnorm_Sigma_wij_ref[it]:
      assert numpy.allclose(acc_Fnorm_Sigma_wij_ref[it][k], trainer.m_acc_Fnorm_Sigma_wij[k], 1e-5)
    assert numpy.allclose(acc_Snorm_ref[it], trainer.m_acc_Snorm, 1e-5)
    assert numpy.allclose(N_ref[it], trainer.m_N, 1e-5)

    # M-Step
    trainer.m_step(m, data)
    assert numpy.allclose(t_ref[it], m.t, 1e-5)

  # C++ implementation
  # Machine
  m = IVectorMachine(ubm, 2)

  # Initialization
  trainer = IVectorTrainer()
  trainer.initialize(m)
  m.t = t
  m.sigma = sigma
  for it in range(2):
    # E-Step
    trainer.e_step(m, data)
    for k in acc_Nij_Sigma_wij2_ref[it]:
      assert numpy.allclose(acc_Nij_Sigma_wij2_ref[it][k], trainer.acc_nij_wij2[k], 1e-5)
    for k in acc_Fnorm_Sigma_wij_ref[it]:
      assert numpy.allclose(acc_Fnorm_Sigma_wij_ref[it][k], trainer.acc_fnormij_wij[k], 1e-5)

    # M-Step
    trainer.m_step(m)
    assert numpy.allclose(t_ref[it], m.t, 1e-5)


  #testing exceptions
  nose.tools.assert_raises(RuntimeError, trainer.e_step, m, [1,2,2])
Ejemplo n.º 10
0
def test_GMMStats():
  # Test a GMMStats
  # Initializes a GMMStats
  gs = GMMStats(2,3)
  log_likelihood = -3.
  T = 57
  n = numpy.array([4.37, 5.31], '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
  assert gs.log_likelihood == log_likelihood
  assert gs.t == T
  assert (gs.n == n).all()
  assert (gs.sum_px == sumpx).all()
  assert (gs.sum_pxx == sumpxx).all()
  assert gs.shape==(2,3)

  # Saves and reads from file
  filename = str(tempfile.mkstemp(".hdf5")[1])
  gs.save(bob.io.base.HDF5File(filename, 'w'))
  gs_loaded = GMMStats(bob.io.base.HDF5File(filename))
  assert gs == gs_loaded
  assert (gs != gs_loaded ) is False
  assert gs.is_similar_to(gs_loaded)
  
  # Saves and reads from file using the keyword argument
  filename = str(tempfile.mkstemp(".hdf5")[1])
  gs.save(hdf5=bob.io.base.HDF5File(filename, 'w'))
  gs_loaded = GMMStats(bob.io.base.HDF5File(filename))
  assert gs == gs_loaded
  assert (gs != gs_loaded ) is False
  assert gs.is_similar_to(gs_loaded)

  # Saves and load from file using the keyword argument
  filename = str(tempfile.mkstemp(".hdf5")[1])
  gs.save(hdf5=bob.io.base.HDF5File(filename, 'w'))
  gs_loaded = GMMStats()
  gs_loaded.load(bob.io.base.HDF5File(filename))
  assert gs == gs_loaded
  assert (gs != gs_loaded ) is False
  assert gs.is_similar_to(gs_loaded)

  # Saves and load from file using the keyword argument
  filename = str(tempfile.mkstemp(".hdf5")[1])
  gs.save(hdf5=bob.io.base.HDF5File(filename, 'w'))
  gs_loaded = GMMStats()
  gs_loaded.load(hdf5=bob.io.base.HDF5File(filename))
  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 (gs.is_similar_to(gs_loaded)) is False
  # Accumulates from another GMMStats
  gs2 = GMMStats(2,3)
  gs2.log_likelihood = log_likelihood
  gs2.t = T
  gs2.n = n
  gs2.sum_px = sumpx
  gs2.sum_pxx = sumpxx
  gs2 += gs
  eps = 1e-8
  assert gs2.log_likelihood == 2*log_likelihood
  assert gs2.t == 2*T
  assert numpy.allclose(gs2.n, 2*n, eps)
  assert numpy.allclose(gs2.sum_px, 2*sumpx, eps)
  assert numpy.allclose(gs2.sum_pxx, 2*sumpxx, eps)

  # Reinit and checks for zeros
  gs_loaded.init()
  assert gs_loaded.log_likelihood == 0
  assert gs_loaded.t == 0
  assert (gs_loaded.n == 0).all()
  assert (gs_loaded.sum_px == 0).all()
  assert (gs_loaded.sum_pxx == 0).all()
  # Resize and checks size
  assert  gs_loaded.shape==(2,3)
  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)