def test_GMMMachine(): # Test a GMMMachine basic features weights = np.array([0.5, 0.5], "float64") weights2 = np.array([0.6, 0.4], "float64") means = np.array([[3, 70, 0], [4, 72, 0]], "float64") means2 = np.array([[3, 7, 0], [4, 72, 0]], "float64") variances = np.array([[1, 10, 1], [2, 5, 2]], "float64") variances2 = np.array([[10, 10, 1], [2, 5, 2]], "float64") varianceThresholds = np.array([[0, 0, 0], [0, 0, 0]], "float64") varianceThresholds2 = np.array([[0.0005, 0.0005, 0.0005], [0, 0, 0]], "float64") # Initializes a GMMMachine gmm = GMMMachine(n_gaussians=2) # Sets the weights, means, variances and varianceThresholds and # Checks correctness gmm.weights = weights gmm.means = means gmm.variances = variances gmm.variance_thresholds = varianceThresholds assert gmm.shape == (2, 3) np.testing.assert_equal(gmm.weights, weights) np.testing.assert_equal(gmm.means, means) np.testing.assert_equal(gmm.variances, variances) np.testing.assert_equal(gmm.variance_thresholds, varianceThresholds) newMeans = np.array([[3, 70, 2], [4, 72, 2]], "float64") newVariances = np.array([[1, 1, 1], [2, 2, 2]], "float64") # Checks particular varianceThresholds-related methods varianceThresholds1D = np.array([0.3, 1, 0.5], "float64") gmm.variance_thresholds = varianceThresholds1D np.testing.assert_equal(gmm.variance_thresholds, varianceThresholds1D) gmm.variance_thresholds = 0.005 np.testing.assert_equal(gmm.variance_thresholds, 0.005) gmm.means = newMeans gmm.variances = newVariances np.testing.assert_equal(gmm.means, newMeans) np.testing.assert_equal(gmm.variances, newVariances) # Checks comparison gmm2 = deepcopy(gmm) gmm3 = GMMMachine(n_gaussians=2) gmm3.weights = weights2 gmm3.means = means gmm3.variances = variances gmm3.variance_thresholds = varianceThresholds gmm4 = GMMMachine(n_gaussians=2) gmm4.weights = weights gmm4.means = means2 gmm4.variances = variances gmm4.variance_thresholds = varianceThresholds gmm5 = GMMMachine(n_gaussians=2) gmm5.weights = weights gmm5.means = means gmm5.variances = variances2 gmm5.variance_thresholds = varianceThresholds gmm6 = GMMMachine(n_gaussians=2) gmm6.weights = weights gmm6.means = means gmm6.variances = variances gmm6.variance_thresholds = varianceThresholds2 assert_gmm_equal(gmm, gmm2) assert (gmm != gmm2) is False assert gmm.is_similar_to(gmm2) assert gmm != gmm3 assert gmm.is_similar_to(gmm3) is False assert gmm != gmm4 assert gmm.is_similar_to(gmm4) is False assert gmm != gmm5 assert gmm.is_similar_to(gmm5) is False assert gmm != gmm6 assert gmm.is_similar_to(gmm6) is False # Saving and loading with tempfile.NamedTemporaryFile(suffix=".hdf5") as f: filename = f.name gmm.save(HDF5File(filename, "w")) # Using from_hdf5 gmm1 = GMMMachine.from_hdf5(HDF5File(filename, "r")) assert type(gmm1.n_gaussians) is np.int64 assert type(gmm1.update_means) is np.bool_ assert type(gmm1.update_variances) is np.bool_ assert type(gmm1.update_weights) is np.bool_ assert type(gmm1.trainer) is str assert gmm1.ubm is None assert_gmm_equal(gmm, gmm1) # Using load gmm1 = GMMMachine(n_gaussians=gmm.n_gaussians) gmm1.load(HDF5File(filename, "r")) assert type(gmm1.n_gaussians) is np.int64 assert type(gmm1.update_means) is np.bool_ assert type(gmm1.update_variances) is np.bool_ assert type(gmm1.update_weights) is np.bool_ assert type(gmm1.trainer) is str assert gmm1.ubm is None assert_gmm_equal(gmm, gmm1) with tempfile.NamedTemporaryFile(suffix=".hdf5") as f: filename = f.name gmm.save(filename) gmm1 = GMMMachine.from_hdf5(filename) assert_gmm_equal(gmm, gmm1) # Weights n_gaussians = 5 machine = GMMMachine(n_gaussians) default_weights = np.full(shape=(n_gaussians, ), fill_value=1.0 / n_gaussians) default_log_weights = np.full(shape=(n_gaussians, ), fill_value=np.log(1.0 / n_gaussians)) # Test weights getting and setting np.testing.assert_almost_equal(machine.weights, default_weights) np.testing.assert_almost_equal(machine.log_weights, default_log_weights) modified_weights = default_weights modified_weights[:n_gaussians // 2] = (1 / n_gaussians) / 2 modified_weights[n_gaussians // 2 + n_gaussians % 2:] = (1 / n_gaussians) * 1.5 # Ensure setter works (log_weights is updated correctly) machine.weights = modified_weights np.testing.assert_almost_equal(machine.weights, modified_weights) np.testing.assert_almost_equal(machine.log_weights, np.log(modified_weights))
def 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
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