-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_old.m
167 lines (146 loc) · 4.84 KB
/
train_old.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
function model = train_old(name, model, pos, neg )
% model = train(name, model, pos, neg)
% Train LSVM. (For now it's just an SVM)
%
% SVM learning parameters
C = 0.002*model.numcomponents;
J = 1;
maxsize = 2^28;
globals;
hdrfile = [tmpdir name '.hdr'];
datfile = [tmpdir name '.dat'];
modfile = [tmpdir name '.mod'];
inffile = [tmpdir name '.inf'];
lobfile = [tmpdir name '.lob'];
labelsize = 5; % [label id level x y]
negpos = 0; % last position in data mining
% approximate bound on the number of examples used in each iteration
dim = 0;
for i = 1:model.numcomponents
dim = max(dim, model.components{i}.dim);
end
maxnum = floor(maxsize / (dim * 4));
% Reset some of the tempoaray files, just in case
% reset header file
writeheader(hdrfile, 0, labelsize, model);
% reset info file
fid = fopen(inffile, 'w');
fclose(fid);
% reset initial model
fid = fopen(modfile, 'wb');
fwrite(fid, zeros(sum(model.blocksizes), 1), 'double');
fclose(fid);
% reset lower bounds
writelob(lobfile, model)
% reset data file
fid = fopen(datfile, 'wb');
fclose(fid);
% Find the positive examples and safe them in the data file
fid = fopen(datfile, 'w');
num = poswarp(name, model, 1, pos, fid);
% Add random negatives
num = num + negrandom(name, model, 1, neg, maxnum-num, fid);
fclose(fid);
%num = 79793;
% learn model
writeheader(hdrfile, num, labelsize, model);
% reset initial model
fid = fopen(modfile, 'wb');
fwrite(fid, zeros(sum(model.blocksizes), 1), 'double');
fclose(fid);
% Call the SVM learning code
cmd = sprintf('./learn %.4f %.4f %s %s %s %s %s', ...
C, J, hdrfile, datfile, modfile, inffile, lobfile);
fprintf('executing: %s\n', cmd);
status = unix(cmd);
if status ~= 0
fprintf('command `%s` failed\n', cmd);
keyboard;
end
fprintf('parsing model\n');
blocks = readmodel(modfile, model);
model = parsemodel(model, blocks);
[labels, vals, unique] = readinfo(inffile);
% compute threshold for high recall
P = find((labels == 1) .* unique);
pos_vals = sort(vals(P));
model.thresh = pos_vals(ceil(length(pos_vals)*0.05));
% cache model
save([cachedir name '_model'], 'model');
% get positive examples by warping positive bounding boxes
% we create virtual examples by flipping each image left to right
function num = poswarp(name, model, c, pos, fid)
numpos = length(pos);
warped = warppos(name, model, c, pos);
ridx = model.components{c}.rootindex;
oidx = model.components{c}.offsetindex;
rblocklabel = model.rootfilters{ridx}.blocklabel;
oblocklabel = model.offsets{oidx}.blocklabel;
dim = model.components{c}.dim;
width1 = ceil(model.rootfilters{ridx}.size(2)/2);
width2 = floor(model.rootfilters{ridx}.size(2)/2);
pixels = model.rootfilters{ridx}.size * model.sbin;
minsize = prod(pixels);
num = 0;
for i = 1:numpos
if mod(i,100)==0
fprintf('%s: warped positive: %d/%d\n', name, i, numpos);
end
bbox = [pos(i).x1 pos(i).y1 pos(i).x2 pos(i).y2];
% skip small examples
if (bbox(3)-bbox(1)+1)*(bbox(4)-bbox(2)+1) < minsize
continue
end
% get example
im = warped{i};
feat = features(im, model.sbin);
feat(:,1:width2,:) = feat(:,1:width2,:) + flipfeat(feat(:,width1+1:end,:));
feat = feat(:,1:width1,:);
fwrite(fid, [1 2*i-1 0 0 0 2 dim], 'int32');
fwrite(fid, [oblocklabel 1], 'single');
fwrite(fid, rblocklabel, 'single');
fwrite(fid, feat, 'single');
% get flipped example
feat = features(im(:,end:-1:1,:), model.sbin);
feat(:,1:width2,:) = feat(:,1:width2,:) + flipfeat(feat(:,width1+1:end,:));
feat = feat(:,1:width1,:);
fwrite(fid, [1 2*i 0 0 0 2 dim], 'int32');
fwrite(fid, [oblocklabel 1], 'single');
fwrite(fid, rblocklabel, 'single');
fwrite(fid, feat, 'single');
num = num+2;
end
% get random negative examples
function num = negrandom(name, model, c, neg, maxnum, fid)
numneg = length(neg);
rndneg = floor(maxnum/numneg);
ridx = model.components{c}.rootindex;
oidx = model.components{c}.offsetindex;
rblocklabel = model.rootfilters{ridx}.blocklabel;
oblocklabel = model.offsets{oidx}.blocklabel;
rsize = model.rootfilters{ridx}.size;
width1 = ceil(rsize(2)/2);
width2 = floor(rsize(2)/2);
dim = model.components{c}.dim;
num = 0;
for i = 1:numneg
if mod(i,100)==0
fprintf('%s: random negatives: %d/%d\n', name, i, numneg);
end
im = color(imread(neg(i).im));
feat = features(double(im), model.sbin);
if size(feat,2) > rsize(2) && size(feat,1) > rsize(1)
for j = 1:rndneg
x = random('unid', size(feat,2)-rsize(2)+1);
y = random('unid', size(feat,1)-rsize(1)+1);
f = feat(y:y+rsize(1)-1, x:x+rsize(2)-1,:);
f(:,1:width2,:) = f(:,1:width2,:) + flipfeat(f(:,width1+1:end,:));
f = f(:,1:width1,:);
fwrite(fid, [-1 (i-1)*rndneg+j 0 0 0 2 dim], 'int32');
fwrite(fid, [oblocklabel 1], 'single');
fwrite(fid, rblocklabel, 'single');
fwrite(fid, f, 'single');
end
num = num+rndneg;
end
end