forked from fanxu/ffld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertmodel5.m
100 lines (84 loc) · 3.62 KB
/
convertmodel5.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
%--------------------------------------------------------------------------
% Implementation of the papers "Exact Acceleration of Linear Object
% Detectors", 12th European Conference on Computer Vision, 2012 and "Deformable
% Part Models with Individual Part Scaling", 24th British Machine Vision
% Conference, 2013.
%
% Copyright (c) 2013 Idiap Research Institute, <http://www.idiap.ch/>
% Written by Charles Dubout <[email protected]>
%
% This file is part of FFLDv2 (the Fast Fourier Linear Detector version 2)
%
% FFLDv2 is free software: you can redistribute it and/or modify it under the
% terms of the GNU Affero General Public License version 3 as published by the
% Free Software Foundation.
%
% FFLDv2 is distributed in the hope that it will be useful, but WITHOUT ANY
% WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
% FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
% details.
%
% You should have received a copy of the GNU Affero General Public License
% along with FFLDv2. If not, see <http://www.gnu.org/licenses/>.
%--------------------------------------------------------------------------
function convertmodel5(model, filename)
%
% convertmodel5(model, filename) convert the models of [1, 2] version 5 into
% a file readable by FFLDv2.
%
% [1] P. Felzenszwalb, R. Girshick, D. McAllester and D. Ramanan.
% Object Detection with Discriminatively Trained Part Based Models.
% IEEE Transactions on Pattern Analysis and Machine Intelligence,
% Vol. 32, No. 9, 2010
%
% [2] R. Girshick, P. Felzenszwalb, and D. McAllester.
% Discriminatively Trained Deformable Part Models, Release 5.
% <http://people.cs.uchicago.edu/~rbg/latent-release5/>
%
fileID = fopen(filename, 'w');
nbModels = length(model.rules{model.start});
fprintf(fileID, '%d\n', nbModels);
for i = 1:nbModels
rhs = model.rules{model.start}(i).rhs;
nbParts = length(rhs);
% Assume the root filter is first on the rhs of the start rules
if model.symbols(rhs(1)).type == 'T'
% Handle case where there's no deformation model for the root
root = model.symbols(rhs(1)).filter;
bias = 0;
else
% Handle case where there is a deformation model for the root
root = model.symbols(model.rules{rhs(1)}(1).rhs).filter;
bias = model_get_block(model, model.rules{model.start}(i).offset) * model.features.bias;
end
fprintf(fileID, '%d %g\n', nbParts, bias);
% FFLD adds instead of subtracting the deformation cost
def = -model_get_block(model, model.rules{rhs(1)}(1).def);
% Swap features 28 and 31
w = model_get_block(model, model.filters(root));
w = w(:, :, [1:27 31 29 30 28 32]);
fprintf(fileID, '%d %d %d 0 0 %g %g %g %g\n', size(w,1), ...
size(w,2), size(w,3), def);
for y = 1:size(w,1)
for x = 1:size(w,2)
fprintf(fileID, '%g ', w(y, x, :));
end
fprintf(fileID, '\n');
end
for j = 2:nbParts
part = model.symbols(model.rules{rhs(j)}(1).rhs).filter;
anc = model.rules{model.start}(i).anchor{j};
def = -model_get_block(model, model.rules{rhs(j)}(1).def);
w = model_get_block(model, model.filters(part));
w = w(:, :, [1:27 31 29 30 28 32]);
fprintf(fileID, '%d %d %d %d %d %g %g %g %g\n', size(w,1), ...
size(w,2), size(w,3), anc(1), anc(2), def);
for y = 1:size(w,1)
for x = 1:size(w,2)
fprintf(fileID, '%g ', w(y, x, :));
end
fprintf(fileID, '\n');
end
end
end
fclose(fileID);