-
Notifications
You must be signed in to change notification settings - Fork 27
/
model.py
155 lines (138 loc) · 6.21 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
import torch
from torch import nn
from models import shufflenet, shufflenetv2, resnet, mobilenet, mobilenetv2
from utils import _construct_depth_model
def generate_model(args):
assert args.model_type in ['resnet', 'shufflenet', 'shufflenetv2', 'mobilenet', 'mobilenetv2']
if args.pre_train_model == False or args.mode == 'test':
print('Without Pre-trained model')
if args.model_type == 'resnet':
assert args.model_depth in [18, 50, 101]
if args.model_depth == 18:
model = resnet.resnet18(
output_dim=args.feature_dim,
sample_size=args.sample_size,
sample_duration=args.sample_duration,
shortcut_type=args.shortcut_type,
tracking=args.tracking,
pre_train = args.pre_train_model
)
elif args.model_depth == 50:
model = resnet.resnet50(
output_dim=args.feature_dim,
sample_size=args.sample_size,
sample_duration=args.sample_duration,
shortcut_type=args.shortcut_type,
tracking=args.tracking,
pre_train=args.pre_train_model
)
elif args.model_depth == 101:
model = resnet.resnet101(
output_dim=args.feature_dim,
sample_size=args.sample_size,
sample_duration=args.sample_duration,
shortcut_type=args.shortcut_type,
tracking=args.tracking,
pre_train=args.pre_train_model
)
elif args.model_type == 'shufflenet':
model = shufflenet.get_model(
groups=args.groups,
width_mult=args.width_mult,
output_dim=args.feature_dim,
pre_train=args.pre_train_model
)
elif args.model_type == 'shufflenetv2':
model = shufflenetv2.get_model(
output_dim=args.feature_dim,
sample_size=args.sample_size,
width_mult=args.width_mult,
pre_train=args.pre_train_model
)
elif args.model_type == 'mobilenet':
model = mobilenet.get_model(
sample_size=args.sample_size,
width_mult=args.width_mult,
pre_train=args.pre_train_model
)
elif args.model_type == 'mobilenetv2':
model = mobilenetv2.get_model(
sample_size=args.sample_size,
width_mult=args.width_mult,
pre_train=args.pre_train_model
)
model = nn.DataParallel(model, device_ids=None)
else:
if args.model_type == 'resnet':
pre_model_path = './premodels/kinetics_resnet_' + str(args.model_depth) + '_RGB_16_best.pth'
###default pre-trained model is trained on kinetics dataset which has 600 classes
if args.model_depth == 18:
model = resnet.resnet18(
output_dim=args.feature_dim,
sample_size=args.sample_size,
sample_duration=args.sample_duration,
shortcut_type='A',
tracking=args.tracking,
pre_train=args.pre_train_model
)
elif args.model_depth == 50:
model = resnet.resnet50(
output_dim=args.feature_dim,
sample_size=args.sample_size,
sample_duration=args.sample_duration,
shortcut_type='B',
tracking=args.tracking,
pre_train=args.pre_train_model
)
elif args.model_depth == 101:
model = resnet.resnet101(
output_dim=args.feature_dim,
sample_size=args.sample_size,
sample_duration=args.sample_duration,
shortcut_type='B',
tracking=args.tracking,
pre_train=args.pre_train_model
)
elif args.model_type == 'shufflenet':
pre_model_path = './premodels/kinetics_shufflenet_'+str(args.width_mult)+'x_G3_RGB_16_best.pth'
model = shufflenet.get_model(
groups=args.groups,
width_mult=args.width_mult,
output_dim=args.feature_dim,
pre_train=args.pre_train_model
)
elif args.model_type == 'shufflenetv2':
pre_model_path = './premodels/kinetics_shufflenetv2_'+str(args.width_mult)+'x_RGB_16_best.pth'
model = shufflenetv2.get_model(
output_dim=args.feature_dim,
sample_size=args.sample_size,
width_mult=args.width_mult,
pre_train = args.pre_train_model
)
elif args.model_type == 'mobilenet':
pre_model_path = './premodels/kinetics_mobilenet_'+ str(args.width_mult) +'x_RGB_16_best.pth'
model = mobilenet.get_model(
sample_size=args.sample_size,
width_mult=args.width_mult,
pre_train=args.pre_train_model
)
elif args.model_type == 'mobilenetv2':
pre_model_path = './premodels/kinetics_mobilenetv2_' + str(args.width_mult) + 'x_RGB_16_best.pth'
model = mobilenetv2.get_model(
sample_size=args.sample_size,
width_mult=args.width_mult,
pre_train=args.pre_train_model
)
model = nn.DataParallel(model, device_ids=None) # in order to load pre-trained model
model_dict = model.state_dict()
pretrained_dict = torch.load(pre_model_path)['state_dict']
#print(len(pretrained_dict.keys()))
#print({k for k, v in pretrained_dict.items() if k not in model_dict})
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
#print(len(pretrained_dict.keys()))
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
model = _construct_depth_model(model)
if args.use_cuda:
model = model.cuda()
return model