-
Notifications
You must be signed in to change notification settings - Fork 4
/
chamfer_loss.py
76 lines (64 loc) · 2.23 KB
/
chamfer_loss.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class ChamferLoss(nn.Module):
def __init__(self):
super(ChamferLoss, self).__init__()
self.use_cuda = torch.cuda.is_available()
def forward(self,preds,gts):
P = self.batch_pairwise_dist(gts, preds)
mins, _ = torch.min(P, 1)#[b,n]
loss_1 = torch.sum(mins)
mins, _ = torch.min(P, 2)
loss_2 = torch.sum(mins)
return loss_1 + loss_2
def batch_pairwise_dist(self,x,y):
bs, num_points_x, points_dim = x.size()
_, num_points_y, _ = y.size()
xx = torch.bmm(x, x.transpose(2,1))
yy = torch.bmm(y, y.transpose(2,1))
zz = torch.bmm(x, y.transpose(2,1))
if self.use_cuda:
dtype = torch.cuda.LongTensor
else:
dtype = torch.LongTensor
diag_ind_x = torch.arange(0, num_points_x).type(dtype)
diag_ind_y = torch.arange(0, num_points_y).type(dtype)
#brk()
rx = xx[:, diag_ind_x, diag_ind_x].unsqueeze(1).expand_as(zz.transpose(2,1))
ry = yy[:, diag_ind_y, diag_ind_y].unsqueeze(1).expand_as(zz)
P = (rx.transpose(2,1) + ry - 2*zz)
return P
class GlobalAlignLoss(nn.Module):
def __init__(self):
super(GlobalAlignLoss, self).__init__()
self.use_cuda = torch.cuda.is_available()
def forward(self,preds,gts, c):
P = self.batch_pairwise_dist(gts, preds)
mins, _ = torch.min(P, 1)
mins = self.huber_loss(mins,c)
loss_1 = torch.sum(mins)
mins, _ = torch.min(P, 2)
mins = self.huber_loss(mins,c)
loss_2 = torch.sum(mins)
return loss_1 + loss_2
def huber_loss(self,x,c):
x = torch.where(x<c,0.5*(x**2),c*x-0.5*(c**2))
return x
def batch_pairwise_dist(self,x,y):
bs, num_points_x, points_dim = x.size()
_, num_points_y, _ = y.size()
xx = torch.bmm(x, x.transpose(2,1))
yy = torch.bmm(y, y.transpose(2,1))
zz = torch.bmm(x, y.transpose(2,1))
if self.use_cuda:
dtype = torch.cuda.LongTensor
else:
dtype = torch.LongTensor
diag_ind_x = torch.arange(0, num_points_x).type(dtype)
diag_ind_y = torch.arange(0, num_points_y).type(dtype)
rx = xx[:, diag_ind_x, diag_ind_x].unsqueeze(1).expand_as(zz.transpose(2,1))
ry = yy[:, diag_ind_y, diag_ind_y].unsqueeze(1).expand_as(zz)
P = (rx.transpose(2,1) + ry - 2*zz)
return P