-
Notifications
You must be signed in to change notification settings - Fork 11
/
classify.py
281 lines (236 loc) · 9.59 KB
/
classify.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
# -*- coding: utf-8 -*-
import time
import torch
import numpy as np
import torch.nn as nn
import torchvision.models
import torch.nn.functional as F
from torch.nn.modules.loss import _Loss
import math, evolve
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class Mnist_CNN(nn.Module):
def __init__(self):
super(Mnist_CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(500, 50)
self.fc2 = nn.Linear(50, 5)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
res = self.fc2(x)
return [x, res]
class VGG16(nn.Module):
def __init__(self, n_classes):
super(VGG16, self).__init__()
model = torchvision.models.vgg16_bn(pretrained=True)
self.feature = model.features
self.feat_dim = 512 * 2 * 2
self.n_classes = n_classes
self.bn = nn.BatchNorm1d(self.feat_dim)
self.bn.bias.requires_grad_(False) # no shift
self.fc_layer = nn.Linear(self.feat_dim, self.n_classes)
def forward(self, x):
feature = self.feature(x)
feature = feature.view(feature.size(0), -1)
feature = self.bn(feature)
res = self.fc_layer(feature)
return [feature, res]
def predict(self, x):
feature = self.feature(x)
feature = feature.view(feature.size(0), -1)
feature = self.bn(feature)
res = self.fc_layer(feature)
out = F.softmax(res, dim=1)
return out
class VGG16_vib(nn.Module):
def __init__(self, n_classes):
super(VGG16_vib, self).__init__()
model = torchvision.models.vgg16_bn(pretrained=True)
self.feature = model.features
self.feat_dim = 512 * 2 * 2
self.k = self.feat_dim // 2
self.n_classes = n_classes
self.st_layer = nn.Linear(self.feat_dim, self.k * 2)
self.fc_layer = nn.Linear(self.k, self.n_classes)
def forward(self, x, mode="train"):
feature = self.feature(x)
feature = feature.view(feature.size(0), -1)
statis = self.st_layer(feature)
mu, std = statis[:, :self.k], statis[:, self.k:]
std = F.softplus(std-5, beta=1)
eps = torch.FloatTensor(std.size()).normal_().cuda()
res = mu + std * eps
out = self.fc_layer(res)
return [feature, out, mu, std]
def predict(self, x):
feature = self.feature(x)
feature = feature.view(feature.size(0), -1)
statis = self.st_layer(feature)
mu, std = statis[:, :self.k], statis[:, self.k:]
std = F.softplus(std-5, beta=1)
eps = torch.FloatTensor(std.size()).normal_().cuda()
res = mu + std * eps
out = self.fc_layer(res)
return out
class CrossEntropyLoss(_Loss):
def forward(self, out, gt, mode="reg"):
bs = out.size(0)
loss = - torch.mul(gt.float(), torch.log(out.float() + 1e-7))
if mode == "dp":
loss = torch.sum(loss, dim=1).view(-1)
else:
loss = torch.sum(loss) / bs
return loss
class BinaryLoss(_Loss):
def forward(self, out, gt):
bs = out.size(0)
loss = - (gt * torch.log(out.float()+1e-7) + (1-gt) * torch.log(1-out.float()+1e-7))
loss = torch.mean(loss)
return loss
class FaceNet(nn.Module):
def __init__(self, num_classes=1000):
super(FaceNet, self).__init__()
self.feature = evolve.IR_50_112((112, 112))
self.feat_dim = 512
self.num_classes = num_classes
self.fc_layer = nn.Linear(self.feat_dim, self.num_classes)
def predict(self, x):
feat = self.feature(x)
feat = feat.view(feat.size(0), -1)
out = self.fc_layer(feat)
return out
def forward(self, x):
# print("input shape:", x.shape)
# import pdb; pdb.set_trace()
feat = self.feature(x)
feat = feat.view(feat.size(0), -1)
out = self.fc_layer(feat)
return [feat, out]
class FaceNet64(nn.Module):
def __init__(self, num_classes = 1000):
super(FaceNet64, self).__init__()
self.feature = evolve.IR_50_64((64, 64))
self.feat_dim = 512
self.num_classes = num_classes
self.output_layer = nn.Sequential(nn.BatchNorm2d(512),
nn.Dropout(),
Flatten(),
nn.Linear(512 * 4 * 4, 512),
nn.BatchNorm1d(512))
self.fc_layer = nn.Linear(self.feat_dim, self.num_classes)
def forward(self, x):
feat = self.feature(x)
feat = self.output_layer(feat)
feat = feat.view(feat.size(0), -1)
out = self.fc_layer(feat)
__, iden = torch.max(out, dim=1)
iden = iden.view(-1, 1)
return feat, out
class IR152(nn.Module):
def __init__(self, num_classes=1000):
super(IR152, self).__init__()
self.feature = evolve.IR_152_64((64, 64))
self.feat_dim = 512
self.num_classes = num_classes
self.output_layer = nn.Sequential(nn.BatchNorm2d(512),
nn.Dropout(),
Flatten(),
nn.Linear(512 * 4 * 4, 512),
nn.BatchNorm1d(512))
self.fc_layer = nn.Linear(self.feat_dim, self.num_classes)
def forward(self, x):
feat = self.feature(x)
feat = self.output_layer(feat)
feat = feat.view(feat.size(0), -1)
out = self.fc_layer(feat)
return feat, out
class IR152_vib(nn.Module):
def __init__(self, num_classes=1000):
super(IR152_vib, self).__init__()
self.feature = evolve.IR_152_64((64, 64))
self.feat_dim = 512
self.k = self.feat_dim // 2
self.n_classes = num_classes
self.output_layer = nn.Sequential(nn.BatchNorm2d(512),
nn.Dropout(),
Flatten(),
nn.Linear(512 * 4 * 4, 512),
nn.BatchNorm1d(512))
self.st_layer = nn.Linear(self.feat_dim, self.k * 2)
self.fc_layer = nn.Sequential(
nn.Linear(self.k, self.n_classes),
nn.Softmax(dim = 1))
def forward(self, x):
feature = self.output_layer(self.feature(x))
feature = feature.view(feature.size(0), -1)
statis = self.st_layer(feature)
mu, std = statis[:, :self.k], statis[:, self.k:]
std = F.softplus(std-5, beta=1)
eps = torch.FloatTensor(std.size()).normal_().cuda()
res = mu + std * eps
out = self.fc_layer(res)
__, iden = torch.max(out, dim=1)
iden = iden.view(-1, 1)
return feature, out, iden, mu, st
class IR50(nn.Module):
def __init__(self, num_classes=1000):
super(IR50, self).__init__()
self.feature = evolve.IR_50_64((64, 64))
self.feat_dim = 512
self.num_classes = num_classes
self.output_layer = nn.Sequential(nn.BatchNorm2d(512),
nn.Dropout(),
Flatten(),
nn.Linear(512 * 4 * 4, 512),
nn.BatchNorm1d(512))
self.st_layer = nn.Linear(self.feat_dim, self.k * 2)
self.fc_layer = nn.Sequential(
nn.Linear(self.k, self.n_classes),
nn.Softmax(dim = 1))
def forward(self, x):
feature = self.output_layer(self.feature(x))
feature = feature.view(feature.size(0), -1)
statis = self.st_layer(feature)
mu, std = statis[:, :self.k], statis[:, self.k:]
std = F.softplus(std-5, beta=1)
eps = torch.FloatTensor(std.size()).normal_().cuda()
res = mu + std * eps
out = self.fc_layer(res)
__, iden = torch.max(out, dim=1)
iden = iden.view(-1, 1)
return feature, out, iden, mu, std
class IR50_vib(nn.Module):
def __init__(self, num_classes=1000):
super(IR50_vib, self).__init__()
self.feature = evolve.IR_50_64((64, 64))
self.feat_dim = 512
self.n_classes = num_classes
self.k = self.feat_dim // 2
self.output_layer = nn.Sequential(nn.BatchNorm2d(512),
nn.Dropout(),
Flatten(),
nn.Linear(512 * 4 * 4, 512),
nn.BatchNorm1d(512))
self.st_layer = nn.Linear(self.feat_dim, self.k * 2)
self.fc_layer = nn.Sequential(
nn.Linear(self.k, self.n_classes),
nn.Softmax(dim=1))
def forward(self, x):
feat = self.output_layer(self.feature(x))
feat = feat.view(feat.size(0), -1)
statis = self.st_layer(feat)
mu, std = statis[:, :self.k], statis[:, self.k:]
std = F.softplus(std-5, beta=1)
eps = torch.FloatTensor(std.size()).normal_().cuda()
res = mu + std * eps
out = self.fc_layer(res)
__, iden = torch.max(out, dim=1)
iden = iden.view(-1, 1)
return feat, out, iden, mu, std