|
| 1 | +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +import paddle |
| 20 | + |
| 21 | + |
| 22 | +class TestCompatUnfold(unittest.TestCase): |
| 23 | + def _compare_with_origin( |
| 24 | + self, input_tensor, kernel_size, dilation, padding, stride |
| 25 | + ): |
| 26 | + unfold_compat = paddle.compat.Unfold( |
| 27 | + kernel_size=kernel_size, |
| 28 | + dilation=dilation, |
| 29 | + padding=padding, |
| 30 | + stride=stride, |
| 31 | + ) |
| 32 | + unfold_origin = paddle.nn.Unfold( |
| 33 | + kernel_sizes=kernel_size, |
| 34 | + dilations=dilation, |
| 35 | + paddings=padding, |
| 36 | + strides=stride, |
| 37 | + ) |
| 38 | + expected_res = unfold_origin(input_tensor).numpy() |
| 39 | + np.testing.assert_allclose( |
| 40 | + unfold_compat(input_tensor).numpy(), expected_res |
| 41 | + ) |
| 42 | + |
| 43 | + # test with tensor input |
| 44 | + to_tensor = lambda x: x if isinstance(x, int) else paddle.to_tensor(x) |
| 45 | + kernel_size = to_tensor(kernel_size) |
| 46 | + dilation = to_tensor(dilation) |
| 47 | + padding = to_tensor(padding) |
| 48 | + stride = to_tensor(stride) |
| 49 | + unfold_compat = paddle.compat.Unfold( |
| 50 | + kernel_size=kernel_size, |
| 51 | + dilation=dilation, |
| 52 | + padding=padding, |
| 53 | + stride=stride, |
| 54 | + ) |
| 55 | + np.testing.assert_allclose( |
| 56 | + unfold_compat(input_tensor).numpy(), expected_res |
| 57 | + ) |
| 58 | + |
| 59 | + def test_compare_with_origin(self): |
| 60 | + input_shape = (3, 4, 5, 6) |
| 61 | + input_tensor = paddle.arange(360, dtype=paddle.float32).reshape( |
| 62 | + input_shape |
| 63 | + ) |
| 64 | + self._compare_with_origin(input_tensor, [3, 3], [1, 1], (1, 2), [1, 1]) |
| 65 | + |
| 66 | + input_shape = (5, 10, 13, 13) |
| 67 | + input_tensor = paddle.ones(input_shape, dtype=paddle.float64) |
| 68 | + self._compare_with_origin(input_tensor, [4, 4], [2, 2], 1, (1, 2)) |
| 69 | + |
| 70 | + input_shape = (12, 4, 10, 10) |
| 71 | + input_tensor = paddle.ones(input_shape, dtype=paddle.float64) |
| 72 | + self._compare_with_origin(input_tensor, 3, 2, 1, (1, 1)) |
| 73 | + |
| 74 | + def test_error_handling(self): |
| 75 | + """Test whether there will be correct exception when users pass paddle.split kwargs in paddle.compat.split, vice versa.""" |
| 76 | + x = paddle.randn([3, 9, 5]) |
| 77 | + |
| 78 | + msg_gt_1 = "paddle.nn.Unfold() received unexpected keyword arguments 'dilation', 'stride'. \nDid you mean to use paddle.compat.Unfold() instead?" |
| 79 | + msg_gt_2 = "paddle.compat.Unfold() received unexpected keyword argument 'paddings'. \nDid you mean to use paddle.nn.Unfold() instead?" |
| 80 | + msg_gt_3 = "The `padding` field of paddle.compat.Unfold can only have size 1 or 2, now len=4. \nDid you mean to use paddle.nn.Unfold() instead?" |
| 81 | + msg_gt_4 = "paddle.compat.Unfold does not allow paddle.Tensor or pir.Value as inputs in static graph mode." |
| 82 | + |
| 83 | + with self.assertRaises(TypeError) as cm: |
| 84 | + unfold = paddle.nn.Unfold([3, 3], dilation=[2, 2], stride=[1, 1]) |
| 85 | + self.assertEqual(str(cm.exception), msg_gt_1) |
| 86 | + |
| 87 | + with self.assertRaises(TypeError) as cm: |
| 88 | + unfold = paddle.compat.Unfold([3, 3], paddings=[2, 1]) |
| 89 | + self.assertEqual(str(cm.exception), msg_gt_2) |
| 90 | + |
| 91 | + with self.assertRaises(ValueError) as cm: |
| 92 | + unfold = paddle.compat.Unfold([3, 3], padding=[2, 1, 2, 2]) |
| 93 | + res = unfold(paddle.ones([2, 2, 5, 5])) |
| 94 | + self.assertEqual(str(cm.exception), msg_gt_3) |
| 95 | + |
| 96 | + with self.assertRaises(TypeError) as cm: |
| 97 | + paddle.enable_static() |
| 98 | + input_data = np.random.randn(2, 4, 8, 8).astype(np.float32) |
| 99 | + with paddle.static.program_guard(paddle.static.Program()): |
| 100 | + x = paddle.static.data( |
| 101 | + name='x', shape=[None, None, 8, 8], dtype='float32' |
| 102 | + ) |
| 103 | + place = ( |
| 104 | + paddle.CUDAPlace(0) |
| 105 | + if paddle.is_compiled_with_cuda() |
| 106 | + else paddle.CPUPlace() |
| 107 | + ) |
| 108 | + unfold_pass = paddle.compat.Unfold( |
| 109 | + kernel_size=paddle.to_tensor([3, 3]), |
| 110 | + padding=paddle.to_tensor([1, 2]), |
| 111 | + ) |
| 112 | + result = unfold_pass(x) |
| 113 | + exe = paddle.static.Executor(place) |
| 114 | + feed = {'x': input_data} |
| 115 | + exe_res = exe.run(feed=feed) |
| 116 | + paddle.disable_static() |
| 117 | + self.assertEqual(str(cm.exception), msg_gt_4) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + unittest.main() |
0 commit comments