forked from rbgirshick/DeepPyramid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_deep_pyramid.m
113 lines (95 loc) · 3.09 KB
/
demo_deep_pyramid.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
function pyra = demo_deep_pyramid(im)
% Demonstrate basic usage and visualize features.
%
%
% AUTORIGHTS
% ---------------------------------------------------------
% Copyright (c) 2014, Ross Girshick
%
% This file is part of the DeepPyramid 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.
% ---------------------------------------------------------
if exist('caffe') ~= 3
error('You must add matcaffe to your path.');
end
if ~exist('data/caffe_nets/ilsvrc_2012_train_iter_310k')
error(['You need the CNN model in %s. ' ...
'You can get this model by following ' ...
'the R-CNN installation instructions.'], ...
'data/caffe_nets/ilsvrc_2012_train_iter_310k');
end
if 1
% real use settings (compute features using the GPU)
USE_GPU = true;
USE_CACHE = false;
USE_CAFFE = true;
else
% fast demo settings
USE_GPU = false;
USE_CACHE = true;
USE_CAFFE = false;
end
if ~exist('im', 'var') || isempty(im)
im = imread('000084.jpg');
end
bbox = [263 145 381 225];
cnn = init_cnn_model('use_gpu', USE_GPU, 'use_caffe', USE_CAFFE);
if USE_CACHE
cache_opts.cache_file = './cached_pyra.mat';
cache_opts.debug = true;
cache_opts.write_on_miss = true;
th = tic;
pyra = deep_pyramid_cache_wrapper(im, cnn, cache_opts);
fprintf('deep_pyramid_cache_wrapper took %.3fs\n', toc(th));
else
th = tic;
pyra = deep_pyramid(im, cnn);
fprintf('deep_pyramid took %.3fs\n', toc(th));
end
padx = 0;
pady = 0;
pyra = deep_pyramid_add_padding(pyra, padx, pady);
fprintf(['Press almost any key (with fig focused) to loop through ' ...
'feature channels (or esc to exit).\n']);
for channel = 1:256
vis_pyramid(im, pyra, bbox, channel);
[~, ~, key_code] = ginput(1);
if key_code == 27
break;
end
end
% ------------------------------------------------------------------------
function vis_pyramid(im, pyra, bbox, channel)
% ------------------------------------------------------------------------
pyra_boxes = im_to_pyra_coords(pyra, bbox);
clf;
rows = 2;
cols = 4;
subplot(rows, cols, 1);
imagesc(im);
axis image;
rectangle('Position', bbox_to_xywh(bbox), 'EdgeColor', 'g');
title(sprintf('input image feature %d', channel));
max_val = 0;
for level = 1:pyra.num_levels
f = pyra.feat{level}(:,:,channel);
max_val = max(max_val, max(f(:)));
end
ld = load('green_colormap');
colormap(ld.map); clear ld;
for level = 1:pyra.num_levels
subplot(rows, cols, level+1);
imagesc(pyra.feat{level}(:,:,channel), [0 max_val]);
axis image;
rectangle('Position', bbox_to_xywh(pyra_boxes{level}), 'EdgeColor', 'r');
title(sprintf('level %d; scale = %.2fx', level, pyra.scales(level)));
% project pyramid box back to image and display as red
im_bbox = pyra_to_im_coords(pyra, [pyra_boxes{level} level]);
subplot(rows, cols, 1);
rectangle('Position', bbox_to_xywh(im_bbox), 'EdgeColor', 'r');
%text(im_bbox(1), im_bbox(2), sprintf('%d', level));
end
function xywh = bbox_to_xywh(bbox)
xywh = [bbox(1) bbox(2) bbox(3)-bbox(1)+1 bbox(4)-bbox(2)+1];