-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
300 lines (266 loc) · 9.08 KB
/
models.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
"""
Implementation of models in paper:
MVTec,
Uninformed Students: Student–Teacher Anomaly Detection with Discriminative Latent Embeddings.
CVPR, 2020.
Author: Luyao Chen
Date: 2020.10
"""
import torch
import numpy as np
from torch import nn
from fast_dense_feature_extractor import *
class _Teacher17(nn.Module):
"""
T^ net for patch size 17.
"""
def __init__(self):
super(_Teacher17, self).__init__()
self.net = nn.Sequential(
# Input n*3*17*17
# ???? kernel_size=5????
nn.Conv2d(3, 128, kernel_size=6, stride=1),
nn.LeakyReLU(5e-3),
# n*128*12*12
nn.Conv2d(128, 256, kernel_size=5, stride=1),
nn.LeakyReLU(5e-3),
# n*256*8*8
nn.Conv2d(256, 256, kernel_size=5, stride=1),
nn.LeakyReLU(5e-3),
# n*256*4*4
nn.Conv2d(256, 128, kernel_size=4, stride=1),
# n*128*1*1
)
self.decode = nn.Linear(128, 512)
# nn.Sequential(
# # nn.LeakyReLU(5e-3),
# # # n*128*1*1
# # nn.Conv2d(128, 512, kernel_size=1, stride=1),
# # output n*512*1*1
# )
def forward(self, x):
x = self.net(x)
x = x.view(-1, 128)
x = self.decode(x)
return x
class _Teacher33(nn.Module):
"""
T^ net for patch size 33.
"""
def __init__(self):
super(_Teacher33, self).__init__()
self.net = nn.Sequential(
# Input n*3*33*33
nn.Conv2d(3, 128, kernel_size=3, stride=1),
# nn.BatchNorm2d(128),
nn.LeakyReLU(5e-3),
# n*128*29*29
nn.MaxPool2d(kernel_size=2, stride=2),
# n*128*14*14
nn.Conv2d(128, 256, kernel_size=5, stride=1),
# nn.BatchNorm2d(256),
nn.LeakyReLU(5e-3),
# n*256*10*10
nn.MaxPool2d(kernel_size=2, stride=2),
# n*256*5*5
nn.Conv2d(256, 256, kernel_size=2, stride=1),
# nn.BatchNorm2d(256),
nn.LeakyReLU(5e-3),
# n*256*4*4
nn.Conv2d(256, 128, kernel_size=4, stride=1),
# n*128*1*1
)
self.decode = nn.Linear(128, 512)
def forward(self, x):
x = self.net(x)
x = x.view(-1, 128)
x = self.decode(x)
return x
class _Teacher65(nn.Module):
"""
T^ net for patch size 65.
"""
def __init__(self):
super(_Teacher65, self).__init__()
self.net = nn.Sequential(
# Input n*3*65*65
nn.Conv2d(3, 128, kernel_size=5, stride=1),
nn.LeakyReLU(5e-3),
# n*128*61*61
nn.MaxPool2d(kernel_size=2, stride=2),
# n*128*30*30
nn.Conv2d(128, 128, kernel_size=5, stride=1),
nn.LeakyReLU(5e-3),
# n*128*26*26
nn.MaxPool2d(kernel_size=2, stride=2),
# n*128*13*13
nn.Conv2d(128, 128, kernel_size=5, stride=1),
nn.LeakyReLU(5e-3),
# n*128*9*9
nn.MaxPool2d(kernel_size=2, stride=2),
# n*256*4*4
nn.Conv2d(128, 256, kernel_size=4, stride=1),
nn.LeakyReLU(5e-3),
# n*256*1*1
# ???? kernel_size=3????
nn.Conv2d(256, 128, kernel_size=1, stride=1),
# n*128*1*1
)
self.decode = nn.Linear(128, 512)
def forward(self, x):
x = self.net(x)
x = x.view(-1, 128)
x = self.decode(x)
return x
class Teacher17(nn.Module):
"""
Teacher network with patch size 17.
It has same architecture as T^17 because with no striding or pooling layers.
"""
def __init__(self, base_net: _Teacher17):
super(Teacher17, self).__init__()
self.multiPoolPrepare = multiPoolPrepare(17, 17)
self.net = base_net.net
def forward(self, x):
x = self.multiPoolPrepare(x)
x = self.net(x)
x = x.permute(0, 2, 3, 1)
return x
class Teacher33(nn.Module):
"""
Teacher network with patch size 33.
"""
def __init__(self, base_net: _Teacher33, imH, imW):
super(Teacher33, self).__init__()
self.imH = imH
self.imW = imW
self.sL1 = 2
self.sL2 = 2
# image height and width should be multiples of sL1∗sL2∗sL3...
# self.imW = int(np.ceil(imW / (self.sL1 * self.sL2)) * self.sL1 * self.sL2)
# self.imH = int(np.ceil(imH / (self.sL1 * self.sL2)) * self.sL1 * self.sL2)
assert imH % (self.sL1 * self.sL2) == 0, \
"image height should be multiples of (sL1∗sL2) which is " + \
str(self.sL1 * self.sL2)
assert imW % (self.sL1 * self.sL2) == 0, \
"image width should be multiples of (sL1∗sL2) which is " + \
str(self.sL1 * self.sL2)
self.outChans = base_net.net[-1].out_channels
self.net = nn.Sequential(
multiPoolPrepare(33, 33),
base_net.net[0],
base_net.net[1],
multiMaxPooling(self.sL1, self.sL1, self.sL1, self.sL1),
base_net.net[3],
base_net.net[4],
multiMaxPooling(self.sL2, self.sL2, self.sL2, self.sL2),
base_net.net[6],
base_net.net[7],
base_net.net[8],
unwrapPrepare(),
unwrapPool(self.outChans, imH / (self.sL1 * self.sL2),
imW / (self.sL1 * self.sL2), self.sL2, self.sL2),
unwrapPool(self.outChans, imH / self.sL1,
imW / self.sL1, self.sL1, self.sL1),
)
def forward(self, x):
x = self.net(x)
x = x.view(x.shape[0], self.imH, self.imW, -1)
x = x.permute(3, 1, 2, 0)
return x
class Teacher65(nn.Module):
"""
Teacher network with patch size 65.
"""
def __init__(self, base_net: _Teacher65, imH, imW):
super(Teacher65, self).__init__()
self.imH = imH
self.imW = imW
self.sL1 = 2
self.sL2 = 2
self.sL3 = 2
# image height and width should be multiples of sL1∗sL2∗sL3...
# self.imW = int(np.ceil(imW / (self.sL1 * self.sL2)) * self.sL1 * self.sL2)
# self.imH = int(np.ceil(imH / (self.sL1 * self.sL2)) * self.sL1 * self.sL2)
assert imH % (self.sL1 * self.sL2 * self.sL3) == 0, \
'image height should be multiples of (sL1∗sL2*sL3) which is ' + \
str(self.sL1 * self.sL2 * self.sL3) + '.'
assert imW % (self.sL1 * self.sL2 * self.sL3) == 0, \
'image width should be multiples of (sL1∗sL2*sL3) which is ' + \
str(self.sL1 * self.sL2 * self.sL3) + '.'
self.outChans = base_net.net[-1].out_channels
self.net = nn.Sequential(
multiPoolPrepare(65, 65),
base_net.net[0],
base_net.net[1],
multiMaxPooling(self.sL1, self.sL1, self.sL1, self.sL1),
base_net.net[3],
base_net.net[4],
multiMaxPooling(self.sL2, self.sL2, self.sL2, self.sL2),
base_net.net[6],
base_net.net[7],
multiMaxPooling(self.sL3, self.sL3, self.sL3, self.sL3),
base_net.net[9],
base_net.net[10],
base_net.net[11],
unwrapPrepare(),
unwrapPool(self.outChans, imH / (self.sL1 * self.sL2 * self.sL3),
imW / (self.sL1 * self.sL2 * self.sL3), self.sL3, self.sL3),
unwrapPool(self.outChans, imH / (self.sL1 * self.sL2),
imW / (self.sL1 * self.sL2), self.sL2, self.sL2),
unwrapPool(self.outChans, imH / self.sL1,
imW / self.sL1, self.sL1, self.sL1),
)
def forward(self, x):
x = self.net(x)
# print(x.shape)
x = x.view(x.shape[0], self.imH, self.imW, -1)
x = x.permute(3,1,2,0)
return x
def _Teacher(patch_size):
if patch_size == 17:
return _Teacher17()
if patch_size == 33:
return _Teacher33()
if patch_size == 65:
return _Teacher65()
else:
print('No implementation of net wiht patch_size: ' + str(patch_size))
return None
def TeacherOrStudent(patch_size, base_net, imH=None, imW=None):
if patch_size == 17:
return Teacher17(base_net)
if patch_size == 33:
if imH is None or imW is None:
print('imH and imW are necessary.')
return None
return Teacher33(base_net, imH, imW)
if patch_size == 65:
if imH is None or imW is None:
print('imH and imW are necessary.')
return None
return Teacher65(base_net, imH, imW)
else:
print('No implementation of net wiht patch_size: '+str(patch_size))
return None
if __name__ == "__main__":
model=multiPoolPrepare(17,17)
# net = _Teacher17()
imH = 256
imW = 256
#
# T = Teacher17(net)
# # T = Teacher33(net, imH, imW)
x = torch.ones((2, 3, imH, imW))
#
x_ = torch.ones((2, 3, 17, 17))
out=model(x)
print(out.shape)
#
# y = T(x)
# y_ = net(x_)
#
# # print(y)
# print(y.shape)
# print(y_.shape)
# # print(T)