-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_sphere.py
309 lines (254 loc) · 10.8 KB
/
net_sphere.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'''
code version 1.0 by hjc (from nju to ucas)
'''
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch.nn import Parameter
import math
def myphi(x,m):
x = x * m
return 1-x**2/math.factorial(2)+x**4/math.factorial(4)-x**6/math.factorial(6) + \
x**8/math.factorial(8) - x**9/math.factorial(9)
import math
import torch
from torch import nn
from scipy.special import binom
class LSoftmaxLinear(nn.Module):
def __init__(self, input_features, output_features, margin=4, device='cuda'):
super().__init__()
self.input_dim = input_features # number of input feature i.e. output of the last fc layer
self.output_dim = output_features # number of output = class numbers
self.margin = margin # m
self.beta = 100
self.beta_min = 0
self.scale = 0.99
# self.device = device # gpu or cpu
use_cuda = not False and torch.cuda.is_available()
self.device = torch.device("cuda" if use_cuda else "cpu")
# Initialize L-Softmax parameters
self.weight = nn.Parameter(torch.FloatTensor(input_features, output_features))
self.divisor = math.pi / self.margin # pi/m
self.C_m_2n = torch.Tensor(binom(margin, range(0, margin + 1, 2))).to(device) # C_m{2n}
self.cos_powers = torch.Tensor(range(self.margin, -1, -2)).to(device) # m - 2n
self.sin2_powers = torch.Tensor(range(len(self.cos_powers))).to(device) # n
self.signs = torch.ones(margin // 2 + 1).to(device) # 1, -1, 1, -1, ...
self.signs[1::2] = -1
def calculate_cos_m_theta(self, cos_theta):
sin2_theta = 1 - cos_theta**2
cos_terms = cos_theta.unsqueeze(1) ** self.cos_powers.unsqueeze(0) # cos^{m - 2n}
sin2_terms = (sin2_theta.unsqueeze(1) # sin2^{n}
** self.sin2_powers.unsqueeze(0))
cos_m_theta = (self.signs.unsqueeze(0) * # -1^{n} * C_m{2n} * cos^{m - 2n} * sin2^{n}
self.C_m_2n.unsqueeze(0) *
cos_terms *
sin2_terms).sum(1) # summation of all terms
return cos_m_theta
def reset_parameters(self):
nn.init.kaiming_normal_(self.weight.data.t())
def find_k(self, cos):
# to account for acos numerical errors
eps = 1e-7
cos = torch.clamp(cos, -1 + eps, 1 - eps)
acos = cos.acos()
k = (acos / self.divisor).floor().detach()
return k
def forward(self, input, target=None):
a = 0
if self.training:
assert target is not None
x, w = input, self.weight
beta = max(self.beta, self.beta_min)
logit = x.mm(w)
indexes = range(logit.size(0))
logit_target = logit[indexes, target]
# cos(theta) = w * x / ||w||*||x||
w_target_norm = w[:, target].norm(p=2, dim=0)
x_norm = x.norm(p=2, dim=1)
cos_theta_target = logit_target / (w_target_norm * x_norm + 1e-10)
# equation 7
cos_m_theta_target = self.calculate_cos_m_theta(cos_theta_target)
# find k in equation 6
k = self.find_k(cos_theta_target)
# f_y_i
logit_target_updated = (w_target_norm *
x_norm *
(((-1) ** k * cos_m_theta_target) - 2 * k))
logit_target_updated_beta = (logit_target_updated + beta * logit[indexes, target]) / (1 + beta)
logit[indexes, target] = logit_target_updated_beta
self.beta *= self.scale
return logit
else:
assert target is None
return input.mm(self.weight)
class AngleLinear(nn.Module):
def __init__(self, in_features, out_features, m=4, phiflag=True):
super(AngleLinear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.Tensor(in_features, out_features))
self.weight.data.uniform_(-1, 1).renorm_(2, 1, 1e-5).mul_(1e5)
self.phiflag = phiflag
self.m = m
self.mlambda = [
lambda x: x**0,
lambda x: x**1,
lambda x: 2*x**2-1,
lambda x: 4*x**3-3*x,
lambda x: 8*x**4-8*x**2+1,
lambda x: 16*x**5-20*x**3+5*x
]
def forward(self, input):
x = input # size=(B,F) F is feature len (128*512)
w = self.weight # size=(F,Classnum) F=in_features Classnum=out_features
# w = 512*227
ww = w.renorm(2, 1, 1e-5).mul(1e5)
xlen = x.pow(2).sum(1).pow(0.5) # size=B
wlen = ww.pow(2).sum(0).pow(0.5) # size=Classnum
cos_theta = x.mm(ww) # size=(B,Classnum)
cos_theta = cos_theta / xlen.view(-1, 1) / wlen.view(1, -1)
cos_theta = cos_theta.clamp(-1, 1)
if self.phiflag:
cos_m_theta = self.mlambda[self.m](cos_theta)
theta = Variable(cos_theta.data.acos())
k = (self.m*theta/3.14159265).floor()
n_one = k*0.0 - 1
phi_theta = (n_one**k) * cos_m_theta - 2*k
else:
theta = cos_theta.acos()
phi_theta = myphi(theta, self.m)
phi_theta = phi_theta.clamp(-1*self.m, 1)
cos_theta = cos_theta * xlen.view(-1, 1)
phi_theta = phi_theta * xlen.view(-1, 1)
output = (cos_theta, phi_theta)
return output # size=(B,Classnum,2)
class AngleLoss(nn.Module):
def __init__(self, gamma=0):
super(AngleLoss, self).__init__()
self.gamma = gamma
self.it = 0
self.LambdaMin = 5.0
self.LambdaMax = 1500.0
self.lamb = 1500.0
def forward(self, input, target):
self.it += 1
cos_theta, phi_theta = input
target = target.view(-1, 1) # size=(B,1)
index = cos_theta.data * 0.0 # size=(B, Classnum)
index.scatter_(1, target.data.view(-1,1), 1)
index = index.byte()
index = Variable(index)
index=index.bool()
self.lamb = max(self.LambdaMin, self.LambdaMax/(1+0.1*self.it))
output = cos_theta * 1.0 # size=(B,Classnum)
output[index] -= cos_theta[index]*(1.0+0)/(1+self.lamb)
output[index] += phi_theta[index]*(1.0+0)/(1+self.lamb)
logpt = F.log_softmax(output,dim=1)
logpt = logpt.gather(1, target)
logpt = logpt.view(-1)
pt = Variable(logpt.data.exp())
loss = -1 * (1-pt)**self.gamma * logpt
loss = loss.mean()
return loss
# class STN(nn.Module):
# def __init__(self ):
# super(STN, self).__init__()
# self.localization = nn.Sequential(
# nn.Conv2d(3, 8, kernel_size=7),
# nn.MaxPool2d(2, stride=2),
# nn.ReLU(True),
# nn.Conv2d(8, 10, kernel_size=5),
# nn.MaxPool2d(2, stride=2),
# nn.ReLU(True)
# )
# self.fc_loc = nn.Sequential(
# nn.Linear(10*24*20, 32),
# nn.ReLU(True),
# nn.Linear(32, 3 * 2)
# )
#
# # Initialize the weights/bias with identity transformation
# # self.fc_loc[2].weight.data.zero_()
# # self.fc_loc[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))
#
# def forward(self, x):
# xs = self.localization(x)
# xs = xs.view(-1, 10*24*20)
# theta = self.fc_loc(xs)
# theta = theta.view(-1, 2, 3)
#
# grid = F.affine_grid(theta, x.size())
# x = F.grid_sample(x, grid)
#
# return x
class sphere20a(nn.Module):
def __init__(self, classnum=10574, feature=False):
# classnum = dataloader.dataset.class_num = 227
super(sphere20a, self).__init__()
self.classnum = classnum
self.feature = feature
# input = B*3*112*96
self.conv1_1 = nn.Conv2d(3, 64, 3, 2, 1) # =>B*64*56*48
self.relu1_1 = nn.PReLU(64)
self.conv1_2 = nn.Conv2d(64, 64, 3, 1, 1)
self.relu1_2 = nn.PReLU(64)
self.conv1_3 = nn.Conv2d(64, 64, 3, 1, 1)
self.relu1_3 = nn.PReLU(64)
self.conv2_1 = nn.Conv2d(64, 128, 3, 2, 1) # =>B*128*28*24
self.relu2_1 = nn.PReLU(128)
self.conv2_2 = nn.Conv2d(128, 128, 3, 1, 1)
self.relu2_2 = nn.PReLU(128)
self.conv2_3 = nn.Conv2d(128, 128, 3, 1, 1)
self.relu2_3 = nn.PReLU(128)
self.conv2_4 = nn.Conv2d(128, 128, 3, 1, 1) # =>B*128*28*24
self.relu2_4 = nn.PReLU(128)
self.conv2_5 = nn.Conv2d(128, 128, 3, 1, 1)
self.relu2_5 = nn.PReLU(128)
self.conv3_1 = nn.Conv2d(128, 256, 3, 2, 1) # =>B*256*14*12
self.relu3_1 = nn.PReLU(256)
self.conv3_2 = nn.Conv2d(256, 256, 3, 1, 1)
self.relu3_2 = nn.PReLU(256)
self.conv3_3 = nn.Conv2d(256, 256, 3, 1, 1)
self.relu3_3 = nn.PReLU(256)
self.conv3_4 = nn.Conv2d(256, 256, 3, 1, 1) # =>B*256*14*12
self.relu3_4 = nn.PReLU(256)
self.conv3_5 = nn.Conv2d(256, 256, 3, 1, 1)
self.relu3_5 = nn.PReLU(256)
self.conv3_6 = nn.Conv2d(256, 256, 3, 1, 1) # =>B*256*14*12
self.relu3_6 = nn.PReLU(256)
self.conv3_7 = nn.Conv2d(256, 256, 3, 1, 1)
self.relu3_7 = nn.PReLU(256)
self.conv3_8 = nn.Conv2d(256, 256, 3, 1, 1) # =>B*256*14*12
self.relu3_8 = nn.PReLU(256)
self.conv3_9 = nn.Conv2d(256, 256, 3, 1, 1)
self.relu3_9 = nn.PReLU(256)
self.conv4_1 = nn.Conv2d(256, 512, 3, 2, 1) # =>B*512*7*6
self.relu4_1 = nn.PReLU(512)
self.conv4_2 = nn.Conv2d(512, 512, 3, 1, 1)
self.relu4_2 = nn.PReLU(512)
self.conv4_3 = nn.Conv2d(512, 512, 3, 1, 1)
self.relu4_3 = nn.PReLU(512)
self.fc5 = nn.Linear(512*7*6, 512)
self.fc6 = AngleLinear(512, self.classnum)
# self.stn = STN()
def forward(self, x, target=None):
# x = self.stn(x)
x = self.relu1_1(self.conv1_1(x))
x = x + self.relu1_3(self.conv1_3(self.relu1_2(self.conv1_2(x))))
x = self.relu2_1(self.conv2_1(x))
x = x + self.relu2_3(self.conv2_3(self.relu2_2(self.conv2_2(x))))
x = x + self.relu2_5(self.conv2_5(self.relu2_4(self.conv2_4(x))))
x = self.relu3_1(self.conv3_1(x))
x = x + self.relu3_3(self.conv3_3(self.relu3_2(self.conv3_2(x))))
x = x + self.relu3_5(self.conv3_5(self.relu3_4(self.conv3_4(x))))
x = x + self.relu3_7(self.conv3_7(self.relu3_6(self.conv3_6(x))))
x = x + self.relu3_9(self.conv3_9(self.relu3_8(self.conv3_8(x))))
x = self.relu4_1(self.conv4_1(x))
x = x + self.relu4_3(self.conv4_3(self.relu4_2(self.conv4_2(x))))
x = x.view(x.size(0), -1)
x = self.fc5(x) # 128*512
if self.feature: # self.feature=False
return x
x = self.fc6(x)
return x