-
Notifications
You must be signed in to change notification settings - Fork 37
/
main_video_class.m
147 lines (123 loc) · 5.85 KB
/
main_video_class.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% main function for bag-of-word (BoW) codebook learning %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% May 15, 2014, Jing Shao
% If you use this code, please cite the paper:
% J. Shao, C. C. Loy, X. Wang, "Scene-Independent Group Profiling in Crowd", CVPR, 2014.
clc;clear;
%% path setting
path = '.\';
path_descr = [path, 'result_groupDescr\'];
load([path, 'gt_labels.mat'], 'training_labels');
load([path, 'gt_videoName_bool.mat'], 'videoName_bool');
%% load data -- old version
load([path_descr, 'collectiveness.mat'], 'collectiveness');
X1_temp = collectiveness; % 2d max:[44 51]
X1 = zeros(size(collectiveness,1),3);
X1(:,1) = (X1_temp(:,1)-min(X1_temp(:,1))) / (max(X1_temp(:,1))-min(X1_temp(:,1)));
X1(:,2) = (X1_temp(:,2)-min(X1_temp(:,2))) / (max(X1_temp(:,2))-min(X1_temp(:,2)));
X1((X1_temp(:,3)./(X1_temp(:,4)+eps))>3, 3) = 1;
X1((X1_temp(:,3)./(X1_temp(:,4)+eps))<0.45, 3) = -1;
load([path_descr, 'purityMean.mat'], 'purityMean');
X2 = purityMean; % 2d max:[219 2]
X2(:,1) = (X2(:,1)-min(X2(:,1))) / (max(X2(:,1))-min(X2(:,1)));
X2(:,2) = (X2(:,2)-min(X2(:,2))) / (max(X2(:,2))-min(X2(:,2)));
load([path_descr, 'stab_invKnnNum.mat'], 'stab_invKnnNum');
load([path_descr, 'stab_rankKnn.mat'], 'stab_rankKnn');
load([path_descr, 'stab_graph_hist.mat'], 'stab_graph_hist');
X3 = stab_invKnnNum(:,1); % 1d max:13
X3 = (X3-min(X3)) / (max(X3)-min(X3));
X4 = stab_rankKnn(:,1:15); % 15d max:6
X4 = (X4-min(min(X4))) / (max(max(X4))-min(min(X4)));
X5 = stab_graph_hist(:,1:201); % 201d max:1404, sparse
X5 = (X5-min(min(X5))) / (max(max(X5))-min(min(X5)));
load([path_descr, 'groupSizePercent_max.mat'], 'groupSizePercent_max');
X6 = groupSizePercent_max; % (good) 11d max:3
X6 = (X6-min(min(X6))) / (max(max(X6))-min(min(X6)));
load([path_descr, 'CodeVector_kmeans_max_pooling.mat'], 'CodeVector');
coflict = CodeVector';
X7 = coflict; % 78d max:0.9986, sparse
X7 = (X7-min(min(X7))) / (max(max(X7))-min(min(X7)));
%%
% feature combination
X = [X1, X2, X3, X4, X5, X6, X7]; % collectiveness + purityMean + stab_invKnnNum + stab_rankKnn + stab_graph_hist + friction + groupSizePer
nGroup = size(X, 1);
label_unique = unique(training_labels);
test_num = 50;
perc = 1;
svm_n = 0;% 0-nonlinear(libsvm); 1-linear(liblinear)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TODO: leave-one-out svm: each time, select one scene to test its
% independency. That is, first select a sample, and delete samples with
% the same scene as this sample; and then for the other classes,
% randomly select a sample from each class to build the final test set.
for test_n = 1 : test_num
num = 0;
param_alpha = 1e1;
while param_alpha <= 1e8
num = num + 1;
param_alpha = param_alpha * 1e1;
% generate data index for training
for n = 1 : length(label_unique)
class_num(n) = length(find(training_labels == label_unique(n)));
end
% random select one out to test
rndIdx = []; rndIdx_1 = [];
for n = 1 : length(label_unique)
K = round(class_num(n) * perc);
idx_rand = randperm(class_num(n));
idx_curGr = find(training_labels == label_unique(n));
% rndIdx_1 = [rndIdx_1; idx_curGr(idx_rand(2:K))]; % leave 1st in idx_rand
% rndIdx = [rndIdx; idx_curGr(idx_rand(2:min(70,K)))];
rndIdx = [rndIdx; idx_curGr(idx_rand(2:K))];
end
restIdx = setdiff(1:nGroup, rndIdx);
% if there is other video clips from the same scene exists, remove them from training set
for n = 1 : length(restIdx)
video_bool = videoName_bool(restIdx(n),2);
[class_idx,~] = find(videoName_bool(:,2) == video_bool);
if length(class_idx) > 1
[~,delete_idx] = intersect(rndIdx, class_idx);
rndIdx(delete_idx) = [];
end
end
% classification
train_data = X(rndIdx, :);
train_label = training_labels(rndIdx);
test_data = X(restIdx, :);
test_label = training_labels(restIdx);
options = ['-c ' num2str(param_alpha) ' -s ' num2str(0) ' -t ' num2str(2) ' -b ' num2str(1)];
model = svmtrain(train_label, train_data, options);
[class_label, accuracy, q] = svmpredict(test_label, test_data, model);
class_accuracy(num, :) = [svm_n, perc, param_alpha, accuracy(1)];
if num == 1
max_accuracy_para = class_accuracy(num,2:4); % [percentage, param_alpha, accruacy]
label_test = [restIdx', test_label, class_label]; % [test_id, gt_label, test_label]
label_train = [rndIdx, train_label];
elseif class_accuracy(num,4) > max_accuracy_para(3)
max_accuracy_para = class_accuracy(num,2:4);
label_test = [restIdx', test_label, class_label]; % [test_id, gt_label, test_label]
label_train = [rndIdx, train_label];
end
end
record{test_n,1}.train = label_train;
record{test_n,1}.test = label_test; % [test_id, gt_label, test_label]
[C,order] = confusionmat(record{test_n,1}.test(:,2),record{test_n,1}.test(:,3));
confusion_matrix = C./repmat(sum(C,2),1,size(C,1));
accuracy_mean = mean(diag(confusion_matrix));
record{test_n,1}.acc = [max_accuracy_para, accuracy_mean];
end
%% confusion matrix
C_sum = 0;
for i = 1 : length(record)
[C,order] = confusionmat(record{i,1}.test(:,2), record{i,1}.test(:,3));
C_sum = C_sum + C;
end
C_sum_percent = C_sum./test_num;
class_name = [{'1'}, {'2'}, {'3'}, {'4'}, {'5'}, {'6'}, {'7'}, {'8'}];
figure
draw_cm(C_sum_percent, class_name, length(class_name))
mean_acc = mean(diag(C_sum_percent));
fprintf('Average accuracy: %.4f\n', mean_acc);
%%
% save([path,'v_class_record_old.mat'], 'record')