Skip to content

Commit

Permalink
add demo code. And update some training codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangliliang committed Aug 24, 2016
1 parent 8f50c86 commit dcfbd37
Show file tree
Hide file tree
Showing 10 changed files with 594 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ imdb/cache
*.caffemodel
*.mat


# Windows Installer files
*.cab
*.msi
Expand Down
13 changes: 7 additions & 6 deletions datasets/caltech/extract_img_anno.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

% extract_img_anno()
% --------------------------------------------------------
% RPN_BF
% Copyright (c) 2016, Liliang Zhang
% Licensed under The MIT License [see LICENSE for details]
% --------------------------------------------------------

dataDir='./datasets/caltech/';
addpath(genpath('./external/code3.2.1'));

for s=1:2
if(s==1), type='test'; skip=[]; else type='train'; skip=3; end
dbInfo(['Usa' type]); %if(s==2), type=['train' int2str2(skip,2)]; end
dbInfo(['Usa' type]);
if(exist([dataDir type '/annotations'],'dir')), continue; end
dbExtract([dataDir type],1,skip);
end

% dataset.imdb_test = imdb_from_caltech_flip('./datasets/caltech', 'test', false) ;

% dataset.imdb_train = imdb_from_caltech_flip('./datasets/caltech', 'train') ;
% dataset.roidb_test = dataset.imdb_test.roidb_func(dataset.imdb_test);
109 changes: 109 additions & 0 deletions experiments/+Faster_RCNN_Train/do_proposal_test_caltech_boost.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
function roidb_BF = do_proposal_test_caltech_boost(conf, model_stage, imdb, roidb)

cache_dir = fullfile(pwd, 'output', conf.exp_name, 'rpn_cachedir', model_stage.cache_name, imdb.name);
save_roidb_name = fullfile(cache_dir, [ 'roidb_' imdb.name '_BF.mat']);
if exist(save_roidb_name, 'file')
ld = load(save_roidb_name);
roidb_BF = ld.roidb_BF;
clear ld;
return;
end


aboxes = proposal_test_caltech(conf, imdb, ...
'net_def_file', model_stage.test_net_def_file, ...
'net_file', model_stage.output_model_file, ...
'cache_name', model_stage.cache_name);


fprintf('Doing nms ... ');
% average_thres = model_stage.nms.nms_overlap_thres;
average_thres = -1;
ave_per_image_topN = model_stage.nms.after_nms_topN;
model_stage.nms.after_nms_topN = -1;
aboxes = boxes_filter(aboxes, model_stage.nms.per_nms_topN, model_stage.nms.nms_overlap_thres, model_stage.nms.after_nms_topN, conf.use_gpu, average_thres);
fprintf(' Done.\n');

% find the lower score threshold
max_sample_num = 5000;
sample_aboxes = aboxes(randperm(length(aboxes), min(length(aboxes), max_sample_num)));
scores = zeros(ave_per_image_topN*length(sample_aboxes), 1);
for i = 1:length(sample_aboxes)
s_scores = sort([scores; sample_aboxes{i}(:, end)], 'descend');
scores = s_scores(1:ave_per_image_topN*length(sample_aboxes));
end
score_thresh = scores(end);
fprintf('score_threshold:%f\n', score_thresh);
% drop the boxes which scores are lower than the thres
for i = 1:length(aboxes)
aboxes{i} = aboxes{i}(aboxes{i}(:, end) > score_thresh, :);
end

% eval the gt recall
gt_num = 0;
gt_re_num_5 = 0;
gt_re_num_7 = 0;
gt_re_num_8 = 0;
gt_re_num_9 = 0;
for i = 1:length(roidb.rois)
% gts = roidb.rois(i).boxes;
gts = roidb.rois(i).boxes(roidb.rois(i).ignores~=1, :);
if ~isempty(gts)
rois = aboxes{i}(:, 1:4);
max_ols = max(boxoverlap(rois, gts));
gt_num = gt_num + size(gts, 1);
gt_re_num_5 = gt_re_num_5 + sum(max_ols >= 0.5);
gt_re_num_7 = gt_re_num_7 + sum(max_ols >= 0.7);
gt_re_num_8 = gt_re_num_8 + sum(max_ols >= 0.8);
gt_re_num_9 = gt_re_num_9 + sum(max_ols >= 0.9);
end
end
fprintf('gt recall rate (ol >0.5) = %.4f\n', gt_re_num_5 / gt_num);
fprintf('gt recall rate (ol >0.7) = %.4f\n', gt_re_num_7 / gt_num);
fprintf('gt recall rate (ol >0.8) = %.4f\n', gt_re_num_8 / gt_num);
fprintf('gt recall rate (ol >0.9) = %.4f\n', gt_re_num_9 / gt_num);

roidb_regions.boxes = aboxes;
roidb_regions.images = imdb.image_ids;
roidb_BF = roidb_from_proposal_score(imdb, roidb, roidb_regions, ...
'keep_raw_proposal', false);

save(save_roidb_name, 'roidb_BF', '-v7.3');
end

function aboxes = boxes_filter(aboxes, per_nms_topN, nms_overlap_thres, after_nms_topN, use_gpu, average_thres)
% to speed up nms
if per_nms_topN > 0
aboxes = cellfun(@(x) x(1:min(size(x, 1), per_nms_topN), :), aboxes, 'UniformOutput', false);
end
% do nms
if nms_overlap_thres > 0 && nms_overlap_thres < 1
if average_thres > 0
for i = 1:length(aboxes)
tic_toc_print('weighted ave nms: %d / %d \n', i, length(aboxes));
aboxes{i} = get_keep_boxes(aboxes{i}, 0, nms_overlap_thres, average_thres);
end
else
if use_gpu
for i = 1:length(aboxes)
tic_toc_print('nms: %d / %d \n', i, length(aboxes));
aboxes{i} = aboxes{i}(nms(aboxes{i}, nms_overlap_thres, use_gpu), :);
end
else
parfor i = 1:length(aboxes)
aboxes{i} = aboxes{i}(nms(aboxes{i}, nms_overlap_thres), :);
end
end
end
end
aver_boxes_num = mean(cellfun(@(x) size(x, 1), aboxes, 'UniformOutput', true));
% fprintf('aver_boxes_num = %d, select top %d\n', round(aver_boxes_num), after_nms_topN);
if after_nms_topN > 0
aboxes = cellfun(@(x) x(1:min(size(x, 1), after_nms_topN), :), aboxes, 'UniformOutput', false);
end
end
%
% function regions = make_roidb_regions(aboxes, images)
% regions.boxes = aboxes;
% regions.images = images;
% end
175 changes: 175 additions & 0 deletions experiments/script_rpn_bf_pedestrian_VGG16_caltech_demo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
function script_rpn_bf_pedestrian_VGG16_caltech_demo()
close all;
clc;
clear mex;
clear is_valid_handle; % to clear init_key
run(fullfile(fileparts(fileparts(mfilename('fullpath'))), 'startup'));
%% -------------------- CONFIG --------------------
opts.caffe_version = 'caffe_faster_rcnn';
opts.gpu_id = auto_select_gpu;
active_caffe_mex(opts.gpu_id, opts.caffe_version);

opts.per_nms_topN = -1;
opts.nms_overlap_thres = 0.7;
opts.after_nms_topN = 100;
opts.use_gpu = true;

opts.test_scales = 720;
opts.test_max_size = 960;
opts.feat_stride = 16;
opts.test_binary = false;
opts.test_min_box_size = 16;
opts.test_min_box_height = 50;
opts.test_drop_boxes_runoff_image = true;


%% -------------------- INIT_MODEL --------------------
model_dir = fullfile(pwd, 'output', 'VGG16_caltech_final');


rpn_bf_model.rpn_net_def ...
= fullfile(model_dir, 'rpn_test.prototxt');
rpn_bf_model.rpn_net ...
= fullfile(model_dir, 'final');
rpn_bf_model.bf_net_def ...
= fullfile(model_dir, 'bf_test.prototxt');

rpn_bf_model.conf_rpn.test_scales = opts.test_scales;
rpn_bf_model.conf_rpn.test_max_size = opts.test_max_size;
rpn_bf_model.conf_rpn.max_size = opts.test_max_size;
rpn_bf_model.conf_rpn.feat_stride = opts.feat_stride;
rpn_bf_model.conf_rpn.test_binary = opts.test_binary;
rpn_bf_model.conf_rpn.test_min_box_size = opts.test_min_box_size;
rpn_bf_model.conf_rpn.test_min_box_height = opts.test_min_box_height;
rpn_bf_model.conf_rpn.test_drop_boxes_runoff_image = opts.test_drop_boxes_runoff_image;


rpn_bf_model.conf_bf.test_scales = opts.test_scales;
rpn_bf_model.conf_bf.test_max_size = opts.test_max_size;
rpn_bf_model.conf_bf.max_size = opts.test_max_size;

if opts.use_gpu
ld = load(fullfile(model_dir, 'mean_image'));
rpn_bf_model.conf_rpn.image_means = gpuArray(ld.image_mean);
rpn_bf_model.conf_bf.image_means = gpuArray(ld.image_mean);
clear ld;
end
ld = load(fullfile(model_dir, 'anchors'));
rpn_bf_model.conf_rpn.anchors = ld.anchors;
clear ld;

rpn_net = caffe.Net(rpn_bf_model.rpn_net_def, 'test');
rpn_net.copy_from(rpn_bf_model.rpn_net);
fast_rcnn_net = caffe.Net(rpn_bf_model.bf_net_def, 'test');


% set gpu/cpu
if opts.use_gpu
caffe.set_mode_gpu();
else
caffe.set_mode_cpu();
end

% addpath('external/code3.2.1');
addpath(genpath('external/toolbox'));
ld = load(fullfile(model_dir, 'DeepCaltech_otfDetector'));
detector = ld.detector;
clear ld;

rpn_bf_model.conf_bf.nms_thres = 0.5;
rpn_bf_model.conf_bf.cascThr = -1;

featmap_blobs_names = {'conv3_3', 'conv4_3_atrous'};

%% -------------------- WARM UP --------------------
% the first run will be slower; use an empty image to warm up

for j = 1:2 % we warm up 2 times
im = uint8(ones(375, 500, 3)*128);
if opts.use_gpu
im = gpuArray(im);
end
[boxes, scores] = proposal_im_detect_caltech(rpn_bf_model.conf_rpn, rpn_net, im);
aboxes = boxes_filter([boxes, scores], opts.per_nms_topN, opts.nms_overlap_thres, opts.after_nms_topN, opts.use_gpu);
featmap_blobs = cell(size(featmap_blobs_names));
for i = 1:length(featmap_blobs_names);
featmap_blobs{i} = rpn_net.blobs(featmap_blobs_names{i});
end
feat = rois_get_features_from_featmap_ratio(rpn_bf_model.conf_bf, fast_rcnn_net, im, featmap_blobs, aboxes(:, 1:4), 2000, 1);
end

%% -------------------- TESTING --------------------
im_names = {'ped1.jpg', 'ped2.jpg', 'ped3.jpg'};


running_time = [];
for j = 1:length(im_names)

im = imread(fullfile(pwd, im_names{j}));

if opts.use_gpu
im = gpuArray(im);
end

% test rpn
th = tic();
[boxes, scores] = proposal_im_detect(rpn_bf_model.conf_rpn, rpn_net, im);
t_proposal = toc(th);
th = tic();
aboxes = boxes_filter([boxes, scores], opts.per_nms_topN, opts.nms_overlap_thres, opts.after_nms_topN, opts.use_gpu);
t_nms = toc(th);

% test bf
th = tic();
featmap_blobs = cell(size(featmap_blobs_names));
for i = 1:length(featmap_blobs_names);
featmap_blobs{i} = rpn_net.blobs(featmap_blobs_names{i});
end
feat = rois_get_features_from_featmap_ratio(rpn_bf_model.conf_bf, fast_rcnn_net, im, featmap_blobs, aboxes(:, 1:4), 2000, 1);
scores = adaBoostApply(feat, detector.clf);
bbs = [aboxes(:, 1:4) scores];
sel_idx = nms(bbs, rpn_bf_model.conf_bf.nms_thres);
sel_idx = intersect(sel_idx, find(bbs(:, end) > rpn_bf_model.conf_bf.cascThr));
scores = scores(sel_idx, :);
boxes = aboxes(sel_idx, 1:4);
t_detection = toc(th);

fprintf('%s (%dx%d): time %.3fs (resize+conv+proposal: %.3fs, nms+regionwise: %.3fs)\n', im_names{j}, ...
size(im, 2), size(im, 1), t_proposal + t_nms + t_detection, t_proposal, t_nms+t_detection);
running_time(end+1) = t_proposal + t_nms + t_detection;

% visualize
classes = {'pedestrian'};
boxes_cell = cell(length(classes), 1);
thres = 0.6;
for i = 1:length(boxes_cell)
boxes_cell{i} = [boxes(:, (1+(i-1)*4):(i*4)), scores(:, i)];
boxes_cell{i} = boxes_cell{i}(nms(boxes_cell{i}, 0.3), :);

I = boxes_cell{i}(:, 5) >= thres;
boxes_cell{i} = boxes_cell{i}(I, :);
end
figure(j);
showboxes(im, boxes_cell, classes, 'voc');
pause(0.1);
end
fprintf('mean time: %.3fs\n', mean(running_time));

caffe.reset_all();
clear mex;

end

function aboxes = boxes_filter(aboxes, per_nms_topN, nms_overlap_thres, after_nms_topN, use_gpu)
% to speed up nms
if per_nms_topN > 0
aboxes = aboxes(1:min(length(aboxes), per_nms_topN), :);
end
% do nms
if nms_overlap_thres > 0 && nms_overlap_thres < 1
aboxes = aboxes(nms(aboxes, nms_overlap_thres, use_gpu), :);
end
if after_nms_topN > 0
aboxes = aboxes(1:min(length(aboxes), after_nms_topN), :);
end
end
1 change: 0 additions & 1 deletion experiments/script_rpn_pedestrian_VGG16_caltech.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function script_rpn_pedestrian_VGG16_caltech()
model = Model.VGG16_for_rpn_pedestrian_caltech(exp_name);
% cache base
cache_base_proposal = 'rpn_caltech_vgg_16layers';
cache_base_fast_rcnn = '';
% train/test data
dataset = [];
% use_flipped = true;
Expand Down
Loading

0 comments on commit dcfbd37

Please sign in to comment.