-
Notifications
You must be signed in to change notification settings - Fork 21
/
exercise5.m
142 lines (117 loc) · 3.95 KB
/
exercise5.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
% EXERCISE5
setup ;
% Training cofiguration
targetClass = 1 ;
numHardNegativeMiningIterations = 5 ;
schedule = [1 2 5 5 5] ;
% Scale space configuration
hogCellSize = 8 ;
minScale = -1 ;
maxScale = 3 ;
numOctaveSubdivisions = 3 ;
scales = 2.^linspace(...
minScale,...
maxScale,...
numOctaveSubdivisions*(maxScale-minScale+1)) ;
% -------------------------------------------------------------------------
% Step 5.1: Construct custom training data
% -------------------------------------------------------------------------
% Load object examples
trainImages = {} ;
trainBoxes = [] ;
trainBoxPatches = {} ;
trainBoxImages = {} ;
trainBoxLabels = [] ;
% Construct negative data
names = dir('data/myNegatives/*.jpeg') ;
trainImages = fullfile('data', 'myNegatives', {names.name}) ;
% Construct positive data
names = dir('data/myPositives/*.jpeg') ;
names = fullfile('data', 'myPositives', {names.name}) ;
for i=1:numel(names)
im = imread(names{i}) ;
im = imresize(im, [64 64]) ;
trainBoxes(:,i) = [0.5 ; 0.5 ; 64.5 ; 64.5] ;
trainBoxPatches{i} = im2single(im) ;
trainBoxImages{i} = names{i} ;
trainBoxLabels(i) = 1 ;
end
trainBoxPatches = cat(4, trainBoxPatches{:}) ;
% Compute HOG features of examples (see Step 1.2)
trainBoxHog = {} ;
for i = 1:size(trainBoxPatches,4)
trainBoxHog{i} = vl_hog(trainBoxPatches(:,:,:,i), hogCellSize) ;
end
trainBoxHog = cat(4, trainBoxHog{:}) ;
modelWidth = size(trainBoxHog,2) ;
modelHeight = size(trainBoxHog,1) ;
% -------------------------------------------------------------------------
% Step 5.2: Visualize the training images
% -------------------------------------------------------------------------
figure(1) ; clf ;
subplot(1,2,1) ;
imagesc(vl_imarraysc(trainBoxPatches)) ;
axis off ;
title('Training images (positive samples)') ;
axis equal ;
subplot(1,2,2) ;
imagesc(mean(trainBoxPatches,4)) ;
box off ;
title('Average') ;
axis equal ;
% -------------------------------------------------------------------------
% Step 5.3: Train with hard negative mining
% -------------------------------------------------------------------------
% Initial positive and negative data
pos = trainBoxHog(:,:,:,ismember(trainBoxLabels,targetClass)) ;
neg = zeros(size(pos,1),size(pos,2),size(pos,3),0) ;
for t=1:numHardNegativeMiningIterations
numPos = size(pos,4) ;
numNeg = size(neg,4) ;
C = 1 ;
lambda = 1 / (C * (numPos + numNeg)) ;
fprintf('Hard negative mining iteration %d: pos %d, neg %d\n', ...
t, numPos, numNeg) ;
% Train an SVM model (see Step 2.2)
x = cat(4, pos, neg) ;
x = reshape(x, [], numPos + numNeg) ;
y = [ones(1, size(pos,4)) -ones(1, size(neg,4))] ;
w = vl_svmtrain(x,y,lambda,'epsilon',0.01,'verbose') ;
w = single(reshape(w, modelHeight, modelWidth, [])) ;
% Plot model
figure(2) ; clf ;
imagesc(vl_hog('render', w)) ;
colormap gray ;
axis equal ;
title('SVM HOG model') ;
% Evaluate on training data and mine hard negatives
figure(3) ;
[matches, moreNeg] = ...
evaluateModel(...
vl_colsubset(trainImages', schedule(t), 'beginning'), ...
trainBoxes, trainBoxImages, ...
w, hogCellSize, scales) ;
% Add negatives
neg = cat(4, neg, moreNeg) ;
% Remove negative duplicates
z = reshape(neg, [], size(neg,4)) ;
[~,keep] = unique(z','stable','rows') ;
neg = neg(:,:,:,keep) ;
end
% -------------------------------------------------------------------------
% Step 5.3: Evaluate the model on the test data
% -------------------------------------------------------------------------
im = imread('data/myTestImage.jpeg') ;
im = im2single(im) ;
% Compute detections
[detections, scores] = detect(im, w, hogCellSize, scales) ;
keep = boxsuppress(detections, scores, 0.25) ;
detections = detections(:, keep(1:10)) ;
scores = scores(keep(1:10)) ;
% Plot top detection
figure(3) ; clf ;
imagesc(im) ; axis equal ;
hold on ;
vl_plotbox(detections, 'g', 'linewidth', 2, ...
'label', arrayfun(@(x)sprintf('%.2f',x),scores,'uniformoutput',0)) ;
title('Multiple detections') ;