MCPcopy Create free account
hub / github.com/ActiveVisionLab/DFNet / triplet_loss

Function triplet_loss

script/feature/misc.py:355–369  ·  view source on GitHub ↗

naive implementation of triplet loss :param criterion: loss function :param f1: [lvl, B, C, H, W] :param f2: [lvl, B, C, H, W] :return: loss

(f1, f2, margin=1.)

Source from the content-addressed store, hash-verified

353 return loss
354
355def triplet_loss(f1, f2, margin=1.):
356 '''
357 naive implementation of triplet loss
358 :param criterion: loss function
359 :param f1: [lvl, B, C, H, W]
360 :param f2: [lvl, B, C, H, W]
361 :return:
362 loss
363 '''
364 criterion = nn.TripletMarginLoss(margin=margin, reduction='mean')
365 anchor = f1
366 positive = f2
367 negative = torch.roll(f2, shifts=1, dims=1)
368 loss = criterion(anchor, positive, negative)
369 return loss
370
371def triplet_loss_hard_negative_mining(f1, f2, margin=1.):
372 '''

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected