-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
431 lines (387 loc) · 21.8 KB
/
model.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import os
import torch
from torch import nn
from models import consensus_module_2dcnn, consensus_module_3dcnn, consensus_module_ts
def generate_model_3d(opt):
assert opt.model in ['resnext', 'mobilenetv2', 'res3d_clstm_mn', 'raar3d', 'ni3d', 'EAN_16f']
from models.consensus_module_3dcnn import get_fine_tuning_parameters
if opt.model == 'mobilenetv2':
model = consensus_module_3dcnn.get_model(
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
width_mult=opt.width_mult,
net=opt.model,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr,
feat_fusion=opt.feat_fusion,
ssa_loss=opt.SSA_loss)
elif opt.model == 'resnext':
model = consensus_module_3dcnn.get_model(
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
shortcut_type=opt.resnet_shortcut,
cardinality=opt.resnext_cardinality,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration,
net=opt.model,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr,
feat_fusion=opt.feat_fusion,
ssa_loss=opt.SSA_loss)
elif opt.model == 'res3d_clstm_mn':
model = consensus_module_3dcnn.get_model(
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration,
net=opt.model,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr)
elif opt.model == 'raar3d':
# from models.res3d_clstm_mobilenet import get_fine_tuning_parameters
model = consensus_module_3dcnn.get_model(
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration,
net=opt.model,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr,
shallow_layer_num=opt.shallow_layer_num,
middle_layer_num=opt.middle_layer_num,
high_layer_num=opt.high_layer_num)
elif opt.model == 'ni3d':
# from models.res3d_clstm_mobilenet import get_fine_tuning_parameters
model = consensus_module_3dcnn.get_model(
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration,
net=opt.model,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr,
shallow_layer_num=opt.shallow_layer_num,
middle_layer_num=opt.middle_layer_num,
high_layer_num=opt.high_layer_num)
elif opt.model == 'EAN_16f':
# from models.res3d_clstm_mobilenet import get_fine_tuning_parameters
model = consensus_module_3dcnn.get_model(
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration,
net=opt.model,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr
)
if opt.gpu is not None:
model = model.cuda()
model = nn.DataParallel(model, device_ids=None)
'''
pytorch_total_params = sum(p.numel() for p in model.parameters() if
p.requires_grad)
print("Total number of trainable parameters: ", pytorch_total_params)
'''
if opt.pretrain_path:
if '.pth' in opt.pretrain_path:
print('loading pretrained model {}'.format(opt.pretrain_path))
pretrain = torch.load(opt.pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
if 'pretrained_models' in opt.pretrain_path:
# if len(opt.modalities) == 1:
# For model pretrained on Jester
state_dict = {key.replace('module.', 'module.cnns.0.'): value for key, value in pretrain['state_dict'].items()}
state_dict = {key.replace('module.cnns.0.fc', 'module.cnns.0.classifier'): value for key, value in state_dict.items()}
# For ex-novo model trained on ChaLearn
# state_dict = {key.replace('module.cnns.0.0.', 'module.cnns.0.'): value for key, value in pretrain['state_dict'].items()}
model.load_state_dict(state_dict)
else:
model.load_state_dict(pretrain['state_dict'])
else:
opt.pretrain_path = os.path.join(opt.pretrain_path, opt.dataset, opt.model)
for i in range(len(opt.modalities)):
pretrain_path = '_'.join([opt.dataset, opt.model, opt.modalities[i], 'none', 'best.pth'])
pretrain_path = os.path.join(opt.pretrain_path, pretrain_path)
print('loading pretrained model {}'.format(pretrain_path))
pretrain = torch.load(pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
# state_dict = {key.replace('module.', ''): value for key, value in pretrain['state_dict'].items()}
state_dict = {key.replace('module.cnns.0.', ''): value for key, value in pretrain['state_dict'].items()}
# state_dict = {key.replace('module.cnns.0.0.', ''): value for key, value in pretrain['state_dict'].items()}
model.module.cnns[i].load_state_dict(state_dict)
# model.module.cnns[i].load_state_dict(pretrain['state_dict'])
if opt.test or opt.ft_portion == 'none':
return model, model.parameters()
if opt.n_classes != opt.n_finetune_classes:
# change the output of the final output
if opt.mod_aggr == 'MLP':
model.module.aggregator = nn.Sequential(
# nn.Dropout(0.9),
nn.ReLU(),
nn.Linear(model.module.feat_dim, opt.n_finetune_classes))
model.module.aggregator = model.module.aggregator.cuda()
# change the output size of single cnn
for i in range(len(opt.modalities)):
if opt.model == 'mobilenetv2':
model.module.cnns[i].classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(model.module.cnns[i].classifier[1].in_features, opt.n_finetune_classes),
)
elif opt.model in ['resnext', 'res3d_clstm_mn']:
model.module.cnns[i].classifier = nn.Linear(model.module.cnns[i].classifier.in_features, opt.n_finetune_classes)
model.module.cnns[i].classifier.cuda()
parameters = get_fine_tuning_parameters(model, opt.ft_portion)
return model, parameters
else:
if opt.pretrain_path:
print('loading pretrained model {}'.format(opt.pretrain_path))
pretrain = torch.load(opt.pretrain_path)
assert opt.arch == pretrain['arch']
model.load_state_dict(pretrain['state_dict'])
if opt.model in ['mobilenetv2']:
model.module.classifier = nn.Sequential(
nn.Dropout(0.9),
nn.Linear(model.module.classifier[1].in_features, opt.n_finetune_classes)
)
elif opt.model in ['resnext']:
'''
model.module.classifier = nn.Sequential(
nn.Dropout(p=0.8),
nn.Linear(model.module.classifier.in_features, opt.n_finetune_classes))
'''
model.module.classifier = nn.Linear(model.module.classifier.in_features, opt.n_finetune_classes)
#'''
parameters = get_fine_tuning_parameters(model, opt.ft_begin_index)
return model, parameters
return model, model.parameters()
def generate_model_2d(opt):
assert opt.model in ['mobilenetv2_2d', 'resnext_2d']
if opt.model == 'mobilenetv2_2d':
from models.consensus_module_2dcnn import get_fine_tuning_parameters
model = consensus_module_2dcnn.get_model(
net=opt.model,
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr,
temp_aggr=opt.temp_aggr,
width_mult=opt.width_mult)
elif opt.model == 'resnext_2d':
assert opt.model_depth in [101]
from models.consensus_module_2dcnn import get_fine_tuning_parameters
if opt.model_depth == 101:
model = consensus_module_2dcnn.get_model(
net=opt.model,
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr,
temp_aggr=opt.temp_aggr,
groups=opt.groups,
width_per_group=opt.resnext_cardinality)
if opt.gpu is not None:
model = model.cuda()
model = nn.DataParallel(model, device_ids=None)
'''
pytorch_total_params = sum(p.numel() for p in model.parameters() if
p.requires_grad)
print("Total number of trainable parameters: ", pytorch_total_params)
'''
if opt.pretrain_path:
if '.pth' in opt.pretrain_path:
print('loading pretrained model {}'.format(opt.pretrain_path))
pretrain = torch.load(opt.pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
model.load_state_dict(pretrain['state_dict'])
else:
opt.pretrain_path = os.path.join(opt.pretrain_path, opt.dataset, opt.model)
for i in range(len(opt.modalities)):
pretrain_path = '_'.join([opt.dataset, opt.model, opt.modalities[i], opt.temp_aggr, 'none', 'best.pth'])
pretrain_path = os.path.join(opt.pretrain_path, pretrain_path)
print('loading pretrained model {}'.format(pretrain_path))
pretrain = torch.load(pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
# state_dict = {key.replace('module.', ''): value for key, value in pretrain['state_dict'].items()}
state_dict = {key.replace('module.mod_nets.0.', ''): value for key, value in pretrain['state_dict'].items()}
# state_dict = {key.replace('module.cnns.0.0.', ''): value for key, value in pretrain['state_dict'].items()}
# print('mode_nets lenght: {}'.format(len(model.module.mod_nets)))
model.module.mod_nets[i].load_state_dict(state_dict)
# model.module.mod_nets[i].load_state_dict(pretrain['state_dict'])
if opt.test or opt.ft_portion == 'none':
return model, model.parameters()
# change the output of the final output
if opt.temp_aggr == 'MLP':
model.module.temp_aggregator = nn.Sequential(
# nn.Dropout(0.9),
nn.ReLU(),
nn.Linear(opt.n_finetune_classes * opt.sample_duration, opt.n_finetune_classes))
model.module.temp_aggregator = model.module.temp_aggregator.cuda()
elif opt.temp_aggr == 'LSTM':
model.module.temp_aggregator = nn.LSTM(opt.n_finetune_classes, opt.n_finetune_classes, batch_first=False, bidirectional=True)
model.module.temp_aggregator = model.module.temp_aggregator.cuda()
'''
model.module.aggregator = nn.Sequential(
nn.LSTM(opt.n_finetune_classes, opt.n_finetune_classes)
'''
if opt.n_classes != opt.n_finetune_classes:
if opt.mod_aggr == 'MLP':
model.module.mod_aggregator = nn.Sequential(
# nn.Dropout(0.2),
nn.ReLU(),
nn.Linear(opt.n_finetune_classes * len(opt.modalities), self.n_finetune_classes)
)
model.module.mod_aggregator = model.module.mod_aggregator.cuda()
for i in range(len(opt.modalities)):
'''
# change the output size of single cnn
for j in range(opt.sample_duration):
model.module.mod_nets[i][j][0].classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(model.module.mod_nets[i][j][0].classifier[1].in_features, opt.n_finetune_classes),
)
# print('########## {}° network ##########\n{}################################'.format(i, model.module.cnns[i][0].classifier))
model.module.mod_nets[i][j][0].classifier.cuda()
# print('########## CNNs ##########\n{}################################'.format(model.module.cnns))
'''
model.module.mod_nets[i][0].classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(model.module.mod_nets[i][0].classifier[1].in_features, opt.n_finetune_classes),
)
'''
else:
model.module.aggregator = nn.Linear(model.module.aggregator.in_features, opt.n_finetune_classes)
model.module.aggregator = model.module.aggregator.cuda()
'''
parameters = get_fine_tuning_parameters(model, opt.ft_portion)
return model, parameters
else:
if opt.pretrain_path:
print('loading pretrained model {}'.format(opt.pretrain_path))
pretrain = torch.load(opt.pretrain_path)
assert opt.arch == pretrain['arch']
model.load_state_dict(pretrain['state_dict'])
if opt.test or opt.ft_portion == 'none':
return model, model.parameters()
if opt.temp_aggr == 'MLP':
model.module.temp_aggregator = nn.Sequential(
# nn.Dropout(0.9),
nn.ReLU(),
nn.Linear(model.module.temp_aggregator[1].in_features, opt.n_finetune_classes))
model.module.temp_aggregator = model.module.temp_aggregator.cuda()
elif opt.temp_aggr == 'LSTM':
self.temp_aggregator = nn.LSTM(input_size=self.n_finetune_classes, hidden_size=self.n_finetune_classes, batch_first=False, bidirectional=True)
model.module.temp_aggregator = model.module.temp_aggregator.cuda()
if opt.mod_aggr == 'MLP':
model.module.mod_aggregator = nn.Sequential(
# nn.Dropout(0.2),
nn.ReLU(),
nn.Linear(opt.n_finetune_classes * len(opt.modalities), self.n_finetune_classes)
)
parameters = get_fine_tuning_parameters(model, opt.ft_begin_index)
return model, parameters
return model, model.parameters()
def generate_model_ts(opt):
assert opt.model in ['timesformer']
from models.consensus_module_ts import get_fine_tuning_parameters
if opt.model == 'timesformer':
model = consensus_module_ts.get_model(
num_classes=opt.n_classes,
n_finetune_classes=opt.n_finetune_classes,
sample_size=opt.sample_size,
net=opt.model,
modalities=opt.modalities,
mod_aggr=opt.mod_aggr,
feat_fusion=opt.feat_fusion,
ssa_loss=opt.SSA_loss)
else:
print('ERROR: Passed model is not compatible.')
return
if opt.gpu is not None:
model = model.cuda()
model = nn.DataParallel(model, device_ids=None)
'''
pytorch_total_params = sum(p.numel() for p in model.parameters() if
p.requires_grad)
print("Total number of trainable parameters: ", pytorch_total_params)
'''
if opt.pretrain_path:
if '.pth' in opt.pretrain_path:
print('loading pretrained model {}'.format(opt.pretrain_path))
pretrain = torch.load(opt.pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
model.load_state_dict(pretrain['state_dict'])
elif opt.pretrain_path:
opt.pretrain_path = os.path.join(opt.pretrain_path, opt.dataset, opt.model)
for i in range(len(opt.modalities)):
pretrain_path = '_'.join([opt.dataset, opt.model, opt.modalities[i], 'none', 'best.pth'])
pretrain_path = os.path.join(opt.pretrain_path, pretrain_path)
print('loading pretrained model {}'.format(pretrain_path))
pretrain = torch.load(pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
'''
# state_dict = {key.replace('module.', ''): value for key, value in pretrain['state_dict'].items()}
state_dict = {key.replace('module.nets.0.', ''): value for key, value in pretrain['state_dict'].items()}
# state_dict = {key.replace('module.nets.0.0.', ''): value for key, value in pretrain['state_dict'].items()}
model.module.nets[i].load_state_dict(state_dict)
'''
model.module.nets[i].load_state_dict(pretrain['state_dict'])
if opt.test or opt.ft_portion == 'none':
return model, model.parameters()
if opt.n_classes != opt.n_finetune_classes:
# change the output of the final output
if opt.mod_aggr == 'MLP':
model.module.aggregator = nn.Sequential(
# nn.Dropout(0.9),
nn.ReLU(),
nn.Linear(model.module.feat_dim, opt.n_finetune_classes))
model.module.aggregator = model.module.aggregator.cuda()
# change the output size of single cnn
for i in range(len(opt.modalities)):
model.module.nets[i].model.reset_classifier(opt.n_finetune_classes)
model.module.nets[i].model.get_classifier().cuda()
parameters = get_fine_tuning_parameters(model, opt.ft_portion)
return model, parameters
else:
if opt.pretrain_path:
if '.pth' in opt.pretrain_path:
print('loading pretrained model {}'.format(opt.pretrain_path))
pretrain = torch.load(opt.pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
model.load_state_dict(pretrain['state_dict'])
elif opt.pretrain_path:
opt.pretrain_path = os.path.join(opt.pretrain_path, opt.dataset, opt.model)
for i in range(len(opt.modalities)):
pretrain_path = '_'.join([opt.dataset, opt.model, opt.modalities[i], 'none', 'best.pth'])
pretrain_path = os.path.join(opt.pretrain_path, pretrain_path)
print('loading pretrained model {}'.format(pretrain_path))
pretrain = torch.load(pretrain_path, map_location=torch.device('cpu'))
assert opt.arch == pretrain['arch']
'''
# state_dict = {key.replace('module.', ''): value for key, value in pretrain['state_dict'].items()}
state_dict = {key.replace('module.nets.0.', ''): value for key, value in pretrain['state_dict'].items()}
# state_dict = {key.replace('module.nets.0.0.', ''): value for key, value in pretrain['state_dict'].items()}
model.module.nets[i].load_state_dict(state_dict)
'''
model.module.nets[i].load_state_dict(pretrain['state_dict'])
if opt.test or opt.ft_portion == 'none':
return model, model.parameters()
if opt.n_classes != opt.n_finetune_classes:
# change the output of the final output
if opt.mod_aggr == 'MLP':
model.module.aggregator = nn.Sequential(
# nn.Dropout(0.9),
nn.ReLU(),
nn.Linear(model.module.feat_dim, opt.n_finetune_classes))
model.module.aggregator = model.module.aggregator.cuda()
# change the output size of single cnn
for i in range(len(opt.modalities)):
model.module.nets[i].reset_classifier(opt.n_finetune_classes)
# model.module.cnns[i].classifier.cuda()
parameters = get_fine_tuning_parameters(model, opt.ft_portion)
return model, parameters
return model, model.parameters()