-
Notifications
You must be signed in to change notification settings - Fork 23
/
roi_pooling_2d.py
317 lines (271 loc) · 13.9 KB
/
roi_pooling_2d.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
ROI pooling.
Basically copied from chainer's officials, but I modified a few lines
because chainer (1.9.0)'s implementation did not work on cpu.
Souece: https://github.com/pfnet/chainer/blob/65030ac4ce0050685212d746ef8545ee0817d42e/chainer/functions/pooling/roi_pooling_2d.py
'''
import numpy
import six
from chainer import cuda
from chainer import function
from chainer.utils import type_check
def _roi_pooling_slice(size, stride, max_size, roi_offset):
start = int(numpy.floor(size * stride))
end = int(numpy.ceil((size + 1) * stride))
start = min(max(start + roi_offset, 0), max_size)
end = min(max(end + roi_offset, 0), max_size)
return slice(start, end), end - start
class ROIPooling2D(function.Function):
"""RoI pooling over a set of 2d planes."""
def __init__(self, outh, outw, spatial_scale):
self.outh, self.outw = outh, outw
self.spatial_scale = spatial_scale
def check_type_forward(self, in_types):
type_check.expect(in_types.size() == 2)
x_type, roi_type = in_types
type_check.expect(
x_type.dtype == numpy.float32,
x_type.ndim == 4,
roi_type.dtype == numpy.float32,
roi_type.ndim == 2,
roi_type.shape[1] == 5,
)
def forward_cpu(self, inputs):
bottom_data, bottom_rois = inputs
#<Here is moditied part>
channels, height, width = bottom_data.shape[1:]
n_rois = bottom_rois.shape[0]
#</Here is moditied part>
top_data = numpy.empty((n_rois, channels, self.outh, self.outw),
dtype=numpy.float32)
self.argmax_data = numpy.empty_like(top_data).astype(numpy.int32)
for i_roi in six.moves.range(n_rois):
idx, xmin, ymin, xmax, ymax = bottom_rois[i_roi]
xmin = int(round(xmin * self.spatial_scale))
xmax = int(round(xmax * self.spatial_scale))
ymin = int(round(ymin * self.spatial_scale))
ymax = int(round(ymax * self.spatial_scale))
roi_width = max(xmax - xmin + 1, 1)
roi_height = max(ymax - ymin + 1, 1)
strideh = 1. * roi_height / self.outh
stridew = 1. * roi_width / self.outw
for outh in six.moves.range(self.outh):
sliceh, lenh = _roi_pooling_slice(
outh, strideh, height, ymin)
if sliceh.stop <= sliceh.start:
continue
for outw in six.moves.range(self.outw):
slicew, lenw = _roi_pooling_slice(
outw, stridew, width, xmin)
if slicew.stop <= slicew.start:
continue
roi_data = bottom_data[int(idx), :, sliceh, slicew]\
.reshape(channels, -1)
top_data[i_roi, :, outh, outw] =\
numpy.max(roi_data, axis=1)
# get the max idx respect to feature_maps coordinates
max_idx_slice = numpy.unravel_index(
numpy.argmax(roi_data, axis=1), (lenh, lenw))
max_idx_slice_h = max_idx_slice[0] + sliceh.start
max_idx_slice_w = max_idx_slice[1] + slicew.start
max_idx_slice = max_idx_slice_h * width + max_idx_slice_w
self.argmax_data[i_roi, :, outh, outw] = max_idx_slice
return top_data,
def forward_gpu(self, inputs):
bottom_data, bottom_rois = inputs
channels, height, width = bottom_data.shape[1:]
n_rois = bottom_rois.shape[0]
top_data = cuda.cupy.empty((n_rois, channels, self.outh,
self.outw), dtype=numpy.float32)
self.argmax_data = cuda.cupy.empty_like(top_data).astype(numpy.int32)
cuda.cupy.ElementwiseKernel(
'''
raw float32 bottom_data, float32 spatial_scale, int32 channels,
int32 height, int32 width, int32 pooled_height, int32 pooled_width,
raw float32 bottom_rois
''',
'float32 top_data, int32 argmax_data',
'''
// pos in output filter
int pw = i % pooled_width;
int ph = (i / pooled_width) % pooled_height;
int c = (i / pooled_width / pooled_height) % channels;
int num = i / pooled_width / pooled_height / channels;
int roi_batch_ind = bottom_rois[num * 5 + 0];
int roi_start_w = round(bottom_rois[num * 5 + 1] * spatial_scale);
int roi_start_h = round(bottom_rois[num * 5 + 2] * spatial_scale);
int roi_end_w = round(bottom_rois[num * 5 + 3] * spatial_scale);
int roi_end_h = round(bottom_rois[num * 5 + 4] * spatial_scale);
// Force malformed ROIs to be 1x1
int roi_width = max(roi_end_w - roi_start_w + 1, 1);
int roi_height = max(roi_end_h - roi_start_h + 1, 1);
float bin_size_h = static_cast<float>(roi_height)
/ static_cast<float>(pooled_height);
float bin_size_w = static_cast<float>(roi_width)
/ static_cast<float>(pooled_width);
int hstart = static_cast<int>(floor(static_cast<float>(ph)
* bin_size_h));
int wstart = static_cast<int>(floor(static_cast<float>(pw)
* bin_size_w));
int hend = static_cast<int>(ceil(static_cast<float>(ph + 1)
* bin_size_h));
int wend = static_cast<int>(ceil(static_cast<float>(pw + 1)
* bin_size_w));
// Add roi offsets and clip to input boundaries
hstart = min(max(hstart + roi_start_h, 0), height);
hend = min(max(hend + roi_start_h, 0), height);
wstart = min(max(wstart + roi_start_w, 0), width);
wend = min(max(wend + roi_start_w, 0), width);
bool is_empty = (hend <= hstart) || (wend <= wstart);
// Define an empty pooling region to be zero
float maxval = is_empty ? 0 : -1E+37;
// If nothing is pooled, argmax=-1 causes nothing to be backprop'd
int maxidx = -1;
int data_offset = (roi_batch_ind * channels + c) * height * width;
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
int bottom_index = h * width + w;
if (bottom_data[data_offset + bottom_index] > maxval) {
maxval = bottom_data[data_offset + bottom_index];
maxidx = bottom_index;
}
}
}
top_data = maxval;
argmax_data = maxidx;
''', 'roi_poolig_2d_fwd'
)(bottom_data, self.spatial_scale, channels, height, width,
self.outh, self.outw, bottom_rois, top_data,
self.argmax_data)
return top_data,
def backward_cpu(self, inputs, gy):
bottom_data, bottom_rois = inputs
n_rois, channels, height, width = bottom_data.shape
bottom_delta = numpy.zeros_like(bottom_data, dtype=numpy.float32)
for i_roi in six.moves.range(n_rois):
idx, xmin, ymin, xmax, ymax = bottom_rois[i_roi]
idx = int(idx)
xmin = int(round(xmin * self.spatial_scale))
xmax = int(round(xmax * self.spatial_scale))
ymin = int(round(ymin * self.spatial_scale))
ymax = int(round(ymax * self.spatial_scale))
roi_width = max(xmax - xmin + 1, 1)
roi_height = max(ymax - ymin + 1, 1)
strideh = float(roi_height) / float(self.outh)
stridew = float(roi_width) / float(self.outw)
# iterate all the w, h (from feature map) that fall into this ROIs
for w in six.moves.range(xmin, xmax + 1):
for h in six.moves.range(ymin, ymax + 1):
phstart = int(numpy.floor(float(h - ymin) / strideh))
phend = int(numpy.ceil(float(h - ymin + 1) / strideh))
pwstart = int(numpy.floor(float(w - xmin) / stridew))
pwend = int(numpy.ceil(float(w - xmin + 1) / stridew))
phstart = min(max(phstart, 0), self.outh)
phend = min(max(phend, 0), self.outh)
pwstart = min(max(pwstart, 0), self.outw)
pwend = min(max(pwend, 0), self.outw)
for ph in six.moves.range(phstart, phend):
for pw in six.moves.range(pwstart, pwend):
max_idx_tmp = self.argmax_data[i_roi, :, ph, pw]
for c in six.moves.range(channels):
if max_idx_tmp[c] == (h * width + w):
bottom_delta[idx, c, h, w] += \
gy[0][i_roi, c, ph, pw]
return bottom_delta, None
def backward_gpu(self, inputs, gy):
bottom_data, bottom_rois = inputs
channels, height, width = bottom_data.shape[1:]
bottom_diff = cuda.cupy.zeros_like(bottom_data, dtype=numpy.float32)
cuda.cupy.ElementwiseKernel(
'''
raw float32 top_diff, raw int32 argmax_data, int32 num_rois,
float32 spatial_scale, int32 channels, int32 height, int32 width,
int32 pooled_height, int32 pooled_width, raw float32 bottom_rois
''',
'float32 bottom_diff',
'''
int w = i % width;
int h = (i / width) % height;
int c = (i / (width * height)) % channels;
int num = i / (width * height * channels);
float gradient = 0;
// Accumulate gradient over all ROIs that pooled this element
for (int roi_n = 0; roi_n < num_rois; ++roi_n) {
// Skip if ROI's batch index doesn't match num
if (num != static_cast<int>(bottom_rois[roi_n * 5])) {
continue;
}
int roi_start_w = round(bottom_rois[roi_n * 5 + 1]
* spatial_scale);
int roi_start_h = round(bottom_rois[roi_n * 5 + 2]
* spatial_scale);
int roi_end_w = round(bottom_rois[roi_n * 5 + 3]
* spatial_scale);
int roi_end_h = round(bottom_rois[roi_n * 5 + 4]
* spatial_scale);
// Skip if ROI doesn't include (h, w)
const bool in_roi = (w >= roi_start_w && w <= roi_end_w &&
h >= roi_start_h && h <= roi_end_h);
if (!in_roi) {
continue;
}
int offset = (roi_n * channels + c) * pooled_height
* pooled_width;
// Compute feasible set of pooled units that could have pooled
// this bottom unit
// Force malformed ROIs to be 1x1
int roi_width = max(roi_end_w - roi_start_w + 1, 1);
int roi_height = max(roi_end_h - roi_start_h + 1, 1);
float bin_size_h = static_cast<float>(roi_height)
/ static_cast<float>(pooled_height);
float bin_size_w = static_cast<float>(roi_width)
/ static_cast<float>(pooled_width);
int phstart = floor(static_cast<float>(h - roi_start_h)
/ bin_size_h);
int phend = ceil(static_cast<float>(h - roi_start_h + 1)
/ bin_size_h);
int pwstart = floor(static_cast<float>(w - roi_start_w)
/ bin_size_w);
int pwend = ceil(static_cast<float>(w - roi_start_w + 1)
/ bin_size_w);
phstart = min(max(phstart, 0), pooled_height);
phend = min(max(phend, 0), pooled_height);
pwstart = min(max(pwstart, 0), pooled_width);
pwend = min(max(pwend, 0), pooled_width);
for (int ph = phstart; ph < phend; ++ph) {
for (int pw = pwstart; pw < pwend; ++pw) {
int index_ = ph * pooled_width + pw + offset;
if (argmax_data[index_] == (h * width + w)) {
gradient += top_diff[index_];
}
}
}
}
bottom_diff = gradient;
''', 'roi_pooling_2d_bwd'
)(gy[0], self.argmax_data, bottom_rois.shape[0], self.spatial_scale,
channels, height, width, self.outh, self.outw,
bottom_rois, bottom_diff)
return bottom_diff, None
def roi_pooling_2d(x, rois, outh, outw, spatial_scale):
"""Spatial Region of Interest (ROI) pooling function.
This function acts similarly to :class:`~functions.MaxPooling2D`, but
it computes the maximum of input spatial patch for each channel
with the region of interest.
Args:
x (~chainer.Variable): Input variable. The shape is expected to be
4 dimentional: (n: batch, c: channel, h, height, w: width).
rois (~chainer.Variable): Input roi variable. The shape is expected to
be (n: data size, 5), and each datum is set as below:
(batch_index, x_min, y_min, x_max, y_max).
outh (int): Height of output image after pooled.
outw (int): Width of output image after pooled.
spatial_scale (float): Scale of the roi is resized.
Returns:
~chainer.Variable: Ouptut variable.
See the original paper proposing ROIPooling:
`Fast R-CNN <http://arxiv.org/abs/1504.08083>`_.
"""
return ROIPooling2D(outh, outw, spatial_scale)(x, rois)