-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
144 lines (100 loc) · 2.71 KB
/
test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import torch
import time
from shape_metric import EMDLoss, ChamferLoss, VarifoldLoss
'''
Test Wasserstein(EMD) Loss!
'''
dist01 = EMDLoss()
print('compute EMDLoss ALG1')
p1 = torch.rand(32,1024,3).cuda()#.double()
p2 = torch.rand(32,1024,3).cuda()#.double()
p1.requires_grad = True
p2.requires_grad = True
s = time.time()
cost = dist01(p1, p2)
print('Wasserstein (EMD) cost from ALG1:')
print(cost)
loss1 = torch.sum(cost)
print('Wasserstein (EMD) loss from ALG1: %.05f'%loss1)
loss1.backward()
emd_time = time.time() - s
print('Time: ', emd_time)
'''
Test Chamfer Loss!
'''
print('compute ChamferLoss')
dist02 = ChamferLoss()
s2 = time.time()
cost1, cost2 = dist02(p1, p2)
print('chamfer cost1')
print(cost1)
print('chamfer cost2')
print(cost2)
loss = (torch.mean(cost1)) + (torch.mean(cost2))
print('chamfer loss: %.05f' %loss.data.cpu().numpy())
cd_time = time.time() - s2
print('Time: ', cd_time)
'''
Test Varifold pseudo-metric Loss!
'''
print('compute VarifoldLoss')
dist03 = VarifoldLoss()
s3 = time.time()
print(loss)
cost3 = dist03(p1,p2)
print('Varifold loss: %.05f' %cost3.data.cpu().numpy())
varifold_time = time.time() - s3
print('Time: ', varifold_time)
import scipy.io as sio
d = sio.loadmat('bunny.mat')
print('test on a real 3D object - bunny vs sphere')
# points and normals are provided
pts1 = torch.unsqueeze( torch.FloatTensor(d['pts']),0)
pts2 = torch.unsqueeze(torch.FloatTensor(d['pts2']),0)
nor1 = torch.unsqueeze( torch.FloatTensor(d['normal']),0)
nor2 = torch.unsqueeze(torch.FloatTensor(d['normal2']),0)
nor1_norm = torch.unsqueeze(torch.sqrt(torch.sum(torch.pow(nor1,2),2)),-1)
nor2_norm = torch.unsqueeze(torch.sqrt(torch.sum(torch.pow(nor2,2),2)),-1)
pts1 = pts1.cuda()
pts2 = pts1.cuda()
nor1 = nor1/nor1_norm
nor1 = nor1.cuda()
nor2 = nor2/nor2_norm
nor2 = nor2.cuda()
pts1.requires_grad = True
pts2.requires_grad = True
emd = dist01(pts1, pts2)
print('Wasserstein (EMD) cost from ALG1:')
print(emd)
loss1 = torch.sum(emd)
print('Wasserstein (EMD) loss from ALG1: %.05f'%loss1)
loss1.backward()
emd_time = time.time() - s
print('Time: ', emd_time)
'''
Test Chamfer Loss!
'''
print('compute ChamferLoss')
dist02 = ChamferLoss()
s2 = time.time()
cd1, cd2 = dist02(pts1, pts2)
print('chamfer cost1')
print(cd1)
print('chamfer cost2')
print(cd2)
loss2 = (torch.mean(cd1)) + (torch.mean(cd2))
print('chamfer loss: %.05f' %loss2.data.cpu().numpy())
loss2.backward()
cd_time = time.time() - s2
print('Time: ', cd_time)
'''
Test Varifold pseudo-metric Loss!
'''
print('compute VarifoldLoss')
dist03 = VarifoldLoss()
s3 = time.time()
loss3 = dist03(pts1,pts2,nor1,nor2)
print('Varifold loss: %.05f' %loss3.data.cpu().numpy())
loss3.backward()
varifold_time = time.time() - s3
print('Time: ', varifold_time)