def test_increment(): # arguments values mode_values = ["concatenation", "subtraction"] n_features_per_vertex_values = [2, 3] sparse_values = [True, False] n_components_values = [None, 30] # create graph n_vertices = 6 edges = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0]]) graphs = [ DirectedGraph.init_from_edges(edges, n_vertices), UndirectedGraph(np.zeros((n_vertices, n_vertices))), ] for n_features_per_vertex in n_features_per_vertex_values: # create samples n_samples = 100 samples = [] for i in range(n_samples): samples.append(np.random.rand(n_vertices * n_features_per_vertex)) for graph in graphs: for mode in mode_values: for sparse in sparse_values: for n_components in n_components_values: # Incremental GMRF gmrf1 = GMRFVectorModel( samples[:50], graph, mode=mode, sparse=sparse, n_components=n_components, dtype=np.float64, incremental=True, ) gmrf1.increment(samples[50::]) # Non incremental GMRF gmrf2 = GMRFVectorModel( samples, graph, mode=mode, sparse=sparse, n_components=n_components, dtype=np.float64, ) # Compare if sparse: assert_array_almost_equal( gmrf1.precision.todense(), gmrf2.precision.todense()) assert_array_almost_equal(gmrf1.mean_vector, gmrf2.mean_vector)
def test_mahalanobis_distance(): # arguments values mode_values = ['concatenation', 'subtraction'] n_features_per_vertex_values = [2, 3] sparse_values = [True, False] subtract_mean_values = [True, False] n_components_values = [None, 30] # create graph n_vertices = 6 edges = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0]]) graphs = [ DirectedGraph.init_from_edges(edges, n_vertices), UndirectedGraph(np.zeros((n_vertices, n_vertices))) ] for n_features_per_vertex in n_features_per_vertex_values: # create samples n_samples = 50 samples = [] for i in range(n_samples): samples.append( PointCloud(np.random.rand(n_vertices, n_features_per_vertex))) test_sample = PointCloud( np.random.rand(n_vertices, n_features_per_vertex)) for graph in graphs: for mode in mode_values: for sparse in sparse_values: for n_components in n_components_values: # train GMRF gmrf = GMRFModel(samples, graph, mode=mode, sparse=sparse, n_components=n_components, dtype=np.float64) for subtract_mean in subtract_mean_values: # compute costs if graph.n_edges == 0: cost1 = _compute_sum_cost_block_diagonal( samples, test_sample, graph, n_features_per_vertex, subtract_mean) else: cost1 = _compute_sum_cost_block_sparse( samples, test_sample, graph, n_features_per_vertex, subtract_mean, mode) cost2 = gmrf.mahalanobis_distance( test_sample, subtract_mean=subtract_mean) assert_almost_equal(cost1, cost2)
def test_mahalanobis_distance(): # arguments values mode_values = ['concatenation', 'subtraction'] n_features_per_vertex_values = [2, 3] sparse_values = [True, False] subtract_mean_values = [True, False] n_components_values = [None, 30] # create graph n_vertices = 6 edges = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0]]) graphs = [DirectedGraph.init_from_edges(edges, n_vertices), UndirectedGraph(np.zeros((n_vertices, n_vertices)))] for n_features_per_vertex in n_features_per_vertex_values: # create samples n_samples = 50 samples = [] for i in range(n_samples): samples.append(PointCloud(np.random.rand(n_vertices, n_features_per_vertex))) test_sample = PointCloud(np.random.rand(n_vertices, n_features_per_vertex)) for graph in graphs: for mode in mode_values: for sparse in sparse_values: for n_components in n_components_values: # train GMRF gmrf = GMRFModel( samples, graph, mode=mode, sparse=sparse, n_components=n_components, dtype=np.float64) for subtract_mean in subtract_mean_values: # compute costs if graph.n_edges == 0: cost1 = _compute_sum_cost_block_diagonal( samples, test_sample, graph, n_features_per_vertex, subtract_mean) else: cost1 = _compute_sum_cost_block_sparse( samples, test_sample, graph, n_features_per_vertex, subtract_mean, mode) cost2 = gmrf.mahalanobis_distance( test_sample, subtract_mean=subtract_mean) assert_almost_equal(cost1, cost2)
def test_increment(): # arguments values mode_values = ['concatenation', 'subtraction'] n_features_per_vertex_values = [2, 3] sparse_values = [True, False] n_components_values = [None, 30] # create graph n_vertices = 6 edges = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0]]) graphs = [DirectedGraph.init_from_edges(edges, n_vertices), UndirectedGraph(np.zeros((n_vertices, n_vertices)))] for n_features_per_vertex in n_features_per_vertex_values: # create samples n_samples = 100 samples = [] for i in range(n_samples): samples.append(np.random.rand(n_vertices * n_features_per_vertex)) for graph in graphs: for mode in mode_values: for sparse in sparse_values: for n_components in n_components_values: # Incremental GMRF gmrf1 = GMRFVectorModel( samples[:50], graph, mode=mode, sparse=sparse, n_components=n_components, dtype=np.float64, incremental=True) gmrf1.increment(samples[50::]) # Non incremental GMRF gmrf2 = GMRFVectorModel( samples, graph, mode=mode, sparse=sparse, n_components=n_components, dtype=np.float64) # Compare if sparse: assert_array_almost_equal(gmrf1.precision.todense(), gmrf2.precision.todense()) assert_array_almost_equal(gmrf1.mean_vector, gmrf2.mean_vector)
def __init__(self, adjacency_array_appearance=None, gaussian_per_patch=True, adjacency_array_deformation=None, root_vertex_deformation=None, adjacency_array_shape=None, features=no_op, patch_shape=(17, 17), normalization_diagonal=None, n_levels=2, downscale=2, scaled_shape_models=False, use_procrustes=True, max_shape_components=None, n_appearance_parameters=None): # check parameters checks.check_n_levels(n_levels) checks.check_downscale(downscale) checks.check_normalization_diagonal(normalization_diagonal) max_shape_components = checks.check_max_components( max_shape_components, n_levels, 'max_shape_components') features = checks.check_features(features, n_levels) n_appearance_parameters = _check_n_parameters( n_appearance_parameters, n_levels, 'n_appearance_parameters') # appearance graph if adjacency_array_appearance is None: self.graph_appearance = None elif adjacency_array_appearance == 'yorgos': self.graph_appearance = 'yorgos' else: self.graph_appearance = UndirectedGraph(adjacency_array_appearance) # shape graph if adjacency_array_shape is None: self.graph_shape = None else: self.graph_shape = UndirectedGraph(adjacency_array_shape) # check adjacency_array_deformation, root_vertex_deformation if adjacency_array_deformation is None: self.graph_deformation = None if root_vertex_deformation is None: self.root_vertex = 0 else: if root_vertex_deformation is None: self.graph_deformation = DirectedGraph( adjacency_array_deformation) else: self.graph_deformation = Tree(adjacency_array_deformation, root_vertex_deformation) # store parameters self.features = features self.patch_shape = patch_shape self.normalization_diagonal = normalization_diagonal self.n_levels = n_levels self.downscale = downscale self.scaled_shape_models = scaled_shape_models self.max_shape_components = max_shape_components self.n_appearance_parameters = n_appearance_parameters self.use_procrustes = use_procrustes self.gaussian_per_patch = gaussian_per_patch
[0, 1, 1, 0, 0, 0], [1, 0, 1, 1, 0, 0], [1, 1, 0, 0, 1, 0], [0, 1, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0], ] ) g_undirected = UndirectedGraph(adj_undirected) pg_undirected = PointUndirectedGraph(points, adj_undirected) # Define directed graph and pointgraph adj_directed = csr_matrix( ([1] * 8, ([1, 2, 1, 2, 1, 2, 3, 3], [0, 0, 2, 1, 3, 4, 4, 5])), shape=(6, 6) ) g_directed = DirectedGraph(adj_directed) pg_directed = PointDirectedGraph(points, adj_directed) # Define tree and pointtree adj_tree = np.array( [ [0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]
import numpy as np from numpy.testing import assert_allclose from nose.tools import raises from menpo.shape import UndirectedGraph, DirectedGraph, Tree # Define adjacency arrays adj_undirected = np.array([[0, 1], [0, 2], [1, 2], [1, 3], [2, 4], [3, 4], [3, 5]]) g_undirected = UndirectedGraph(adj_undirected) adj_directed = np.array([[1, 0], [2, 0], [1, 2], [2, 1], [1, 3], [2, 4], [3, 4], [3, 5]]) g_directed = DirectedGraph(adj_directed) adj_tree = np.array([[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [3, 6], [4, 7], [5, 8]]) g_tree = Tree(adj_tree, 0) @raises(ValueError) def test_create_graph_exception(): adj = np.array([[0, 1, 3], [0, 2, 4]]) UndirectedGraph(adj) @raises(ValueError) def test_create_tree_exception_1(): adj = np.array([[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [3, 6], [4, 7], [5, 8], [0, 4]]) Tree(adj, 0)