-
Notifications
You must be signed in to change notification settings - Fork 3
/
losses.py
166 lines (127 loc) · 5.48 KB
/
losses.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
### Contrastive Loss
import os
import sys
import time
import math
import librosa
import numpy as np
import matplotlib.pyplot as plt
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.checkpoint as cp
from torch.nn.parameter import Parameter
import datetime
import _pickle as cPickle
#from prototype_memory import *
import copy
from itertools import combinations
# seed = 0
# torch.backends.cudnn.deterministic = True
# random.seed(seed)
# torch.manual_seed(seed)
# torch.cuda.manual_seed(seed)
# np.random.seed(seed)
class PairSelector:
def __init__(self, balance=True):
self.balance = balance
def get_pairs(self, embeddings, labels):
labels = labels.cpu().data.numpy()
all_pairs = np.array(list(combinations(range(len(labels)),2)))
all_pairs = torch.LongTensor(all_pairs)
#print(np.shape((labels[all_pairs[:,0]] == labels[all_pairs[:,1]])))
positive_pairs = all_pairs[(labels[all_pairs[:,0]] == labels[all_pairs[:,1]]).nonzero()]
negative_pairs = all_pairs[(labels[all_pairs[:,0]] != labels[all_pairs[:,1]]).nonzero()]
#print(np.shape(positive_pairs), np.shape(negative_pairs))
if self.balance:
negative_pairs = negative_pairs[torch.randperm(len(negative_pairs))[:len(positive_pairs)]]
return positive_pairs, negative_pairs
class OnlineContrastiveLoss(nn.Module):
def __init__(self, proto_net, pair_selector, margin=1):
super(OnlineContrastiveLoss, self).__init__()
self.proto_net = proto_net
self.margin = margin
self.pair_selector = pair_selector
def forward(self, embeddings, labels):
labels = torch.argmax(labels, dim=1)
positive_pairs, negative_pairs = self.pair_selector.get_pairs(embeddings, labels)
if embeddings.is_cuda:
positive_pairs = positive_pairs.cuda()
negative_pairs = negative_pairs.cuda()
positive_loss = (embeddings[positive_pairs[:,0]] - embeddings[positive_pairs[:,1]]).pow(2).sum(1)
negative_loss = F.relu(self.margin - (embeddings[negative_pairs[:,0]] - embeddings[negative_pairs[:,1]]).pow(2).sum(1).sqrt()).pow(2)
loss = torch.cat([positive_loss, negative_loss], dim = 0)
return loss.mean()
class OnlineContrastiveLossWithPrototypes(nn.Module):
def __init__(self, proto_net, pair_selector, margin=1):
super(OnlineContrastiveLossWithPrototypes, self).__init__()
self.proto_net = proto_net
self.margin = margin
self.pair_selector = pair_selector
def forward(self, embeddings, labels,proto_net):
labels = torch.argmax(labels, dim=1)
prototypes = torch.from_numpy(np.array(list(proto_net.memory.prototypes.values()))).float().cuda()
proto_keys = torch.from_numpy(np.array(list(proto_net.memory.prototypes.keys()))).cuda()
#import pdb; pdb.set_trace()
embeddings = torch.cat((embeddings,prototypes),0)
labels = torch.cat((labels,proto_keys),0)
positive_pairs, negative_pairs = self.pair_selector.get_pairs(embeddings, labels)
if embeddings.is_cuda:
positive_pairs = positive_pairs.cuda()
negative_pairs = negative_pairs.cuda()
positive_loss = (embeddings[positive_pairs[:,0]] - embeddings[positive_pairs[:,1]]).pow(2).sum(1)
negative_loss = F.relu(self.margin - (embeddings[negative_pairs[:,0]] - embeddings[negative_pairs[:,1]]).pow(2).sum(1).sqrt()).pow(2)
loss = torch.cat([positive_loss, negative_loss], dim = 0)
return loss.mean()
class OnlinePrototypicalContrastiveLoss(nn.Module):
def __init__(self, proto_net, T, maxClass):
super(OnlinePrototypicalContrastiveLoss, self).__init__()
self.proto_net = proto_net
self.key2idx = torch.empty(maxClass,dtype=torch.long).cuda()
self.T = T
def forward(self, embeddings, labels):
labels = torch.argmax(labels, dim=1, keepdim=True) ## NX1
prototypes = torch.from_numpy(np.array(list(self.proto_net.memory.prototypes.values()))).float().cuda()
proto_keys = list(self.proto_net.memory.prototypes.keys())
#import pdb; pdb.set_trace()
self.key2idx[proto_keys] = torch.arange(len(proto_keys)).cuda()
dist = self.compute_similarity(embeddings, prototypes) # N X C
#print(dist)
#mask = torch.zeros(dist.shape).cuda()
#mask = mask.scatter_(1,labels,1.)
#print(mask)
#masked_softmax = torch.gather(dist, 1, mask)
#print(dist*mask)
#import pdb; pdb.set_trace()
masked_dist = dist.gather(1,self.key2idx[labels[:,0]].view(-1,1))
loss = - torch.log(masked_dist)
#print(loss)
loss = loss.mean()
#print(loss)
return loss
#loss =
def compute_similarity(self,embeddings, prototypes):
contrast = torch.div(torch.matmul(embeddings, prototypes.T), self.T)
output = F.softmax(contrast, dim=1)
return output
class DSoftmaxLoss(nn.Module):
def __init__(self, d, maxClass):
super(DSoftmaxLoss, self).__init__()
self.epsilon = torch.exp(d).cuda()
self.maxClass = maxClass
self.key2idx = torch.empty(self.maxClass,dtype=torch.long).cuda()
def forward(self, distances, labels, proto_net):
labels = torch.argmax(labels, dim=1, keepdim=True)
proto_keys = list(proto_net.memory.prototypes.keys())
#import pdb; pdb.set_trace()
self.key2idx[proto_keys] = torch.arange(len(proto_keys)).cuda()
intra_dist = distances.gather(1,self.key2idx[labels[:,0]].view(-1,1))
#import pdb; pdb.set_trace()
nb_classes = distances.shape[1]
inter_dist = distances[torch.ones_like(distances).scatter_(1, labels, 0.).bool()].view(-1,nb_classes-1)
#inter_dist = distances.gather(1, self.key2idx[inter_labels])
intra_loss = torch.log(1 + torch.div(self.epsilon, torch.exp(-intra_dist)))
inter_loss = torch.log(1 + torch.sum(torch.exp(-inter_dist), dim=1, keepdim=True))
loss = intra_loss + inter_loss
return loss.mean()