-
Notifications
You must be signed in to change notification settings - Fork 178
/
NCEAverage.py
204 lines (172 loc) · 7.71 KB
/
NCEAverage.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import torch
from torch import nn
from .alias_multinomial import AliasMethod
import math
class NCEAverage(nn.Module):
def __init__(self, inputSize, outputSize, K, T=0.07, momentum=0.5, use_softmax=False):
super(NCEAverage, self).__init__()
self.nLem = outputSize
self.unigrams = torch.ones(self.nLem)
self.multinomial = AliasMethod(self.unigrams)
self.multinomial.cuda()
self.K = K
self.use_softmax = use_softmax
self.register_buffer('params', torch.tensor([K, T, -1, -1, momentum]))
stdv = 1. / math.sqrt(inputSize / 3)
self.register_buffer('memory_l', torch.rand(outputSize, inputSize).mul_(2 * stdv).add_(-stdv))
self.register_buffer('memory_ab', torch.rand(outputSize, inputSize).mul_(2 * stdv).add_(-stdv))
def forward(self, l, ab, y, idx=None):
K = int(self.params[0].item())
T = self.params[1].item()
Z_l = self.params[2].item()
Z_ab = self.params[3].item()
momentum = self.params[4].item()
batchSize = l.size(0)
outputSize = self.memory_l.size(0)
inputSize = self.memory_l.size(1)
# score computation
if idx is None:
idx = self.multinomial.draw(batchSize * (self.K + 1)).view(batchSize, -1)
idx.select(1, 0).copy_(y.data)
# sample
weight_l = torch.index_select(self.memory_l, 0, idx.view(-1)).detach()
weight_l = weight_l.view(batchSize, K + 1, inputSize)
out_ab = torch.bmm(weight_l, ab.view(batchSize, inputSize, 1))
# sample
weight_ab = torch.index_select(self.memory_ab, 0, idx.view(-1)).detach()
weight_ab = weight_ab.view(batchSize, K + 1, inputSize)
out_l = torch.bmm(weight_ab, l.view(batchSize, inputSize, 1))
if self.use_softmax:
out_ab = torch.div(out_ab, T)
out_l = torch.div(out_l, T)
out_l = out_l.contiguous()
out_ab = out_ab.contiguous()
else:
out_ab = torch.exp(torch.div(out_ab, T))
out_l = torch.exp(torch.div(out_l, T))
# set Z_0 if haven't been set yet,
# Z_0 is used as a constant approximation of Z, to scale the probs
if Z_l < 0:
self.params[2] = out_l.mean() * outputSize
Z_l = self.params[2].clone().detach().item()
print("normalization constant Z_l is set to {:.1f}".format(Z_l))
if Z_ab < 0:
self.params[3] = out_ab.mean() * outputSize
Z_ab = self.params[3].clone().detach().item()
print("normalization constant Z_ab is set to {:.1f}".format(Z_ab))
# compute out_l, out_ab
out_l = torch.div(out_l, Z_l).contiguous()
out_ab = torch.div(out_ab, Z_ab).contiguous()
# # update memory
with torch.no_grad():
l_pos = torch.index_select(self.memory_l, 0, y.view(-1))
l_pos.mul_(momentum)
l_pos.add_(torch.mul(l, 1 - momentum))
l_norm = l_pos.pow(2).sum(1, keepdim=True).pow(0.5)
updated_l = l_pos.div(l_norm)
self.memory_l.index_copy_(0, y, updated_l)
ab_pos = torch.index_select(self.memory_ab, 0, y.view(-1))
ab_pos.mul_(momentum)
ab_pos.add_(torch.mul(ab, 1 - momentum))
ab_norm = ab_pos.pow(2).sum(1, keepdim=True).pow(0.5)
updated_ab = ab_pos.div(ab_norm)
self.memory_ab.index_copy_(0, y, updated_ab)
return out_l, out_ab
# =========================
# InsDis and MoCo
# =========================
class MemoryInsDis(nn.Module):
"""Memory bank with instance discrimination"""
def __init__(self, inputSize, outputSize, K, T=0.07, momentum=0.5, use_softmax=False):
super(MemoryInsDis, self).__init__()
self.nLem = outputSize
self.unigrams = torch.ones(self.nLem)
self.multinomial = AliasMethod(self.unigrams)
self.multinomial.cuda()
self.K = K
self.use_softmax = use_softmax
self.register_buffer('params', torch.tensor([K, T, -1, momentum]))
stdv = 1. / math.sqrt(inputSize / 3)
self.register_buffer('memory', torch.rand(outputSize, inputSize).mul_(2 * stdv).add_(-stdv))
def forward(self, x, y, idx=None):
K = int(self.params[0].item())
T = self.params[1].item()
Z = self.params[2].item()
momentum = self.params[3].item()
batchSize = x.size(0)
outputSize = self.memory.size(0)
inputSize = self.memory.size(1)
# score computation
if idx is None:
idx = self.multinomial.draw(batchSize * (self.K + 1)).view(batchSize, -1)
idx.select(1, 0).copy_(y.data)
# sample
weight = torch.index_select(self.memory, 0, idx.view(-1))
weight = weight.view(batchSize, K + 1, inputSize)
out = torch.bmm(weight, x.view(batchSize, inputSize, 1))
if self.use_softmax:
out = torch.div(out, T)
out = out.squeeze().contiguous()
else:
out = torch.exp(torch.div(out, T))
if Z < 0:
self.params[2] = out.mean() * outputSize
Z = self.params[2].clone().detach().item()
print("normalization constant Z is set to {:.1f}".format(Z))
# compute the out
out = torch.div(out, Z).squeeze().contiguous()
# # update memory
with torch.no_grad():
weight_pos = torch.index_select(self.memory, 0, y.view(-1))
weight_pos.mul_(momentum)
weight_pos.add_(torch.mul(x, 1 - momentum))
weight_norm = weight_pos.pow(2).sum(1, keepdim=True).pow(0.5)
updated_weight = weight_pos.div(weight_norm)
self.memory.index_copy_(0, y, updated_weight)
return out
class MemoryMoCo(nn.Module):
"""Fixed-size queue with momentum encoder"""
def __init__(self, inputSize, outputSize, K, T=0.07, use_softmax=False):
super(MemoryMoCo, self).__init__()
self.outputSize = outputSize
self.inputSize = inputSize
self.queueSize = K
self.T = T
self.index = 0
self.use_softmax = use_softmax
self.register_buffer('params', torch.tensor([-1]))
stdv = 1. / math.sqrt(inputSize / 3)
self.register_buffer('memory', torch.rand(self.queueSize, inputSize).mul_(2 * stdv).add_(-stdv))
print('using queue shape: ({},{})'.format(self.queueSize, inputSize))
def forward(self, q, k):
batchSize = q.shape[0]
k = k.detach()
Z = self.params[0].item()
# pos logit
l_pos = torch.bmm(q.view(batchSize, 1, -1), k.view(batchSize, -1, 1))
l_pos = l_pos.view(batchSize, 1)
# neg logit
queue = self.memory.clone()
l_neg = torch.mm(queue.detach(), q.transpose(1, 0))
l_neg = l_neg.transpose(0, 1)
out = torch.cat((l_pos, l_neg), dim=1)
if self.use_softmax:
out = torch.div(out, self.T)
out = out.squeeze().contiguous()
else:
out = torch.exp(torch.div(out, self.T))
if Z < 0:
self.params[0] = out.mean() * self.outputSize
Z = self.params[0].clone().detach().item()
print("normalization constant Z is set to {:.1f}".format(Z))
# compute the out
out = torch.div(out, Z).squeeze().contiguous()
# # update memory
with torch.no_grad():
out_ids = torch.arange(batchSize).cuda()
out_ids += self.index
out_ids = torch.fmod(out_ids, self.queueSize)
out_ids = out_ids.long()
self.memory.index_copy_(0, out_ids, k)
self.index = (self.index + batchSize) % self.queueSize
return out