test_unpool_op.py 3.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.

S
sweetsky0901 已提交
15 16
import unittest
import numpy as np
17
from op_test import OpTest
S
sweetsky0901 已提交
18 19 20 21


def unpool2dmax_forward_naive(input, indices, ksize, strides, paddings):
    s0, s1, s2, s3 = input.shape
22 23 24
    out_hsize = (s2 - 1) * strides[0] - 2 * paddings[0] + ksize[0]
    out_wsize = (s2 - 1) * strides[1] - 2 * paddings[1] + ksize[1]
    out = np.zeros((s0, s1, out_hsize, out_wsize))
25 26 27 28
    for nidx in range(s0):
        for cidx in range(s1):
            for h in range(s2):
                for w in range(s3):
S
sweetsky0901 已提交
29
                    index = indices[nidx, cidx, h, w]
30 31
                    hidx = (index - index % out_wsize) / out_wsize
                    widx = index % out_wsize
S
sweetsky0901 已提交
32 33
                    out[nidx, cidx, int(hidx), int(widx)] = \
                            input[nidx, cidx, h, w]
S
sweetsky0901 已提交
34 35 36 37 38 39 40 41 42

    return out


class TestUnpoolOp(OpTest):
    def setUp(self):
        self.op_type = "unpool"
        self.init_test_case()
        pre_input = np.random.random(self.shape).astype("float32")
43 44
        nsize, csize, hsize, wsize = pre_input.shape
        hsize_out = (hsize - self.ksize[0] + 2 * self.paddings[0]) / \
S
sweetsky0901 已提交
45
                self.strides[0] + 1
46
        wsize_out = (wsize - self.ksize[1] + 2 * self.paddings[1]) / \
S
sweetsky0901 已提交
47
                self.strides[1] + 1
48 49
        input = np.zeros((nsize, csize, hsize_out, wsize_out))
        indices = np.zeros((nsize, csize, hsize_out, wsize_out))
50 51
        for i in range(hsize_out):
            for j in range(wsize_out):
S
sweetsky0901 已提交
52
                r_start = np.max((i * self.strides[0] - self.paddings[0], 0))
S
sweetsky0901 已提交
53
                r_end = np.min((i * self.strides[0] + self.ksize[0] - \
54
                        self.paddings[0], hsize))
S
sweetsky0901 已提交
55
                c_start = np.max((j * self.strides[1] - self.paddings[1], 0))
S
sweetsky0901 已提交
56
                c_end = np.min((j * self.strides[1] + self.ksize[1] - \
57
                        self.paddings[1], wsize))
58 59
                for nidx in range(nsize):
                    for cidx in range(csize):
S
sweetsky0901 已提交
60 61
                        x_masked = pre_input[nidx, cidx, r_start:r_end, \
                                c_start:c_end]
S
sweetsky0901 已提交
62 63
                        input[nidx, cidx, i, j] = x_masked.max()
                        arg = x_masked.argmax()
S
sweetsky0901 已提交
64
                        indices[nidx, cidx, i, j] = \
65
                                (r_start + arg / self.ksize[1]) * wsize + \
S
sweetsky0901 已提交
66
                                c_start + arg % self.ksize[1]
S
sweetsky0901 已提交
67
        output = self.unpool2d_forward_naive(input, indices, self.ksize, \
S
sweetsky0901 已提交
68
                self.strides, self.paddings).astype("float32")
S
sweetsky0901 已提交
69 70 71
        self.inputs = {
            'X': input.astype('float32'),
            'Indices': indices.astype('int32')
S
sweetsky0901 已提交
72
        }
S
sweetsky0901 已提交
73
        self.attrs = {
S
sweetsky0901 已提交
74 75 76 77
            'strides': self.strides,
            'paddings': self.paddings,
            'ksize': self.ksize,
            'unpooling_type': self.unpooling_type,
S
sweetsky0901 已提交
78
        }
S
sweetsky0901 已提交
79 80 81 82 83 84
        self.outputs = {'Out': output.astype('float32')}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
S
sweetsky0901 已提交
85
        self.check_grad(['X'], 'Out')
S
sweetsky0901 已提交
86 87

    def init_test_case(self):
S
sweetsky0901 已提交
88
        self.unpool2d_forward_naive = unpool2dmax_forward_naive
S
sweetsky0901 已提交
89
        self.unpooling_type = "max"
S
sweetsky0901 已提交
90
        self.shape = [6, 4, 5, 5]
S
sweetsky0901 已提交
91 92 93 94
        self.ksize = [3, 3]
        self.strides = [2, 2]
        self.paddings = [0, 0]

S
sweetsky0901 已提交
95

S
sweetsky0901 已提交
96 97
if __name__ == '__main__':
    unittest.main()