-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathloss.py
87 lines (72 loc) · 3.31 KB
/
loss.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
import torch
import torch.nn as nn
class Loss(nn.modules.loss._Loss):
def __init__(self):
super(Loss, self).__init__()
def forward(self, outputs, labels):
cross_entropy_loss = nn.CrossEntropyLoss()
triplet_loss = TripletLoss(margin=1.2)
Triplet_Loss = [triplet_loss(output, labels) for output in outputs[1:4]]
Triplet_Loss = sum(Triplet_Loss) / len(Triplet_Loss)
CrossEntropy_Loss = [cross_entropy_loss(output, labels) for output in outputs[4:]]
CrossEntropy_Loss = sum(CrossEntropy_Loss) / len(CrossEntropy_Loss)
loss_sum =Triplet_Loss + CrossEntropy_Loss
print('\rtotal loss:%.2f Triplet_Loss:%.2f CrossEntropy_Loss:%.2f'%(
loss_sum.data.cpu().numpy(),
Triplet_Loss.data.cpu().numpy(),
CrossEntropy_Loss.data.cpu().numpy()))
return loss_sum
class Modfied_Loss(nn.modules.loss._Loss):
def __init__(self):
super(Modfied_Loss, self).__init__()
def forward(self, outputs, labels):
triplet_loss = TripletLoss(margin=1.2)
cross_entropy_loss = nn.CrossEntropyLoss()
Triplet_Loss = triplet_loss(outputs, labels)
CrossEntropy_Loss = cross_entropy_loss(outputs,labels)
loss_sum =Triplet_Loss + 2*CrossEntropy_Loss
print('\rtotal loss:%.2f Triplet_Loss:%.2f CrossEntropy_Loss:%.2f'%(
loss_sum.data.cpu().numpy(),
Triplet_Loss.data.cpu().numpy(),
CrossEntropy_Loss.data.cpu().numpy()))
return loss_sum
class TripletLoss(nn.Module):
"""Triplet loss with hard positive/negative mining.
Reference:
Hermans et al. In Defense of the Triplet Loss for Person Re-Identification. arXiv:1703.07737.
Code imported from https://github.com/Cysu/open-reid/blob/master/reid/loss/triplet.py.
Args:
margin (float): margin for triplet.
"""
def __init__(self, margin=0.3, mutual_flag=False):
super(TripletLoss, self).__init__()
self.margin = margin
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
self.mutual = mutual_flag
def forward(self, inputs, targets):
"""
Args:
inputs: feature matrix with shape (batch_size, feat_dim)
targets: ground truth labels with shape (num_classes)
"""
n = inputs.size(0)
# inputs = 1. * inputs / (torch.norm(inputs, 2, dim=-1, keepdim=True).expand_as(inputs) + 1e-12)
# Compute pairwise distance, replace by the official when merged
dist = torch.pow(inputs, 2).sum(dim=1, keepdim=True).expand(n, n)
dist = dist + dist.t()
dist.addmm_(1, -2, inputs, inputs.t())
dist = dist.clamp(min=1e-12).sqrt() # for numerical stability
# For each anchor, find the hardest positive and negative
mask = targets.expand(n, n).eq(targets.expand(n, n).t())
dist_ap, dist_an = [], []
for i in range(n):
dist_ap.append(dist[i][mask[i]].max().unsqueeze(0))
dist_an.append(dist[i][mask[i] == 0].min().unsqueeze(0))
dist_ap = torch.cat(dist_ap)
dist_an = torch.cat(dist_an)
# Compute ranking hinge loss
y = torch.ones_like(dist_an)
loss = self.ranking_loss(dist_an, dist_ap, y)
if self.mutual:
return loss, dist
return loss