test_pool2d_op.py 7.6 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


C
chengduoZH 已提交
6
def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0):
C
chengduoZH 已提交
7 8

    N, C, H, W = x.shape
C
chengduoZH 已提交
9 10
    if global_pool == 1:
        ksize = [H, W]
C
chengduoZH 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    H_out = (H - ksize[0] + 2 * paddings[0]) / strides[0] + 1
    W_out = (W - ksize[1] + 2 * paddings[1]) / strides[1] + 1
    out = np.zeros((N, C, H_out, W_out))
    for i in xrange(H_out):
        for j in xrange(W_out):
            r_start = np.max((i * strides[0] - paddings[0], 0))
            r_end = np.min((i * strides[0] + ksize[0] - paddings[0], H))
            c_start = np.max((j * strides[1] - paddings[1], 0))
            c_end = np.min((j * strides[1] + ksize[1] - paddings[1], W))
            x_masked = x[:, :, r_start:r_end, c_start:c_end]

            out[:, :, i, j] = np.max(x_masked, axis=(2, 3))
    return out


C
chengduoZH 已提交
26
def avg_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0):
C
chengduoZH 已提交
27 28

    N, C, H, W = x.shape
C
chengduoZH 已提交
29 30
    if global_pool == 1:
        ksize = [H, W]
C
chengduoZH 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    H_out = (H - ksize[0] + 2 * paddings[0]) / strides[0] + 1
    W_out = (W - ksize[1] + 2 * paddings[1]) / strides[1] + 1
    out = np.zeros((N, C, H_out, W_out))
    for i in xrange(H_out):
        for j in xrange(W_out):
            r_start = np.max((i * strides[0] - paddings[0], 0))
            r_end = np.min((i * strides[0] + ksize[0] - paddings[0], H))
            c_start = np.max((j * strides[1] - paddings[1], 0))
            c_end = np.min((j * strides[1] + ksize[1] - paddings[1], W))
            x_masked = x[:, :, r_start:r_end, c_start:c_end]

            out[:, :, i, j] = np.sum(x_masked, axis=(2, 3)) / (
                (r_end - r_start) * (c_end - c_start))
    return out


class TestPool2d_Op(OpTest):
    def setUp(self):
C
chengduoZH 已提交
49 50 51
        self.init_test_case()
        self.init_op_type()
        self.init_pool_type()
C
fix bug  
chengduoZH 已提交
52 53
        if self.global_pool:
            self.paddings = [0 for _ in range(len(self.paddings))]
C
chengduoZH 已提交
54 55
        input = np.random.random(self.shape).astype("float32")
        output = self.pool2D_forward_naive(input, self.ksize, self.strides,
C
fix bug  
chengduoZH 已提交
56 57
                                           self.paddings,
                                           self.global_pool).astype("float32")
58
        self.inputs = {'X': input}
C
chengduoZH 已提交
59 60 61 62 63

        self.attrs = {
            'strides': self.strides,
            'paddings': self.paddings,
            'ksize': self.ksize,
C
chengduoZH 已提交
64 65
            'pooling_type': self.pool_type,
            'global_pooling': self.global_pool,
C
chengduoZH 已提交
66 67
        }

Y
Yu Yang 已提交
68
        self.outputs = {'Out': output.astype('float32')}
C
chengduoZH 已提交
69 70 71 72 73

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
C
chengduoZH 已提交
74
        if self.pool_type != "max":
75
            self.check_grad(set(['X']), 'Out', max_relative_error=0.07)
C
chengduoZH 已提交
76

C
chengduoZH 已提交
77
    def init_test_case(self):
C
chengduoZH 已提交
78
        self.global_pool = True
C
chengduoZH 已提交
79
        self.pool2D_forward_naive = avg_pool2D_forward_naive
C
chengduoZH 已提交
80 81 82 83 84
        self.shape = [2, 3, 5, 5]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [0, 0]

C
chengduoZH 已提交
85 86 87 88 89 90
    def init_op_type(self):
        self.op_type = "pool2d"

    def init_pool_type(self):
        self.pool_type = "avg"

C
chengduoZH 已提交
91

C
chengduoZH 已提交
92
class TestCase1(TestPool2d_Op):
C
chengduoZH 已提交
93
    def init_test_case(self):
C
chengduoZH 已提交
94
        self.global_pool = False
C
chengduoZH 已提交
95
        self.pool2D_forward_naive = avg_pool2D_forward_naive
C
chengduoZH 已提交
96
        self.shape = [2, 3, 7, 7]
C
chengduoZH 已提交
97 98
        self.ksize = [3, 3]
        self.strides = [1, 1]
C
chengduoZH 已提交
99
        self.paddings = [0, 0]
C
chengduoZH 已提交
100

C
chengduoZH 已提交
101 102 103 104 105 106
    def init_op_type(self):
        self.op_type = "pool2d"

    def init_pool_type(self):
        self.pool_type = "avg"

C
chengduoZH 已提交
107

C
chengduoZH 已提交
108
class TestCase2(TestPool2d_Op):
C
chengduoZH 已提交
109
    def init_test_case(self):
C
chengduoZH 已提交
110
        self.global_pool = False
C
chengduoZH 已提交
111
        self.pool2D_forward_naive = avg_pool2D_forward_naive
C
chengduoZH 已提交
112
        self.shape = [2, 3, 7, 7]
C
chengduoZH 已提交
113 114 115 116
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [1, 1]

C
chengduoZH 已提交
117 118 119 120 121 122
    def init_op_type(self):
        self.op_type = "pool2d"

    def init_pool_type(self):
        self.pool_type = "avg"

C
chengduoZH 已提交
123

C
chengduoZH 已提交
124
class TestCase3(TestPool2d_Op):
C
chengduoZH 已提交
125
    def init_test_case(self):
C
chengduoZH 已提交
126
        self.global_pool = True
C
chengduoZH 已提交
127 128 129 130
        self.pool2D_forward_naive = max_pool2D_forward_naive
        self.shape = [2, 3, 5, 5]
        self.ksize = [3, 3]
        self.strides = [1, 1]
C
chengduoZH 已提交
131
        self.paddings = [0, 0]
C
chengduoZH 已提交
132

C
chengduoZH 已提交
133 134 135 136 137 138
    def init_op_type(self):
        self.op_type = "pool2d"

    def init_pool_type(self):
        self.pool_type = "max"

C
chengduoZH 已提交
139

C
chengduoZH 已提交
140
class TestCase4(TestPool2d_Op):
C
chengduoZH 已提交
141
    def init_test_case(self):
C
chengduoZH 已提交
142
        self.global_pool = False
C
chengduoZH 已提交
143
        self.pool2D_forward_naive = max_pool2D_forward_naive
C
chengduoZH 已提交
144 145 146 147 148
        self.shape = [2, 3, 7, 7]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [0, 0]

C
chengduoZH 已提交
149 150 151 152 153 154
    def init_op_type(self):
        self.op_type = "pool2d"

    def init_pool_type(self):
        self.pool_type = "max"

C
chengduoZH 已提交
155

C
chengduoZH 已提交
156
class TestCase5(TestPool2d_Op):
C
chengduoZH 已提交
157
    def init_test_case(self):
C
chengduoZH 已提交
158
        self.global_pool = False
C
chengduoZH 已提交
159 160 161 162 163 164 165
        self.pool2D_forward_naive = max_pool2D_forward_naive
        self.shape = [2, 3, 7, 7]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [1, 1]

    def init_op_type(self):
C
chengduoZH 已提交
166
        self.op_type = "pool2d"
C
chengduoZH 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

    def init_pool_type(self):
        self.pool_type = "max"


#--------------------test pool2d_cudnn--------------------
class TestCaseCudnn1(TestPool2d_Op):
    def init_test_case(self):
        self.global_pool = True
        self.pool2D_forward_naive = avg_pool2D_forward_naive
        self.shape = [2, 3, 5, 5]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [0, 0]

    def init_op_type(self):
        self.op_type = "pool2d_cudnn"

    def init_pool_type(self):
        self.pool_type = "avg"


class TestCaseCudnn2(TestPool2d_Op):
    def init_test_case(self):
        self.global_pool = False
        self.pool2D_forward_naive = avg_pool2D_forward_naive
        self.shape = [2, 3, 7, 7]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [0, 0]

    def init_op_type(self):
        self.op_type = "pool2d_cudnn"

    def init_pool_type(self):
        self.pool_type = "avg"


class TestCaseCudnn3(TestPool2d_Op):
    def init_test_case(self):
        self.global_pool = False
        self.pool2D_forward_naive = avg_pool2D_forward_naive
        self.shape = [2, 3, 7, 7]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [1, 1]

    def init_op_type(self):
        self.op_type = "pool2d_cudnn"

    def init_pool_type(self):
        self.pool_type = "avg"


class TestCaseCudnn4(TestPool2d_Op):
    def init_test_case(self):
        self.global_pool = True
        self.pool2D_forward_naive = max_pool2D_forward_naive
        self.shape = [2, 3, 5, 5]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [0, 0]

    def init_op_type(self):
        self.op_type = "pool2d_cudnn"

    def init_pool_type(self):
        self.pool_type = "max"


class TestCaseCudnn5(TestPool2d_Op):
    def init_test_case(self):
        self.global_pool = False
        self.pool2D_forward_naive = max_pool2D_forward_naive
        self.shape = [2, 3, 7, 7]
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [0, 0]

    def init_op_type(self):
        self.op_type = "pool2d_cudnn"

    def init_pool_type(self):
C
chengduoZH 已提交
250
        self.pool_type = "max"
C
chengduoZH 已提交
251 252 253 254 255


class TestCaseCudnn6(TestPool2d_Op):
    def init_test_case(self):
        self.global_pool = False
C
chengduoZH 已提交
256 257
        self.pool2D_forward_naive = max_pool2D_forward_naive
        self.shape = [2, 3, 7, 7]
C
chengduoZH 已提交
258 259 260 261
        self.ksize = [3, 3]
        self.strides = [1, 1]
        self.paddings = [1, 1]

C
chengduoZH 已提交
262 263 264 265 266 267
    def init_op_type(self):
        self.op_type = "pool2d_cudnn"

    def init_pool_type(self):
        self.pool_type = "max"

C
chengduoZH 已提交
268 269 270

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