test_pool2d_op.py 7.4 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
chengduoZH 已提交
52 53
        input = np.random.random(self.shape).astype("float32")
        output = self.pool2D_forward_naive(input, self.ksize, self.strides,
C
chengduoZH 已提交
54
                                           self.paddings, self.global_pool)
55
        self.inputs = {'X': input}
C
chengduoZH 已提交
56 57 58 59 60

        self.attrs = {
            'strides': self.strides,
            'paddings': self.paddings,
            'ksize': self.ksize,
C
fix doc  
chengduoZH 已提交
61 62
            'poolingType': self.pool_type,
            'globalPooling': self.global_pool,
C
chengduoZH 已提交
63 64
        }

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

    def test_check_output(self):
        self.check_output()

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

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

C
chengduoZH 已提交
82 83 84 85 86 87
    def init_op_type(self):
        self.op_type = "pool2d"

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

C
chengduoZH 已提交
88

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

C
chengduoZH 已提交
98 99 100 101 102 103
    def init_op_type(self):
        self.op_type = "pool2d"

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

C
chengduoZH 已提交
104

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

C
chengduoZH 已提交
114 115 116 117 118 119
    def init_op_type(self):
        self.op_type = "pool2d"

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

C
chengduoZH 已提交
120

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

C
chengduoZH 已提交
130 131 132 133 134 135
    def init_op_type(self):
        self.op_type = "pool2d"

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

C
chengduoZH 已提交
136

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

C
chengduoZH 已提交
146 147 148 149 150 151
    def init_op_type(self):
        self.op_type = "pool2d"

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

C
chengduoZH 已提交
152

C
chengduoZH 已提交
153
class TestCase5(TestPool2d_Op):
C
chengduoZH 已提交
154
    def init_test_case(self):
C
chengduoZH 已提交
155
        self.global_pool = False
C
chengduoZH 已提交
156 157 158 159 160 161 162
        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 已提交
163
        self.op_type = "pool2d"
C
chengduoZH 已提交
164 165 166 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

    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 已提交
247
        self.pool_type = "max"
C
chengduoZH 已提交
248 249 250 251 252


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

C
chengduoZH 已提交
259 260 261 262 263 264
    def init_op_type(self):
        self.op_type = "pool2d_cudnn"

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

C
chengduoZH 已提交
265 266 267

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