-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmodel.py
executable file
·273 lines (216 loc) · 10.3 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
import torch
import torch.nn as nn
import torch.nn.init as torch_init
torch.set_default_tensor_type('torch.FloatTensor')
def weight_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
torch_init.xavier_uniform_(m.weight)
if m.bias is not None:
m.bias.data.fill_(0)
class _NonLocalBlockND(nn.Module):
def __init__(self, in_channels, inter_channels=None, dimension=3, sub_sample=True, bn_layer=True):
super(_NonLocalBlockND, self).__init__()
assert dimension in [1, 2, 3]
self.dimension = dimension
self.sub_sample = sub_sample
self.in_channels = in_channels
self.inter_channels = inter_channels
if self.inter_channels is None:
self.inter_channels = in_channels // 2
if self.inter_channels == 0:
self.inter_channels = 1
if dimension == 3:
conv_nd = nn.Conv3d
max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2))
bn = nn.BatchNorm3d
elif dimension == 2:
conv_nd = nn.Conv2d
max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2))
bn = nn.BatchNorm2d
else:
conv_nd = nn.Conv1d
max_pool_layer = nn.MaxPool1d(kernel_size=(2))
bn = nn.BatchNorm1d
self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0)
if bn_layer:
self.W = nn.Sequential(
conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels,
kernel_size=1, stride=1, padding=0),
bn(self.in_channels)
)
nn.init.constant_(self.W[1].weight, 0)
nn.init.constant_(self.W[1].bias, 0)
else:
self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels,
kernel_size=1, stride=1, padding=0)
nn.init.constant_(self.W.weight, 0)
nn.init.constant_(self.W.bias, 0)
self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0)
self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0)
if sub_sample:
self.g = nn.Sequential(self.g, max_pool_layer)
self.phi = nn.Sequential(self.phi, max_pool_layer)
def forward(self, x, return_nl_map=False):
"""
:param x: (b, c, t, h, w)
:param return_nl_map: if True return z, nl_map, else only return z.
:return:
"""
batch_size = x.size(0)
g_x = self.g(x).view(batch_size, self.inter_channels, -1)
g_x = g_x.permute(0, 2, 1)
theta_x = self.theta(x).view(batch_size, self.inter_channels, -1)
theta_x = theta_x.permute(0, 2, 1)
phi_x = self.phi(x).view(batch_size, self.inter_channels, -1)
f = torch.matmul(theta_x, phi_x)
N = f.size(-1)
f_div_C = f / N
y = torch.matmul(f_div_C, g_x)
y = y.permute(0, 2, 1).contiguous()
y = y.view(batch_size, self.inter_channels, *x.size()[2:])
W_y = self.W(y)
z = W_y + x
if return_nl_map:
return z, f_div_C
return z
class NONLocalBlock1D(_NonLocalBlockND):
def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True):
super(NONLocalBlock1D, self).__init__(in_channels,
inter_channels=inter_channels,
dimension=1, sub_sample=sub_sample,
bn_layer=bn_layer)
class Aggregate(nn.Module):
def __init__(self, len_feature):
super(Aggregate, self).__init__()
bn = nn.BatchNorm1d
self.len_feature = len_feature
self.conv_1 = nn.Sequential(
nn.Conv1d(in_channels=len_feature, out_channels=512, kernel_size=3,
stride=1,dilation=1, padding=1),
nn.ReLU(),
bn(512)
# nn.dropout(0.7)
)
self.conv_2 = nn.Sequential(
nn.Conv1d(in_channels=len_feature, out_channels=512, kernel_size=3,
stride=1, dilation=2, padding=2),
nn.ReLU(),
bn(512)
# nn.dropout(0.7)
)
self.conv_3 = nn.Sequential(
nn.Conv1d(in_channels=len_feature, out_channels=512, kernel_size=3,
stride=1, dilation=4, padding=4),
nn.ReLU(),
bn(512)
# nn.dropout(0.7),
)
self.conv_4 = nn.Sequential(
nn.Conv1d(in_channels=2048, out_channels=512, kernel_size=1,
stride=1, padding=0, bias = False),
nn.ReLU(),
# nn.dropout(0.7),
)
self.conv_5 = nn.Sequential(
nn.Conv1d(in_channels=2048, out_channels=2048, kernel_size=3,
stride=1, padding=1, bias=False), # should we keep the bias?
nn.ReLU(),
nn.BatchNorm1d(2048),
# nn.dropout(0.7)
)
self.non_local = NONLocalBlock1D(512, sub_sample=False, bn_layer=True)
def forward(self, x):
# x: (B, T, F)
out = x.permute(0, 2, 1)
residual = out
out1 = self.conv_1(out)
out2 = self.conv_2(out)
out3 = self.conv_3(out)
out_d = torch.cat((out1, out2, out3), dim = 1)
out = self.conv_4(out)
out = self.non_local(out)
out = torch.cat((out_d, out), dim=1)
out = self.conv_5(out) # fuse all the features together
out = out + residual
out = out.permute(0, 2, 1)
# out: (B, T, 1)
return out
class Model(nn.Module):
def __init__(self, n_features, batch_size):
super(Model, self).__init__()
self.batch_size = batch_size
self.num_segments = 32
self.k_abn = self.num_segments // 10
self.k_nor = self.num_segments // 10
self.Aggregate = Aggregate(len_feature=2048)
self.fc1 = nn.Linear(n_features, 512)
self.fc2 = nn.Linear(512, 128)
self.fc3 = nn.Linear(128, 1)
self.drop_out = nn.Dropout(0.7)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
self.apply(weight_init)
def forward(self, inputs):
k_abn = self.k_abn
k_nor = self.k_nor
out = inputs
bs, ncrops, t, f = out.size()
out = out.view(-1, t, f)
out = self.Aggregate(out)
out = self.drop_out(out)
features = out
scores = self.relu(self.fc1(features))
scores = self.drop_out(scores)
scores = self.relu(self.fc2(scores))
scores = self.drop_out(scores)
scores = self.sigmoid(self.fc3(scores))
scores = scores.view(bs, ncrops, -1).mean(1)
scores = scores.unsqueeze(dim=2)
normal_features = features[0:self.batch_size*10]
normal_scores = scores[0:self.batch_size]
abnormal_features = features[self.batch_size*10:]
abnormal_scores = scores[self.batch_size:]
feat_magnitudes = torch.norm(features, p=2, dim=2)
feat_magnitudes = feat_magnitudes.view(bs, ncrops, -1).mean(1)
nfea_magnitudes = feat_magnitudes[0:self.batch_size] # normal feature magnitudes
afea_magnitudes = feat_magnitudes[self.batch_size:] # abnormal feature magnitudes
n_size = nfea_magnitudes.shape[0]
if nfea_magnitudes.shape[0] == 1: # this is for inference, the batch size is 1
afea_magnitudes = nfea_magnitudes
abnormal_scores = normal_scores
abnormal_features = normal_features
select_idx = torch.ones_like(nfea_magnitudes)
select_idx = self.drop_out(select_idx)
####### process abnormal videos -> select top3 feature magnitude #######
afea_magnitudes_drop = afea_magnitudes * select_idx
idx_abn = torch.topk(afea_magnitudes_drop, k_abn, dim=1)[1]
idx_abn_feat = idx_abn.unsqueeze(2).expand([-1, -1, abnormal_features.shape[2]])
abnormal_features = abnormal_features.view(n_size, ncrops, t, f)
abnormal_features = abnormal_features.permute(1, 0, 2,3)
total_select_abn_feature = torch.zeros(0, device=inputs.device)
for abnormal_feature in abnormal_features:
feat_select_abn = torch.gather(abnormal_feature, 1, idx_abn_feat) # top 3 features magnitude in abnormal bag
total_select_abn_feature = torch.cat((total_select_abn_feature, feat_select_abn))
idx_abn_score = idx_abn.unsqueeze(2).expand([-1, -1, abnormal_scores.shape[2]])
score_abnormal = torch.mean(torch.gather(abnormal_scores, 1, idx_abn_score), dim=1) # top 3 scores in abnormal bag based on the top-3 magnitude
####### process normal videos -> select top3 feature magnitude #######
select_idx_normal = torch.ones_like(nfea_magnitudes)
select_idx_normal = self.drop_out(select_idx_normal)
nfea_magnitudes_drop = nfea_magnitudes * select_idx_normal
idx_normal = torch.topk(nfea_magnitudes_drop, k_nor, dim=1)[1]
idx_normal_feat = idx_normal.unsqueeze(2).expand([-1, -1, normal_features.shape[2]])
normal_features = normal_features.view(n_size, ncrops, t, f)
normal_features = normal_features.permute(1, 0, 2, 3)
total_select_nor_feature = torch.zeros(0, device=inputs.device)
for nor_fea in normal_features:
feat_select_normal = torch.gather(nor_fea, 1, idx_normal_feat) # top 3 features magnitude in normal bag (hard negative)
total_select_nor_feature = torch.cat((total_select_nor_feature, feat_select_normal))
idx_normal_score = idx_normal.unsqueeze(2).expand([-1, -1, normal_scores.shape[2]])
score_normal = torch.mean(torch.gather(normal_scores, 1, idx_normal_score), dim=1) # top 3 scores in normal bag
feat_select_abn = total_select_abn_feature
feat_select_normal = total_select_nor_feature
return score_abnormal, score_normal, feat_select_abn, feat_select_normal, feat_select_abn, feat_select_abn, scores, feat_select_abn, feat_select_abn, feat_magnitudes