forked from IIPCVLAB/LCCNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
178 lines (142 loc) · 7.03 KB
/
losses.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -------------------------------------------------------------------
# Copyright (C) 2020 Università degli studi di Milano-Bicocca, iralab
# Author: Daniele Cattaneo ([email protected])
# Released under Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# http://creativecommons.org/licenses/by-nc-sa/4.0/
# -------------------------------------------------------------------
# Modified Author: Xudong Lv
# based on github.com/cattaneod/CMRNet/blob/master/losses.pyy
import torch
from torch import nn as nn
from quaternion_distances import quaternion_distance
from utils import quat2mat, rotate_back, rotate_forward, tvector2mat, quaternion_from_matrix
class GeometricLoss(nn.Module):
def __init__(self):
super().__init__()
self.sx = torch.nn.Parameter(torch.Tensor([0.0]), requires_grad=True)
self.sq = torch.nn.Parameter(torch.Tensor([-3.0]), requires_grad=True)
self.transl_loss = nn.SmoothL1Loss(reduction='none')
def forward(self, target_transl, target_rot, transl_err, rot_err):
loss_transl = self.transl_loss(transl_err, target_transl).sum(1).mean()
loss_rot = quaternion_distance(rot_err, target_rot, rot_err.device).mean()
total_loss = torch.exp(-self.sx) * loss_transl + self.sx
total_loss += torch.exp(-self.sq) * loss_rot + self.sq
return total_loss
class ProposedLoss(nn.Module):
def __init__(self, rescale_trans, rescale_rot):
super(ProposedLoss, self).__init__()
self.rescale_trans = rescale_trans
self.rescale_rot = rescale_rot
self.transl_loss = nn.SmoothL1Loss(reduction='none')
self.losses = {}
def forward(self, target_transl, target_rot, transl_err, rot_err):
loss_transl = 0.
if self.rescale_trans != 0.:
loss_transl = self.transl_loss(transl_err, target_transl).sum(1).mean() * 100
loss_rot = 0.
if self.rescale_rot != 0.:
loss_rot = quaternion_distance(rot_err, target_rot, rot_err.device).mean()
total_loss = self.rescale_trans*loss_transl + self.rescale_rot*loss_rot
self.losses['total_loss'] = total_loss
self.losses['transl_loss'] = loss_transl
self.losses['rot_loss'] = loss_rot
return self.losses
class L1Loss(nn.Module):
def __init__(self, rescale_trans, rescale_rot):
super(L1Loss, self).__init__()
self.rescale_trans = rescale_trans
self.rescale_rot = rescale_rot
self.transl_loss = nn.SmoothL1Loss(reduction='none')
def forward(self, target_transl, target_rot, transl_err, rot_err):
loss_transl = self.transl_loss(transl_err, target_transl).sum(1).mean()
loss_rot = self.transl_loss(rot_err, target_rot).sum(1).mean()
total_loss = self.rescale_trans*loss_transl + self.rescale_rot*loss_rot
return total_loss
class DistancePoints3D(nn.Module):
def __init__(self):
super(DistancePoints3D, self).__init__()
def forward(self, point_clouds, target_transl, target_rot, transl_err, rot_err):
"""
Points Distance Error
Args:
point_cloud: list of B Point Clouds, each in the relative GT frame
transl_err: network estimate of the translations
rot_err: network estimate of the rotations
Returns:
The mean distance between 3D points
"""
#start = time.time()
total_loss = torch.tensor([0.0]).to(transl_err.device)
for i in range(len(point_clouds)):
point_cloud_gt = point_clouds[i].to(transl_err.device)
point_cloud_out = point_clouds[i].clone()
R_target = quat2mat(target_rot[i])
T_target = tvector2mat(target_transl[i])
RT_target = torch.mm(T_target, R_target)
R_predicted = quat2mat(rot_err[i])
T_predicted = tvector2mat(transl_err[i])
RT_predicted = torch.mm(T_predicted, R_predicted)
RT_total = torch.mm(RT_target.inverse(), RT_predicted)
point_cloud_out = rotate_forward(point_cloud_out, RT_total)
error = (point_cloud_out - point_cloud_gt).norm(dim=0)
error.clamp(100.)
total_loss += error.mean()
#end = time.time()
#print("3D Distance Time: ", end-start)
return total_loss/target_transl.shape[0]
# The combination of L1 loss of translation part,
# quaternion angle loss of rotation part,
# distance loss of the pointclouds
class CombinedLoss(nn.Module):
def __init__(self, rescale_trans, rescale_rot, weight_point_cloud):
super(CombinedLoss, self).__init__()
self.rescale_trans = rescale_trans
self.rescale_rot = rescale_rot
self.transl_loss = nn.SmoothL1Loss(reduction='none')
self.weight_point_cloud = weight_point_cloud
self.loss = {}
def forward(self, point_clouds, target_transl, target_rot, transl_err, rot_err):
"""
The Combination of Pose Error and Points Distance Error
Args:
point_cloud: list of B Point Clouds, each in the relative GT frame
target_transl: groundtruth of the translations
target_rot: groundtruth of the rotations
transl_err: network estimate of the translations
rot_err: network estimate of the rotations
Returns:
The combination loss of Pose error and the mean distance between 3D points
"""
loss_transl = 0.
if self.rescale_trans != 0.:
loss_transl = self.transl_loss(transl_err, target_transl).sum(1).mean()
loss_rot = 0.
if self.rescale_rot != 0.:
loss_rot = quaternion_distance(rot_err, target_rot, rot_err.device).mean()
pose_loss = self.rescale_trans*loss_transl + self.rescale_rot*loss_rot
#start = time.time()
point_clouds_loss = torch.tensor([0.0]).to(transl_err.device)
for i in range(len(point_clouds)):
point_cloud_gt = point_clouds[i].to(transl_err.device)
point_cloud_out = point_clouds[i].clone()
R_target = quat2mat(target_rot[i])
T_target = tvector2mat(target_transl[i])
RT_target = torch.mm(T_target, R_target)
R_predicted = quat2mat(rot_err[i])
T_predicted = tvector2mat(transl_err[i])
RT_predicted = torch.mm(T_predicted, R_predicted)
RT_total = torch.mm(RT_target.inverse(), RT_predicted)
point_cloud_out = rotate_forward(point_cloud_out, RT_total)
error = (point_cloud_out - point_cloud_gt).norm(dim=0)
error.clamp(100.)
point_clouds_loss += error.mean()
#end = time.time()
#print("3D Distance Time: ", end-start)
total_loss = (1 - self.weight_point_cloud) * pose_loss +\
self.weight_point_cloud * (point_clouds_loss/target_transl.shape[0])
self.loss['total_loss'] = total_loss
self.loss['transl_loss'] = loss_transl
self.loss['rot_loss'] = loss_rot
self.loss['point_clouds_loss'] = point_clouds_loss/target_transl.shape[0]
return self.loss