Ejemplo n.º 1
0
def generate_texture_model_from_image_3d_fits(images_and_fits,
                                              lambda_=0.01,
                                              n_components=0.99):
    """Build an ITW texture model from a list of images with associated dense
    3D fits (one per image). Note that the input images should already have an
    image feature taken on them, and have all been resized to a consistent
    scale."""
    f = open("/content/fk.txt", "w")
    f.write("shit")
    f.close()
    feat_img, fit_2d = images_and_fits[0]
    n_channels = feat_img.n_channels
    n_features = n_channels * fit_2d.n_points
    n_samples = len(images_and_fits)
    X = np.empty((n_samples, n_features), dtype=feat_img.pixels.dtype)
    M = np.empty_like(X, dtype=np.bool)
    proportion_masked = []
    for i, (img, fit_2d) in enumerate(
            print_progress(images_and_fits,
                           prefix='Extracting masks & features')):
        features, mask = extract_per_vertex_colour_with_occlusion(fit_2d, img)
        mask_repeated = np.repeat(mask.ravel(), n_channels)
        X[i] = features.ravel()
        M[i] = mask_repeated.ravel()
        proportion_masked.append(mask.sum() / mask.shape[0])
    print('Performing R-PCA to complete missing textures')
    A, E = rpca_missing(X, M, verbose=True, lambda_=lambda_)
    print('R-PCA completed. Building PCA model of features on completed '
          'samples.')
    model = PCAVectorModel(A, inplace=True)
    print('Trimming the components to retain only what was required.')
    model.trim_components(n_components=n_components)

    return model, X, M
Ejemplo n.º 2
0
def generate_texture_model_from_itwmm(images, mm, id_ind, exp_ind,
                                      template_camera, p, qs, cs,
                                      lambda_=0.01,
                                      n_components=0.99):
    r"""Build a new texture model from an existing model and fitting
    information to a collection of images."""
    n_channels = images[0].n_channels
    n_features = n_channels * mm.n_vertices
    n_samples = len(images)
    X = np.empty((n_samples, n_features), dtype=mm.texture_model.mean().dtype)
    M = np.empty_like(X, dtype=np.bool)
    proportion_masked = []
    for i, (img, q, c) in enumerate(zip(print_progress(images, 'Extracting '
                                                               'masks and '
                                                               'features'), qs,
                                        cs)):
        i_in_img = instance_for_params(mm, id_ind, exp_ind,
                                       template_camera,
                                       p, q, c)['instance_in_img']
        features, mask = extract_per_vertex_colour_with_occlusion(i_in_img, img)
        mask_repeated = np.repeat(mask.ravel(), n_channels)
        X[i] = features.ravel()
        M[i] = mask_repeated.ravel()
        proportion_masked.append(mask.sum() / mask.shape[0])
    print('Extraction concluded. Self-occlusions on average masked {:.0%} of '
          'vertices.'.format(np.array(proportion_masked).mean()))
    print('Performing R-PCA to complete missing textures')
    A, E = rpca_missing(X, M, verbose=True, lambda_=lambda_)
    print('R-PCA completed. Building PCA model of features on completed '
          'samples.')
    model = PCAVectorModel(A, inplace=True)
    print('Trimming the components to retain only what was required.')
    model.trim_components(n_components=n_components)
    return model, X, M
Ejemplo n.º 3
0
def test_pca_variance_after_trim():
    samples = [np.random.randn(10) for _ in range(10)]
    model = PCAVectorModel(samples)
    # set number of active components
    model.trim_components(5)
    # kept variance must be smaller than total variance
    assert(model.variance() < model.original_variance())
    # kept variance ratio must be smaller than 1.0
    assert(model.variance_ratio() < 1.0)
    # noise variance must be bigger than 0.0
    assert(model.noise_variance() > 0.0)
    # noise variance ratio must also be bigger than 0.0
    assert(model.noise_variance_ratio() > 0.0)
    # inverse noise variance is computable
    assert(model.inverse_noise_variance() == 1 / model.noise_variance())
Ejemplo n.º 4
0
def test_pca_variance_after_trim():
    samples = [np.random.randn(10) for _ in range(10)]
    model = PCAVectorModel(samples)
    # set number of active components
    model.trim_components(5)
    # kept variance must be smaller than total variance
    assert model.variance() < model.original_variance()
    # kept variance ratio must be smaller than 1.0
    assert model.variance_ratio() < 1.0
    # noise variance must be bigger than 0.0
    assert model.noise_variance() > 0.0
    # noise variance ratio must also be bigger than 0.0
    assert model.noise_variance_ratio() > 0.0
    # inverse noise variance is computable
    assert model.inverse_noise_variance() == 1 / model.noise_variance()
Ejemplo n.º 5
0
def test_pca_trim_variance_limit():
    samples = [np.random.randn(10) for _ in range(10)]
    model = PCAVectorModel(samples)
    # impossible to keep more than 1.0 ratio variance
    model.trim_components(2.5)
Ejemplo n.º 6
0
def test_pca_trim_variance_limit():
    samples = [np.random.randn(10) for _ in range(10)]
    model = PCAVectorModel(samples)
    with raises(ValueError):
        # impossible to keep more than 1.0 ratio variance
        model.trim_components(2.5)