forked from abhi2610/exemplarsvm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esvm_train_single_exemplar.m
91 lines (77 loc) · 3.46 KB
/
esvm_train_single_exemplar.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
% DEMO: Training Exemplar-SVMs from a single image
%
% Copyright (C) 2011-12 by Tomasz Malisiewicz
% (Restructured and edited by Abhinav Shrivastava)
% All rights reserved.
%
% This file is part of the Exemplar-SVM library and is made
% available under the terms of the MIT license (see COPYING file).
% Project homepage: https://github.com/quantombone/exemplarsvm
%
function [models] = esvm_train_single_exemplar(I, bb, negFolder)
% Train model for a single image: esvm_train_single_exemplar returns the
% trained model for given input, trained against the given dataset of
% images.
% [models]: Trained model
% [I]: Input image in double format
% [bb]: Bounding box of the object of interest
% (for complete image bb should be [1 1 size(I,2) size(I,1)]
% [negFolder]: Folder path (string) that contains negative images
if nargin==0
%Input query image
I = im2double(imread('./images/painting.jpg'));
%Bounding-box of region-of-interest (which is usually entire images in case
%of full image matching and object bounding-box in case of detection tasks.
bb = [1 1 size(I, 2) size(I, 1)];
%Folder that contains our negative image or rather dataset images that are
%random images from web.
negFolder = '~/datasetImages/';
end
addpath(genpath(pwd))
%% Make Positive and Negative sets
[pos_set, neg_set] = esvm_get_positive_negative_sets(I, bb, negFolder);
models_name = 'image';
%% Set exemplar-initialization parameters
params = esvm_get_default_params;
params.init_params.sbin = 8;
params.init_params.MAXDIM = 15;
params.model_type = 'exemplar';
%enable display so that nice visualizations pop up during learning
params.dataset_params.display = 1;
%if localdir is not set, we do not dump files
%params.dataset_params.localdir = '/nfs/baikal/tmalisie/synthetic/';
%%Initialize exemplar stream
stream_params.stream_set_name = 'trainval';
stream_params.stream_max_ex = 10;
stream_params.must_have_seg = 0;
stream_params.must_have_seg_string = '';
stream_params.model_type = 'exemplar'; %must be scene or exemplar
%assign pos_set as variable, because we need it for visualization
stream_params.pos_set = pos_set;
stream_params.cls = '';
%% Get the positive stream
e_stream_set = esvm_get_pascal_stream(stream_params, params.dataset_params);
%% Initialize Exemplars
% Each exemplar will have a figure, where on the first image is
% the exemplar's image, along with the exemplar bounding box and
% HOG grid overlayed. The second image shows the HOG mask along
% with its offset to the ground-truth bounding box. The third
% image shows the initial HOG features used to define the exemplar.
initial_models = esvm_initialize_exemplars(e_stream_set, params, ...
models_name);
%% Set exemplar-svm training parameters
train_params = params;
train_params.detect_max_scale = 1.0;
train_params.train_max_mined_images = 10000;
train_params.detect_max_windows_per_exemplar = 400;
%% Perform Exemplar-SVM training
% Because display is turned on, we will show the result of each
% exemplar's training iteration. Each iteration shows a
% diagnostic first column then the remaining rows are the top
% negative support vectors used to define the exemplar's decision
% boundary. The diagnostic row shows: exemplar, w's positive
% part, w's negative part, and four mean support vector images,
% where the means are computed with the first 1:N/4, 1:N/2, .. ,
% 1:N support vectors.
[models] = esvm_train_exemplars(initial_models, ...
neg_set, train_params);