-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloss.py
More file actions
86 lines (70 loc) · 3.6 KB
/
loss.py
File metadata and controls
86 lines (70 loc) · 3.6 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
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
def pairwise_loss(outputs1, outputs2, label1, label2, sigmoid_param=1.0, l_threshold=15.0, class_num=1.0):
similarity = Variable(torch.mm(label1.data.float(), label2.data.float().t()) > 0).float()
dot_product = sigmoid_param * torch.mm(outputs1, outputs2.t())
exp_product = torch.exp(dot_product)
mask_dot = dot_product.data > l_threshold
mask_exp = dot_product.data <= l_threshold
mask_positive = similarity.data > 0
mask_negative = similarity.data <= 0
mask_dp = mask_dot & mask_positive
mask_dn = mask_dot & mask_negative
mask_ep = mask_exp & mask_positive
mask_en = mask_exp & mask_negative
dot_loss = dot_product * (1-similarity)
exp_loss = (torch.log(1+exp_product) - similarity * dot_product)
loss = (torch.sum(torch.masked_select(exp_loss, Variable(mask_ep))) + torch.sum(torch.masked_select(dot_loss, Variable(mask_dp)))) * class_num + torch.sum(torch.masked_select(exp_loss, Variable(mask_en))) + torch.sum(torch.masked_select(dot_loss, Variable(mask_dn)))
return loss / (torch.sum(mask_positive.float()) * class_num + torch.sum(mask_negative.float()))
def pairwise_loss_updated(outputs1,outputs2,label1,label2):
similarity = Variable(torch.mm(label1.data.float(), label2.data.float().t()) > 0).float()
dot_product = torch.mm(outputs1, outputs2.t())
#exp_product = torch.exp(dot_product)
mask_positive = similarity.data > 0
mask_negative = similarity.data <= 0
exp_loss = torch.log(1+torch.exp(-torch.abs(dot_product))) + torch.max(dot_product, Variable(torch.FloatTensor([0.]).cuda()))-similarity * dot_product
#weight
S1 = torch.sum(mask_positive.float())
S0 = torch.sum(mask_negative.float())
S = S0+S1
exp_loss[similarity.data > 0] = exp_loss[similarity.data > 0] * (S / S1)
exp_loss[similarity.data <= 0] = exp_loss[similarity.data <= 0] * (S / S0)
loss = torch.sum(exp_loss) / S
#exp_loss = torch.sum(torch.log(1 + exp_product) - similarity * dot_product)
return loss
class CenterLoss(nn.Module):
"""Center loss.
Reference:
Wen et al. A Discriminative Feature Learning Approach for Deep Face Recognition. ECCV 2016.
Args:
num_classes (int): number of classes.
feat_dim (int): feature dimension.
"""
def __init__(self, num_classes=10, feat_dim=2, use_gpu=True):
super(CenterLoss, self).__init__()
self.num_classes = num_classes
self.feat_dim = feat_dim
self.use_gpu = use_gpu
if self.use_gpu:
self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim).cuda())
else:
self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim))
def forward(self, x, labels):
"""
Args:
x: feature matrix with shape (batch_size, feat_dim).
labels: ground truth labels with shape (batch_size).
"""
batch_size = x.size(0)
distmat = torch.pow(x, 2).sum(dim=1, keepdim=True).expand(batch_size, self.num_classes) + \
torch.pow(self.centers, 2).sum(dim=1, keepdim=True).expand(self.num_classes, batch_size).t()
distmat.addmm_(1, -2, x, self.centers.t())
classes = torch.arange(self.num_classes).long()
if self.use_gpu: classes = classes.cuda()
labels = labels.unsqueeze(1).expand(batch_size, self.num_classes)
mask = labels.eq(classes.expand(batch_size, self.num_classes))
dist = distmat * mask.float()
loss = dist.clamp(min=1e-12, max=1e+12).sum() / batch_size
return loss