-
Notifications
You must be signed in to change notification settings - Fork 1
/
loss_functions.py
executable file
·207 lines (148 loc) · 6.22 KB
/
loss_functions.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
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
import torch
import torch.nn.functional as F
import numpy as np
import diff_operators
def latent_loss(emb):
return torch.mean(emb**2)
def info_nce_force_emb_reg(
energy_function_list, N_neg, force_emb_, force_emb, a_t, DEVICE="cuda"
):
loss = 0
force_emb = force_emb.squeeze()
force_emb_ = force_emb_.squeeze()
d = len(energy_function_list)
for d_i, e_f in enumerate(energy_function_list):
p_ = e_f(torch.cat([force_emb_, a_t, force_emb[: d_i + 1]], axis=0))
p_ = torch.clip(p_, -2, 2)
p = torch.exp(-p_)
n_samples = torch.FloatTensor(N_neg, d_i + 1).uniform_(-0.3, 0.3)
n_samples = n_samples.to(DEVICE)
p_weight = force_emb.repeat(N_neg).reshape(N_neg, d)
actions = a_t.repeat(N_neg).reshape(N_neg, 7)
ef_input = torch.cat((p_weight, actions, n_samples), dim=1)
n = torch.clip(e_f(ef_input)[0], -5, 5)
n = torch.sum(torch.exp(-n))
loss = loss - torch.log(p / (n + p))[0]
return loss
def info_nce_reg(
energy_function_list, N_neg, emb_dic, idx_list, idx_c, idx_f, a_t, DEVICE="cuda"
):
d = len(energy_function_list)
loss = 0
for d_, e_f in enumerate(energy_function_list):
y_i = emb_dic(idx_f)[d_].reshape(-1)
p_ = e_f(torch.cat([emb_dic(idx_c), a_t, y_i], axis=0))
p_ = torch.clip(p_, -5, 5)
p = torch.sum(torch.exp(-p_))
n_samples = torch.FloatTensor(N_neg).uniform_(-0.010, 0.010)
n_samples = n_samples.unsqueeze(-1).to(DEVICE)
p_weight = emb_dic(idx_c).repeat(N_neg).reshape(N_neg, d)
actions = a_t.repeat(N_neg).reshape(N_neg, 7)
ef_input = torch.cat((p_weight, actions, n_samples), dim=1)
ef_output = e_f(ef_input)
n_tmp = torch.clip(ef_output, -5, 5)
n = torch.sum(torch.exp(-n_tmp))
loss = loss - torch.log(p / (n + p))
return loss
def info_nce(
energy_function, N_neg, emb_dic, idx_list, idx_c, idx_f, a_t, DEVICE="cuda"
):
p_ = energy_function(torch.cat([emb_dic(idx_c), emb_dic(idx_f), a_t], axis=0))
p_ = torch.clip(p_, -5, 5)
p = torch.exp(-p_)
idx_list = np.delete(
idx_list, np.where(idx_list == int(idx_f) * np.ones_like(idx_list))
)
np.random.shuffle(idx_list)
n = 0
d = emb_dic.weight.size()[1]
n_samples = torch.zeros((N_neg, d))
for i in range(d):
n_samples[:, i] = torch.FloatTensor(N_neg).uniform_(-0.15, 0.15)
n_samples[:, i] += torch.FloatTensor(N_neg).normal_(0, 0.02)
n_samples = n_samples.to(DEVICE)
p_weight = emb_dic(idx_c).repeat(N_neg).reshape(N_neg, d)
actions = a_t.repeat(N_neg).reshape(N_neg, 7)
ef_input = torch.cat((p_weight, n_samples, actions), dim=1)
n_tmp = torch.clip(energy_function(ef_input)[0], -5, 5)
n = torch.sum(torch.exp(-n_tmp))
loss = -torch.log(p / (n + p))
return loss[0]
def emb_action_loss(emb1, emb2):
loss = torch.abs(emb1 - emb2).mean()
return loss
def function_mse(model_output, gt):
return {"func_loss": ((model_output["model_out"] - gt["func"]) ** 2).mean()}
def gradients_mse(model_output, gt):
# compute gradients on the model
gradients = diff_operators.gradient(
model_output["model_out"], model_output["model_in"]
)
# compare them with the ground-truth
gradients_loss = torch.mean((gradients - gt["gradients"]).pow(2).sum(-1))
return {"gradients_loss": gradients_loss}
def hypo_weight_loss(model_output):
weight_sum = 0
total_weights = 0
for weight in model_output["hypo_params"].values():
weight_sum += torch.sum(weight**2)
total_weights += weight.numel()
return weight_sum * (1 / total_weights)
def hyper_loss(model_output, gt_sdf, ks, ki, kg, gt_normals=None, kn=None):
losses = {}
gt_sdf = gt_sdf.unsqueeze(0).float()
if gt_normals is not None:
gt_normals = gt_normals.unsqueeze(0).float()
coords = model_output["model_in"].float()
pred_sdf = model_output["model_out"].float()
sdf_constraint = torch.where(
gt_sdf == 0, torch.abs(pred_sdf), torch.zeros_like(pred_sdf)
)
losses['sdf'] = torch.abs(sdf_constraint).mean() * ks
pred_sdf_c = torch.clip(pred_sdf, -0.3, 0.3).float()
gt_sdf_c = torch.clip(gt_sdf, -0.3, 0.3).float()
inter_constraint = torch.where(
gt_sdf == 0, torch.zeros_like(pred_sdf), abs(gt_sdf_c - pred_sdf_c)
)
losses['inter'] = inter_constraint.mean() * ki
gradient = diff_operators.gradient(pred_sdf, coords)
if gt_normals is not None:
norm = (1 - F.cosine_similarity(gradient, gt_normals, dim=-1))[..., None]
normal_constraint = torch.where(
gt_sdf == 0, norm, torch.zeros_like(gradient[..., :1])
)
losses['normal_constraint'] = normal_constraint.mean() * kn
grad_constraint = abs(1 - torch.clip(torch.linalg.norm(gradient, dim=-1), 0, 1))
losses['grad_constraint'] = grad_constraint.mean() * kg
return losses
def hyper_loss_deform(model_output, gt, kl, fw, ks, ki, kn, kg):
gt_sdf = gt["sdf"]
coords = model_output["model_in"]
pred_sdf = model_output["model_out"]
sdf_constraint = torch.where(
gt_sdf == 0, torch.abs(pred_sdf), torch.zeros_like(pred_sdf)
)
pred_sdf_c = torch.clip(pred_sdf, -0.3, 0.3)
gt_sdf_c = torch.clip(gt_sdf, -0.3, 0.3)
inter_constraint = torch.where(
gt_sdf == 0, torch.zeros_like(pred_sdf), abs(gt_sdf_c - pred_sdf_c)
)
if kn != 0:
gradient = diff_operators.gradient(pred_sdf, coords)
gt_normals = gt["normals"]
norm = (1 - F.cosine_similarity(gradient, gt_normals, dim=-1))[..., None]
normal_constraint = torch.where(
gt_sdf == 0, norm, torch.zeros_like(gradient[..., :1])
)
grad_constraint = abs(1 - torch.linalg.norm(gradient, dim=-1))
else:
normal_constraint = torch.tensor([0]).float()
grad_constraint = torch.tensor([0]).float()
return {
"latent_loss": kl * latent_loss(model_output),
"hypo_weight_loss": fw * hypo_weight_loss(model_output),
"sdf": torch.abs(sdf_constraint).mean() * ks,
"inter": inter_constraint.mean() * ki,
"normal_constraint": normal_constraint.mean() * kn,
"grad_constraint": grad_constraint.mean() * kg,
}