test_pad_op.py 2.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

W
wanghaoshuang 已提交
17 18
import unittest
import numpy as np
19
from op_test import OpTest
20
import paddle.fluid.core as core
W
wanghaoshuang 已提交
21 22


W
wanghaoshuang 已提交
23
class TestPadOp(OpTest):
W
wanghaoshuang 已提交
24
    def setUp(self):
W
wanghaoshuang 已提交
25
        self.initTestCase()
26
        self.dtype = self.get_dtype()
W
wanghaoshuang 已提交
27
        self.op_type = "pad"
28
        self.inputs = {'X': np.random.random(self.shape).astype(self.dtype), }
W
wanghaoshuang 已提交
29
        self.attrs = {}
W
wanghaoshuang 已提交
30 31
        self.attrs['paddings'] = np.array(self.paddings).flatten()
        self.attrs['pad_value'] = self.pad_value
W
wanghaoshuang 已提交
32 33
        self.outputs = {
            'Out': np.pad(self.inputs['X'],
W
wanghaoshuang 已提交
34
                          self.paddings,
W
wanghaoshuang 已提交
35
                          mode='constant',
W
wanghaoshuang 已提交
36
                          constant_values=self.pad_value)
W
wanghaoshuang 已提交
37 38
        }

39 40 41
    def get_dtype(self):
        return np.float32

W
wanghaoshuang 已提交
42 43 44 45
    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
W
wanghaoshuang 已提交
46
        self.check_grad(['X'], 'Out', max_relative_error=0.006)
W
wanghaoshuang 已提交
47

W
wanghaoshuang 已提交
48 49 50
    def initTestCase(self):
        self.shape = (16, 16)
        self.paddings = [(0, 1), (2, 3)]
Y
Yang Yang(Tony) 已提交
51
        self.pad_value = 0.0
W
wanghaoshuang 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64


class TestCase1(TestPadOp):
    def initTestCase(self):
        self.shape = (2, 3, 4, 4)
        self.paddings = [(0, 1), (2, 3), (2, 1), (1, 1)]
        self.pad_value = 0.5


class TestCase2(TestPadOp):
    def initTestCase(self):
        self.shape = (2, 2, 2)
        self.paddings = [(0, 0), (0, 0), (1, 2)]
Y
Yang Yang(Tony) 已提交
65
        self.pad_value = 1.0
W
wanghaoshuang 已提交
66 67 68 69 70 71 72 73


class TestCase3(TestPadOp):
    def initTestCase(self):
        self.shape = (8)
        self.paddings = [(0, 1)]
        self.pad_value = 0.9

W
wanghaoshuang 已提交
74

75 76 77 78
#----------------Pad Fp16----------------


def create_test_fp16(parent):
79 80
    @unittest.skipIf(not core.is_compiled_with_cuda(),
                     "core is not compiled with CUDA")
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    class TestPadFp16(parent):
        def get_dtype(self):
            return np.float16

        def test_check_grad_normal(self):
            self.check_grad(['X'], 'Out', max_relative_error=0.3)

    cls_name = "{0}_{1}".format(parent.__name__, "Fp16")
    TestPadFp16.__name__ = cls_name
    globals()[cls_name] = TestPadFp16


create_test_fp16(TestPadOp)
create_test_fp16(TestCase1)
create_test_fp16(TestCase2)
create_test_fp16(TestCase3)

W
wanghaoshuang 已提交
98 99
if __name__ == '__main__':
    unittest.main()