diff --git a/paddle/fluid/operators/roi_align_op.h b/paddle/fluid/operators/roi_align_op.h index 2f99fa5718fa1aae63b42e24366e4aafcbeb7069..0459a47db749f69f64a58432935ebc96f66a132f 100644 --- a/paddle/fluid/operators/roi_align_op.h +++ b/paddle/fluid/operators/roi_align_op.h @@ -290,9 +290,6 @@ class CPUROIAlignGradOpKernel : public framework::OpKernel { for (int n = 0; n < rois_num; ++n) { int roi_batch_idx = roi_batch_id_data[n]; - T* batch_grad_data = in_grad_data + roi_batch_idx * in_stride[0]; - const T* batch_out_grad_data = - out_grad_data + roi_batch_idx * out_stride[0]; T roi_xmin = rois_data[0] * spatial_scale; T roi_ymin = rois_data[1] * spatial_scale; T roi_xmax = rois_data[2] * spatial_scale; @@ -303,6 +300,10 @@ class CPUROIAlignGradOpKernel : public framework::OpKernel { static_cast(roi_height) / static_cast(pooled_height); T bin_size_w = static_cast(roi_width) / static_cast(pooled_width); for (int c = 0; c < channels; ++c) { + T* batch_grad_data = + in_grad_data + roi_batch_idx * in_stride[0] + c * in_stride[1]; + const T* batch_out_grad_data = + out_grad_data + n * out_stride[0] + c * out_stride[1]; for (int ph = 0; ph < pooled_height; ++ph) { for (int pw = 0; pw < pooled_width; ++pw) { int pool_index = ph * pooled_width + pw; @@ -329,8 +330,6 @@ class CPUROIAlignGradOpKernel : public framework::OpKernel { } } } - batch_grad_data += in_stride[1]; - batch_out_grad_data += out_stride[1]; } rois_data += roi_stride[0]; } diff --git a/python/paddle/fluid/tests/unittests/test_roi_align_op.py b/python/paddle/fluid/tests/unittests/test_roi_align_op.py index 588e402e2ba5b32a8b41879ed392ac9c50151240..1028d38759bddf7c0f5337f3a2fc3c87e9a48c32 100644 --- a/python/paddle/fluid/tests/unittests/test_roi_align_op.py +++ b/python/paddle/fluid/tests/unittests/test_roi_align_op.py @@ -37,7 +37,7 @@ class TestROIAlignOp(OpTest): self.outputs = {'Out': self.out_data} def init_test_case(self): - self.batch_size = 1 + self.batch_size = 3 self.channels = 3 self.height = 8 self.width = 6 @@ -45,10 +45,10 @@ class TestROIAlignOp(OpTest): # n, c, h, w self.x_dim = (self.batch_size, self.channels, self.height, self.width) - self.spatial_scale = 1.0 / 1.0 + self.spatial_scale = 1.0 / 2.0 self.pooled_height = 2 self.pooled_width = 2 - self.sampling_ratio = 2 + self.sampling_ratio = -1 self.x = np.random.random(self.x_dim).astype('float32') @@ -57,7 +57,7 @@ class TestROIAlignOp(OpTest): count = roi_bin_grid_h * roi_bin_grid_w bilinear_pos = np.zeros( [self.channels, self.pooled_height, self.pooled_width, count, 4], - np.int32) + np.float32) bilinear_w = np.zeros( [self.pooled_height, self.pooled_width, count, 4], np.float32) for ph in range(self.pooled_width): @@ -85,7 +85,7 @@ class TestROIAlignOp(OpTest): if x_low >= self.width - 1: x = x_high = x_low = self.width - 1 else: - x_high = x_low = self.width - 1 + x_high = x_low + 1 ly = y - y_low lx = x - x_low hy = 1 - ly @@ -107,8 +107,9 @@ class TestROIAlignOp(OpTest): return bilinear_pos, bilinear_w def calc_roi_align(self): - self.out_data = np.zeros((self.rois_num, self.channels, - self.pooled_height, self.pooled_width)) + self.out_data = np.zeros( + (self.rois_num, self.channels, self.pooled_height, + self.pooled_width)).astype('float32') for i in range(self.rois_num): roi = self.rois[i] @@ -118,14 +119,14 @@ class TestROIAlignOp(OpTest): roi_ymin = roi[2] * self.spatial_scale roi_xmax = roi[3] * self.spatial_scale roi_ymax = roi[4] * self.spatial_scale - roi_width = int(max(roi_xmax - roi_xmin, 1)) - roi_height = int(max(roi_ymax - roi_ymin, 1)) + roi_width = max(roi_xmax - roi_xmin, 1) + roi_height = max(roi_ymax - roi_ymin, 1) bin_size_h = float(roi_height) / float(self.pooled_height) bin_size_w = float(roi_width) / float(self.pooled_width) roi_bin_grid_h = self.sampling_ratio if self.sampling_ratio > 0 else \ - math.ceil(roi_height / pooled_height) + math.ceil(roi_height / self.pooled_height) roi_bin_grid_w = self.sampling_ratio if self.sampling_ratio > 0 else \ - math.ceil(roi_width / pooled_width) + math.ceil(roi_width / self.pooled_width) count = int(roi_bin_grid_h * roi_bin_grid_w) pre_size = count * self.pooled_width * self.pooled_height bilinear_pos, bilinear_w = self.pre_calc(x_i, roi_xmin, roi_ymin, @@ -139,7 +140,7 @@ class TestROIAlignOp(OpTest): def make_rois(self): rois = [] - self.rois_lod = [[0]] + self.rois_lod = [[]] for bno in range(self.batch_size): self.rois_lod[0].append(bno + 1) for i in range(bno + 1): @@ -166,4 +167,4 @@ class TestROIAlignOp(OpTest): self.check_output() def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', max_relative_error=0.005)