-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
executable file
·299 lines (230 loc) · 8.73 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Build DNN models.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import pretrainedmodels
import pretrainedmodels.utils as utils
from fabulous.color import fg256
alexnet = pretrainedmodels.__dict__['alexnet'](num_classes=1000, pretrained=None).cuda()
resnet = pretrainedmodels.__dict__['resnet18'](num_classes=1000, pretrained=None).cuda()
class Encoder_Alex(nn.Module):
def __init__(self):
super(Encoder_Alex, self).__init__()
self.features = alexnet._features
def forward(self, x):
x = self.features(x)
return x
class Encoder_R18(nn.Module):
def __init__(self):
super(Encoder_R18, self).__init__()
self.conv1 = resnet.conv1
self.conv1 = resnet.conv1
self.bn1 = resnet.bn1
self.relu = resnet.relu
self.maxpool = resnet.maxpool
self.layer1 = resnet.layer1
self.layer2 = resnet.layer2
self.layer3 = resnet.layer3
self.layer4 = resnet.layer4
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
return x
class Regressor_Alex(nn.Module):
def __init__(self):
super(Regressor_Alex, self).__init__()
self.avgpool = alexnet.avgpool
# AlexNet-original
# self.lin0 = nn.Linear(9216, 1024)
# self.lin1 = nn.Linear(1024, 1024)
# self.lin2 = nn.Linear(1024, 1024)
# AlexNet-reduced
self.lin0 = nn.Linear(9216, 32)
self.lin1 = nn.Linear(32, 256)
self.relu0 = alexnet.relu0
self.relu1 = alexnet.relu1
self.drop0 = alexnet.dropout0
self.drop1 = alexnet.dropout0
self.va_regressor = nn.Linear(256, 2)
def forward(self, x):
x = torch.flatten(self.avgpool(x), 1)
x_btl_1 = self.relu0(self.lin0(self.drop0(x)))
x_btl_2 = self.relu1(self.lin1(self.drop1(x_btl_1)))
x_va = self.va_regressor(x_btl_2)
return x_va, x_btl_1
class Regressor_AL_Category(nn.Module):
def __init__(self):
super(Regressor_AL_Category, self).__init__()
self.avgpool = alexnet.avgpool
self.lin0 = nn.Linear(9216, 32)
self.lin1 = nn.Linear(32, 256)
self.relu0 = alexnet.relu0
self.relu1 = alexnet.relu1
self.drop0 = alexnet.dropout0
self.drop1 = alexnet.dropout0
self.va_regressor = nn.Linear(256, 7)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = torch.flatten(self.avgpool(x), 1)
x_btl_1 = self.relu0(self.lin0(self.drop0(x)))
x_btl_2 = self.relu1(self.lin1(self.drop1(x_btl_1)))
x_category = self.sigmoid(self.va_regressor(x_btl_2))
return x_category, x_btl_1
class Regressor_R18(nn.Module):
def __init__(self):
super(Regressor_R18, self).__init__()
self.avgpool = resnet.avgpool.cuda()
self.last_linear = resnet.last_linear.cuda()
self.lin0 = nn.Linear(1000, 32).cuda()
self.lin1 = nn.Linear(32, 256).cuda()
self.va_regressor = nn.Linear(256, 2).cuda()
def forward(self, x):
x = torch.flatten(self.avgpool(x), 1)
x = self.last_linear(x)
x_btl_1 = F.relu(self.lin0(F.dropout2d(x)))
x_btl_2 = F.relu(self.lin1(F.dropout2d(x_btl_1)))
x_va = self.va_regressor(x_btl_2)
return x_va, x_btl_1
class Regressor_R18_Category(nn.Module):
def __init__(self):
super(Regressor_R18_Category, self).__init__()
self.avgpool = resnet.avgpool.cuda()
self.last_linear = resnet.last_linear.cuda()
self.lin0 = nn.Linear(1000, 32).cuda()
self.lin1 = nn.Linear(32, 256).cuda()
self.va_regressor = nn.Linear(256, 7).cuda()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = torch.flatten(self.avgpool(x), 1)
x = self.last_linear(x)
x_btl_1 = F.relu(self.lin0(F.dropout2d(x)))
x_btl_2 = F.relu(self.lin1(F.dropout2d(x_btl_1)))
x_va = self.sigmoid(self.va_regressor(x_btl_2))
return x_va, x_btl_1
class Regressor_R50(nn.Module):
def __init__(self):
super(Regressor_R50, self).__init__()
self.lin0 = nn.Linear(1000, 32).cuda()
self.lin1 = nn.Linear(32, 256).cuda()
self.va_regressor = nn.Linear(256, 2).cuda()
def forward(self, x):
x_btl_1 = F.relu(self.lin0(F.dropout2d(x)))
x_btl_2 = F.relu(self.lin1(F.dropout2d(x_btl_1)))
x_va = self.va_regressor(x_btl_2)
return x_va, x_btl_1
class Regressor_R101(nn.Module):
def __init__(self):
super(Regressor_R101, self).__init__()
self.lin0 = nn.Linear(1000, 32).cuda()
self.lin1 = nn.Linear(32, 256).cuda()
self.va_regressor = nn.Linear(256, 2).cuda()
def forward(self, x):
x_btl_1 = F.relu(self.lin0(F.dropout2d(x)))
x_btl_2 = F.relu(self.lin1(F.dropout2d(x_btl_1)))
x_va = self.va_regressor(x_btl_2)
return x_va, x_btl_1
class Regressor_MMx(nn.Module):
def __init__(self):
super(Regressor_MMx, self).__init__()
self.avgpool = resnet.avgpool.cuda()
self.last_linear = resnet.last_linear.cuda()
self.lin0 = nn.Linear(64, 32).cuda()
self.lin1 = nn.Linear(32, 256).cuda()
self.va_regressor = nn.Linear(256, 2).cuda()
def forward(self, x):
x_btl_1 = F.relu(self.lin0(F.dropout2d(x)))
x_btl_2 = F.relu(self.lin1(F.dropout2d(x_btl_1)))
x_va = self.va_regressor(x_btl_2)
return x_va, x_btl_1
class SPRegressor_light(nn.Module):
def __init__(self, discrete_opt):
super(SPRegressor_light, self).__init__()
self.discrete_opt = discrete_opt
self.lin1 = nn.Linear(32, 256)
if self.discrete_opt:
self.lin2 = nn.Linear(256, 7)
else:
self.lin2 = nn.Linear(256, 2)
def forward(self, x):
x = F.relu(self.lin1(x))
if self.discrete_opt:
return self.lin2(x)
else:
return 0.5 * torch.tanh(self.lin2(x))
class Variational_regressor(nn.Module):
def __init__(self):
super(Variational_regressor, self).__init__()
self.lin1 = nn.Linear(32, 64)
self.lin2 = nn.Linear(64, 8)
def forward(self, x):
x = F.relu(self.lin1(x))
return F.relu(self.lin2(x))
class Linear_Prob(nn.Module):
def __init__(self, input_dim, output_dim):
super(Linear_Prob, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.linear1 = nn.Linear(self.input_dim, self.output_dim*10)
self.linear2 = nn.Linear(self.output_dim*10, self.output_dim)
self.bn1 = nn.BatchNorm1d(self.output_dim*10, affine=True)
self.layer_blocks = nn.Sequential(
self.linear1,
self.bn1,
nn.ReLU(inplace=True),
self.linear2,
)
def forward(self, inputs):
return self.layer_blocks(inputs)
def encoder_Alex():
encoder = Encoder_Alex()
return encoder
def encoder_R18():
encoder = Encoder_R18()
return encoder
def regressor_Alex():
regressor = Regressor_Alex()
return regressor
def regressor_R18():
regressor = Regressor_R18()
return regressor
def regressor_R50():
regressor = Regressor_R50()
return regressor
def regressor_R101():
regressor = Regressor_R101()
return regressor
def regressor_MMx():
regressor = Regressor_MMx()
return regressor
def regressor_AL_Category():
regressor = Regressor_AL_Category()
return regressor
def regressor_R18_Category():
regressor = Regressor_R18_Category()
return regressor
def spregressor(discrete_opt):
spregressor = SPRegressor_light(discrete_opt)
return spregressor
def vregressor():
vregressor = Variational_regressor()
return vregressor
def load_Linear_Prob(input_dim, output_dim):
return Linear_Prob(input_dim, output_dim)
if __name__ == "__main__":
from pytorch_model_summary import summary
print(fg256("yellow", summary(Encoder_Alex(), torch.ones_like(torch.empty(1, 3, 255, 255)).cuda(), show_input=True)))
print(fg256("cyan", summary(Encoder_R18(), torch.ones_like(torch.empty(1, 3, 255, 255)).cuda(), show_input=True)))
print(fg256("green", summary(Regressor_Alex(), torch.ones_like(torch.empty(1, 256, 6, 6)), show_input=True)))
print(fg256("orange", summary(Regressor_R18(), torch.ones_like(torch.empty(1, 512, 8, 8)).cuda(), show_input=True)))
print(fg256("yellow", summary(SPRegressor_light(), torch.ones_like(torch.empty(1, 32)), show_input=True)))
print(fg256("green", summary(Variational_regressor(), torch.ones_like(torch.empty(1, 32)), show_input=True)))