Ejemplo n.º 1
0
 def call(self, x):
     g0 = scipy.sparse.csr_matrix(self.build_graph()).astype(np.float32)
     graphs0 = []
     for i in range(self.num_layers):
         graphs0.append(g0)
     L = [graph.laplacian(A, normalized=True) for A in graphs0]
     gcn_out = []
     for i in range(self.num_layers):
         with tf.variable_scope('gcn{}'.format(i + 1)):
             with tf.name_scope('filter'):
                 old = x
                 x = self.my_gcn(x, L[i], str(i))
             with tf.name_scope('bias_relu'):
                 x = self.brelu(x)
             with tf.name_scope('pooling'):
                 x = MaxPooling1D(pool_size=1, padding='same')(x)
                 att = activations.softmax(x - old)
                 if i > 0: x = tf.multiply(x, att)
                 gcn_out.append(x)
     #x = tf.multiply(x,att)
     x = tf.concat(gcn_out, -1)
     # print('out_x',x.get_shape())
     x = K.dot(x, self.w_layers)
     # print('dot_x',x.get_shape())
     x = K.squeeze(x, 2)
     return x
Ejemplo n.º 2
0
def get_laplacians(adj_list):
    laplacians_list = []
    for adj_item in adj_list:
        laplacian = graph.laplacian(adj_item, normalized=True)
        laplacian = laplacian_to_sparse(laplacian)
        laplacians_list.append(laplacian)
    return laplacians_list
Ejemplo n.º 3
0
    def build_graph(cls, args):
        number_edges = args.number_edges
        metric = args.metric
        normalized_laplacian = args.normalized_laplacian
        coarsening_levels = args.coarsening_levels
        def grid_graph(m, corners=False):
            z = graph.grid(m)
            # compute pairwise distance
            dist, idx = graph.distance_sklearn_metrics(z, k=number_edges, metric=metric)
            A = graph.adjacency(dist, idx) # build adjacent matrix
            # Connections are only vertical or horizontal on the grid.
            # Corner vertices are connected to 2 neightbors only.
            if corners:
                A = A.toarray()
                A[A < A.max()/1.5] = 0
                A = scipy.sparse.csr_matrix(A)
                print('{} edges'.format(A.nnz))

            print("{} > {} edges".format(A.nnz//2, number_edges*m**2//2))
            return A
        
        g = grid_graph(28, corners=False)
        g = graph.replace_random_edges(g, 0)
        graphs, perm = coarsening.coarsen(g, levels=coarsening_levels, self_connections=False)
        laplacians = [graph.laplacian(g, normalized=True) for g in graphs]
        
        cls.perm = perm
        cls.graphs = graphs
        cls.laplacians = laplacians
Ejemplo n.º 4
0
def build_laplacian(k):
    fullgraph = pickle.load(open(r"C:\Users\veronica\Desktop\study\Deep Learning\Project\full_interactions_graph", 'rb'))[0:k, 0:k]
    A = csr_matrix(fullgraph).astype(np.float32)
    graphs, perm = coarsening.coarsen(A, levels=3, self_connections=False)
    L = [graph.laplacian(A, normalized=True) for A in graphs]
    pickle.dump(L, open("L"+str(k), 'wb'))
    pickle.dump(perm, open("prem"+str(k), 'wb'))
    return L, perm
Ejemplo n.º 5
0
def cal_lap(graph):
    g0 = sparse.csr_matrix(utils.build_graph(graph)).astype(np.float32)
    print('graph_size:', g0.shape)
    graphs0 = []
    for i in range(3):
        graphs0.append(g0)
    L0 = [graph.laplacian(A, normalized=True) for A in graphs0]
    L1 = L0
Ejemplo n.º 6
0
    def __init__(self, adj, classes_num, args):
        super(FineGrainedGCNN, self).__init__()
        self.batch_size = args.batch_size
        self.filter_size = args.filter_size
        self.pooling_size = args.pooling_size
        self.poly_degree = args.poly_degree
        self.adjs_1 = [adj.toarray() for j in range(self.batch_size)]
        self.classes_num = classes_num
        laplacian = graph.laplacian(adj, normalized=True)
        laplacian = laplacian_to_sparse(laplacian)
        laplacians_1 = [laplacian for i in range(self.batch_size)]

        self.feature_num = 5
        self.laplacians_gn = None

        # --- Gating Notwork
        self.gc = ChebshevGCNN(
            in_channels=self.poly_degree[0],  # 25
            out_channels=self.filter_size[0],  # 32
            poly_degree=self.poly_degree[0],  # 25
            pooling_size=self.pooling_size[0],
            laplacians=laplacians_1)
        self.fc = nn.Linear(in_features=62 * self.filter_size[0] *
                            self.feature_num,
                            out_features=classes_num)

        # --- Expert 1
        self.gc_expert_1 = ChebshevGCNN(
            in_channels=self.poly_degree[0],  # 25
            out_channels=self.filter_size[0],  # 32
            poly_degree=self.poly_degree[0],  # 25
            pooling_size=self.pooling_size[0],
            laplacians=laplacians_1)
        self.fc_expert_1 = nn.Linear(in_features=62 * self.filter_size[0] *
                                     self.feature_num,
                                     out_features=self.classes_num)

        # --- Expert 2
        self.gc_expert_2 = ChebshevGCNN(
            in_channels=self.poly_degree[0],  # 25
            out_channels=self.filter_size[0],  # 32
            poly_degree=self.poly_degree[0],  # 25
            pooling_size=self.pooling_size[0],
            laplacians=laplacians_1)
        self.fc_expert_2 = nn.Linear(in_features=62 * self.filter_size[0] *
                                     self.feature_num,
                                     out_features=self.classes_num).to(DEVICE)

        # --- Expert 3
        self.gc_expert_3 = ChebshevGCNN(
            in_channels=self.poly_degree[0],  # 25
            out_channels=self.filter_size[0],  # 32
            poly_degree=self.poly_degree[0],  # 25
            pooling_size=self.pooling_size[0],
            laplacians=laplacians_1)
        self.fc_expert_3 = nn.Linear(in_features=62 * self.filter_size[0] *
                                     self.feature_num,
                                     out_features=self.classes_num).to(DEVICE)
Ejemplo n.º 7
0
 def call(self, x):
     g0 = scipy.sparse.csr_matrix(self.build_graph()).astype(np.float32)
     graphs0 = []
     for i in range(3):
         graphs0.append(g0)
     L = [graph.laplacian(A, normalized=True) for A in graphs0]
     for i in range(2):
         with tf.variable_scope('gcn{}'.format(i + 1)):
             with tf.name_scope('filter'):
                 x = self.my_gcn(x, L[i], str(i))
             with tf.name_scope('bias_relu'):
                 x = self.brelu(x)
             with tf.name_scope('pooling'):
                 x = MaxPooling1D(pool_size=1, padding='same')(x)
     x = K.squeeze(x, 2)
     return x
Ejemplo n.º 8
0
def init_sampling(refer_mesh, data_dir, dataname, ds_factors=(4, 4, 4, 4)):
    # Sampling factor of the mesh at each stage of sampling

    # Generates adjecency matrices A, downsampling matrices D, and upsamling matrices U by sampling
    # the mesh 4 times. Each time the mesh is sampled by a factor of 4
    adj_path = os.path.join(data_dir, 'adjacency')
    ds_path = os.path.join(data_dir, 'downsamp_trans')
    us_path = os.path.join(data_dir, 'upsamp_trans')
    lap_path = os.path.join(data_dir, 'laplacians')

    if not os.path.isfile(lap_path + '0.npz'):
        logger = logging.getLogger('x')
        logger.info('Computing Sampling Parameters')
        adjacencies, downsamp_trans, upsamp_trans = mesh_sampling.generate_transform_matrices(
            dataname, refer_mesh['vertices'], refer_mesh['faces'], ds_factors)
        adjacencies = [x.astype('float32') for x in adjacencies]
        downsamp_trans = [x.astype('float32') for x in downsamp_trans]
        upsamp_trans = [x.astype('float32') for x in upsamp_trans]
        laplacians = [graph.laplacian(a, normalized=True) for a in adjacencies]

        if not os.path.exists(data_dir):
            os.makedirs(data_dir)
        for i, a in enumerate(adjacencies):
            sp.save_npz(adj_path + '{}.npz'.format(i), a)
        for i, d in enumerate(downsamp_trans):
            sp.save_npz(ds_path + '{}.npz'.format(i), d)
        for i, u in enumerate(upsamp_trans):
            sp.save_npz(us_path + '{}.npz'.format(i), u)
        for i, l in enumerate(laplacians):
            sp.save_npz(lap_path + '{}.npz'.format(i), l)
    else:
        adjacencies = []
        downsamp_trans = []
        upsamp_trans = []
        laplacians = []
        for a in sorted(glob('{}*.npz'.format(adj_path))):
            adjacencies.append(sp.load_npz(a))
        for d in sorted(glob('{}*.npz'.format(ds_path))):
            downsamp_trans.append(sp.load_npz(d))
        for u in sorted(glob('{}*.npz'.format(us_path))):
            upsamp_trans.append(sp.load_npz(u))
        for l in sorted(glob('{}*.npz'.format(lap_path))):
            laplacians.append(sp.load_npz(l))

    pool_size = [x.shape[0] for x in adjacencies]
    return laplacians, downsamp_trans, upsamp_trans, pool_size
Ejemplo n.º 9
0
 def build_graph(cls, args):
     number_edges = args.number_edges
     metric = args.metric
     normalized_laplacian = args.normalized_laplacian
     coarsening_levels = args.coarsening_levels
     data_dir = 'data/20news'
     embed_path = os.path.join(data_dir, 'embeddings.npy')
     graph_data = np.load(embed_path).astype(np.float32)
     dist, idx = graph.distance_sklearn_metrics(graph_data,
                                                k=number_edges,
                                                metric=metric)
     adj_matrix = graph.adjacency(dist, idx)
     print("{} > {} edges".format(adj_matrix.nnz // 2,
                                  number_edges * graph_data.shape[0] // 2))
     adj_matrix = graph.replace_random_edges(adj_matrix, 0)
     graphs, perm = coarsening.coarsen(adj_matrix,
                                       levels=coarsening_levels,
                                       self_connections=False)
     laplacians = [
         graph.laplacian(g, normalized=normalized_laplacian) for g in graphs
     ]
     cls.perm = perm
     cls.graphs = graphs
     cls.laplacians = laplacians
Ejemplo n.º 10
0

t_start = time.process_time()
tile_number = int((np.log2(
    (FLAGS.vertex_number - 2) / 10)) / 2)  #2**(2*tile_num)*10+2=vertex_number#
graphs_adjacency = []
z_xyz = []
vertex_theta = []
for m in range(tile_number, tile_number - FLAGS.coarsening_levels - 1, -1):
    z, z_theta, A = grid_graph(
        m, 1,
        corners=False)  #m is the tile_number,r is the radius of the sphere
    z_xyz.append(z)
    vertex_theta.append(z_theta)
    graphs_adjacency.append(A)
L = [graph.laplacian(A, normalized=True) for A in graphs_adjacency]
print(len(L))

index = []
for m in range(FLAGS.coarsening_levels):
    temp = []
    for j in z_xyz[m + 1]:
        temp.append(z_xyz[m].index(j))
    index.append(temp)
print('the shape of index', len(index), len(index[0]), len(index[1]))

project_data_path = 'data/mnist_2562_TT'  # for mnist
#project_data_path = 'data/cifar10_2562_FFT' # for cifar10
#project_data_path = '/mnt/data/modelnet40_40962_TTT' # for modelnet40

if not os.path.exists(project_data_path):
Ejemplo n.º 11
0
        site_train[i] = 0

print("Training samples shape")
print(X_train.shape)


# Get the graph structure
dist, idx = graph.distance_scipy_spatial(coords, k=10, metric='euclidean')
A = graph.adjacency(dist, idx).astype(np.float32)

graphs = []
for i in range(3):
    graphs.append(A)

# Calculate Laplacians
L = [graph.laplacian(A, normalized=True) for A in graphs]

# Number of nodes in graph and features
print("Number of controls in the dataset: ")
print(y.sum())

# Prepare training testing and validation sets
X_test, y_test, site_test = prepare_pairs(X, y, site, test_idx)

n, m, f, _ = X_train.shape

# Graph Conv-net
features = 64
K = 3
params = dict()
params['num_epochs']     = 80
Ejemplo n.º 12
0
def main():
    createFolder('Result')
    config_file = sys.argv[1]
    with open(config_file, 'r') as f:
        config = yaml.load(f)

    PPI_data = config["PPI_data"]
    Response_data = config["Response_data"]
    Gene_data = config["Gene_data"]
    n_fold = config["n_fold"]
    test_size = config["test_size"]
    num_epochs = config["num_epochs"]
    batch_size = config["batch_size"]
    brelu = config["brelu"]
    pool = config["pool"]
    regularization = config["regularization"]
    dropout = config["dropout"]
    learning_rate = config["learning_rate"]
    decay_rate = config["decay_rate"]
    momentum = config["momentum"]
    Name = config["Name"]
    F = config["F"]
    K = config["K"]
    p = config["p"]
    M = config["M"]

    data_PPI = pd.read_csv(PPI_data)
    data_PPI.drop(['Unnamed: 0'], axis='columns', inplace=True)
    data_IC50 = pd.read_csv(Response_data)
    data_IC50.drop(['Unnamed: 0'], axis='columns', inplace=True)
    data_Gene = pd.read_csv(Gene_data)
    data_Gene.drop(['Unnamed: 0'], axis='columns', inplace=True)
    data_Gene = np.array(data_Gene)

    df = np.array(data_PPI)
    A = coo_matrix(df, dtype=np.float32)
    print(A.nnz)
    graphs, perm = coarsening.coarsen(A, levels=6, self_connections=False)
    L = [graph.laplacian(A, normalized=True) for A in graphs]
    graph.plot_spectrum(L)

    n_fold = n_fold
    PCC = []
    SPC = []
    RMSE = []

    X_train, X_test, Y_train, Y_test = train_test_split(data_Gene,
                                                        data_IC50,
                                                        test_size=test_size,
                                                        shuffle=True,
                                                        random_state=20)

    for cv in range(n_fold):
        Y_pred = np.zeros([Y_test.shape[0], Y_test.shape[1]])
        Y_test = np.zeros([Y_test.shape[0], Y_test.shape[1]])
        j = 0
        for i in range(Y.test.shape[1]):
            data1 = data_IC50.iloc[:, i]
            data1 = np.array(data1)
            data_minmax = data1[~np.isnan(data1)]
            min = data_minmax.min()
            max = data_minmax.max()
            data1 = (data1 - min) / (max - min)

            train_data_split, test_data_split, train_labels_split, test_labels_split = train_test_split(
                data_Gene,
                data1,
                test_size=test_size,
                shuffle=True,
                random_state=20)
            train_data = np.array(
                train_data_split[~np.isnan(train_labels_split)]).astype(
                    np.float32)

            list_train, list_val = Validation(n_fold, train_data,
                                              train_labels_split)

            train_data_V = train_data[list_train[cv]]
            val_data = train_data[list_val[cv]]
            test_data = np.array(test_data_split[:]).astype(np.float32)
            train_labels = np.array(
                train_labels_split[~np.isnan(train_labels_split)]).astype(
                    np.float32)
            train_labels_V = train_labels[list_train[cv]]
            val_labels = train_labels[list_val[cv]]
            test_labels = np.array(test_labels_split[:]).astype(np.float32)
            train_data_V = coarsening.perm_data(train_data_V, perm)
            val_data = coarsening.perm_data(val_data, perm)
            test_data = coarsening.perm_data(test_data, perm)

            common = {}
            common['num_epochs'] = num_epochs
            common['batch_size'] = batch_size
            common['decay_steps'] = train_data.shape[0] / common['batch_size']
            common['eval_frequency'] = 10 * common['num_epochs']
            common['brelu'] = brelu
            common['pool'] = pool

            common['regularization'] = regularization
            common['dropout'] = dropout
            common['learning_rate'] = learning_rate
            common['decay_rate'] = decay_rate
            common['momentum'] = momentum
            common['F'] = F
            common['K'] = K
            common['p'] = p
            common['M'] = M

            if True:
                name = Name
                params = common.copy()

            model = models.cgcnn(L, **params)
            loss, t_step = model.fit(train_data_V, train_labels_V, val_data,
                                     val_labels)

            Y_pred[:, j] = model.predict(test_data)
            Y_test[:, j] = test_labels
            j = j + 1

        np.savez(('Result/GraphCNN_CV_{}'.format(cv)),
                 Y_true=Y_test,
                 Y_pred=Y_pred)
Ejemplo n.º 13
0
def cross_validate_convNN(X,
                          y,
                          adjacency,
                          name_param,
                          value_param,
                          k,
                          num_levels=5):

    split_index = split_test_train_for_cv(X.shape[0], k_fold=k)
    graphs, perm = coarsening.coarsen(sp.csr_matrix(
        adjacency.astype(np.float32)),
                                      levels=num_levels,
                                      self_connections=False)

    accuracy = []
    loss = []
    for param_val in value_param:
        accuracy_param = []
        loss_param = []
        for k_ in range(k):
            test_samples = split_index[k_]
            train_samples = split_index[~(
                np.arange(split_index.shape[0]) == k_)].flatten()

            X_train = X[train_samples]
            X_test = X[test_samples]
            y_train = y[train_samples]
            y_test = y[test_samples]

            X_train = coarsening.perm_data(X_train, perm)
            X_test = coarsening.perm_data(X_test, perm)
            n_train = X_train.shape[0]

            L = [graph.laplacian(A, normalized=True) for A in graphs]

            # Conv NN parameters
            params = dict()
            params['dir_name'] = 'demo'
            params['num_epochs'] = 30
            params['batch_size'] = 30
            params['eval_frequency'] = 30

            # Building blocks.
            params['filter'] = 'chebyshev5'
            params['brelu'] = 'b1relu'
            params['pool'] = 'apool1'

            # Number of classes.
            C = y.max() + 1
            assert C == np.unique(y).size

            # Architecture.
            params['F'] = [4, 8]  # Number of graph convolutional filters.
            params['K'] = [3, 3]  # Polynomial orders.
            params['p'] = [2, 8]  # Pooling sizes.
            params['M'] = [
                256, C
            ]  # Output dimensionality of fully connected layers.

            # Optimization.
            params['regularization'] = 4e-5
            params['dropout'] = 1
            params['learning_rate'] = 3e-3
            params['decay_rate'] = 0.9
            params['momentum'] = 0.8
            params['decay_steps'] = n_train / params['batch_size']
            params[name_param] = param_val

            model = models.cgcnn(L, **params)
            test_acc, train_loss, t_step = model.fit(X_train, y_train, X_test,
                                                     y_test)
            accuracy_param.append([max(test_acc), np.mean(test_acc)])
            loss_param.append([max(train_loss), np.mean(train_loss)])
        print(np.array(accuracy_param))
        pm = np.mean(np.array(accuracy_param), axis=0)
        pl = np.mean(np.array(loss_param), axis=0)
        print(
            "IIIII Accuracy: %0.2f (max) %0.2f (mean) Loss: %0.2f (max) %0.2f (mean)"
            % (pm[0], pm[1], pl[0], pl[1]))
        accuracy.append(pm)
        loss.append(pl)
    return accuracy, loss
Ejemplo n.º 14
0
# Generates adjecency matrices A, downsampling matrices D, and upsamling matrices U by sampling
# the mesh 4 times. Each time the mesh is sampled by a factor of 4

M,A,D,U = mesh_sampling.generate_transform_matrices(facedata.reference_mesh, ds_factors)

A = map(lambda x:x.astype('float32'), A)
D = map(lambda x:x.astype('float32'), D)
U = map(lambda x:x.astype('float32'), U)
p = map(lambda x:x.shape[0], A)

X_train = facedata.vertices_train.astype('float32')
X_val = facedata.vertices_val.astype('float32')
X_test = facedata.vertices_test.astype('float32')

print("Computing Graph Laplacians ..")
L = [graph.laplacian(a, normalized=True) for a in A]

n_train = X_train.shape[0]
params = dict()
params['dir_name']       = args.name
params['num_epochs']     = args.num_epochs
params['batch_size']     = args.batch_size
params['eval_frequency'] = args.eval_frequency
# Building blocks.
params['filter']         = args.filter
params['brelu']          = 'b1relu'
params['pool']           = 'poolwT'
params['unpool']		 = 'poolwT'


# Architecture.
Ejemplo n.º 15
0
def ABIDE_save(num_subjects, filename):
    rs = 33
    print("Random state is %d" % rs)
    prng = np.random.RandomState(rs)

    # Split into training, validation and testing sets
    training_num = num_subjects
    lines = int(1.2 * num_subjects)
    # I am guessing this is to create a validation set within training data
    # Used in the next moving step

    # Get subject features
    atlas = 'ho'
    kind = 'correlation'

    subject_IDs = abide_utils.get_ids(lines)
    # Get all subject networks
    networks = abide_utils.load_all_networks(subject_IDs,
                                             kind,
                                             atlas_name=atlas)
    X = np.array(networks)

    # with open('GCN_train.pkl', 'wb') as f:  # Python 3: open(..., 'wb')
    #     pickle.dump(X, f, 2)
    # f.close()

    # Number of nodes
    nodes = X.shape[1]

    # Get ROI coordinates
    coords = abide_utils.get_atlas_coords(atlas_name=atlas)

    # Get subject labels
    label_dict = abide_utils.get_subject_label(subject_IDs,
                                               label_name='DX_GROUP')
    y = np.array([int(label_dict[x]) - 1 for x in sorted(label_dict)])

    # Get site ID
    site = abide_utils.get_subject_label(subject_IDs, label_name='SITE_ID')
    unq = np.unique(list(site.values())).tolist()
    site = np.array([unq.index(site[x]) for x in sorted(site)])

    # Choose site IDs to include in the analysis
    site_mask = range(20)
    X = X[np.in1d(site, site_mask)]
    y = y[np.in1d(site, site_mask)]
    site = site[np.in1d(site, site_mask)]

    tr_idx, test_idx = split_data(site, 0.6)
    # training_num = int(0.6 * X.shape[0])

    prng.shuffle(test_idx)
    subs_to_add = training_num - len(
        tr_idx)  # subjects that need to be moved from testing to training set
    tr_idx.extend(test_idx[:subs_to_add])
    test_idx = test_idx[subs_to_add:]
    print("The test indices are the following: ")
    print(test_idx)

    all_combs = []
    tr_mat = np.array(tr_idx).reshape([int(len(tr_idx) / 6), 6])
    for i in range(3):
        x1 = tr_mat[:, i * 2].flatten()
        x2 = tr_mat[:, i * 2 + 1].flatten()
        combs = np.transpose([np.tile(x1, len(x2)), np.repeat(x2, len(x1))])
        all_combs.append(combs)

    all_combs = np.vstack(all_combs)

    # print(all_combs.shape)
    n, m, f = X.shape
    X_train = np.ones((all_combs.shape[0], m, f, 2), dtype=np.float32)
    y_train = np.ones(all_combs.shape[0], dtype=np.int32)
    site_train = np.ones(all_combs.shape[0], dtype=np.int32)

    for i in range(all_combs.shape[0]):
        X_train[i, :, :, 0] = X[all_combs[i, 0], :, :]
        X_train[i, :, :, 1] = X[all_combs[i, 1], :, :]
        if y[all_combs[i, 0]] != y[all_combs[i, 1]]:
            y_train[i] = 0  # -1
        if site[all_combs[i, 0]] != site[all_combs[i, 1]]:
            site_train[i] = 0

    print("Training samples shape")
    print(X_train.shape)

    # Get the graph structure
    dist, idx = graph.distance_scipy_spatial(coords, k=10, metric='euclidean')
    A = graph.adjacency(dist, idx).astype(np.float32)

    graphs = []
    for i in range(3):
        graphs.append(A)

    # Calculate Laplacians
    L = [graph.laplacian(A, normalized=True) for A in graphs]

    # Number of nodes in graph and features
    print("Number of controls in the dataset: ")
    print(y.sum())

    # Prepare training testing and validation sets
    X_test, y_test, site_test = prepare_pairs(X, y, site, test_idx)

    # Saving training data for comparison with ann siamese

    np.savez(filename,
             '.npz',
             name1=X_train,
             name2=y_train,
             name3=X_test,
             name4=y_test,
             name5=all_combs,
             name6=site_train,
             name7=site_test,
             name8=tr_idx)

    return None
Ejemplo n.º 16
0
    if corners:
        import scipy.sparse
        A = A.toarray()
        A[A < A.max()/1.5] = 0
        A = scipy.sparse.csr_matrix(A)
        print('{} edges'.format(A.nnz))

    print("{} > {} edges".format(A.nnz//2, FLAGS.number_edges*m**2//2))
    return A

t_start = time.process_time()
A = grid_graph(28, corners=False)
A = graph.replace_random_edges(A, 0)
#graphs, perm = coarsening.coarsen(A, levels=FLAGS.coarsening_levels, self_connections=False)
#L = [graph.laplacian(A, normalized=True) for A in graphs]
L = [graph.laplacian(A, normalized=True)]
print('Execution time: {:.2f}s'.format(time.process_time() - t_start))
graph.plot_spectrum(L)
del A

from tensorflow.examples.tutorials.mnist import input_data
print(FLAGS.dir_data)
mnist = input_data.read_data_sets(FLAGS.dir_data, one_hot=False)

train_data = mnist.train.images.astype(np.float32)
val_data = mnist.validation.images.astype(np.float32)
test_data = mnist.test.images.astype(np.float32)
train_labels = mnist.train.labels
val_labels = mnist.validation.labels
test_labels = mnist.test.labels
Ejemplo n.º 17
0
    y_val   = y[n_train:, ...]

    A = np.load('/Neutron9/joyneel.misra/npys/meanFC_d'+str(d)+'.npy');
    A = A - np.min(A)
    A = scipy.sparse.csr_matrix(A)
    d = X.shape[1]

    assert A.shape == (d, d)
    print('d = |V| = {}, k|V| < |E| = {}'.format(d, A.nnz))

    graphs, perm = coarsening.coarsen(A, levels=3, self_connections=False)

    X_train = coarsening.perm_data(X_train, perm)
    X_val = coarsening.perm_data(X_val, perm)

    L = [graph.laplacian(A, normalized=True) for A in graphs]
    L = [elem.astype(np.float32) for elem in L]

    params = dict()
    params['dir_name']       = 'demo'
    params['num_epochs']     = 10
    params['batch_size']     = 40
    params['eval_frequency'] = 100

    # Building blocks.
    params['filter']         = 'chebyshev5'
    params['brelu']          = 'b1relu'
    params['pool']           = 'apool1'

    # Number of attributes in target.
    C = np.max(y)+1
Ejemplo n.º 18
0
def constructingGraph(inFIDDic,
                      data_type,
                      mean_geometry=0,
                      std_geometry=1,
                      is_distance=True):
    #inFiDDic {key,[label,pointlist]}
    if len(inFIDDic) < 1: return None

    vertices_Geometry, adjacencies, labels, inFIDs, process_count = [], [], [], [], 0

    for k in inFIDDic:
        [label, Node_coords, Node_features] = inFIDDic[k]
        assert len(Node_coords) == len(Node_features)
        subObject_size = len(Node_coords)

        # # 1 get the label of this sample.
        label = 1 if label == 3 else 0

        # # 3 get the adjacency graph of the building group (one sample).
        # #   MST, Delaunay, K-NN
        points = np.array(Node_coords)
        adjacency = np.zeros((subObject_size, subObject_size))

        tri = Delaunay(points[:, 0:2])

        for i in range(0, tri.nsimplex):
            if i > tri.neighbors[i, 2]:
                adjacency[tri.vertices[i, 0], tri.vertices[i, 1]] = 1
                adjacency[tri.vertices[i, 1], tri.vertices[i, 0]] = 1
            if i > tri.neighbors[i, 0]:
                adjacency[tri.vertices[i, 1], tri.vertices[i, 2]] = 1
                adjacency[tri.vertices[i, 2], tri.vertices[i, 1]] = 1
            if i > tri.neighbors[i, 1]:
                adjacency[tri.vertices[i, 2], tri.vertices[i, 0]] = 1
                adjacency[tri.vertices[i, 0], tri.vertices[i, 2]] = 1

        adjacency = scipy.sparse.coo_matrix(adjacency,
                                            shape=(subObject_size,
                                                   subObject_size))
        # In order to make the calculation simpler, only the distance between the center points of the buildings is provided here.
        # According to the author's experience, the closest distance of two building outlines coule be a better opition for this task.
        distances = sklearn.metrics.pairwise.pairwise_distances(
            points[:, 0:2], metric="euclidean", n_jobs=1)

        if False:
            # K-nearest neighbor graph.
            # Distance matrix. is it necessary to be normalized?
            idx = np.argsort(distances)[:, 1:1 + 1]
            distances.sort()
            distances = graph.adjacency(distances[:, 1:1 + 1], idx)
            adjacency = scipy.sparse.coo_matrix(
                np.ones((subObject_size, subObject_size)),
                shape=(subObject_size, subObject_size)).multiply(distances)
            # print(distances.toarray())# adjacency = adjacency.multiply(distances)
        else:
            adjacency = adjacency.multiply(distances)
            if False:
                # MST graph.
                adjacency = scipy.sparse.csgraph.minimum_spanning_tree(
                    adjacency)
                adjacency = scipy.sparse.csr_matrix(adjacency).toarray()
                adjacency += adjacency.T - np.diag(adjacency.diagonal())
            else:
                # Delaunay graph.
                adjacency = scipy.sparse.csr_matrix(adjacency).toarray()

        #if is_distance:
        #    # Distance matrix. is it necessary to be normalized?
        #    distances = sklearn.metrics.pairwise.pairwise_distances(points[:,0:2], metric="euclidean", n_jobs=1)
        #    adjacency = adjacency.multiply(distances)

        adjacency = scipy.sparse.csr_matrix(adjacency)
        assert subObject_size == points.shape[0]
        assert type(adjacency) is scipy.sparse.csr.csr_matrix

        # # 4 collecting the sample: vertice_Geometry,vertice_Fourier,adjacency,label.
        vertices_Geometry.append(Node_features)
        adjacencies.append(adjacency)
        labels.append(label)
        inFIDs.append(k)

    # preprocessing inputs.
    pro_method = True  # to control the m
    if pro_method:
        # standardizing
        if data_type == 1:
            # Calculate the mean and std of train dataset, they also will be used to validation and test dataset.
            concatenate_Geometry = np.concatenate(vertices_Geometry, axis=0)
            mean_geometry = concatenate_Geometry.mean(axis=0)
            std_geometry = concatenate_Geometry.std(axis=0)

            if data_type == 1:
                file = r'C:\Users\wh\Desktop\gcnn_classification\lib\data\_used_new22.txt'
                file = "./lib/data/_config_22.txt"
                conc = np.vstack((mean_geometry, std_geometry))
                np.savetxt(file, conc, fmt='%.18f')
        if data_type == -1:  # for the extra experiment.
            # Import the mean and std of train dataset.
            file = r'C:\Users\wh\Desktop\gcnn_classification\lib\data\_used_new22.txt'
            file = "./lib/data/_config_22.txt"
            conc = np.loadtxt(file)
            mean_geometry, std_geometry = conc[0, :], conc[1, :]
            mean_fourier, std_fourier = conc[0, :], conc[
                1, :]  # This two parameters are just for fun, do not matter.
            print(
                "\n========import the mean and std of train dataset from text file========\n"
            )
            #print(mean_geometry)
            #print(std_geometry)

        if True:
            # # The efficiency can be improved by means of vectorization.s
            for i in range(0, len(vertices_Geometry)):
                vertices_shape = np.array((vertices_Geometry[i])).shape
                vertices_Geometry[i] -= np.tile(
                    mean_geometry, vertices_shape[0]).reshape(vertices_shape)
                vertices_Geometry[i] /= np.tile(
                    std_geometry, vertices_shape[0]).reshape(vertices_shape)

                # vertices_shape=np.array((vertices_Fourier[i])).shape
                # vertices_Fourier[i] -= np.tile(mean_fourier,vertices_shape[0]).reshape(vertices_shape)
                # vertices_Fourier[i] /= np.tile(std_fourier,vertices_shape[0]).reshape(vertices_shape)
    else:
        # normalizing, it is not working very well.
        if data_type == 1:
            # Calculate the mean and std of train dataset, they also will be used to validation and test dataset.
            concatenate_Geometry = np.concatenate(vertices_Geometry, axis=0)
            mean_geometry = concatenate_Geometry.min(axis=0)
            std_geometry = concatenate_Geometry.max(axis=0)

            file = r'C:\Users\wh\Desktop\gcnn_classification\lib\data\_used2.txt'
            file = "./data/_config_ex.txt"
            conc = np.vstack((mean_geometry, std_geometry))
            np.savetxt(file, conc, fmt='%.18f')

        if not pro_method:
            # # The efficiency can be improved by means of vectorization.s
            for i in range(0, len(vertices_Geometry)):
                vertices_shape = np.array((vertices_Geometry[i])).shape
                vertices_Geometry[i] = (vertices_Geometry[i] - np.tile(
                    mean_geometry, vertices_shape[0]).reshape(vertices_shape)
                                        ) / (std_geometry - mean_geometry)

    # padding.
    # the max number of vertices in a group (sample).
    maxnum_vertices = 128  #max([len(vertices_Geometry[i]) for i in range(0,len(vertices_Geometry))])
    graph_vertices_geo, graph_adjacencies = [], []

    assert len(vertices_Geometry) == len(adjacencies) == len(labels)

    #print(len(vertices_Geometry))
    #print(len(vertices_Geometry[i]))
    #print(np.pad(vertices_Geometry[i], ((0, maxnum_vertices-len(vertices_Geometry[i])),(0,0)), 'constant', constant_values=(0)).shape)
    #exit()

    for i in range(0, len(vertices_Geometry)):
        graph_vertices_geo.append(
            np.pad(vertices_Geometry[i],
                   ((0, maxnum_vertices - len(vertices_Geometry[i])), (0, 0)),
                   'constant',
                   constant_values=(0)))
        graph_adjacencies.append(
            np.pad(adjacencies[i].toarray(),
                   ((0, maxnum_vertices - adjacencies[i].shape[0]),
                    (0, maxnum_vertices - adjacencies[i].shape[0])),
                   'constant',
                   constant_values=(0)))
    # collecting.
    graph_vertices_geo = np.stack(graph_vertices_geo, axis=0).astype(
        np.float32)  #NSample x NVertices x NFeature
    graph_adjacencies = np.stack(graph_adjacencies, axis=0).astype(
        np.float32)  #NSample x NVertices x NVertices
    graph_labels = np.array(labels).astype(np.int64)  #NSample x 1
    graph_inFIDs = np.array(inFIDs).astype(np.int64)  #NSample x 1
    graph_size = graph_labels.shape[0]  #NSample
    graph_Laplacian = np.stack([
        graph.laplacian(
            scipy.sparse.csr_matrix(A), normalized=True, rescaled=True)
        for A in graph_adjacencies
    ],
                               axis=0)

    return [
        graph_vertices_geo, graph_Laplacian, graph_labels, graph_inFIDs,
        graph_size, mean_geometry, std_geometry
    ]


# load_data('gz1124i.json', [0.6, 0.2, 0.2])
Ejemplo n.º 19
0
M, A, D, U = mesh_sampling.generate_transform_matrices(facedata.reference_mesh,
                                                       ds_factors)

#A = list(map(lambda x:x.astype('float32'), A))
D = list(map(lambda x: x.astype('float32'), D))
U = list(map(lambda x: x.astype('float32'), U))
#p = list(map(lambda x:x.shape[0], A))
p = list(map(lambda x: x.v.shape[0], A))

X_train = facedata.vertices_train.astype('float32')
X_val = facedata.vertices_val.astype('float32')
X_test = facedata.vertices_test.astype('float32')

print("Computing Graph Laplacians ..")
L = [graph.laplacian(a, mode='cotan', normalized=True) for a in A]
L = list(map(lambda x: x.astype('float32'), L))

n_train = X_train.shape[0]
params = dict()
params['dir_name'] = args.name
params['num_epochs'] = args.num_epochs
params['batch_size'] = args.batch_size
params['eval_frequency'] = args.eval_frequency
# Building blocks.
params['filter'] = args.filter
params['brelu'] = 'b1relu'
params['pool'] = 'poolwT'
params['unpool'] = 'poolwT'

# Architecture.
Ejemplo n.º 20
0
def gcn_run(fname, train_num, batch_size):
    rs = 222

    print("Random state is %d" % rs)
    prng = np.random.RandomState(rs)

    np.random.seed(seed=222)
    data = np.load(fname + '.npz')

    X_train = np.array(data['name1'], dtype=np.float32)
    y_train = np.array(data['name2'], dtype=np.float32)

    X_test = np.array(data['name3'], dtype=np.float32)
    y_test = np.array(data['name4'], dtype=np.float32)

    all_combs = np.array(data['name5'], dtype=np.float32)
    site_train = np.array(data['name6'], dtype=np.float32)
    site_test = np.array(data['name7'], dtype=np.float32)
    tr_idx = np.array(data['name8'], dtype=np.float32)

    dist, idx = graph.distance_scipy_spatial(coords, k=10, metric='euclidean')
    A = graph.adjacency(dist, idx).astype(np.float32)

    graphs = []
    for i in range(3):
        graphs.append(A)

    # Calculate Laplacians
    L = [graph.laplacian(A, normalized=True) for A in graphs]

    n, m, f, _ = X_train.shape

    # Graph Conv-net
    features = 64
    K = 3
    params = dict()
    params['num_epochs'] = train_num
    params['batch_size'] = batch_size
    # params['eval_frequency'] = X_train.shape[0] / (params['batch_size'] * 2)
    params['eval_frequency'] = 1
    # Building blocks.
    params['filter'] = 'chebyshev5'
    params['brelu'] = 'b2relu'
    params['pool'] = 'apool1'

    # Architecture.
    params['F'] = [features,
                   features]  # Number of graph convolutional filters.
    params['K'] = [K, K]  # Polynomial orders.
    params['p'] = [1, 1]  # Pooling sizes.
    params['M'] = [1]  # Output dimensionality of fully cofeannected layers.
    params['input_features'] = f
    params['lamda'] = 0.35
    params['mu'] = 0.6

    # Optimization.
    params['regularization'] = 5e-3
    params['dropout'] = 0.8
    params['learning_rate'] = 1e-2
    params['decay_rate'] = 0.95
    params['momentum'] = 0
    params['decay_steps'] = X_train.shape[0] / params['batch_size']

    params['dir_name'] = 'siamese_' + time.strftime("%Y_%m_%d_%H_%M") + '_feat' + str(params['F'][0]) + '_' + \
                         str(params['F'][1]) + '_K' + str(K) + '_state'

    print(params)

    # Run model
    model = models_siamese.siamese_cgcnn_cor(L, **params)

    print("Constructor finished")
    accuracy, loss, scores_summary, tr_error, test_error = model.fit(
        X_train, y_train, site_train, X_test, y_test, site_test)
    #print('Time per step: {:.2f} ms'.format(t_step*1000))

    # Save training
    tr_res = model.evaluate(X_train, y_train, site_train)

    # Evaluate test data
    print("Test accuracy is:")
    res = model.evaluate(X_test, y_test, site_test)
    print(res[0])

    return tr_error, test_error