Skip to content

Commit 3221817

Browse files
committed
change loss function
1 parent ce68fba commit 3221817

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

simlearner3d/models/criterion/masked_triplet_loss.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class NMaskedPairLoss:
2929
def __init__(self, margin =0.3):
3030
self.margin= margin
3131
def __call__(self, matching, non_matching, masq) -> torch.Tensor:
32-
return torch.clamp_min(non_matching - matching + self.margin,0).mul(masq)
32+
loss = torch.clamp_min(non_matching - matching + self.margin,0)
33+
valuable_loss = (loss > 0 )
34+
return loss [masq & valuable_loss]
3335

3436
class SimpleTripletLoss:
3537
def __init__(self,margin=0.3):

simlearner3d/models/generic_model_n_uplet.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def corr(fmap1, fmap2):
9797
fmap1 = fmap1.view(B, D, H, W1)
9898
fmap2 = fmap2.view(B, D, H, W2)
9999
corr = torch.einsum('aijk,aijh->ajkh', fmap1, fmap2)
100-
#corr = corr.reshape(B, H, W1, 1, W2).contiguous()
101100
return corr #/ torch.sqrt(torch.tensor(D).float())
102101

103102
def training_step (self, batch, batch_idx: int):
@@ -129,7 +128,7 @@ def training_step (self, batch, batch_idx: int):
129128
grid = torch.cat([xgrido,ygrido], dim=1).permute(0,2,3,1) # B,H1,W1, 2
130129
# compute features at coordinates
131130
f_map2_pos = F.grid_sample(fmap2, grid, align_corners=True)
132-
131+
f_map2_pos = F.normalize(f_map2_pos,p=2.0, dim=1)
133132
# compute all pairs cosine similarities
134133
# B,H,W1,W2
135134
all_corr = Model.corr(fmap1,fmap2)
@@ -152,11 +151,9 @@ def training_step (self, batch, batch_idx: int):
152151
mask_non_matching.scatter_(3,x_lower_bound,0)
153152
corr_matching = corr_matching.unsqueeze(-1).repeat(1,1,1,W2)
154153

155-
training_loss = self.criterion(corr_matching,all_corr,mask_non_matching)
154+
training_loss = self.criterion(corr_matching,all_corr,mask_non_matching & masq_defined)
155+
training_loss= training_loss.sum()
156156

157-
training_loss= training_loss.sum().div(
158-
torch.logical_and(mask_non_matching, masq_defined).count_nonzero()
159-
+ 1e-12)
160157
self.log("training_loss",
161158
training_loss,
162159
prog_bar=True,
@@ -194,7 +191,7 @@ def validation_step(self,batch,batch_idx: int):
194191
grid = torch.cat([xgrido,ygrido], dim=1).permute(0,2,3,1) # B,H1,W1, 2
195192
# compute features at coordinates
196193
f_map2_pos = F.grid_sample(fmap2, grid, align_corners=True)
197-
194+
f_map2_pos = F.normalize(f_map2_pos,p=2.0, dim=1)
198195
# compute all pairs cosine similarities
199196
# B,H,W1,W2
200197
all_corr = Model.corr(fmap1,fmap2)
@@ -217,11 +214,9 @@ def validation_step(self,batch,batch_idx: int):
217214
mask_non_matching.scatter_(3,x_lower_bound,0)
218215
corr_matching = corr_matching.unsqueeze(-1).repeat(1,1,1,W2)
219216

220-
validation_loss = self.criterion(corr_matching,all_corr,mask_non_matching)
217+
validation_loss = self.criterion(corr_matching,all_corr,mask_non_matching & masq_defined)
218+
validation_loss= validation_loss.sum()
221219

222-
validation_loss= validation_loss.sum().div(
223-
torch.logical_and(mask_non_matching, masq_defined).count_nonzero()
224-
+ 1e-12)
225220
self.log("val_loss",
226221
validation_loss,
227222
prog_bar=True,
@@ -260,7 +255,7 @@ def test_step(self,batch,batch_idx: int):
260255
grid = torch.cat([xgrido,ygrido], dim=1).permute(0,2,3,1) # B,H1,W1, 2
261256
# compute features at coordinates
262257
f_map2_pos = F.grid_sample(fmap2, grid, align_corners=True)
263-
258+
f_map2_pos = F.normalize(f_map2_pos,p=2.0, dim=1)
264259
# compute all pairs cosine similarities
265260
# B,H,W1,W2
266261
all_corr = Model.corr(fmap1,fmap2)
@@ -284,11 +279,10 @@ def test_step(self,batch,batch_idx: int):
284279
mask_non_matching.scatter_(3,x_lower_bound,0)
285280
corr_matching = corr_matching.unsqueeze(-1).repeat(1,1,1,W2)
286281

287-
test_loss = self.criterion(corr_matching,all_corr,mask_non_matching)
282+
test_loss = self.criterion(corr_matching,all_corr,mask_non_matching & masq_defined)
283+
284+
test_loss= test_loss.sum()
288285

289-
test_loss= test_loss.sum().div(
290-
torch.logical_and(mask_non_matching, masq_defined).count_nonzero()
291-
+ 1e-12)
292286
self.log("test_loss",
293287
test_loss,
294288
prog_bar=True,

0 commit comments

Comments
 (0)