test_unpool_op.py 3.2 KB
Newer Older
S
sweetsky0901 已提交
1 2 3 4 5 6 7
import unittest
import numpy as np
from op_test import OpTest


def unpool2dmax_forward_naive(input, indices, ksize, strides, paddings):
    s0, s1, s2, s3 = input.shape
8 9 10
    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))
S
sweetsky0901 已提交
11 12 13 14 15
    for nidx in xrange(s0):
        for cidx in xrange(s1):
            for h in xrange(s2):
                for w in xrange(s3):
                    index = indices[nidx, cidx, h, w]
16 17
                    hidx = (index - index % out_wsize) / out_wsize
                    widx = index % out_wsize
S
sweetsky0901 已提交
18 19
                    out[nidx, cidx, int(hidx), int(widx)] = \
                            input[nidx, cidx, h, w]
S
sweetsky0901 已提交
20 21 22 23 24 25 26 27 28

    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")
29 30
        nsize, csize, hsize, wsize = pre_input.shape
        hsize_out = (hsize - self.ksize[0] + 2 * self.paddings[0]) / \
S
sweetsky0901 已提交
31
                self.strides[0] + 1
32
        wsize_out = (wsize - self.ksize[1] + 2 * self.paddings[1]) / \
S
sweetsky0901 已提交
33
                self.strides[1] + 1
34 35 36 37
        input = np.zeros((nsize, csize, hsize_out, wsize_out))
        indices = np.zeros((nsize, csize, hsize_out, wsize_out))
        for i in xrange(hsize_out):
            for j in xrange(wsize_out):
S
sweetsky0901 已提交
38
                r_start = np.max((i * self.strides[0] - self.paddings[0], 0))
S
sweetsky0901 已提交
39
                r_end = np.min((i * self.strides[0] + self.ksize[0] - \
40
                        self.paddings[0], hsize))
S
sweetsky0901 已提交
41
                c_start = np.max((j * self.strides[1] - self.paddings[1], 0))
S
sweetsky0901 已提交
42
                c_end = np.min((j * self.strides[1] + self.ksize[1] - \
43 44 45
                        self.paddings[1], wsize))
                for nidx in xrange(nsize):
                    for cidx in xrange(csize):
S
sweetsky0901 已提交
46 47
                        x_masked = pre_input[nidx, cidx, r_start:r_end, \
                                c_start:c_end]
S
sweetsky0901 已提交
48 49
                        input[nidx, cidx, i, j] = x_masked.max()
                        arg = x_masked.argmax()
S
sweetsky0901 已提交
50
                        indices[nidx, cidx, i, j] = \
51
                                (r_start + arg / self.ksize[1]) * wsize + \
S
sweetsky0901 已提交
52
                                c_start + arg % self.ksize[1]
S
sweetsky0901 已提交
53
        output = self.unpool2d_forward_naive(input, indices, self.ksize, \
S
sweetsky0901 已提交
54
                self.strides, self.paddings).astype("float32")
S
sweetsky0901 已提交
55 56 57
        self.inputs = {
            'X': input.astype('float32'),
            'Indices': indices.astype('int32')
S
sweetsky0901 已提交
58
        }
S
sweetsky0901 已提交
59
        self.attrs = {
S
sweetsky0901 已提交
60 61 62 63
            'strides': self.strides,
            'paddings': self.paddings,
            'ksize': self.ksize,
            'unpooling_type': self.unpooling_type,
S
sweetsky0901 已提交
64
        }
S
sweetsky0901 已提交
65 66 67 68 69 70
        self.outputs = {'Out': output.astype('float32')}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
S
sweetsky0901 已提交
71
        self.check_grad(['X'], 'Out')
S
sweetsky0901 已提交
72 73

    def init_test_case(self):
S
sweetsky0901 已提交
74
        self.unpool2d_forward_naive = unpool2dmax_forward_naive
S
sweetsky0901 已提交
75
        self.unpooling_type = "max"
S
sweetsky0901 已提交
76
        self.shape = [6, 4, 5, 5]
S
sweetsky0901 已提交
77 78 79 80
        self.ksize = [3, 3]
        self.strides = [2, 2]
        self.paddings = [0, 0]

S
sweetsky0901 已提交
81

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