forked from ShaoqingRen/SPP_net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript_spp_voc.m
142 lines (118 loc) · 6.07 KB
/
Script_spp_voc.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
function Script_spp_voc()
% Script_spp_voc()
%
% AUTORIGHTS
% ---------------------------------------------------------
% Copyright (c) 2014, Shaoqing Ren
%
% This file is part of the SPP code and is available
% under the terms of the Simplified BSD License provided in
% LICENSE. Please retain this notice and LICENSE if you use
% this file (or any portion of it) in your project.
% ---------------------------------------------------------
clc;
% -------------------- CONFIG --------------------
% model def
opts.net_file = fullfile(pwd, 'data\cnn_model\Zeiler_conv5\Zeiler_conv5');
opts.net_def_file = fullfile(pwd, 'data\cnn_model\Zeiler_conv5\Zeiler_spm_scale224_test_conv5.prototxt');
opts.spp_params_def = fullfile(pwd, 'data\cnn_model\Zeiler_conv5\spp_config');
% for feature cache
opts.feat_cache_copy_from = 'Zeiler_conv5';
opts.feat_cache = 'Zeiler_conv5';
opts.spm_im_size = [480 576 688 874 1200];
% for finetune
opts.flip_finetune = true;
opts.finetune_cache_name = 'Zeiler_conv5_ft(5s_flip)';
opts.finetune_rst_dir = fullfile(pwd, 'finetuning', opts.finetune_cache_name);
opts.finetune_net_def_file = fullfile(pwd, 'model-defs\pascal_finetune_fc_spm_solver.prototxt');
% for svm train and test
opts.layer = 7;
opts.flip = false;
opts.cache_name = 'Zeiler_conv5_ft(5s_flip)_fc7';
% change to point to your VOCdevkit install
opts.devkit = './datasets/VOCdevkit2007';
% detail setting
opts.with_selective_search = true;
opts.with_edge_box = false;
opts.with_hard_samples = false;
% train set
opts = perpare_train_data(opts, opts.flip | opts.flip_finetune);
opts.feat_cache_train = {opts.feat_cache};
opts.imdb_for_negative_mining = [1];
opts.neg_ovr_threshs = {[-1, 0.3]};
% test set
opts.imdb_test = imdb_from_voc(opts.devkit, 'test', '2007');
opts.roidb_test = opts.imdb_test.roidb_func(opts.imdb_test, opts.with_hard_samples, opts.with_selective_search, opts.with_edge_box);
opts.feat_cache_test = opts.feat_cache;
opts.gpu_id = 1;
% ------------------------------------------------
% ------------------------------------------------
g = gpuDevice(opts.gpu_id);
%
%% extract last conv feature
opts = perpare_train_data(opts, opts.flip | opts.flip_finetune);
spp_exp_cache_features_voc('trainval', opts);
spp_exp_cache_features_voc('test', opts);
%% finetune fc layers and change model to finetuned one
opts = perpare_train_data(opts, opts.flip_finetune);
[~, ~, test_net_file, opts.max_iter] = parse_copy_finetune_prototxt(opts.finetune_net_def_file, opts.finetune_rst_dir);
finetuned_model_path = spp_finetune_voc(opts); % finetune
% finetuned_model_path = fullfile(opts.finetune_rst_dir, 'FT_iter_186000'); % load from finetuned file
opts.net_file = finetuned_model_path;
opts.net_def_file = fullfile(opts.finetune_rst_dir, test_net_file);
%
%% svm train and test
opts = perpare_train_data(opts, opts.flip);
spp_exp_train_and_test_voc(opts);
%% box regression
spp_exp_bbox_reg_train_and_test_voc(opts);
reset(g);
end
function opts = perpare_train_data(opts, flip)
opts.imdb_train = { imdb_from_voc(opts.devkit, 'trainval', '2007', flip) };
opts.roidb_train = cellfun(@(x) x.roidb_func(x, opts.with_hard_samples, opts.with_selective_search, opts.with_edge_box), opts.imdb_train, 'UniformOutput', false);
end
% ------------------------------------------------
function [solver_file, train_net_file, test_net_file, max_iter] = parse_copy_finetune_prototxt(solver_file_path, dest_dir)
% copy solver, train_net and test_net to destination folder
% ------------------------------------------------
[folder, solver_file, ext] = fileparts(solver_file_path);
solver_file = [solver_file, ext];
solver_prototxt_text = textread(solver_file_path, '%[^\n]');
try % for old caffe
train_net_file_pattern = '(?<=train_net[ :]*")[^"]*(?=")';
test_net_file_pattern = '(?<=test_net[ :]*")[^"]*(?=")';
train_net_file = cellfun(@(x) regexp(x, train_net_file_pattern, 'match'), solver_prototxt_text, 'UniformOutput', false);
train_net_file = train_net_file(cellfun(@(x) ~isempty(x), train_net_file, 'UniformOutput', true));
if isempty(train_net_file)
error('invalid solver file %s \n', solver_file_path);
end
train_net_file = train_net_file{1}{1};
test_net_file = cellfun(@(x) regexp(x, test_net_file_pattern, 'match'), solver_prototxt_text, 'UniformOutput', false);
test_net_file = test_net_file(cellfun(@(x) ~isempty(x), test_net_file, 'UniformOutput', true));
if isempty(test_net_file)
error('invalid solver file %s \n', solver_file_path);
end
test_net_file = test_net_file{1}{1};
catch % for new caffe
train_test_net_file_pattern = '(?<=net[ :]*")[^"]*(?=")';
train_test_net_file_pattern = cellfun(@(x) regexp(x, train_test_net_file_pattern, 'match'), solver_prototxt_text, 'UniformOutput', false);
train_test_net_file_pattern = train_test_net_file_pattern(cellfun(@(x) ~isempty(x), train_test_net_file_pattern, 'UniformOutput', true));
if isempty(train_test_net_file_pattern)
error('invalid solver file %s \n', solver_file_path);
end
train_net_file = train_test_net_file_pattern{1}{1};
test_net_file = train_test_net_file_pattern{1}{1};
end
mkdir_if_missing(dest_dir);
copyfile(fullfile(folder, solver_file), dest_dir);
copyfile(fullfile(folder, train_net_file), dest_dir);
copyfile(fullfile(folder, test_net_file), dest_dir);
max_iter_pattern = '(?<=max_iter[ :]*)[0-9]*';
max_iter = cellfun(@(x) regexp(x, max_iter_pattern, 'match'), solver_prototxt_text, 'UniformOutput', false);
max_iter = max_iter(cellfun(@(x) ~isempty(x), max_iter, 'UniformOutput', true));
if isempty(max_iter)
error('invalid solver file %s \n', solver_file_path);
end
max_iter = str2double(max_iter{1}{1});
end