-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
302 lines (247 loc) · 10.1 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
301
import torch
import torch.nn as nn
from torchvision import models
class Identity(nn.Module):
def __init__(self, out_features):
super(Identity, self).__init__()
self.out_features = out_features
def forward(self, x):
return x
class MobileNet(nn.Module):
""" Pretrained mobilenet_v2. """
def __init__(self, model_name, num_classes, pretrained):
"""
Description
-------------
Initialize model
Parameters
-------------
model_name : string, name of the model
num_classes : int, number of classes
pretrained : boolean, whether the backbone is pretrained
"""
super(MobileNet, self).__init__()
self.model_name = model_name
# build model
self.backbone = models.mobilenet_v2(pretrained=pretrained)
self.backbone.classifier[1] = nn.Linear(self.backbone.classifier[1].in_features, num_classes)
def forward(self, input):
"""
Description
-------------
Forward pass
Parameters
-------------
input : tensor of shape (batch_size, c, w, h)
"""
x = self.backbone(input)
return x
class MobileNetLSTM(nn.Module):
""" Model made of pretrained backbone mobilenet_v2 + lstm. """
def __init__(self, model_name, num_classes, pretrained, num_layers_lstm = 1, bidirectional = False, hidden_size_lstm = 32, skip_lstm = False):
"""
Description
-------------
Initialize model
Parameters
-------------
model_name : string, name of the model
num_classes : int, number of classes
pretrained : boolean, whether the backbone is pretrained
num_layers_lstm : int, number of layers in lstm
bidirectional : boolean, whether to make the lstm bidirectional
hidden_size_lstm : int, hidden size of lstm
skip_lstm : whether to skip the lstm (for cnn finetuning)
"""
super(MobileNetLSTM, self).__init__()
self.model_name = model_name
self.num_layers_lstm = num_layers_lstm
self.hidden_size_lstm = hidden_size_lstm
self.skip_lstm = skip_lstm
# build model
self.backbone = models.mobilenet_v2(pretrained=pretrained)
self.backbone.classifier[1] = Identity(self.backbone.classifier[1].in_features) # convert classifier to identity
self.lstm = nn.LSTM(input_size=self.backbone.classifier[1].out_features, hidden_size=hidden_size_lstm, num_layers=num_layers_lstm, batch_first = True, bidirectional=bidirectional)
self.fc_finetuning = nn.Linear(self.backbone.classifier[1].out_features, num_classes)
self.fc_lstm = nn.Linear(hidden_size_lstm + bidirectional * hidden_size_lstm, num_classes) # output size is twice as large if lstm is bidirectional
def freeze_backbone(self):
""" Freeze all parameters of backbone. """
for param in self.backbone.parameters():
param.requires_grad = False
for param in self.fc_finetuning.parameters():
param.requires_grad = False
def unfreeze_backbone(self):
""" Unfreeze all parameters of backbone. """
for param in self.backbone.parameters():
param.requires_grad = True
for param in self.fc_finetuning.parameters():
param.requires_grad = True
def freeze_lstm(self):
""" Freeze all parameters of LSTM. """
for param in self.lstm.parameters():
param.requires_grad = False
for param in self.fc_lstm.parameters():
param.requires_grad = False
def unfreeze_lstm(self):
""" Unfreeze all parameters of LSTM. """
for param in self.lstm.parameters():
param.requires_grad = True
for param in self.fc_lstm.parameters():
param.requires_grad = True
def forward(self, input):
"""
Description
-------------
Forward pass
Parameters
-------------
input : tensor of shape (temporal length, c, w, h) if skip_lstm is True else (batch_size, c, w, h)
"""
x = self.backbone(input)
if not self.skip_lstm:
out, hidden = self.lstm(x[None, :], None) # None is because it expects a batch dim
x = nn.functional.relu(out[0])
x = self.fc_lstm(x)
else:
x = self.fc_finetuning(x)
return x
class MobileNetFC(nn.Module):
""" Model made of pretrained backbone mobilenet_v2 + fc for temporal treatment. """
def __init__(self, model_name, num_classes, pretrained, skip_temp_fc):
"""
Description
-------------
Initialize model
Parameters
-------------
model_name : string, name of the model
num_classes : int, number of classes
pretrained : boolean, whether the backbone is pretrained
skip_temp_fc : whether to skip the fc for temporal treatment (for cnn finetuning)
"""
super(MobileNetFC, self).__init__()
self.model_name = model_name
self.skip_temp_fc = skip_temp_fc
# build model
self.backbone = models.mobilenet_v2(pretrained=pretrained)
self.backbone.classifier[1] = Identity(self.backbone.classifier[1].in_features)
self.fc = nn.Linear(self.backbone.classifier[1].out_features, num_classes)
self.temp_fc = nn.Linear(num_classes, num_classes)
def freeze_backbone(self):
""" Freeze all parameters of backbone. """
for param in self.backbone.parameters():
param.requires_grad = False
def unfreeze_backbone(self):
""" Unfreeze all parameters of backbone. """
for param in self.backbone.parameters():
param.requires_grad = True
def freeze_fc(self):
""" Freeze all parameters of backbone. """
for param in self.fc.parameters():
param.requires_grad = False
def unfreeze_fc(self):
""" Unfreeze all parameters of backbone. """
for param in self.fc.parameters():
param.requires_grad = True
def freeze_temp_fc(self):
""" Freeze all parameters of temporal FC. """
for param in self.temp_fc.parameters():
param.requires_grad = False
def unfreeze_temp_fc(self):
""" Unfreeze all parameters of temporal FC. """
for param in self.temp_fc.parameters():
param.requires_grad = True
def forward(self, input):
"""
Description
-------------
Forward pass
Parameters
-------------
input : tensor of shape (temporal length, c, w, h) if skip_temp_fc is True else (batch_size, c, w, h)
"""
x = self.backbone(input)
x = self.fc(x)
if not self.skip_temp_fc:
# dim of x is (temporal length, num_classes)
x = x[None, :]
# add one batch size dimension
x = self.temp_fc(x)
# remove batch dim
x = x[0]
return x
class MobileNetStage(nn.Module):
""" Model made of backbone mobilenet_v2 + parallel fc for stage treatment. """
def __init__(self, model_name, num_classes, pretrained, num_stages):
"""
Description
-------------
Initialize Hernitia model
Parameters
-------------
model_name : string, name of the model
num_classes : int, number of classes
pretrained : boolean, whether the backbone is pretrained
num_stages : int, number of stages for the operation to consider (e.g. if 20 stages, the operation duration is divided into 20 intervals)
"""
super(MobileNetStage, self).__init__()
self.model_name = model_name
# build model
self.backbone = models.mobilenet_v2(pretrained=pretrained)
self.backbone.classifier[1] = Identity(self.backbone.classifier[1].in_features)
self.fc11 = nn.Linear(self.backbone.classifier[1].out_features, 32)
self.fc12 = nn.Linear(num_stages, 32)
self.fc2 = nn.Linear(32 + 32, num_classes)
def freeze_backbone(self):
""" Freeze all parameters of backbone. """
for param in self.backbone.parameters():
param.requires_grad = False
def unfreeze_backbone(self):
""" Unfreeze all parameters of backbone. """
for param in self.backbone.parameters():
param.requires_grad = True
def freeze_fc11(self):
""" Freeze all parameters of fc11. """
for param in self.fc11.parameters():
param.requires_grad = False
def unfreeze_fc11(self):
""" Unfreeze all parameters of fc11. """
for param in self.fc11.parameters():
param.requires_grad = True
def freeze_fc12(self):
""" Freeze all parameters of fc12. """
for param in self.fc12.parameters():
param.requires_grad = False
def unfreeze_fc12(self):
""" Unfreeze all parameters of fc12. """
for param in self.fc12.parameters():
param.requires_grad = True
def freeze_fc2(self):
""" Freeze all parameters of fc2. """
for param in self.fc2.parameters():
param.requires_grad = False
def unfreeze_fc2(self):
""" Unfreeze all parameters of fc2. """
for param in self.fc2.parameters():
param.requires_grad = True
def forward(self, input):
"""
Description
-------------
Forward pass
Parameters
-------------
input : tuple (tensor : (batch_size, c, w, h), tensor (batch_size, 50))
"""
# frames treatment
x11 = self.backbone(input[0])
x11 = self.fc11(x11)
x11 = torch.nn.ReLU()(x11)
# ratio treatment
x12 = self.fc12(input[1])
x12 = torch.nn.ReLU()(x12)
# concat
x2 = torch.cat([x11, x12], dim=1)
# through last linear layer
x = self.fc2(x2)
return x