Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion paddle/phi/kernels/cpu/roi_align_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/math_function.h"

namespace phi {
Expand Down Expand Up @@ -90,7 +91,11 @@ void RoiAlignGradKernel(const Context& dev_ctx,
if (!dx) {
return;
}

if (x.numel() == 0 || boxes.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(dx->dims())), 0, dx);
return;
}
DenseTensor roi_batch_id_list = Empty<int>(dev_ctx, {rois_num});
int* box_batch_id_data = roi_batch_id_list.data<int>();

Expand Down
6 changes: 4 additions & 2 deletions paddle/phi/kernels/cpu/roi_align_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/full_kernel.h"

namespace phi {

Expand Down Expand Up @@ -193,8 +194,9 @@ void RoiAlignKernel(const Context& dev_ctx,
int width = static_cast<int>(in_dims[3]);
int rois_num = static_cast<int>(boxes.dims()[0]);

if (rois_num == 0) {
dev_ctx.template Alloc<T>(out);
if (x.numel() == 0 || boxes.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
return;
}

Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/kernels/gpu/roi_align_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"

namespace phi {

Expand Down Expand Up @@ -149,6 +150,11 @@ void RoiAlignKernel(const Context& dev_ctx,
dev_ctx.template Alloc<T>(out);
return;
}
if (x.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
return;
}
auto in_dims = x.dims();
int batch_size = in_dims[0];
int channels = in_dims[1];
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/kernels/xpu/roi_align_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "paddle/phi/backends/xpu/xpu_context.h"
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"

namespace phi {

Expand All @@ -41,6 +42,11 @@ void RoiAlignGradKernel(const Context& dev_ctx,
if (!dx) {
return;
}
if (x.numel() == 0 || boxes.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(dx->dims())), 0, dx);
return;
}
DenseTensor roi_batch_id_list;
roi_batch_id_list.Resize({rois_num});
auto cplace = phi::CPUPlace();
Expand Down
7 changes: 4 additions & 3 deletions paddle/phi/kernels/xpu/roi_align_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "paddle/phi/backends/xpu/xpu_context.h"
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/core/kernel_registry.h"

#include "paddle/phi/kernels/full_kernel.h"
namespace phi {

template <typename T, typename Context>
Expand All @@ -40,8 +40,9 @@ void RoiAlignKernel(const Context& dev_ctx,

int rois_num = boxes.dims()[0];

if (rois_num == 0) {
dev_ctx.template Alloc<T>(out);
if (x.numel() == 0 || boxes.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
return;
}

Expand Down
52 changes: 52 additions & 0 deletions test/legacy_test/test_roi_align_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,57 @@ def init_test_case(self):
self.x = np.random.random(self.x_dim).astype('float64')


class TestROIAlignOp_ZeroSize(TestROIAlignOp):
def init_test_case(self):
self.batch_size = 3
self.channels = 3
self.height = 0
self.width = 6

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

self.spatial_scale = 1.0 / 2.0
self.pooled_height = 2
self.pooled_width = 2
self.sampling_ratio = -1
self.aligned = False

self.x = np.random.random(self.x_dim).astype('float64')

def make_rois(self):
rois = []
self.rois_lod = [[]]
for bno in range(self.batch_size):
self.rois_lod[0].append(bno + 1)
for i in range(bno + 1):
x1 = np.random.random_integers(
0, self.width // self.spatial_scale - self.pooled_width
)
x2 = np.random.random_integers(
x1 + self.pooled_width, self.width // self.spatial_scale
)
if self.height == 0:
y1 = 0
y2 = 0
else:
y1 = np.random.random_integers(
0,
self.height // self.spatial_scale - self.pooled_height,
)
y2 = np.random.random_integers(
y1 + self.pooled_height,
self.height // self.spatial_scale,
)

roi = [bno, x1, y1, x2, y2]
rois.append(roi)
self.rois_num = len(rois)
self.rois = np.array(rois).astype("float64")
self.boxes_num = np.array(
[bno + 1 for bno in range(self.batch_size)]
).astype('int32')


if __name__ == '__main__':
unittest.main()
Loading