-
Notifications
You must be signed in to change notification settings - Fork 36
/
get_rcnn_features.m
155 lines (132 loc) · 3.92 KB
/
get_rcnn_features.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
143
144
145
146
147
148
149
150
151
152
153
154
155
function code = get_rcnn_features(net, im, regions, varargin)
% GET_RCNN_FEATURES
% This function gets the fc7 features for an image region,
% extracted from the provided mask.
opts.regionBorder = 0.05 ;
opts.batchSize = 96 ;
opts = vl_argparse(opts, varargin) ;
if ~iscell(im)
im = {im} ;
regions = {regions} ;
end
res = [] ;
cache = struct() ;
resetCache() ;
% for each image
function resetCache()
cache.images = cell(1,opts.batchSize) ;
cache.indexes = zeros(2, opts.batchSize) ;
cache.numCached = 0 ;
end
function flushCache()
if cache.numCached == 0, return ; end
images = cat(4, cache.images{:}) ;
images = bsxfun(@minus, images, net.normalization.averageImage) ;
if net.useGpu
images = gpuArray(images) ;
end
res = vl_simplenn(net, images, ...
[], res, ...
'conserveMemory', true, ...
'sync', true) ;
code_ = squeeze(gather(res(end).x)) ;
code_ = bsxfun(@times, 1./sqrt(sum(code_.^2)), code_) ;
for q=1:cache.numCached
code{cache.indexes(1,q)}{cache.indexes(2,q)} = code_(:,q) ;
end
resetCache() ;
end
function appendCache(i,r,im)
cache.numCached = cache.numCached + 1 ;
cache.images{cache.numCached} = im ;
cache.indexes(:,cache.numCached) = [i ; r] ;
if cache.numCached >= opts.batchSize
flushCache() ;
end
end
code = {} ;
for k=1:numel(im)
% for each region in the image plane
for r = 1:numel(regions{k}.labels)
mask = ismember(regions{k}.basis, regions{k}.labels{r}) ;
appendCache(k, r, ...
getRegion(opts, single(im{k}), mask, net.normalization.imageSize(1))) ;
if 0
figure(1) ; clf ;
subplot(2,2,1) ; imagesc(im{k}) ;
subplot(2,2,2) ; imagesc(mask) ;
subplot(2,2,3) ; imagesc(bsxfun(@times, im2single(im{k}), mask)) ;
subplot(2,2,4) ; imagesc(vl_imsc(cache.images{cache.numCached})) ;
drawnow ;
keyboard
end
end
end
flushCache() ;
for k=1:numel(code)
code{k} = cat(2, code{k}{:}) ;
end
end
% -------------------------------------------------------------------------
function reg = getRegion(opts, im, mask, regionSize)
% -------------------------------------------------------------------------
% get a box around the region
switch 'enclosing'
case 'enclosing'
box = enclosingBox(mask) ;
case 'inside'
box = insideBox(mask) ;
end
% include a border around it
xbox = boxrescale(box, 1 + opts.regionBorder) ;
% make it square
if 0
w = diff(box([1 3])) ;
h = diff(box([2 4])) ;
bx = mean(box([1 3])) ;
by = mean(box([2 4])) ;
if w > h
sbox = [bx - w/2 ; by - w/2 ; bx + w/2 ; by + w/2] ;
else
sbox = [bx - w/2 ; by - w/2 ; bx + w/2 ; by + w/2] ;
end
sbox = round(sbox) ;
else
sbox = round(xbox) ;
end
% clip it
sbox = boxclip(sbox, [size(im,2), size(im,1)]) ;
% crop it
sx = sbox(1):sbox(3) ;
sy = sbox(2):sbox(4) ;
reg = im(sy, sx, :) ;
% resize it
reg = imresize(reg, [regionSize, regionSize], 'bicubic') ;
end
% -------------------------------------------------------------------------
function box = insideBox(mask)
% -------------------------------------------------------------------------
mask_ = pad2(single(mask(1:2:end,1:2:end)), 50, 50, 50, 50) ;
[frames,~,info] = vl_covdet(mask_,'DoubleImage',false) ;
frames = frames(:, info.peakScores > 0)*2 ;
if isempty(frames)
box = enclosingBox(mask) ;
return;
end
boxes = [...
frames(1:2,:) - frames([3 6],:) - 100;
frames(1:2,:) + frames([3 6],:) - 100] ;
area = boxarea(boxes) ;
[~,best] = max(boxarea(boxes)) ;
box = boxes(:,best) ;
%figure(101) ; clf ; imagesc(mask);axis equal ;hold on ;vl_plotbox(boxes) ;
%vl_plotbox(box,'linewidth', 4) ; drawnow ;
end
% -------------------------------------------------------------------------
function box = enclosingBox(mask)
% -------------------------------------------------------------------------
[x,y] = meshgrid(1:size(mask,2), 1:size(mask,1)) ;
x = x(mask) ;
y = y(mask) ;
box = [min(x) ; min(y) ; max(x) ; max(y)] ;
end