forked from okankop/Efficient-3DCNNs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
176 lines (162 loc) · 7.84 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
import torch
from torch import nn
from models import c3d, squeezenet, mobilenet, shufflenet, mobilenetv2, shufflenetv2, resnext, resnet
def generate_model(opt):
assert opt.model in ['c3d', 'squeezenet', 'mobilenet', 'resnext', 'resnet',
'shufflenet', 'mobilenetv2', 'shufflenetv2']
if opt.model == 'c3d':
from models.c3d import get_fine_tuning_parameters
model = c3d.get_model(
num_classes=opt.n_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model == 'squeezenet':
from models.squeezenet import get_fine_tuning_parameters
model = squeezenet.get_model(
version=opt.version,
num_classes=opt.n_classes,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model == 'shufflenet':
from models.shufflenet import get_fine_tuning_parameters
model = shufflenet.get_model(
groups=opt.groups,
width_mult=opt.width_mult,
num_classes=opt.n_classes)
elif opt.model == 'shufflenetv2':
from models.shufflenetv2 import get_fine_tuning_parameters
model = shufflenetv2.get_model(
num_classes=opt.n_classes,
sample_size=opt.sample_size,
width_mult=opt.width_mult)
elif opt.model == 'mobilenet':
from models.mobilenet import get_fine_tuning_parameters
model = mobilenet.get_model(
num_classes=opt.n_classes,
sample_size=opt.sample_size,
width_mult=opt.width_mult)
elif opt.model == 'mobilenetv2':
from models.mobilenetv2 import get_fine_tuning_parameters
model = mobilenetv2.get_model(
num_classes=opt.n_classes,
sample_size=opt.sample_size,
width_mult=opt.width_mult)
elif opt.model == 'resnext':
assert opt.model_depth in [50, 101, 152]
from models.resnext import get_fine_tuning_parameters
if opt.model_depth == 50:
model = resnext.resnext50(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
cardinality=opt.resnext_cardinality,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 101:
model = resnext.resnext101(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
cardinality=opt.resnext_cardinality,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 152:
model = resnext.resnext152(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
cardinality=opt.resnext_cardinality,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model == 'resnet':
assert opt.model_depth in [10, 18, 34, 50, 101, 152, 200]
from models.resnet import get_fine_tuning_parameters
if opt.model_depth == 10:
model = resnet.resnet10(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 18:
model = resnet.resnet18(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 34:
model = resnet.resnet34(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 50:
model = resnet.resnet50(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 101:
model = resnet.resnet101(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 152:
model = resnet.resnet152(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
elif opt.model_depth == 200:
model = resnet.resnet200(
num_classes=opt.n_classes,
shortcut_type=opt.resnet_shortcut,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
if not opt.no_cuda:
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:
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'])
if opt.model in ['mobilenet', 'mobilenetv2', 'shufflenet', 'shufflenetv2']:
model.module.classifier = nn.Sequential(
nn.Dropout(0.9),
nn.Linear(model.module.classifier[1].in_features, opt.n_finetune_classes))
model.module.classifier = model.module.classifier.cuda()
elif opt.model == 'squeezenet':
model.module.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Conv3d(model.module.classifier[1].in_channels, opt.n_finetune_classes, kernel_size=1),
nn.ReLU(inplace=True),
nn.AvgPool3d((1,4,4), stride=1))
model.module.classifier = model.module.classifier.cuda()
else:
model.module.fc = nn.Linear(model.module.fc.in_features, opt.n_finetune_classes)
model.module.fc = model.module.fc.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 ['mobilenet', 'mobilenetv2', 'shufflenet', 'shufflenetv2']:
model.module.classifier = nn.Sequential(
nn.Dropout(0.9),
nn.Linear(model.module.classifier[1].in_features, opt.n_finetune_classes)
)
elif opt.model == 'squeezenet':
model.module.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Conv3d(model.module.classifier[1].in_channels, opt.n_finetune_classes, kernel_size=1),
nn.ReLU(inplace=True),
nn.AvgPool3d((1,4,4), stride=1))
else:
model.module.fc = nn.Linear(model.module.fc.in_features, opt.n_finetune_classes)
parameters = get_fine_tuning_parameters(model, opt.ft_begin_index)
return model, parameters
return model, model.parameters()