-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_model.py
296 lines (212 loc) · 10.6 KB
/
base_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
from backbone.backbone import *
from utils import *
from roi_align.roi_align import RoIAlign # RoIAlign module
class Basenet_volleyball(nn.Module):
"""
main module of base model for the volleyball
"""
def __init__(self, cfg):
super(Basenet_volleyball, self).__init__()
self.cfg=cfg
NFB=self.cfg.num_features_boxes
D=self.cfg.emb_features
K=self.cfg.crop_size[0]
if cfg.backbone=='inv3':
self.backbone=MyInception_v3(transform_input=False,pretrained=True)
elif cfg.backbone=='vgg16':
self.backbone=MyVGG16(pretrained=True)
elif cfg.backbone=='vgg19':
self.backbone=MyVGG19(pretrained=True)
elif cfg.backbone == 'res18':
self.backbone = MyRes18(pretrained = True)
else:
assert False
self.roi_align=RoIAlign(*self.cfg.crop_size)
self.fc_emb = nn.Linear(K*K*D,NFB)
self.dropout_emb = nn.Dropout(p=self.cfg.train_dropout_prob)
self.fc_actions=nn.Linear(NFB,self.cfg.num_actions)
self.fc_activities=nn.Linear(NFB,self.cfg.num_activities)
for m in self.modules():
if isinstance(m,nn.Linear):
nn.init.kaiming_normal_(m.weight)
nn.init.zeros_(m.bias)
def savemodel(self,filepath):
state = {
'backbone_state_dict': self.backbone.state_dict(),
'fc_emb_state_dict':self.fc_emb.state_dict(),
'fc_actions_state_dict':self.fc_actions.state_dict(),
'fc_activities_state_dict':self.fc_activities.state_dict()
}
torch.save(state, filepath)
print('model saved to:',filepath)
def loadmodel(self,filepath):
state = torch.load(filepath)
self.backbone.load_state_dict(state['backbone_state_dict'])
self.fc_emb.load_state_dict(state['fc_emb_state_dict'])
self.fc_actions.load_state_dict(state['fc_actions_state_dict'])
self.fc_activities.load_state_dict(state['fc_activities_state_dict'])
print('Load model states from: ',filepath)
def forward(self,batch_data):
images_in, boxes_in = batch_data
# read config parameters
B=images_in.shape[0]
T=images_in.shape[1]
H, W=self.cfg.image_size
OH, OW=self.cfg.out_size
N=self.cfg.num_boxes
NFB=self.cfg.num_features_boxes
# Reshape the input data
images_in_flat=torch.reshape(images_in,(B*T,3,H,W)) #B*T, 3, H, W
boxes_in_flat=torch.reshape(boxes_in,(B*T*N,4)) #B*T*N, 4
boxes_idx=[i * torch.ones(N, dtype=torch.int) for i in range(B*T) ]
boxes_idx=torch.stack(boxes_idx).to(device=boxes_in.device) # B*T, N
boxes_idx_flat=torch.reshape(boxes_idx,(B*T*N,)) #B*T*N,
# Use backbone to extract features of images_in
# Pre-precess first
images_in_flat=prep_images(images_in_flat)
outputs=self.backbone(images_in_flat)
# Build multiscale features
features_multiscale=[]
for features in outputs:
if features.shape[2:4]!=torch.Size([OH,OW]):
features=F.interpolate(features,size=(OH,OW),mode='bilinear',align_corners=True)
features_multiscale.append(features)
features_multiscale=torch.cat(features_multiscale,dim=1) #B*T, D, OH, OW
# ActNet
boxes_in_flat.requires_grad=False
boxes_idx_flat.requires_grad=False
# features_multiscale.requires_grad=False
# RoI Align
boxes_features=self.roi_align(features_multiscale,
boxes_in_flat,
boxes_idx_flat) #B*T*N, D, K, K,
boxes_features=boxes_features.reshape(B*T*N,-1) # B*T*N, D*K*K [48, 26400]
# Embedding to hidden state
boxes_features=self.fc_emb(boxes_features) # B*T*N, NFB K*K*D,NFB
boxes_features=F.relu(boxes_features)
boxes_features=self.dropout_emb(boxes_features)
boxes_states=boxes_features.reshape(B,T,N,NFB)
# Predict actions
boxes_states_flat=boxes_states.reshape(-1,NFB) #B*T*N, NFB
actions_scores=self.fc_actions(boxes_states_flat) #B*T*N, actn_num
# Predict activities
boxes_states_pooled,_=torch.max(boxes_states,dim=2) #B, T, NFB
boxes_states_pooled_flat=boxes_states_pooled.reshape(-1,NFB) #B*T, NFB
activities_scores=self.fc_activities(boxes_states_pooled_flat) #B*T, acty_num
if T!=1:
actions_scores=actions_scores.reshape(B,T,N,-1).mean(dim=1).reshape(B*N,-1)
activities_scores=activities_scores.reshape(B,T,-1).mean(dim=1)
return actions_scores, activities_scores
class Basenet_collective(nn.Module):
"""
main module of base model for collective dataset
"""
def __init__(self, cfg):
super(Basenet_collective, self).__init__()
self.cfg=cfg
D=self.cfg.emb_features
K=self.cfg.crop_size[0]
NFB=self.cfg.num_features_boxes
NFR, NFG=self.cfg.num_features_relation, self.cfg.num_features_gcn
# self.backbone=MyInception_v3(transform_input=False,pretrained=True)
# self.backbone=MyVGG16(pretrained=True)
if cfg.backbone=='inv3':
self.backbone=MyInception_v3(transform_input=False,pretrained=True)
elif cfg.backbone=='vgg16':
self.backbone=MyVGG16(pretrained=True)
elif cfg.backbone=='vgg19':
self.backbone=MyVGG19(pretrained=True)
elif cfg.backbone == 'res18':
self.backbone = MyRes18(pretrained = True)
else:
assert False
if not self.cfg.train_backbone:
for p in self.backbone.parameters():
p.requires_grad=False
self.roi_align=RoIAlign(*self.cfg.crop_size)
self.fc_emb_1=nn.Linear(K*K*D,NFB)
self.dropout_emb_1 = nn.Dropout(p=self.cfg.train_dropout_prob)
# self.nl_emb_1=nn.LayerNorm([NFB])
self.fc_actions=nn.Linear(NFB,self.cfg.num_actions)
self.fc_activities=nn.Linear(NFB,self.cfg.num_activities)
for m in self.modules():
if isinstance(m,nn.Linear):
nn.init.kaiming_normal_(m.weight)
def savemodel(self,filepath):
state = {
'backbone_state_dict': self.backbone.state_dict(),
'fc_emb_state_dict':self.fc_emb_1.state_dict(),
'fc_actions_state_dict':self.fc_actions.state_dict(),
'fc_activities_state_dict':self.fc_activities.state_dict()
}
torch.save(state, filepath)
print('model saved to:',filepath)
def loadmodel(self,filepath):
state = torch.load(filepath)
self.backbone.load_state_dict(state['backbone_state_dict'])
self.fc_emb_1.load_state_dict(state['fc_emb_state_dict'])
print('Load model states from: ',filepath)
def forward(self,batch_data):
images_in, boxes_in, bboxes_num_in = batch_data
# read config parameters
B=images_in.shape[0]
T=images_in.shape[1]
H, W=self.cfg.image_size
OH, OW=self.cfg.out_size
MAX_N=self.cfg.num_boxes
NFB=self.cfg.num_features_boxes
NFR, NFG=self.cfg.num_features_relation, self.cfg.num_features_gcn
EPS=1e-5
D=self.cfg.emb_features
K=self.cfg.crop_size[0]
# Reshape the input data
images_in_flat=torch.reshape(images_in,(B*T,3,H,W)) #B*T, 3, H, W
boxes_in=boxes_in.reshape(B*T,MAX_N,4)
# Use backbone to extract features of images_in
# Pre-precess first
images_in_flat=prep_images(images_in_flat)
outputs=self.backbone(images_in_flat)
# Build multiscale features
features_multiscale=[]
for features in outputs:
if features.shape[2:4]!=torch.Size([OH,OW]):
features=F.interpolate(features,size=(OH,OW),mode='bilinear',align_corners=True)
features_multiscale.append(features)
features_multiscale=torch.cat(features_multiscale,dim=1) #B*T, D, OH, OW
boxes_in_flat=torch.reshape(boxes_in,(B*T*MAX_N,4)) #B*T*MAX_N, 4
boxes_idx=[i * torch.ones(MAX_N, dtype=torch.int) for i in range(B*T) ]
boxes_idx=torch.stack(boxes_idx).to(device=boxes_in.device) # B*T, MAX_N
boxes_idx_flat=torch.reshape(boxes_idx,(B*T*MAX_N,)) #B*T*MAX_N,
# RoI Align
boxes_in_flat.requires_grad=False
boxes_idx_flat.requires_grad=False
boxes_features_all=self.roi_align(features_multiscale,
boxes_in_flat,
boxes_idx_flat) #B*T*MAX_N, D, K, K,
boxes_features_all=boxes_features_all.reshape(B*T,MAX_N,-1) #B*T,MAX_N, D*K*K
# Embedding
boxes_features_all=self.fc_emb_1(boxes_features_all) # B*T,MAX_N, NFB
boxes_features_all=F.relu(boxes_features_all)
boxes_features_all=self.dropout_emb_1(boxes_features_all)
actions_scores=[]
activities_scores=[]
bboxes_num_in=bboxes_num_in.reshape(B*T,) #B*T,
for bt in range(B*T):
N=bboxes_num_in[bt]
boxes_features=boxes_features_all[bt,:N,:].reshape(1,N,NFB) #1,N,NFB
boxes_states=boxes_features
NFS=NFB
# Predict actions
boxes_states_flat=boxes_states.reshape(-1,NFS) #1*N, NFS
actn_score=self.fc_actions(boxes_states_flat) #1*N, actn_num
actions_scores.append(actn_score)
# Predict activities
boxes_states_pooled,_=torch.max(boxes_states,dim=1) #1, NFS
boxes_states_pooled_flat=boxes_states_pooled.reshape(-1,NFS) #1, NFS
acty_score=self.fc_activities(boxes_states_pooled_flat) #1, acty_num
activities_scores.append(acty_score)
actions_scores=torch.cat(actions_scores,dim=0) #ALL_N,actn_num
activities_scores=torch.cat(activities_scores,dim=0) #B*T,acty_num
# print(actions_scores.shape)
# print(activities_scores.shape)
return actions_scores, activities_scores