From 6d72a86b14a1c6680d3fea5d5821271975702e01 Mon Sep 17 00:00:00 2001 From: LielinJiang <50691816+LielinJiang@users.noreply.github.com> Date: Wed, 18 Sep 2019 11:29:51 +0800 Subject: [PATCH] fix_roi_transform_bug (#19785) --- .../operators/detection/roi_perspective_transform_op.cc | 8 ++++---- .../operators/detection/roi_perspective_transform_op.cu | 8 ++++---- .../tests/unittests/test_roi_perspective_transform_op.py | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/paddle/fluid/operators/detection/roi_perspective_transform_op.cc b/paddle/fluid/operators/detection/roi_perspective_transform_op.cc index 6628dde5c2..ce10de40a9 100644 --- a/paddle/fluid/operators/detection/roi_perspective_transform_op.cc +++ b/paddle/fluid/operators/detection/roi_perspective_transform_op.cc @@ -128,11 +128,11 @@ void get_transform_matrix(const int transformed_width, T estimated_width = (len1 + len3) / 2.0; // Get the normalized height and normalized width - int normalized_height = transformed_height; + int normalized_height = std::max(2, transformed_height); int normalized_width = std::round(estimated_width * (normalized_height - 1) / estimated_height) + 1; - normalized_width = std::min(normalized_width, transformed_width); + normalized_width = std::max(2, std::min(normalized_width, transformed_width)); T dx1 = x1 - x2; T dx2 = x3 - x2; @@ -141,9 +141,9 @@ void get_transform_matrix(const int transformed_width, T dy2 = y3 - y2; T dy3 = y0 - y1 + y2 - y3; - matrix[6] = (dx3 * dy2 - dx2 * dy3) / (dx1 * dy2 - dx2 * dy1) / + matrix[6] = (dx3 * dy2 - dx2 * dy3) / (dx1 * dy2 - dx2 * dy1 + 1e-5) / (normalized_width - 1); - matrix[7] = (dx1 * dy3 - dx3 * dy1) / (dx1 * dy2 - dx2 * dy1) / + matrix[7] = (dx1 * dy3 - dx3 * dy1) / (dx1 * dy2 - dx2 * dy1 + 1e-5) / (normalized_height - 1); matrix[8] = 1; diff --git a/paddle/fluid/operators/detection/roi_perspective_transform_op.cu b/paddle/fluid/operators/detection/roi_perspective_transform_op.cu index 19df68faf9..8c9ca9462c 100644 --- a/paddle/fluid/operators/detection/roi_perspective_transform_op.cu +++ b/paddle/fluid/operators/detection/roi_perspective_transform_op.cu @@ -242,10 +242,10 @@ __device__ void get_transform_matrix(const int transformed_width, T estimated_width = (len1 + len3) / 2.0; // Get the normalized height and normalized width - int normalized_height = transformed_height; + int normalized_height = max(2, transformed_height); int normalized_width = round(estimated_width * (normalized_height - 1) / estimated_height) + 1; - normalized_width = min(normalized_width, transformed_width); + normalized_width = max(2, min(normalized_width, transformed_width)); T dx1 = x1 - x2; T dx2 = x3 - x2; @@ -254,9 +254,9 @@ __device__ void get_transform_matrix(const int transformed_width, T dy2 = y3 - y2; T dy3 = y0 - y1 + y2 - y3; - matrix[6] = (dx3 * dy2 - dx2 * dy3) / (dx1 * dy2 - dx2 * dy1) / + matrix[6] = (dx3 * dy2 - dx2 * dy3) / (dx1 * dy2 - dx2 * dy1 + 1e-5) / (normalized_width - 1); - matrix[7] = (dx1 * dy3 - dx3 * dy1) / (dx1 * dy2 - dx2 * dy1) / + matrix[7] = (dx1 * dy3 - dx3 * dy1) / (dx1 * dy2 - dx2 * dy1 + 1e-5) / (normalized_height - 1); matrix[8] = 1; diff --git a/python/paddle/fluid/tests/unittests/test_roi_perspective_transform_op.py b/python/paddle/fluid/tests/unittests/test_roi_perspective_transform_op.py index b56f331e90..e742993c2b 100644 --- a/python/paddle/fluid/tests/unittests/test_roi_perspective_transform_op.py +++ b/python/paddle/fluid/tests/unittests/test_roi_perspective_transform_op.py @@ -87,10 +87,10 @@ def get_transform_matrix(transformed_width, transformed_height, roi_x, roi_y): estimated_height = (len2 + len4) / 2.0 estimated_width = (len1 + len3) / 2.0 - normalized_height = transformed_height + normalized_height = max(2, transformed_height) normalized_width = round(estimated_width * (normalized_height - 1) / estimated_height) + 1 - normalized_width = min(normalized_width, transformed_width) + normalized_width = max(2, min(normalized_width, transformed_width)) dx1 = x1 - x2 dx2 = x3 - x2 @@ -99,9 +99,9 @@ def get_transform_matrix(transformed_width, transformed_height, roi_x, roi_y): dy2 = y3 - y2 dy3 = y0 - y1 + y2 - y3 matrix = np.zeros([9]) - matrix[6] = (dx3 * dy2 - dx2 * dy3) / (dx1 * dy2 - dx2 * dy1) / ( + matrix[6] = (dx3 * dy2 - dx2 * dy3) / (dx1 * dy2 - dx2 * dy1 + 1e-5) / ( normalized_width - 1) - matrix[7] = (dx1 * dy3 - dx3 * dy1) / (dx1 * dy2 - dx2 * dy1) / ( + matrix[7] = (dx1 * dy3 - dx3 * dy1) / (dx1 * dy2 - dx2 * dy1 + 1e-5) / ( normalized_height - 1) matrix[8] = 1 -- GitLab