def Main(): #matplotlib.use('Agg') if len(sys.argv) > 1 and os.path.isfile(sys.argv[1]): extension = os.path.splitext(sys.argv[1])[1] fig = plt.figure() if extension == '.ply': #A = np.expand_dims(A,axis=0) V, A = ply2graph(sys.argv[1],K) V = np.expand_dims(np.array(V),axis=0) print(V.shape) #A = np.expand_dims(np.transpose(np.array(A),axes=(1,0,2)),axis=0) A = np.expand_dims(np.array(A),axis=0) print(A.shape) #eng = matlab.engine.start_matlab() #eng.addpath(os.path.abspath(os.path.join(os.path.dirname(__file__),'../util/pooling/graclus1.2/matlab/'))) #pooler = GraclusPoolingPyramid(len(POOL_RATIOS), GeometricAdjacencyCompander, POOL_RATIOS)#,eng) #pooler = SpectralClusteringPoolingPyramid(len(POOL_RATIOS), MemoryAdjacencyCompander, POOL_RATIOS) pooler = LloydPoolingPyramid(len(POOL_RATIOS), SphericalAdjacencyCompander, POOL_RATIOS) #pooler = SBFSStrideAdjacencyPyramid(len(POOL_RATIOS), GeometricAdjacencyCompander, STRIDE, POOL_RATIOS) #draw3DGraph(V.sum(axis=0), A.sum(axis=(0, 2)), V.shape[1], '141', fig) #plt.show() Plist = pooler.makeP(A.sum(axis=0),V.sum(axis=0),THETA,PHI) #print(Plist[0].shape) Avar = tf.placeholder(tf.float32, shape=(None, None, None, None)) Vvar = tf.placeholder(tf.float32, shape=(None, None, None)) Pvar = tf.placeholder(tf.float32, shape=(None, None, None)) Vout,Aout = make_graph_pooling_layer(Vvar,Avar,Pvar) #vshape = V.shape #V = tf.constant(V,dtype=tf.float32,shape=vshape) #A = tf.constant(A,dtype=tf.float32,shape=A.shape) #Vout,Aout,W = make_graph_embed_pooling(V, A, no_vertices=111) #Vrev,Arev = make_graph_unpooling_layer(Vvar,Avar,Pvar) init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) for i in range(2,2+len(POOL_RATIOS)): subplotId = '16' + str(i) [V,A] = sess.run([Vout,Aout],feed_dict={Vvar: V, Avar: A, Pvar: np.expand_dims(Plist[i-2],axis=0)}) print(V.shape) if extension == '.ply': draw3DGraph(V.sum(axis=0),A.sum(axis=(0,2)),Plist[i - 2].shape[1],subplotId,fig) #fig2 = plt.figure(2) #subplotId = '421' #draw3DGraph(V.sum(axis=0), A.sum(axis=(0, 2)), A.shape[1], subplotId, fig2) #for i in range(2,2 + len(POOL_RATIOS)): # subplotId = '42' + str(i) # print(V.shape) # [V, A] = sess.run([Vrev, Arev], feed_dict={Vvar: V, Avar: A, Pvar: np.expand_dims(Plist[len(POOL_RATIOS) - (i - 2) - 1], axis=0)}) # draw3DGraph(V.sum(axis=0), A.sum(axis=(0, 2)), Plist[len(POOL_RATIOS) - (i - 2) - 1].shape[1], subplotId, fig2) plt.show() else: print('Could not find path LOL!')
def processFile(filename): V, A = ply2graph(filename, K) V = np.expand_dims(np.array(V), axis=0) A = np.expand_dims(np.array(A), axis=0) # BxNxLxN? print(A.shape) pooler = LloydPoolingPyramid(len(POOL_RATIOS), SphericalAdjacencyCompander, POOL_RATIOS) Plist = pooler.makeP(A.sum(axis=0), V.sum(axis=0), THETA, PHI) for i in range(len(Plist)): Plist[i] = np.expand_dims(np.expand_dims(Plist[i], axis=0), axis=0) return V, A, Plist
def processFile(filename): V, A = ply2graph(filename, K) V = np.expand_dims(np.array(V), axis=0) A = np.expand_dims(np.transpose(np.array(A), axes=(1, 0, 2)), axis=0) # BxNxLxN? pooler = LloydPoolingPyramid(len(POOL_RATIOS), SphericalAdjacencyCompander, POOL_RATIOS) P = np.expand_dims(np.expand_dims(np.transpose( pooler.makeP( np.transpose(A, axes=(0, 2, 1, 3)).sum(axis=0), V.sum(axis=0), THETA, PHI)[0]), axis=0), axis=0) return V, A, P
def processFile(filename): V, A = ply2graph(filename, K) V = np.expand_dims(np.array(V), axis=0) A = np.expand_dims(np.transpose(np.array(A), axes=(1, 0, 2)), axis=0) # BxNxLxN? pooler = LloydPoolingPyramid(len(POOL_RATIOS), SphericalAdjacencyCompander, POOL_RATIOS) return V, A
def Main(): np.set_printoptions(linewidth=180) V1, A1, P1 = processFile(FILE) V2, A2, P2 = processFile(FILE2) A = np.concatenate((A1, A2), axis=0) P = np.concatenate((P1, P2), axis=0) V = np.concatenate((V1, V2), axis=0) print(A.shape) indices = np.transpose(np.nonzero(A)).astype(np.int64) values = np.ravel(A[np.nonzero(A)]).astype(np.float32) # print(values.shape) dense_shape = np.asarray(A.shape, dtype=np.int64) pooler = LloydPoolingPyramid(len(POOL_RATIOS), GeometricAdjacencyCompander, POOL_RATIOS) Pindices = np.transpose(np.nonzero(P)).astype(np.int64) Pvals = P[np.nonzero(P)].astype(np.float32) Pshape = np.asarray(P.shape, dtype=np.int64) #A = np.transpose(np.array([[[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]],[[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]]]),axes=(0,2,1,3)) #print(A.shape) #indices = np.transpose(np.nonzero(A)).astype(np.int64) #values = A[np.nonzero(A)].astype(np.float32) #V = np.transpose(np.array([[[9,10,11,12],[5,6,7,8],[1,2,3,4]],[[1,2,3,4],[5,6,7,8],[9,10,11,12]]]),axes=(0,2,1)) #print(V.shape) #P = np.transpose(np.array([[[[0,1,0,0.1],[1,0,1,0]]],[[[1,0,1,0],[0,1,0,0.1]]]]),axes=(0,1,3,2)) #dense_shape = np.asarray(A.shape, dtype=np.int64) #Pindices = np.transpose(np.nonzero(P)).astype(np.int64) #Pvals = P[np.nonzero(P)].astype(np.float32) #Pshape = np.asarray(P.shape, dtype=np.int64) print("PSHAPE: {0}".format(Pshape)) Avarsparse = tf.sparse_placeholder(tf.float32, shape=dense_shape) Avar = tf.placeholder(tf.float32, shape=(None, None, None, None)) Vvar = tf.placeholder(tf.float32, shape=V.shape) idxTensor = tf.placeholder(tf.int64, shape=(None, None)) valTensor = tf.placeholder(tf.float32, shape=(None)) PidxTensor = tf.placeholder(tf.int64, shape=(None, None)) PvalTensor = tf.placeholder(tf.float32, shape=(None)) Asparse = tf.SparseTensor(indices=idxTensor, values=valTensor, dense_shape=dense_shape) Adense = tf.cast( tf.sparse_to_dense(Asparse.indices, Asparse.dense_shape, Asparse.values), tf.float32) Psparse = tf.SparseTensor(indices=PidxTensor, values=PvalTensor, dense_shape=Pshape) Pdense = tf.squeeze(tf.cast( tf.sparse_to_dense(Psparse.indices, Psparse.dense_shape, Psparse.values), tf.float32), axis=1) # W = make_variable('weights', [V.shape[2]*A.shape[2], 16], initializer=tf.truncated_normal_initializer(stddev=math.sqrt(1.0/(V.shape[2]*(A.shape[2]+1)*GraphCNNGlobal.GRAPHCNN_INIT_FACTOR)))) # W = tf.ones([V.shape[2]*A.shape[2], 16],dtype=tf.float32) # W = tf.fill([V.shape[2]*A.shape[2], 16],2.0) W = tf.random_uniform([V.shape[2] * A.shape[2], NUM_FILTERS], dtype=tf.float32) VmaxpoolOutSparse, Aignore = make_sparse_max_graph_pooling_layer( Vvar, Asparse, Psparse) dV = tf.gradients(VmaxpoolOutSparse, Vvar) dP = tf.gradients(VmaxpoolOutSparse, PvalTensor) init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) outputs = [VmaxpoolOutSparse, dV, dP] [VmaxpoolOutSparseOut,dVOut,dPOut] = sess.run(outputs, \ feed_dict={idxTensor: indices, \ valTensor: values, \ PidxTensor: Pindices, \ PvalTensor: Pvals, Vvar: V}) dVMaxPoolTheory, dVMaxPoolNumeric = tf.test.compute_gradient(Vvar, \ np.array(V.shape), \ VmaxpoolOutSparse, \ np.array([V.shape[0], int(V.shape[1] * POOL_RATIOS[0]), V.shape[2]]), extra_feed_dict={idxTensor: indices, \ valTensor: values, \ PidxTensor: Pindices, \ PvalTensor: Pvals, Vvar: V}) dPMaxPoolTheory, dPMaxPoolNumeric = tf.test.compute_gradient(PvalTensor, \ np.array(Pvals.shape), \ VmaxpoolOutSparse, \ np.array([V.shape[0], int(V.shape[1] * POOL_RATIOS[0]), V.shape[2]]), extra_feed_dict={idxTensor: indices, \ valTensor: values, \ PidxTensor: Pindices, \ PvalTensor: Pvals, Vvar: V}) dVMaxPoolError = tf.test.compute_gradient_error(Vvar, \ np.array(V.shape), \ VmaxpoolOutSparse, \ np.array([V.shape[0], int(V.shape[1] * POOL_RATIOS[0]), V.shape[2]]),extra_feed_dict={idxTensor: indices, \ valTensor: values, \ PidxTensor: Pindices, \ PvalTensor: Pvals, Vvar: V}) dPMaxPoolError = tf.test.compute_gradient_error(PvalTensor, \ np.array(Pvals.shape), \ VmaxpoolOutSparse, \ np.array([V.shape[0], int(V.shape[1] * POOL_RATIOS[0]), V.shape[2]]), extra_feed_dict={idxTensor: indices, \ valTensor: values, \ PidxTensor: Pindices, \ PvalTensor: Pvals, Vvar: V}) print(V) print(np.transpose(P, axes=(0, 1, 3, 2))) print('VMaxPoolResults') print(VmaxpoolOutSparseOut) #print(dVOut[0].shape) print(dVOut[0]) dPOutArray = np.zeros(P.shape) dPOutArray[np.nonzero(P)] = dPOut[0] print(dPOutArray) print(dVMaxPoolTheory) print(dVMaxPoolNumeric) print(dPMaxPoolTheory) print(dPMaxPoolNumeric) mse = mean_squared_error(np.ravel(dVMaxPoolTheory), np.ravel(dVMaxPoolNumeric)) print('MSE dV: {0}'.format(mse)) print('MSE dV: {0}'.format(dVMaxPoolError)) print(np.linalg.norm(dVMaxPoolTheory)) print(np.linalg.norm(dVMaxPoolNumeric)) mse = mean_squared_error(np.ravel(dPMaxPoolTheory), np.ravel(dPMaxPoolNumeric)) print('MSE dP: {0}'.format(mse)) print('MSE dP: {0}'.format(dPMaxPoolError)) print(np.linalg.norm(dPMaxPoolTheory)) print(np.linalg.norm(dPMaxPoolNumeric)) print(np.linalg.norm(VmaxpoolOutSparseOut))