diff --git a/python/paddle/compat.py b/python/paddle/compat.py index c278041b4612626c1e9965b81a7f597e1f0c5a27..dfa9f1822bff8dd0bf538bd31eb0aa8ec8501372 100644 --- a/python/paddle/compat.py +++ b/python/paddle/compat.py @@ -13,7 +13,6 @@ # limitations under the License. import six -import math __all__ = [] @@ -197,30 +196,3 @@ def _to_bytes(obj, encoding): return obj else: return six.b(obj) - - -# math related functions -def round(x, d=0): - """ - Compatible round which act the same behaviour in Python3. - - Args: - x(float) : The number to round halfway. - - Returns: - round result of x - """ - if six.PY3: - # The official walkaround of round in Python3 is incorrect - # we implement according this answer: https://www.techforgeek.info/round_python.html - if x > 0.0: - p = 10**d - return float(math.floor((x * p) + math.copysign(0.5, x))) / p - elif x < 0.0: - p = 10**d - return float(math.ceil((x * p) + math.copysign(0.5, x))) / p - else: - return math.copysign(0.0, x) - else: - import __builtin__ - return __builtin__.round(x, d) diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py index e7ecdeda8b6c8d3ac83a95e825f4d22b658315ed..e4fedede47ce3dfcb5e78408c8edff845798ebb0 100644 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ b/python/paddle/fluid/tests/unittests/test_compat.py @@ -236,18 +236,6 @@ class TestCompatible(unittest.TestCase): for i in l2: self.assertTrue(isinstance(i, bytes)) - def test_round(self): - self.assertEqual(3.0, cpt.round(3.4)) - self.assertEqual(4.0, cpt.round(3.5)) - self.assertEqual(0.0, cpt.round(0.1)) - self.assertEqual(0.0, cpt.round(0.0)) - self.assertEqual(-0.0, cpt.round(-0.0)) - self.assertEqual(-0.0, cpt.round(-0.1)) - self.assertEqual(-3.0, cpt.round(-3.4)) - self.assertEqual(-4.0, cpt.round(-3.5)) - self.assertEqual(5.0, cpt.round(5)) - self.assertRaises(TypeError, cpt.round, None) - if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_roi_pool_op.py b/python/paddle/fluid/tests/unittests/test_roi_pool_op.py index 97b058f0e1327e9c66f4df19a31180f70b2a67e3..16ff2f57fb8f0cca776786022c86102796a6beac 100644 --- a/python/paddle/fluid/tests/unittests/test_roi_pool_op.py +++ b/python/paddle/fluid/tests/unittests/test_roi_pool_op.py @@ -17,10 +17,20 @@ import unittest import numpy as np import math import sys -import paddle.compat as cpt from op_test import OpTest import paddle.fluid as fluid +from decimal import Decimal, ROUND_HALF_UP + + +def _round(x): + """In Python3 round function rounds to the nearest even number, + we use this function to make the result always round up when the + remainder is 0.5. See more at: + https://stackoverflow.com/questions/33019698/how-to-properly-round-up-half-float-numbers + """ + return Decimal(x).to_integral_value(rounding=ROUND_HALF_UP) + class TestROIPoolOp(OpTest): @@ -67,10 +77,10 @@ class TestROIPoolOp(OpTest): for i in range(self.rois_num): roi = self.rois[i] roi_batch_id = int(roi[0]) - roi_start_w = int(cpt.round(roi[1] * self.spatial_scale)) - roi_start_h = int(cpt.round(roi[2] * self.spatial_scale)) - roi_end_w = int(cpt.round(roi[3] * self.spatial_scale)) - roi_end_h = int(cpt.round(roi[4] * self.spatial_scale)) + roi_start_w = int(_round(roi[1] * self.spatial_scale)) + roi_start_h = int(_round(roi[2] * self.spatial_scale)) + roi_end_w = int(_round(roi[3] * self.spatial_scale)) + roi_end_h = int(_round(roi[4] * self.spatial_scale)) roi_height = int(max(roi_end_h - roi_start_h + 1, 1)) roi_width = int(max(roi_end_w - roi_start_w + 1, 1))