generated from dongliangcao/pytorch-framework
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfmap_util.py
67 lines (55 loc) · 2.03 KB
/
fmap_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import torch
def nn_query(feat_x, feat_y, dim=-2):
"""
Find correspondences via nearest neighbor query
Args:
feat_x: feature vector of shape x. [V1, C].
feat_y: feature vector of shape y. [V2, C].
dim: number of dimension
Returns:
p2p: point-to-point map (shape y -> shape x). [V2].
"""
dist = torch.cdist(feat_x, feat_y) # [V1, V2]
p2p = dist.argmin(dim=dim)
return p2p
def fmap2pointmap(C12, evecs_x, evecs_y):
"""
Convert functional map to point-to-point map
Args:
C12: functional map (shape x->shape y). Shape [K, K]
evecs_x: eigenvectors of shape x. Shape [V1, K]
evecs_y: eigenvectors of shape y. Shape [V2, K]
Returns:
p2p: point-to-point map (shape y -> shape x). [V2]
"""
return nn_query(torch.matmul(evecs_x, C12.t()), evecs_y)
def pointmap2fmap(p2p, evecs_x, evecs_y):
"""
Convert a point-to-point map to functional map
Args:
p2p (np.ndarray): point-to-point map (shape x -> shape y). [Vx]
evecs_x (np.ndarray): eigenvectors of shape x. [Vx, K]
evecs_y (np.ndarray): eigenvectors of shape y. [Vy, K]
Returns:
C21 (np.ndarray): functional map (shape y -> shape x). [K, K]
"""
C21 = torch.linalg.lstsq(evecs_x, evecs_y[p2p, :]).solution
return C21
def refine_pointmap_zoomout(p2p, evecs_x, evecs_y, k_start, step=1):
"""
ZoomOut to refine a point-to-point map
Args:
p2p: point-to-point map: shape x -> shape y. [Vx]
evecs_x: eigenvectors of shape x. [Vx, K]
evecs_y: eigenvectors of shape y. [Vy, K]
k_start (int): number of eigenvectors to start
step (int, optional): step size. Default 1.
"""
k_end = evecs_x.shape[1]
inds = np.arange(k_start, k_end + step, step)
p2p_refined = p2p
for i in inds:
C21_refined = pointmap2fmap(p2p_refined, evecs_x[:, :i], evecs_y[:, :i])
p2p_refined = fmap2pointmap(C21_refined, evecs_y[:, :i], evecs_x[:, :i])
return p2p_refined