示例#1
0
 def write_biometric_reference(self, model: GMMMachine, model_file):
     """Write the enrolled reference (MAP GMMMachine) into a file."""
     return model.save(model_file)
示例#2
0
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))