-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss.py
More file actions
223 lines (179 loc) · 8.01 KB
/
Copy pathloss.py
File metadata and controls
223 lines (179 loc) · 8.01 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'''
Author: Jin Zeng, Yaxuan Chen
Date: 2023-04-13
LastEditTime: 2025-01-20
Description: loss function
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def iq2d(iq):
theta = torch.atan2(iq[:,0:1,:,:], iq[:,1:2,:,:])
theta[torch.lt(theta, 0)] = theta[torch.lt(theta, 0)]+2*np.pi
depth = (theta / (2*np.pi) )
return depth
class GLoss(nn.Module):
def __init__(self, device, weight=0.05, alpha=0.3, lambda_attconf_reg=0.005, lambda_inter_sparse=0.005):
super(GLoss, self).__init__()
self.weight = weight
self.max_range_iq = 0.15
self.max_range_d = 1
self.device = device
# GIGA loss func params
self.alpha = alpha # iq weight
self.lambda_attconf_reg = lambda_attconf_reg
self.lambda_inter_sparse = lambda_inter_sparse
def forward(self,
out_0, out_1, out_2,
ideal_IQ, ideal_d,
inter_graph_0, attconf_0,
inter_graph_1, attconf_1,
inter_graph_2, attconf_2):
# === GT ===
ideal_IQ_0 = ideal_IQ[:, 0:2, :, :]
ideal_IQ_1 = ideal_IQ[:, 2:4, :, :]
ideal_IQ_2 = ideal_IQ[:, 4:6, :, :]
# === mask ===
d_mask = (ideal_d != 0) & (ideal_d < 10)
iq_mask = torch.cat([d_mask, d_mask], dim=1)
other_d = torch.ones_like(ideal_d).to(self.device)
other_iq = torch.ones_like(ideal_IQ_0).to(self.device)
# # === weighted IQ loss func ===
def weighted_iq_loss(pred, gt, att):
abs_error = torch.abs(pred - gt)
error_clip = torch.min(abs_error, self.max_range_iq * other_iq)
att = att.expand_as(pred).detach() # [B, 1, H, W] → [B, 2, H, W]
weighted = att * error_clip
unweighted = error_clip
return (1 - self.alpha) * unweighted[iq_mask].mean() + self.alpha * weighted[iq_mask].mean()
# === regularization ===
def attconf_reg_loss(att):
return (att.mean() - 0.5) ** 2
def inter_sparse_loss(inter):
entropy = -inter * torch.log(inter + 1e-6)
return -entropy.sum(dim=1).mean()
# === IQ loss ===
iq_loss_0 = weighted_iq_loss(out_0, ideal_IQ_0, attconf_0)
iq_loss_1 = weighted_iq_loss(out_1, ideal_IQ_1, attconf_1)
iq_loss_2 = weighted_iq_loss(out_2, ideal_IQ_2, attconf_2)
loss_sup = iq_loss_0 + iq_loss_1 + iq_loss_2
# === Depth loss ===
def d_loss_fn(pred_iq, gt_iq):
d_pred = iq2d(pred_iq)
d_gt = iq2d(gt_iq)
return torch.min(torch.abs(d_pred - d_gt), self.max_range_d * other_d)[d_mask].mean()
d_loss_0 = d_loss_fn(out_0, ideal_IQ_0)
d_loss_1 = d_loss_fn(out_1, ideal_IQ_1)
d_loss_2 = d_loss_fn(out_2, ideal_IQ_2)
loss_sup_d = d_loss_0 + d_loss_1 + d_loss_2
# === conf regularization ===
attconf_reg = attconf_reg_loss(attconf_0) + attconf_reg_loss(attconf_1) + attconf_reg_loss(attconf_2)
# === inter_graph sparse regularization ===
inter_sparse = inter_sparse_loss(inter_graph_0) + inter_sparse_loss(inter_graph_1) + inter_sparse_loss(inter_graph_2)
loss_total = loss_sup \
+ 0.1 * loss_sup_d \
+ self.lambda_attconf_reg * attconf_reg \
+ self.lambda_inter_sparse * inter_sparse
return loss_total
class GLoss_test(nn.Module):
def __init__(self, weight=0.05):
super(GLoss_test, self).__init__()
self.weight = weight
self.max_range_iq = 0.15
self.max_range_d = 1
def forward(self, out_0, out_1, out_2, ideal_IQ, ideal_d):
"""
:param out: [batch_size, 2, H, W]*3
:param ideal: [batch_size, 2, H, W]
:return:
"""
ideal_IQ_0 = ideal_IQ[:,0:2,:,:]
ideal_IQ_1 = ideal_IQ[:,2:4,:,:]
ideal_IQ_2 = ideal_IQ[:,4:6,:,:]
d_mask = (ideal_d != 0) * (ideal_d < 10)
iq_mask = torch.concatenate([d_mask,d_mask], axis=1)
iq_loss_0 = torch.abs(out_0[iq_mask] - ideal_IQ_0[iq_mask]).mean()
iq_loss_1 = torch.abs(out_1[iq_mask] - ideal_IQ_1[iq_mask]).mean()
iq_loss_2 = torch.abs(out_2[iq_mask] - ideal_IQ_2[iq_mask]).mean()
d_0 = iq2d(out_0)
d_1 = iq2d(out_1)
d_2 = iq2d(out_2)
d_ideal_0 = iq2d(ideal_IQ_0)
d_ideal_1 = iq2d(ideal_IQ_1)
d_ideal_2 = iq2d(ideal_IQ_2)
d_loss_0 = torch.abs(d_0[d_mask] - d_ideal_0[d_mask]).mean()
d_loss_1 = torch.abs(d_1[d_mask] - d_ideal_1[d_mask]).mean()
d_loss_2 = torch.abs(d_2[d_mask] - d_ideal_2[d_mask]).mean()
""" L1 loss """
loss_sup = iq_loss_0 + iq_loss_1 + iq_loss_2
loss_sup_d = d_loss_0 + d_loss_1 + d_loss_2
return loss_sup+0.1*loss_sup_d
class GLoss_MSE(nn.Module):
def __init__(self, device, weight=0.05):
super(GLoss_MSE, self).__init__()
self.weight = weight
self.max_range_iq = 0.15
self.max_range_d = 1
self.device = device
def forward(self, out_0, out_1, out_2, ideal_IQ, ideal_d):
"""
:param out: [batch_size, 2, H, W]*3
:param ideal: [batch_size, 2, H, W]
:return:
"""
ideal_IQ_0 = ideal_IQ[:,0:2,:,:]
ideal_IQ_1 = ideal_IQ[:,2:4,:,:]
ideal_IQ_2 = ideal_IQ[:,4:6,:,:]
d_mask = (ideal_d != 0) * (ideal_d < 10)
iq_mask = torch.cat([d_mask,d_mask], axis=1)
other_d = torch.ones(ideal_d.shape).contiguous().to(self.device)
other_iq = torch.ones(ideal_IQ_0.shape).contiguous().to(self.device)
iq_loss_0 = torch.min((out_0[iq_mask] - ideal_IQ_0[iq_mask]) ** 2, self.max_range_iq ** 2 * other_iq[iq_mask]).mean()
iq_loss_1 = torch.min((out_1[iq_mask] - ideal_IQ_1[iq_mask]) ** 2, self.max_range_iq ** 2 * other_iq[iq_mask]).mean()
iq_loss_2 = torch.min((out_2[iq_mask] - ideal_IQ_2[iq_mask]) ** 2, self.max_range_iq ** 2 * other_iq[iq_mask]).mean()
d_0 = iq2d(out_0)
d_1 = iq2d(out_1)
d_2 = iq2d(out_2)
d_ideal_0 = iq2d(ideal_IQ_0)
d_ideal_1 = iq2d(ideal_IQ_1)
d_ideal_2 = iq2d(ideal_IQ_2)
d_loss_0 = torch.min((d_0[d_mask] - d_ideal_0[d_mask]) ** 2, self.max_range_d ** 2 * other_d[d_mask]).mean()
d_loss_1 = torch.min((d_1[d_mask] - d_ideal_1[d_mask]) ** 2, self.max_range_d ** 2 * other_d[d_mask]).mean()
d_loss_2 = torch.min((d_2[d_mask] - d_ideal_2[d_mask]) ** 2, self.max_range_d ** 2 * other_d[d_mask]).mean()
""" L1 loss """
loss_sup = iq_loss_0 + iq_loss_1 + iq_loss_2
return loss_sup
class GLoss_MSE_test(nn.Module):
def __init__(self, weight=0.05):
super(GLoss_MSE_test, self).__init__()
self.weight = weight
self.max_range_iq = 0.15
self.max_range_d = 1
def forward(self, out_0, out_1, out_2, ideal_IQ, ideal_d):
"""
:param out: [batch_size, 2, H, W]*3
:param ideal: [batch_size, 2, H, W]
:return:
"""
ideal_IQ_0 = ideal_IQ[:,0:2,:,:]
ideal_IQ_1 = ideal_IQ[:,2:4,:,:]
ideal_IQ_2 = ideal_IQ[:,4:6,:,:]
d_mask = (ideal_d != 0) * (ideal_d < 10)
iq_mask = torch.cat([d_mask,d_mask], axis=1)
iq_loss_0 = ((out_0[iq_mask] - ideal_IQ_0[iq_mask]) ** 2).mean()
iq_loss_1 = ((out_1[iq_mask] - ideal_IQ_1[iq_mask]) ** 2).mean()
iq_loss_2 = ((out_2[iq_mask] - ideal_IQ_2[iq_mask]) ** 2).mean()
d_0 = iq2d(out_0)
d_1 = iq2d(out_1)
d_2 = iq2d(out_2)
d_ideal_0 = iq2d(ideal_IQ_0)
d_ideal_1 = iq2d(ideal_IQ_1)
d_ideal_2 = iq2d(ideal_IQ_2)
d_loss_0 = ((d_0[d_mask] - d_ideal_0[d_mask]) ** 2).mean()
d_loss_1 = ((d_1[d_mask] - d_ideal_1[d_mask]) ** 2).mean()
d_loss_2 = ((d_2[d_mask] - d_ideal_2[d_mask]) ** 2).mean()
""" L1 loss """
loss_sup = iq_loss_0 + iq_loss_1 + iq_loss_2
loss_sup_d = d_loss_0 + d_loss_1 + d_loss_2
return loss_sup