diff --git a/paddle/fluid/operators/detection/roi_perspective_transform_op.cc b/paddle/fluid/operators/detection/roi_perspective_transform_op.cc index 6628dde5c2f0c71f6e1dffda0b81829bf939d4ef..ce10de40a9682204f9643296be0b02c74300cebe 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 19df68faf9e265b60c98587169d9ce5609bc5738..8c9ca9462c383fc79ee1c16b7f2bbcdc52d60dc4 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 b56f331e9087ffff9d2f4f4a6074718ac411fa06..e742993c2bf2c0a49e95feff63d97b98c9c85d44 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