test_zeropad2d.py 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   Copyright (c) 2021 PaddlePaddle 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.

from __future__ import print_function

import unittest
import numpy as np
J
Jiabin Yang 已提交
19
import paddle
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
from paddle import to_tensor
from paddle.nn.functional import zeropad2d
from paddle.nn import ZeroPad2D


class TestZeroPad2dAPIError(unittest.TestCase):
    """
    test paddle.zeropad2d error.
    """

    def setUp(self):
        """
        unsupport dtypes
        """
        self.shape = [4, 3, 224, 224]
        self.unsupport_dtypes = ['bool', 'int8']

J
Jiabin Yang 已提交
37
    def func_unsupport_dtypes(self):
38 39 40 41 42 43 44 45 46
        """
        test unsupport dtypes.
        """
        for dtype in self.unsupport_dtypes:
            pad = 2
            x = np.random.randint(-255, 255, size=self.shape)
            x_tensor = to_tensor(x).astype(dtype)
            self.assertRaises(TypeError, zeropad2d, x=x_tensor, padding=pad)

J
Jiabin Yang 已提交
47 48 49 50 51
    def test_unsupport_dtypes(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_unsupport_dtypes()
        self.func_unsupport_dtypes()

52 53 54 55 56 57 58 59 60 61 62 63 64

class TestZeroPad2dAPI(unittest.TestCase):
    """
    test paddle.zeropad2d
    """

    def setUp(self):
        """
        support dtypes
        """
        self.shape = [4, 3, 224, 224]
        self.support_dtypes = ['float32', 'float64', 'int32', 'int64']

J
Jiabin Yang 已提交
65
    def func_support_dtypes(self):
66 67 68 69 70 71 72 73 74 75 76 77
        """
        test support types
        """
        for dtype in self.support_dtypes:
            pad = 2
            x = np.random.randint(-255, 255, size=self.shape).astype(dtype)
            expect_res = np.pad(x, [[0, 0], [0, 0], [pad, pad], [pad, pad]])

            x_tensor = to_tensor(x).astype(dtype)
            ret_res = zeropad2d(x_tensor, [pad, pad, pad, pad]).numpy()
            self.assertTrue(np.allclose(expect_res, ret_res))

J
Jiabin Yang 已提交
78 79 80 81 82 83
    def test_support_dtypes(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_support_dtypes()
        self.func_support_dtypes()

    def func_support_pad2(self):
84 85 86 87 88 89 90 91 92 93 94 95
        """
        test the type of 'pad' is list.
        """
        pad = [1, 2, 3, 4]
        x = np.random.randint(-255, 255, size=self.shape)
        expect_res = np.pad(
            x, [[0, 0], [0, 0], [pad[2], pad[3]], [pad[0], pad[1]]])

        x_tensor = to_tensor(x)
        ret_res = zeropad2d(x_tensor, pad).numpy()
        self.assertTrue(np.allclose(expect_res, ret_res))

J
Jiabin Yang 已提交
96 97 98 99 100 101
    def test_support_pad2(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_support_pad2()
        self.func_support_pad2()

    def func_support_pad3(self):
102 103 104 105 106 107 108 109 110 111 112 113
        """
        test the type of 'pad' is tuple.
        """
        pad = (1, 2, 3, 4)
        x = np.random.randint(-255, 255, size=self.shape)
        expect_res = np.pad(
            x, [[0, 0], [0, 0], [pad[2], pad[3]], [pad[0], pad[1]]])

        x_tensor = to_tensor(x)
        ret_res = zeropad2d(x_tensor, pad).numpy()
        self.assertTrue(np.allclose(expect_res, ret_res))

J
Jiabin Yang 已提交
114 115 116 117 118 119
    def test_support_pad3(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_support_pad3()
        self.func_support_pad3()

    def func_support_pad4(self):
120 121 122 123 124 125 126 127 128 129 130 131 132
        """
        test the type of 'pad' is paddle.Tensor.
        """
        pad = [1, 2, 3, 4]
        x = np.random.randint(-255, 255, size=self.shape)
        expect_res = np.pad(
            x, [[0, 0], [0, 0], [pad[2], pad[3]], [pad[0], pad[1]]])

        x_tensor = to_tensor(x)
        pad_tensor = to_tensor(pad, dtype='int32')
        ret_res = zeropad2d(x_tensor, pad_tensor).numpy()
        self.assertTrue(np.allclose(expect_res, ret_res))

J
Jiabin Yang 已提交
133 134 135 136 137
    def test_support_pad4(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_support_pad4()
        self.func_support_pad4()

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

class TestZeroPad2DLayer(unittest.TestCase):
    """
    test nn.ZeroPad2D
    """

    def setUp(self):
        self.shape = [4, 3, 224, 224]
        self.pad = [2, 2, 4, 1]
        self.padLayer = ZeroPad2D(padding=self.pad)
        self.x = np.random.randint(-255, 255, size=self.shape)
        self.expect_res = np.pad(self.x,
                                 [[0, 0], [0, 0], [self.pad[2], self.pad[3]],
                                  [self.pad[0], self.pad[1]]])

J
Jiabin Yang 已提交
153
    def func_layer(self):
154 155 156 157 158
        self.assertTrue(
            np.allclose(
                zeropad2d(to_tensor(self.x), self.pad).numpy(),
                self.padLayer(to_tensor(self.x))))

J
Jiabin Yang 已提交
159 160 161 162 163
    def test_layer(self):
        with paddle.fluid.framework._test_eager_guard():
            self.func_layer()
        self.func_layer()

164 165 166

if __name__ == '__main__':
    unittest.main()