Skip to content

Commit 90f39b1

Browse files
committed
Merge branch 'roialign' of https://github.com/jerrywgz/Paddle into roialign
2 parents 5e52daf + c0e34ee commit 90f39b1

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

paddle/fluid/operators/roi_align_op.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ class CPUROIAlignGradOpKernel : public framework::OpKernel<T> {
290290

291291
for (int n = 0; n < rois_num; ++n) {
292292
int roi_batch_idx = roi_batch_id_data[n];
293-
T* batch_grad_data = in_grad_data + roi_batch_idx * in_stride[0];
294-
const T* batch_out_grad_data =
295-
out_grad_data + roi_batch_idx * out_stride[0];
296293
T roi_xmin = rois_data[0] * spatial_scale;
297294
T roi_ymin = rois_data[1] * spatial_scale;
298295
T roi_xmax = rois_data[2] * spatial_scale;
@@ -303,6 +300,10 @@ class CPUROIAlignGradOpKernel : public framework::OpKernel<T> {
303300
static_cast<T>(roi_height) / static_cast<T>(pooled_height);
304301
T bin_size_w = static_cast<T>(roi_width) / static_cast<T>(pooled_width);
305302
for (int c = 0; c < channels; ++c) {
303+
T* batch_grad_data =
304+
in_grad_data + roi_batch_idx * in_stride[0] + c * in_stride[1];
305+
const T* batch_out_grad_data =
306+
out_grad_data + n * out_stride[0] + c * out_stride[1];
306307
for (int ph = 0; ph < pooled_height; ++ph) {
307308
for (int pw = 0; pw < pooled_width; ++pw) {
308309
int pool_index = ph * pooled_width + pw;
@@ -329,8 +330,6 @@ class CPUROIAlignGradOpKernel : public framework::OpKernel<T> {
329330
}
330331
}
331332
}
332-
batch_grad_data += in_stride[1];
333-
batch_out_grad_data += out_stride[1];
334333
}
335334
rois_data += roi_stride[0];
336335
}

python/paddle/fluid/tests/unittests/test_roi_align_op.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ def set_data(self):
3737
self.outputs = {'Out': self.out_data}
3838

3939
def init_test_case(self):
40-
self.batch_size = 1
40+
self.batch_size = 3
4141
self.channels = 3
4242
self.height = 8
4343
self.width = 6
4444

4545
# n, c, h, w
4646
self.x_dim = (self.batch_size, self.channels, self.height, self.width)
4747

48-
self.spatial_scale = 1.0 / 1.0
48+
self.spatial_scale = 1.0 / 2.0
4949
self.pooled_height = 2
5050
self.pooled_width = 2
51-
self.sampling_ratio = 2
51+
self.sampling_ratio = -1
5252

5353
self.x = np.random.random(self.x_dim).astype('float32')
5454

@@ -57,7 +57,7 @@ def pre_calc(self, x_i, roi_xmin, roi_ymin, roi_bin_grid_h, roi_bin_grid_w,
5757
count = roi_bin_grid_h * roi_bin_grid_w
5858
bilinear_pos = np.zeros(
5959
[self.channels, self.pooled_height, self.pooled_width, count, 4],
60-
np.int32)
60+
np.float32)
6161
bilinear_w = np.zeros(
6262
[self.pooled_height, self.pooled_width, count, 4], np.float32)
6363
for ph in range(self.pooled_width):
@@ -85,7 +85,7 @@ def pre_calc(self, x_i, roi_xmin, roi_ymin, roi_bin_grid_h, roi_bin_grid_w,
8585
if x_low >= self.width - 1:
8686
x = x_high = x_low = self.width - 1
8787
else:
88-
x_high = x_low = self.width - 1
88+
x_high = x_low + 1
8989
ly = y - y_low
9090
lx = x - x_low
9191
hy = 1 - ly
@@ -107,8 +107,9 @@ def pre_calc(self, x_i, roi_xmin, roi_ymin, roi_bin_grid_h, roi_bin_grid_w,
107107
return bilinear_pos, bilinear_w
108108

109109
def calc_roi_align(self):
110-
self.out_data = np.zeros((self.rois_num, self.channels,
111-
self.pooled_height, self.pooled_width))
110+
self.out_data = np.zeros(
111+
(self.rois_num, self.channels, self.pooled_height,
112+
self.pooled_width)).astype('float32')
112113

113114
for i in range(self.rois_num):
114115
roi = self.rois[i]
@@ -118,14 +119,14 @@ def calc_roi_align(self):
118119
roi_ymin = roi[2] * self.spatial_scale
119120
roi_xmax = roi[3] * self.spatial_scale
120121
roi_ymax = roi[4] * self.spatial_scale
121-
roi_width = int(max(roi_xmax - roi_xmin, 1))
122-
roi_height = int(max(roi_ymax - roi_ymin, 1))
122+
roi_width = max(roi_xmax - roi_xmin, 1)
123+
roi_height = max(roi_ymax - roi_ymin, 1)
123124
bin_size_h = float(roi_height) / float(self.pooled_height)
124125
bin_size_w = float(roi_width) / float(self.pooled_width)
125126
roi_bin_grid_h = self.sampling_ratio if self.sampling_ratio > 0 else \
126-
math.ceil(roi_height / pooled_height)
127+
math.ceil(roi_height / self.pooled_height)
127128
roi_bin_grid_w = self.sampling_ratio if self.sampling_ratio > 0 else \
128-
math.ceil(roi_width / pooled_width)
129+
math.ceil(roi_width / self.pooled_width)
129130
count = int(roi_bin_grid_h * roi_bin_grid_w)
130131
pre_size = count * self.pooled_width * self.pooled_height
131132
bilinear_pos, bilinear_w = self.pre_calc(x_i, roi_xmin, roi_ymin,
@@ -139,7 +140,7 @@ def calc_roi_align(self):
139140

140141
def make_rois(self):
141142
rois = []
142-
self.rois_lod = [[0]]
143+
self.rois_lod = [[]]
143144
for bno in range(self.batch_size):
144145
self.rois_lod[0].append(bno + 1)
145146
for i in range(bno + 1):
@@ -166,4 +167,4 @@ def test_check_output(self):
166167
self.check_output()
167168

168169
def test_check_grad(self):
169-
self.check_grad(['X'], 'Out')
170+
self.check_grad(['X'], 'Out', max_relative_error=0.005)

0 commit comments

Comments
 (0)