-
Notifications
You must be signed in to change notification settings - Fork 1
/
segEval_SVM.m
executable file
·206 lines (150 loc) · 5.74 KB
/
segEval_SVM.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
%% Evaluate Overall, using Avg Error Pixel between Segtrack GT ...
%% Author: Karim S. Ahmed
function [scoreError] = segEval_SVM(video_name,method_num,extension1,extension2,visualize )
format short g
run('vlfeat/toolbox/vl_setup');
% load SVM Model
if (method_num ==1)
C = load('svm_train_s1_method1.mat', 'B', 'W');
elseif (method_num ==2)
C = load('svm_train_s1_method2.mat', 'B', 'W');
end
B = C.B;
W = C.W;
test_path = fullfile( 'test_data/segtrackv1/',video_name); % source
% for gt
gt_test_imgs = dir( fullfile( test_path, 'ground-truth' ,strcat('*.', extension2 )));
% for real image
test_imgs = dir( fullfile( test_path ,strcat('*.', extension1 )));
len = length(test_imgs)*10;
image_size_width = 0;
image_size_height = 0;
%
dim = zeros(length(gt_test_imgs),4);
max_w = 0;
max_h = 0;
labels_path = fullfile( 'results/segtrackv1/stl/', video_name);
%dest_path = fullfile(data_path, 'test_data/segtrackv1/stl_labeled_final/', video_name);
bboxes = zeros(0,4);
confidences = zeros(0,1);
image_ids = cell(0,1);
svm_dim = zeros(length(gt_test_imgs),4);
sum_pixel_errors = 0;
for ind = 1:length(gt_test_imgs)
main_img = imread( fullfile( test_path,'ground-truth', gt_test_imgs(ind).name ));
% copy image
img = main_img(:,:,1);
img(img>0)=1;
img(img<=0)=0;
[m,n] = size (img);
image_size_width = n;
image_size_height = m;
% get x1
x1 =0;
y1 =0;
x2 =0;
y2 =0;
% get x1
for j = 1:n
if (sum(img(:,j)) ~=0)
x1 = j;
new_j = j;
% loop
while(sum(img(:,new_j)) ~=0)
new_j = new_j+1;
end
x2 = new_j-1;
break;
end
end
% get y1
for i = 1:m
if (sum(img(i,:)) ~=0)
y1 = i;
% loop
new_i = i;
while(sum(img(new_i,:)) ~=0)
new_i = new_i+1;
end
y2 = new_i-1;
break;
end
end
if visualize
figure, imshow(main_img), hold on
end
w = x2 -x1;
h = y2 -y1;
if visualize
rectangle('Position',[x1 y1 w h], 'LineWidth',1, 'EdgeColor','r');
end
% following gt dimensions...
dim(ind,1) = x1;
dim(ind,2) = y1;
dim(ind,3) = x2;
dim(ind,4) = y2;
%% Calculate svm results dims
i= ind;
img_copy = imread( fullfile( test_path, test_imgs(i).name ));
% read labels
file_label_path = fullfile(labels_path,strcat ( test_imgs(i).name, '.txt'));
fid = fopen(file_label_path);
gt_info = textscan(fid, '%d %d %d %d %f64');
gt_info_width = gt_info{1,3} - gt_info{1,1};
gt_info_height = gt_info{1,4} - gt_info{1,2};
gt_bboxes_view = [gt_info{1,1}+1, gt_info{1,2}+1, gt_info_width-1, gt_info_height-1];
gt_bboxes = [gt_info{1,1}, gt_info{1,2}, gt_info{1,3}, gt_info{1,4}];
gt_confidences = double(gt_info{1,5});
[sortedValues,sortIndex] = sort(gt_confidences(:),'descend');
maxIndexes = sortIndex(1:4);
gray_img = rgb2gray(img_copy);
hog_cell_size = 8;
all_scores = zeros(length(maxIndexes),1);
for k = 1:length(maxIndexes)
x1 = gt_info{1,1}(maxIndexes(k))+1;
y1 = gt_info{1,2}(maxIndexes(k))+1;
x2 = gt_info{1,3}(maxIndexes(k))-1;
y2 = gt_info{1,4}(maxIndexes(k))-1;
features = vl_hog(im2single(gray_img(y1:y2,x1:x2)), hog_cell_size) ;
test_feats = features(:);
padSize = size(W,1) - size(test_feats,1);
if (padSize<0)
new_w = padarray( W, abs(padSize) ,'symmetric', 'post');
new_w = new_w/ norm(new_w);
all_scores(k) = new_w'*test_feats + B ;
elseif (padSize>0)
test_feats = padarray(test_feats, padSize ,'symmetric', 'post');
all_scores(k) = ( W'*test_feats + B ) ; %* gt_confidences(maxIndexes(k));
end
end
%final_scores = all_scores *
best_index = find( all_scores == max(all_scores));
% following svm dimensions...cl
svm_dim(ind,1) = gt_bboxes(best_index,1)+1;
svm_dim(ind,2) = gt_bboxes(best_index,2)+1;
svm_dim(ind,3) = gt_bboxes(best_index,3)-1;
svm_dim(ind,4) = gt_bboxes(best_index,4)-1;
if visualize
figure, imshow(img_copy), hold on
end
% slect best SVM <<<<<<<<<<<<<<<<
if visualize
rectangle('Position',gt_bboxes_view(best_index,:), 'LineWidth',2, 'EdgeColor','r');
end
% select top 1 stl
%rectangle('Position',gt_bboxes(maxIndexes(1),:), 'LineWidth',2, 'EdgeColor','b');
%% calculate difference in pixels
pixel_err_perframe = 0 ;
obj_box = [svm_dim(ind,1) svm_dim(ind,2) (svm_dim(ind,3) - svm_dim(ind,1)) (svm_dim(ind,4) - svm_dim(ind,2))];
gt_box = [dim(ind,1) dim(ind,2) (dim(ind,3) - dim(ind,1)) (dim(ind,4) - dim(ind,2))]; %gt
overlap_area = rectint(obj_box,gt_box);
gt_area = rectint(gt_box,gt_box);
obj_area = rectint(obj_box,obj_box);
pixel_err_perframe = (gt_area + obj_area) - 2*overlap_area;
sum_pixel_errors = sum_pixel_errors + pixel_err_perframe;
fclose(fid);
end
Avg_pixl_perFrame = sum_pixel_errors/len;
scoreError = floor(Avg_pixl_perFrame);
close all;
end