def compute_partition(args, embeddings, edg_source, edg_target, diff, xyz=0): edge_weight = np.ones_like(edg_source).astype('f4') if args.edge_weight_threshold > 0: edge_weight[diff > 1] = args.edge_weight_threshold if args.edge_weight_threshold < 0: edge_weight = torch.exp( diff * args.edge_weight_threshold).detach().cpu().numpy() / np.exp( args.edge_weight_threshold) ver_value = np.zeros((embeddings.shape[0], 0), dtype='f4') use_spatial = 0 ver_value = np.hstack((ver_value, embeddings.detach().cpu().numpy())) if args.spatial_emb > 0: ver_value = np.hstack( (ver_value, args.spatial_emb * xyz)) # * math.sqrt(args.reg_strength))) #ver_value = xyz * args.spatial_emb use_spatial = 1 #!!! pred_components, pred_in_component = libcp.cutpursuit(ver_value, \ edg_source.astype('uint32'), edg_target.astype('uint32'), edge_weight, \ args.reg_strength / (4 * args.k_nn_adj), cutoff=args.CP_cutoff, spatial = use_spatial, weight_decay = 0.7) #emb2 = libcp.cutpursuit2(ver_value, edg_source.astype('uint32'), edg_target.astype('uint32'), edge_weight, args.reg_strength, cutoff=0, spatial =0) #emb2 = emb2.reshape(ver_value.shape) #((ver_value-emb2)**2).sum(0) #cut = pred_in_component[edg_source]!=pred_in_component[edg_target] return pred_components, pred_in_component
import sys sys.path.append("../build/src") import libcp import numpy as np obs = np.array([[10.,20.],[0.,1.],[0.,0. ]], dtype = 'float32') source = np.array([0, 1], dtype = 'uint32') target = np.array([1, 2], dtype = 'uint32') edge_weight = np.array([[1., 1.]], dtype = 'float32') [comp, in_com] = libcp.cutpursuit(obs, source, target, edge_weight, 1) # sol = libcp.cutpursuit_reg(obs, source, target, edge_weight, 1)
if os.path.isfile(spg_file): print(" reading the existing superpoint graph file...") graph_sp, components, in_component = read_spg(spg_file) else: print(" computing the superpoint graph...") #--- build the spg h5 file -- start = timer() geof[:, 3] = 2. * geof[:, 3] #increase importance of verticality (heuristic) graph_nn["edge_weight"] = np.array( 1. / (args.lambda_edge_weight + graph_nn["distances"] / np.mean(graph_nn["distances"])), dtype='float32') print(" minimal partition...") components, in_component = libcp.cutpursuit( geof, graph_nn["source"], graph_nn["target"], graph_nn["edge_weight"], args.reg_strength) components = np.array(components, dtype='object') end = timer() times[1] = times[1] + end - start print(" computation of the SPG...") start = timer() graph_sp = compute_sp_graph(xyz, args.d_se_max, in_component, components, labels, args.n_labels) end = timer() times[2] = times[2] + end - start write_spg(spg_file, graph_sp, components, in_component) print("Timer : %5.1f / %5.1f / %5.1f " % (times[0], times[1], times[2]))
import libcp obs = np.array([[1000.], [0.], [0.]], dtype='float32') source = np.array([[0, 1, 1, 2]], dtype='uint32') target = np.array([[1, 2, 0, 1]], dtype='uint32') edge_weight = np.array([[1., 1., 1., 1.]], dtype='float32') libcp.cutpursuit(obs, source, target, edge_weight, 0.01)