-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabnormality_event_system.m
212 lines (160 loc) · 5.88 KB
/
abnormality_event_system.m
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
% Distribution code Version 1.0 -- Oct 12, 2013 by Cewu Lu
%
% The Code is to demo Sparse Combination in our Avenue Dataset, based on the method described in the following paper
% [1] "Abnormal Event Detection at 150 FPS in Matlab" , Cewu Lu, Jianping Shi, Jiaya Jia,
% International Conference on Computer Vision, (ICCV), 2013
%
% The code and the algorithm are for non-commercial use only.
%% Parameters
% 360 640
params.H = 120; % loaded video height size
params.W = 160; % loaded video width size
params.patchWin = 10; % 3D patch spatial size
params.tprLen = 5; % 3D patch temporal length
params.BKH = 12; % region number in height
params.BKW = 16; % region number in width
params.srs = 5; % spatial sampling rate in trainning video volume
params.trs = 2; % temporal sampling rate in trainning video volume
params.PCAdim = 100; % PCA Compression dimension
params.MT_thr = 5; % 3D patch selecting threshold
H = params.H;
W = params.W;
patchWin = params.patchWin;
tprLen = params.tprLen;
BKH = params.BKH;
BKW = params.BKW;
PCAdim = params.PCAdim;
testFileNum = 21;
addpath('functions')
addpath('data')
%% Training feature generation (abotu 1 minute)
tic;
fileName = 'data/training_vol';
numEachVol = 7000; % The maximum sample number in each training video is 7000
trainVolDirs = name_filtering(fileName);
Cmatrix = zeros(tprLen*patchWin^2, length(trainVolDirs)*numEachVol);
rand('state', 0);
for ii = 1 : length(trainVolDirs)
[feaRawTrain, LocV3Train] = train_features([fileName,'/', trainVolDirs{ii}], params);
t = randperm(size(feaRawTrain,2));
curFeaNum = min(size(feaRawTrain,2),numEachVol);
Cmatrix(:, numEachVol*(ii - 1) + 1 : numEachVol*(ii - 1) + curFeaNum) = feaRawTrain(:,t(1:curFeaNum));
disp(['Feature extraction in ', num2str(ii),' th training video is done!'])
end
Cmatrix(:,sum(abs(Cmatrix)) == 0) = [];
COEFF = princomp(Cmatrix');
Tw = COEFF(:,1:PCAdim)';
feaMatPCA = Tw*Cmatrix;
save('data/sparse_combinations/Tw.mat','Tw');
toc;
%% Sparse combination learning (about 4 minutes)
tic;
D = sparse_combination(feaMatPCA, 20, 0.1);
% D = sparse_combination(X, Dim, Thr) learns sparse combination
%
% input:
% @X: feature matrix m x N (m is feature dimension, N is feature number)
% @Dim: dimension of a combination
% @Thr: lambda in paper
%
% output:
% @D: sparse combination
for ii = 1 : length(D);
R(ii).val = D(ii).val*inv(D(ii).val'*D(ii).val)*D(ii).val' - eye(size(D(ii).val,1)); % R matrix in Eq. (13).
end
save('data/sparse_combinations/D.mat','D');
save('data/sparse_combinations/R.mat','R');
toc;
%% Testing System
load('data/sparse_combinations/Tw.mat','Tw');
load('data/sparse_combinations/R.mat','R');
ThrTest = 0.20;
ThrMotionVol = 5;
fileNumAll = 0;
timeAll = 0;
for idx = 1 : testFileNum
load(['data/testing_vol/vol', sprintf('%.2d',idx), '.mat']);
imgVol = im2double(vol);
t1 = tic;
volBlur = imgVol;
blurKer = fspecial('gaussian', [3,3], 1);
mask = conv2(ones(H,W), blurKer,'same');
for pp = 1 : size(imgVol,3)
volBlur(:,:,pp) = conv2(volBlur(:,:,pp), blurKer, 'same')./mask;
end
feaVol = abs(volBlur(:,:,1:(end-1)) - volBlur(:,:,2:end));
[feaPCA, LocV3] = test_features(feaVol, Tw, ThrMotionVol, params);
Err = recError(feaPCA, R, ThrTest);
AbEvent = zeros(BKH, BKW, size(imgVol,3));
for ii = 1 : length(Err)
AbEvent(LocV3(1,ii),LocV3(2,ii),LocV3(3,ii)) = Err(ii);
end
AbEvent3 = smooth3( AbEvent, 'box', 5);
t2 = toc(t1);
save(['data/testing_result/regionalRes_',num2str(idx),'.mat'], 'AbEvent3');
fprintf('we can achieve %d FPS in %d th video \n', round(size(imgVol,3)/t2), idx);
fileNumAll = fileNumAll + size(imgVol,3);
timeAll = timeAll + t2;
end
fprintf('average FPS is %d \n', round(fileNumAll/timeAll));
%% Accuracy result
optThr = 0.08;
overlapThr = 0.3;
acc = zeros(1, testFileNum);
testFrameNum = 0;
for idx = 1 : testFileNum
testFrameNum = testFrameNum + size(volLabel{1});
end
gt = zeros(1, testFrameNum);
predict = zeros(1, testFrameNum);
kk_index = 0;
for idx = 1 : testFileNum
load(['data/testing_label_mask/', num2str(idx), '_label.mat'], 'volLabel');
load(['data/testing_result/regionalRes_',num2str(idx),'.mat'], 'AbEvent3');
ratios = zeros(1, length(volLabel));
[Hs, Ws] = size(volLabel{1});
for ii = 1 : length(volLabel)
curFrameTemp = double(AbEvent3(:,:,ii) > optThr);
curFrame = boolean(imresize(curFrameTemp ,[Hs, Ws], 'bilinear') > 0);
unionSet = sum(sum(curFrame|volLabel{ii}));
interSet = sum(sum(curFrame&volLabel{ii}));
if unionSet == 0
ratios(ii) = 1;
else
ratios(ii) = interSet/unionSet;
end
kk_index = kk_index + 1;
if sum(sum(volLabel{ii})) > 0
gt(kk_index) = 1;
else
gt(kk_index) = 0;
end
predict(kk_index) = max(max(AbEvent3(:,:,ii)));
end
acc(idx) = sum(ratios > overlapThr)/length(ratios);
fprintf('Accuracy in %d th video is %.1f %% \n', idx, 100*acc(idx));
end
fprintf('our overall accuracy is %.1f %% \n', 100*mean(acc));
% Sort an by value; keep idxs.
[Y,I] = sort(predict,'descend');
totTrue = sum(gt~=0);
totFalse = sum(gt==0);
skipevery = 10;
i = 0;
thresh = NaN;
for jj=1:skipevery:length(Y)
if thresh==Y(jj)
continue;
end
i=i+1;
thresh = Y(jj);
myLbls = predict >= thresh;
TP = sum((myLbls) & (gt));
FP = sum((myLbls) & (~gt));
TPy(i) = TP/totTrue;
FPx(i) = FP/totFalse;
end
%plot(FPx,TPy);
AUC = trapz(FPx,TPy);
%ylabel(sprintf('AUC=%.3g',AUC));
fprintf('our auc accuracy is %.1f %% \n', AUC*100);