1% This script demonstrates SVMOCAS_LBP solver for training two-class
2% classifiers of gray scale images. The images are described by a pyramid
3% of Local Binary Patterns (LBP) features. In order to make the classifier
4% translation invariant, SVMOCAS_LBP allows to generate virtual examples
5% by shifting a base window along x- and y-axis of the input images.
6% SVMOCAS_LBP implements the COFFIN framework, i.e. the virtual examples
7% are computed on demand which leads to substantial memory savings.
8%
9% For more details see:
10%  S. Sonnenburg, V. Franc.  COFFIN: A Computational Framework for Linear SVMs.
11%  In Proc. of ICML 2010. Haifa.
12%
13% Image database provided by courtesy of
14%  Eyedea Recognition LtD. (http://www.eyedea.cz)
15%
16
17load('./data/gender_images.mat',...
18     'trn_male_images','trn_female_images',...
19     'tst_male_images','tst_female_images','IMAGE_SIZE');
20
21%% Parameter of LBP feature representation
22LBP_PYRAMID = 4;       % height of LBP pyramid; 1 means that LBP filters are
23                       % applied only on the original image otherwise
24                       % the image is LBP_PYRAMID-times downscaled
25
26%% Definition of base window on which the classifier is applied
27WINDOW_SIZE = [60 40];     % [height width]
28WINDOW_TOP_LEFT_COL = 20;
29WINDOW_TOP_LEFT_ROW = 15;
30
31%% Parameters SVM solver
32X0 = 1;                 % added constant feature
33C = 0.001;              % SVM C
34SVMOCAS_BUFSIZE = 100;  % number of cutting planes to store; single CP requires ~ 0.7MB
35SVMOCAS_TOLREL = 0.01;  % precision of SVM solution
36SVMOCAS_METHOD = 1;     % 1.. OCAS, 0..BMRM
37
38%% Virtual examples
39% The virtual examples are created by shifting the base window in x and
40% y-axis and mirroring the image along its vertical axis.
41
42switch input('use virtual examples (0-no, 1-yes)?:')
43    case 0
44        % testing error 20%
45        USE_VIRTUAL_EXAMPLES = 0;
46
47    case 1
48        % testing error 14%
49        USE_VIRTUAL_EXAMPLES = 1;
50        SHIFT_X = [-1 0 1];
51        SHIFT_Y = [-1 0 1];
52        MIRROR = [0];
53
54end
55
56%% prepare training examples
57nTrnMales = size(trn_male_images,2);
58nTrnFemales = size(trn_male_images,2);
59
60if ~USE_VIRTUAL_EXAMPLES
61    BOX = [WINDOW_TOP_LEFT_COL;WINDOW_TOP_LEFT_ROW;0];
62    wins = [ [1:nTrnMales nTrnMales+[1:nTrnFemales]]; repmat(BOX,1,nTrnFemales+nTrnMales)];
63    labels = [ones(1,nTrnMales) -ones(1,nTrnFemales)];
64
65else
66    % define virtual examples by shifting the base window
67    BOX = zeros(3,length(MIRROR)*length(SHIFT_Y)*length(SHIFT_X),'uint32');
68    cnt = 0;
69    for u=SHIFT_X
70        for v=SHIFT_Y
71           for mirror=MIRROR
72               cnt = cnt + 1;
73               BOX(:,cnt) = uint32([WINDOW_TOP_LEFT_COL+u; WINDOW_TOP_LEFT_ROW+v;mirror]);
74           end
75        end
76    end
77
78    % prepare virtual examples for each training image
79    wins = zeros(4,size(BOX,2)*(nTrnMales+nTrnFemales),'uint32');
80    labels = zeros(size(BOX,2)*(nTrnMales+nTrnFemales),1);
81    cnt = 0;
82    for i=1:(nTrnMales+nTrnFemales)
83        wins(1,cnt+1:cnt+size(BOX,2)) = uint32(i);
84        wins(2:4,cnt+1:cnt+size(BOX,2)) = BOX;
85        if i <= nTrnMales
86            labels(cnt+1:cnt+size(BOX,2)) = 1;
87        else
88            labels(cnt+1:cnt+size(BOX,2)) = -1;
89        end
90        cnt = cnt + size(BOX,2);
91    end
92
93end
94
95%% train SVM linear classifier
96[W,W0,stat] = svmocas_lbp([trn_male_images trn_female_images], IMAGE_SIZE,...
97                          uint32(wins), WINDOW_SIZE, LBP_PYRAMID, X0, labels, C, ...
98                          1, SVMOCAS_TOLREL,0,0,SVMOCAS_BUFSIZE);
99
100%% compute features for testing images and evaluate the tained classifier
101BOX = [WINDOW_TOP_LEFT_COL;WINDOW_TOP_LEFT_ROW;0];
102
103nTstMale = size(tst_male_images,2);
104tst_labels = ones(1,nTstMale);
105tst_feat = lbppyr_features(tst_male_images,IMAGE_SIZE, ...
106                          uint32([1:nTstMale;repmat(BOX,1,nTstMale)]), ...
107                          WINDOW_SIZE, LBP_PYRAMID,0);
108
109nTstFemale = size(tst_female_images,2);
110tst_labels = [tst_labels -ones(1,nTstFemale)];
111tst_feat = [tst_feat lbppyr_features(tst_female_images,IMAGE_SIZE, ...
112                          uint32([1:nTstFemale;repmat(BOX,1,nTstFemale)]), ...
113                          WINDOW_SIZE, LBP_PYRAMID,0)];
114
115ypred = sign(W'*double(tst_feat) + W0);
116tst_error = sum(ypred(:) ~= tst_labels(:))/length(tst_labels);
117fprintf('Testing error: %f%%\n', tst_error*100);
118