def data_loader_notredame(data_path, name, dtype, device): edges_path = os.path.join(data_path, name + '_Edges.txt') edges = np.genfromtxt(edges_path, delimiter=',') edges = (np.array(edges) - 1).astype('int64') Qabs_path = os.path.join(data_path, name + '_Qabs.txt') Qabs = np.genfromtxt(Qabs_path, delimiter=',') Qrel_path = os.path.join(data_path, name + '_Qrel.txt') Qrel = np.genfromtxt(Qrel_path, delimiter=',') Qrel = tr.tensor(Qrel, dtype=dtype, device=device).unsqueeze( 1) # do not assigne to gpu for now (this matrix can be huge) Qabs = tr.tensor(Qabs, dtype=dtype, device=device).unsqueeze(1) Qabs[:, :, 1:] *= -1. G, C = make_graphs(edges) #I = np.array(list(G.edges())) degree = np.array((G.degree())) max_index = np.argmax(degree[:, 1]) G, edges, Qabs = swap_nodes(G, edges, Qabs, 0, max_index) # Additional formatting : N x P x d N, P, _ = Qabs.shape Nrel, Prel, _ = Qrel.shape # rotate all absolute poses so that the first camera becomes the reference Qabs = utils.quaternion_X_times_Y_inv( Qabs, Qabs[0, :, :].unsqueeze(0).repeat(N, 1, 1)) wabs = (1. / P) * tr.ones([N, P], dtype=dtype, device=device) wrel = (1. / Prel) * tr.ones([Nrel, Prel], dtype=dtype, device=device) if nx.is_connected(C): return edges, G, Qrel, wrel, Qabs, wabs, None
def data_loader_artsquad(data_path, name, dtype, device): edges_path = os.path.join(data_path, name + '_Edges.txt') edges = np.genfromtxt(edges_path, delimiter=',') edges = (np.array(edges) - 2).astype('int64') Qabs_path = os.path.join(data_path, name + '_Qabs.txt') Qabs = np.genfromtxt(Qabs_path, delimiter=',') Qrel_path = os.path.join(data_path, name + '_Qrel.txt') Qrel = np.genfromtxt(Qrel_path, delimiter=',') Qrel = tr.tensor(Qrel, dtype=dtype, device=device).unsqueeze( 1) # do not assigne to gpu for now (this matrix can be huge) Qabs = tr.tensor(Qabs, dtype=dtype, device=device).unsqueeze(1) Qabs = Qabs[1:, :, :] Qabs[:, :, 1:] *= -1. vals = np.ones(edges.shape[0], dtype=np.double) rows = (np.asarray(edges[:, 0])).astype(int) cols = (np.asarray(edges[:, 1])).astype(int) N = Qabs.shape[0] G = np.zeros((N, N)) G[rows, cols] = vals C = nx.from_numpy_matrix(G) G = nx.from_numpy_matrix(G, create_using=nx.DiGraph()) connected_comp = list(nx.connected_components(C)) #H = G.subgraph(connected_comp[0]) #Qabs = Qabs[connected_comp[0],:,:] connected_comp = list(connected_comp[0]) indx_1 = tr.norm(Qabs, dim=-1) > 0.5 indx_1 = indx_1.int() indx = tr.zeros([Qabs.shape[0], 1, 1]).int().to(device) indx[connected_comp, 0] = 1 indx = indx_1 * indx indx = indx.to(device) indx = indx.bool() #I = np.array(list(G.edges())) # Additional formatting : N x P x d N, P, _ = Qabs.shape Nrel, Prel, _ = Qrel.shape # rotate all absolute poses so that the first camera becomes the reference Qabs = utils.quaternion_X_times_Y_inv( Qabs, Qabs[0, :, :].unsqueeze(0).repeat(N, 1, 1)) wabs = (1. / P) * tr.ones([N, P], dtype=dtype, device=device) wrel = (1. / Prel) * tr.ones([Nrel, Prel], dtype=dtype, device=device) #if nx.is_connected(C): return edges, G, Qrel, wrel, Qabs, wabs, indx
def compute_ratios(self, particles, weights, edges): ratios = [] RM_weights = [] i = edges[:, 0] j = edges[:, 1] xi = particles[i, :, :] xj = particles[j, :, :] if self.grad_type == 'euclidean': ratios = utils.forward_quaternion_X_times_Y_inv(xi, xj) elif self.grad_type == 'quaternion': ratios = utils.quaternion_X_times_Y_inv(xi, xj) #normalize = tr.norm(ratios,dim=-1).clone().detach() #ratios = ratios/normalize.unsqueeze(-1) #RM_weights = weights[i,:]*weights[j,:] N = xi.shape[0] RM_weights = weights[0, :].unsqueeze(0).repeat(N, 1) #ratios = ratios.clone().detach() return ratios, RM_weights
def data_loader_shapenet(data_path, name, dtype, device, conjugate=False): edges_path = os.path.join(data_path, name + '_Edges.txt') edges = np.genfromtxt(edges_path, delimiter=',') edges = (np.array(edges) - 1).astype('int64') Qabs_path = os.path.join(data_path, name + '_Qabs.txt') Qabs = np.genfromtxt(Qabs_path, delimiter=',') Qrel_path = os.path.join(data_path, name + '_Qrel.txt') Qrel = np.genfromtxt(Qrel_path, delimiter=',') Qrel = tr.tensor(Qrel, dtype=dtype, device=device).unsqueeze( 1) # do not assigne to gpu for now (this matrix can be huge) #Qrel = reshape_flat_tensor(Qrel) Qabs = tr.tensor(Qabs, dtype=dtype, device=device).unsqueeze(1) #Qabs = Qabs[1:,:,:] if conjugate: Qabs[:, :, 1:] *= -1. # test error to gt N, P, _ = Qabs.shape Nrel, Prel, _ = Qrel.shape wabs = (1. / P) * tr.ones([N, P], dtype=dtype, device=device) wrel = (1. / Prel) * tr.ones([Nrel, Prel], dtype=dtype, device=device) true_args = make_true_dict(args) # true_prior = get_prior(true_args, dtype, device) # true_RM_map = get_true_rm_map(true_args, edges,dtype,device) # rm, rm_weights = true_RM_map(Qabs, wabs , edges) # dist = utils.quaternion_geodesic_distance(rm,Qrel) # min_dist,_ = torch.min(dist,dim=-1) # avg_best_dist = torch.mean(min_dist,dim=-1) G, C = make_graphs(edges) #connected_comp = list(nx.connected_components(C)) #H = G.subgraph(connected_comp[0]) #Qabs = Qabs[connected_comp[0],:,:] #connected_comp = list(connected_comp[0]) #Qabs = Qabs[connected_comp,:,:] #I = np.array(list(G.edges())) degree = np.array((G.degree())) max_index = np.argmax(degree[:, 1]) G, edges, Qabs = swap_nodes(G, edges, Qabs, 0, max_index) # Additional formatting : N x P x d N, P, _ = Qabs.shape Nrel, Prel, _ = Qrel.shape # rotate all absolute poses so that the first camera becomes the reference Qabs = utils.quaternion_X_times_Y_inv( Qabs, Qabs[0, :, :].unsqueeze(0).repeat(N, 1, 1)) mask = Qabs[:, :, 0] < 0 Qabs[mask] *= -1. mask = Qrel[:, :, 0] < 0 Qrel[mask] *= -1. wabs = (1. / P) * tr.ones([N, P], dtype=dtype, device=device) wrel = (1. / Prel) * tr.ones([Nrel, Prel], dtype=dtype, device=device) # reshaping Qrel if nx.is_connected(C): return edges, G, Qrel, wrel, Qabs, wabs, None
def compute_ratios(self, particles, couplings, edges): weights, coupling_strenght = couplings ratios = [] RM_weights = [] i = edges[:, 0] j = edges[:, 1] xi = particles[i, :, :] xj = particles[j, :, :] N, K, _ = xi.shape N, L, _ = xj.shape #xj = tr.ones_like(xi) #xj = xj/tr.norm(xj,dim=-1).unsqueeze(-1) A = self.coupling(weights[i, :], weights[j, :], coupling_strenght).reshape([N, -1]) A = A / tr.sum(A, dim=-1).unsqueeze(-1).detach() if self.subsample: #mask =tr.multinomial(A,particles.shape[1], replacement=True) mask = tr.multinomial(tr.ones_like(A), particles.shape[1], replacement=True) mask_i = mask / K mask_j = mask - mask_i * K mask_i = tr.nn.functional.one_hot(mask_i, particles.shape[1]).to( particles.device) mask_i = mask_i.type(particles.dtype) xi = tr.einsum('nki,nlk->nli', xi, mask_i) mask_j = tr.nn.functional.one_hot(mask_j, particles.shape[1]).to( particles.device) mask_j = mask_j.type(particles.dtype) xj = tr.einsum('nki,nlk->nli', xj, mask_j) mask = tr.nn.functional.one_hot(mask, A.shape[1]).to(particles.device) mask = mask.type(particles.dtype) A = tr.einsum('nk,nlk->nl', A, mask) #w = tr.einsum('nk,nlk->nl',weights,mask_i) if self.grad_type == 'euclidean': ratios = utils.forward_quaternion_X_times_Y_inv(xi, xj) elif self.grad_type == 'quaternion': ratios = utils.quaternion_X_times_Y_inv(xi, xj) #ratios = ratios.permute([0,3,1,2]).reshape([N,4,-1]).permute([0,2,1]) #RM_weights = self.coupling(weights[i,:],weights[j,:],coupling_strenght).reshape([N,-1]) RM_weights = A / tr.sum(A, dim=-1).unsqueeze(-1).detach() else: if self.grad_type == 'euclidean': ratios = utils.forward_quaternion_X_times_Y_inv_prod(xi, xj) elif self.grad_type == 'quaternion': ratios = utils.quaternion_X_times_Y_inv_prod(xi, xj) ratios = ratios.permute([0, 3, 1, 2]).reshape([N, 4, -1]).permute([0, 2, 1]) RM_weights = A / tr.sum(A, dim=-1).unsqueeze(-1).detach() #RM_weights = A #ratios = particles #RM_weights = weights return ratios, RM_weights