Exemple #1
0
def test_pca_increment_centred():
    pca_samples = [PointCloud(np.random.randn(10, 2)) for _ in range(10)]
    ipca_model = PCAModel(pca_samples[:3])
    ipca_model.increment(pca_samples[3:6])
    ipca_model.increment(pca_samples[6:])

    bpca_model = PCAModel(pca_samples)

    assert_almost_equal(np.abs(ipca_model.components),
                        np.abs(bpca_model.components))
    assert_almost_equal(ipca_model.eigenvalues, bpca_model.eigenvalues)
    assert_almost_equal(ipca_model.mean().as_vector(),
                        bpca_model.mean().as_vector())
Exemple #2
0
def test_pca_increment_centred():
    pca_samples = [PointCloud(np.random.randn(10, 2)) for _ in range(10)]
    ipca_model = PCAModel(pca_samples[:3])
    ipca_model.increment(pca_samples[3:6])
    ipca_model.increment(pca_samples[6:])

    bpca_model = PCAModel(pca_samples)

    assert_almost_equal(np.abs(ipca_model.components),
                        np.abs(bpca_model.components))
    assert_almost_equal(ipca_model.eigenvalues, bpca_model.eigenvalues)
    assert_almost_equal(ipca_model.mean().as_vector(),
                        bpca_model.mean().as_vector())
def test_pca_init_from_covariance():
    n_samples = 30
    n_features = 10
    n_dims = 2
    centre_values = [True, False]
    for centre in centre_values:
        # generate samples list and convert it to nd.array
        samples = [
            PointCloud(np.random.randn(n_features, n_dims))
            for _ in range(n_samples)
        ]
        data, template = as_matrix(samples, return_template=True)
        # compute covariance matrix and mean
        if centre:
            mean_vector = np.mean(data, axis=0)
            mean = template.from_vector(mean_vector)
            X = data - mean_vector
            C = np.dot(X.T, X) / (n_samples - 1)
        else:
            mean = samples[0]
            C = np.dot(data.T, data) / (n_samples - 1)
        # create the 2 pca models
        pca1 = PCAModel.init_from_covariance_matrix(C,
                                                    mean,
                                                    centred=centre,
                                                    n_samples=n_samples)
        pca2 = PCAModel(samples, centre=centre)
        # compare them
        assert_array_almost_equal(pca1.component_vector(0, with_mean=False),
                                  pca2.component_vector(0, with_mean=False))
        assert_array_almost_equal(
            pca1.component(7).as_vector(),
            pca2.component(7).as_vector())
        assert_array_almost_equal(pca1.components, pca2.components)
        assert_array_almost_equal(pca1.eigenvalues, pca2.eigenvalues)
        assert_array_almost_equal(pca1.eigenvalues_cumulative_ratio(),
                                  pca2.eigenvalues_cumulative_ratio())
        assert_array_almost_equal(pca1.eigenvalues_ratio(),
                                  pca2.eigenvalues_ratio())
        weights = np.random.randn(pca1.n_active_components)
        assert_array_almost_equal(
            pca1.instance(weights).as_vector(),
            pca2.instance(weights).as_vector())
        weights2 = np.random.randn(pca1.n_active_components - 4)
        assert_array_almost_equal(pca1.instance_vector(weights2),
                                  pca2.instance_vector(weights2))
        assert_array_almost_equal(pca1.mean().as_vector(),
                                  pca2.mean().as_vector())
        assert_array_almost_equal(pca1.mean_vector, pca2.mean_vector)
        assert (pca1.n_active_components == pca2.n_active_components)
        assert (pca1.n_components == pca2.n_components)
        assert (pca1.n_features == pca2.n_features)
        assert (pca1.n_samples == pca2.n_samples)
        assert (pca1.noise_variance() == pca2.noise_variance())
        assert (pca1.noise_variance_ratio() == pca2.noise_variance_ratio())
        assert_almost_equal(pca1.variance(), pca2.variance())
        assert_almost_equal(pca1.variance_ratio(), pca2.variance_ratio())
        assert_array_almost_equal(pca1.whitened_components(),
                                  pca2.whitened_components())
Exemple #4
0
def test_pca_init_from_covariance():
    n_samples = 30
    n_features = 10
    n_dims = 2
    centre_values = [True, False]
    for centre in centre_values:
        # generate samples list and convert it to nd.array
        samples = [PointCloud(np.random.randn(n_features, n_dims))
                   for _ in range(n_samples)]
        data, template = as_matrix(samples, return_template=True)
        # compute covariance matrix and mean
        if centre:
            mean_vector = np.mean(data, axis=0)
            mean = template.from_vector(mean_vector)
            X = data - mean_vector
            C = np.dot(X.T, X) / (n_samples - 1)
        else:
            mean = samples[0]
            C = np.dot(data.T, data) / (n_samples - 1)
        # create the 2 pca models
        pca1 = PCAModel.init_from_covariance_matrix(C, mean,
                                                    centred=centre,
                                                    n_samples=n_samples)
        pca2 = PCAModel(samples, centre=centre)
        # compare them
        assert_array_almost_equal(pca1.component_vector(0, with_mean=False),
                                  pca2.component_vector(0, with_mean=False))
        assert_array_almost_equal(pca1.component(7).as_vector(),
                                  pca2.component(7).as_vector())
        assert_array_almost_equal(pca1.components, pca2.components)
        assert_array_almost_equal(pca1.eigenvalues, pca2.eigenvalues)
        assert_array_almost_equal(pca1.eigenvalues_cumulative_ratio(),
                                  pca2.eigenvalues_cumulative_ratio())
        assert_array_almost_equal(pca1.eigenvalues_ratio(),
                                  pca2.eigenvalues_ratio())
        weights = np.random.randn(pca1.n_active_components)
        assert_array_almost_equal(pca1.instance(weights).as_vector(),
                                  pca2.instance(weights).as_vector())
        weights2 = np.random.randn(pca1.n_active_components - 4)
        assert_array_almost_equal(pca1.instance_vector(weights2),
                                  pca2.instance_vector(weights2))
        assert_array_almost_equal(pca1.mean().as_vector(),
                                  pca2.mean().as_vector())
        assert_array_almost_equal(pca1.mean_vector,
                                  pca2.mean_vector)
        assert(pca1.n_active_components == pca2.n_active_components)
        assert(pca1.n_components == pca2.n_components)
        assert(pca1.n_features == pca2.n_features)
        assert(pca1.n_samples == pca2.n_samples)
        assert(pca1.noise_variance() == pca2.noise_variance())
        assert(pca1.noise_variance_ratio() == pca2.noise_variance_ratio())
        assert_almost_equal(pca1.variance(), pca2.variance())
        assert_almost_equal(pca1.variance_ratio(), pca2.variance_ratio())
        assert_array_almost_equal(pca1.whitened_components(),
                                  pca2.whitened_components())
Exemple #5
0
def _build_appearance_model_full(all_patches, n_appearance_parameters,
                                 level_str, verbose):
    # build appearance model
    if verbose:
        print_dynamic('{}Training appearance distribution'.format(level_str))

    # apply pca
    appearance_model = PCAModel(all_patches)

    # trim components
    if n_appearance_parameters is not None:
        appearance_model.trim_components(n_appearance_parameters)

    # get mean appearance vector
    app_mean = appearance_model.mean().as_vector()

    # compute covariance matrix
    app_cov = appearance_model.components.T.dot(np.diag(1/appearance_model.eigenvalues)).dot(appearance_model.components)

    return app_mean, app_cov