-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathplot_DETcurve.m
executable file
·171 lines (133 loc) · 5.38 KB
/
plot_DETcurve.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
function plot_DETcurve(models, model_names,pos_path, neg_path)
% PLOT_DETCURVE function to compute de DET plot given a set of models
%
% INPUT:
% models: SVM models to test (as a row vector)
% model_names: names of the models to use it in the DET_plot legends
% (as cell array)
% pos/neg path: path to pos/neg images
%
%
%$ Author: Jose Marcos Rodriguez $
%$ Date: 09-Nov-2013 22:45:23 $
%$ Revision : 1.04 $
%% FILENAME : performance.m
% if paths not specified by parameters
if nargin < 3
pos_path = uigetdir('.\images','Select positive test image path');
neg_path = uigetdir('.\images','Select negative test image path');
if isa(neg_path,'double') || isa(pos_path,'double')
cprintf('Errors','Invalid paths...\nexiting...\n\n')
return
end
end
det_figure_handler = figure('name','DET curves');
set(det_figure_handler,'Visible','off');
det_plot_handlers = zeros(1,max(size(models)));
color = ['b','r','g','y'];
for m_index=1:max(size(models))
hold on;
model = models(m_index);
% getting classification scores
[p_scores, n_scores] = get_scores(model,pos_path,neg_path);
% Plot scores distribution as a Histogram
positives = max(size(p_scores));
negatives = max(size(n_scores));
scores = zeros(min(positives, negatives),2);
for i=1:size(scores)
scores(i,1) = p_scores(i);
scores(i,2) = n_scores(i);
end
figure('name', sprintf('model %s scores distribution',model_names{m_index})); hist(scores);
% Compute Pmiss and Pfa from experimental detection output scores
[P_miss,P_fppw] = Compute_DET(p_scores,n_scores);
% Plot the detection error trade-off
figure(det_figure_handler);
thick = 2;
det_plot_handler = Plot_DET(P_miss,P_fppw,color(m_index)', thick);
det_plot_handlers(m_index) = det_plot_handler;
% Plot the optimum point for the detector
C_miss = 1;
C_fppw = 1;
P_target = 0.5;
Set_DCF(C_miss,C_fppw,P_target);
[DCF_opt, Popt_miss, Popt_fa] = Min_DCF(P_miss,P_fppw);
fprintf('Optimal Decision Cost Function for %s = %d\n',model_names{m_index},DCF_opt)
Plot_DET (Popt_miss,Popt_fa,'ko');
end
legend(det_plot_handlers, model_names);
end
function [p_scores, n_scores] = get_scores(model,pos_path, neg_path)
% Tests a (lib)SVM classifier from the specified images paths
%
% ok: number of correct classifications
% ko: number of wrong classifications
% positive / negative images_path: paths of the images to test
% model: SVMmodel to use.
%
%$ Author: Jose Marcos Rodriguez $
%$ Date: 2013/11/09 $
%$ Revision: 1.2 $
[positive_images, negative_images] = get_files(-1,-1,{pos_path,neg_path});
total_pos_windows = numel(positive_images);
total_neg_windows = numel(negative_images);
%% Init the svm test variables
params = get_params('det_plot_params');
chunk_size = params.chunk_size;
desc_size = params.desc_size;
params = get_params('window_params');
im_h_size = params.height;
im_w_size = params.width;
im_c_depth = params.color_depth;
% ====================================================================
%% Reading all POSITIVE images
% (64x128 images)
% ====================================================================
% SVM scores
p_scores = zeros(total_pos_windows,1);
i = 0;
while i < numel(positive_images)
%% window obtainment
this_chunk = min(chunk_size,numel(positive_images)-i);
windows = uint8(zeros(im_h_size,im_w_size,im_c_depth,this_chunk));
hogs = zeros(this_chunk, desc_size);
labels = ones(size(hogs,1),1);
for l=1:this_chunk
I = imread(positive_images(i+1).name);
windows(:,:,:,l) = get_window(I,im_w_size,im_h_size,'center');
hogs(l,:) = compute_HOG(windows(:,:,:,l),8,2,9);
i = i+1;
end
% just for fixing GUI freezing due to unic thread MatLab issue
drawnow;
%% prediction
[~, ~, scores] = ...
svmpredict(labels, hogs, model, '-b 0');
p_scores(i-this_chunk+1:i,:) = scores(:,:);
end
% ====================================================================
%% Reading all NEGATIVE images
% (64x128 windows)
% ====================================================================
n_scores = zeros(total_neg_windows,1);
i = 0;
while i < numel(negative_images)
%% window obtainment
this_chunk = min(chunk_size,numel(negative_images)-i);
windows = uint8(zeros(im_h_size,im_w_size,im_c_depth,this_chunk));
hogs = zeros(this_chunk, desc_size);
labels = ones(size(hogs,1),1)*(-1);
for l=1:this_chunk
I = imread(negative_images(i+1).name);
windows(:,:,:,l) = get_window(I,im_w_size,im_h_size,[1,1]);
hogs(l,:) = compute_HOG(windows(:,:,:,l),8,2,9);
i = i+1;
end
% just for fixing GUI freezing due to unic thread MatLab issue
drawnow;
%% prediction
[~, ~, scores] = ...
svmpredict(labels, hogs, model, '-b 0');
n_scores(i-this_chunk+1:i,:) = scores(:,:);
end
end