-
Notifications
You must be signed in to change notification settings - Fork 0
/
hausdorff_distance.py
45 lines (37 loc) · 1.41 KB
/
hausdorff_distance.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
import torch
def torch2D_Hausdorff_distance(x,y): # Input be like (Batch,width,height)
x = x.float()
y = y.float()
distance_matrix = torch.cdist(x,y,p=2) # p=2 means Euclidean Distance
value1 = distance_matrix.min(2)[0].max(1, keepdim=True)[0]
value2 = distance_matrix.min(1)[0].max(1, keepdim=True)[0]
value = torch.cat((value1, value2), dim=1)
return value.max(1)[0]
if __name__ == "__main__":
u = torch.Tensor([[[1.0, 0.0],
[0.0, 1.0],
[-1.0, 0.0],
[0.0, -1.0]],
[[1.0, 0.0],
[0.0, 1.0],
[-1.0, 0.0],
[0.0, -1.0]],
[[2.0, 0.0],
[0.0, 2.0],
[-2.0, 0.0],
[0.0, -4.0]]])
v = torch.Tensor([[[0.0, 0.0],
[0.0, 2.0],
[-2.0, 0.0],
[0.0, -3.0]],
[[2.0, 0.0],
[0.0, 2.0],
[-2.0, 0.0],
[0.0, -4.0]],
[[1.0, 0.0],
[0.0, 1.0],
[-1.0, 0.0],
[0.0, -1.0]]])
print("Input shape is (B,W,H):", u.shape, v.shape)
HD = torch2D_Hausdorff_distance(u,v)
print("Hausdorff Distance is:", HD)