test_resize_op.py 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env python3

# Copyright (c) 2023 CINN Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

17 18 19

from cinn.common import *
from cinn.frontend import *
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

# paddle resize is based on cv2 module
# This test requires cv2 module (pip3.6 install opencv_python==3.2.0.7)
# @OpTestTool.skip_if(not is_compiled_with_cuda(),
#                    "x86 test will be skipped due to timeout.")
# class TestResizeOp(OpTest):
#     def setUp(self):
#         self.init_case()

#     def init_case(self):
#         self.in_shape = [1,2,220,300]
#         self.inputs = {
#             "x":
#             (np.random.random(self.in_shape) * 255).astype('int32')
#         }
#         self.out_shape = [240, 240]
#         self.mode = "nearest"

#     def build_paddle_program(self, target):
#         #paddle resize only support [HWC] format.
#         input = self.inputs["x"].reshape(self.in_shape[1:4]).transpose([1,2,0]).astype('uint8')
#         out = F.resize(input, self.out_shape, self.mode)
#         out = paddle.to_tensor(out.transpose([2,0,1]).reshape(self.in_shape[0:2]+self.out_shape), dtype="int32", stop_gradient=False)
#         self.paddle_outputs = [out]

#     def build_cinn_program(self, target):
#         builder = NetBuilder("resize")
#         x = builder.create_input(
#             self.nptype2cinntype(self.inputs["x"].dtype),
#             self.inputs["x"].shape, "x")
#         out = builder.resize(x, self.out_shape, self.mode)
#         prog = builder.build()
#         res = self.get_cinn_output(
#             prog, target, [x], [self.inputs["x"]], [out], passes=[])
#         self.cinn_outputs = [res[0]]

#     def check_outputs_and_grads(self):
#         self.build_paddle_program(self.target)
#         self.build_cinn_program(self.target)
#         expect = self.paddle_outputs[0].numpy()
#         actual = self.cinn_outputs[0]

#         self.assertEqual(
#             expect.dtype,
#             actual.dtype,
#             msg=
#             "[{}] The output dtype different, which expect shape is {} but actual is {}."
#             .format(self._get_device(), expect.dtype, actual.dtype))
#         self.assertEqual(
#             expect.shape,
#             actual.shape,
#             msg=
#             "[{}] The output shape different, which expect shape is {} but actual is {}."
#             .format(self._get_device(), expect.shape, actual.shape))

#         is_allclose = np.allclose(
#                 expect,
#                 actual,
#                 atol=1)
#         error_message = "np.allclose(expect, actual, atol=1) checks error!"
#         self.assertTrue(is_allclose, msg=error_message)

#     def test_check_results(self):
#         self.check_outputs_and_grads()

# @OpTestTool.skip_if(not is_compiled_with_cuda(),
#                   "x86 test will be skipped due to timeout.")
# class TestResizeOp1(TestResizeOp):
#     def init_case(self):
#         self.in_shape = [1,2,220,300]
#         self.inputs = {
#             "x":
#             (np.random.random(self.in_shape) * 255).astype('int32')
#         }
#         self.out_shape = [4, 4]
#         self.mode = "bilinear"

# @OpTestTool.skip_if(not is_compiled_with_cuda(),
#                    "x86 test will be skipped due to timeout.")
# class TestResizeOp2(TestResizeOp):
#     def init_case(self):
#         self.in_shape = [1,2,220,300]
#         self.inputs = {
#             "x":
#             (np.random.random(self.in_shape) * 255).astype('int32')
#         }
#         self.out_shape = [4, 4]
#         self.mode = "bicubic"

# if __name__ == "__main__":
#     unittest.main()