test_pool3d_op.py 6.6 KB
Newer Older
C
chengduoZH 已提交
1 2
import unittest
import numpy as np
3 4

import paddle.v2.fluid.core as core
C
chengduoZH 已提交
5 6 7
from op_test import OpTest


C
chengduoZH 已提交
8
def max_pool3D_forward_naive(x, ksize, strides, paddings, global_pool=0):
C
chengduoZH 已提交
9
    N, C, D, H, W = x.shape
C
chengduoZH 已提交
10 11
    if global_pool == 1:
        ksize = [D, H, W]
C
chengduoZH 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    D_out = (D - ksize[0] + 2 * paddings[0]) / strides[0] + 1
    H_out = (H - ksize[1] + 2 * paddings[1]) / strides[1] + 1
    W_out = (W - ksize[2] + 2 * paddings[2]) / strides[2] + 1
    out = np.zeros((N, C, D_out, H_out, W_out))
    for k in xrange(D_out):
        d_start = np.max((k * strides[0] - paddings[0], 0))
        d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D))
        for i in xrange(H_out):
            h_start = np.max((i * strides[0] - paddings[0], 0))
            h_end = np.min((i * strides[0] + ksize[0] - paddings[0], H))
            for j in xrange(W_out):
                w_start = np.max((j * strides[1] - paddings[1], 0))
                w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W))
                x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end]

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


C
chengduoZH 已提交
31
def avg_pool3D_forward_naive(x, ksize, strides, paddings, global_pool=0):
C
chengduoZH 已提交
32
    N, C, D, H, W = x.shape
C
chengduoZH 已提交
33 34
    if global_pool == 1:
        ksize = [D, H, W]
C
chengduoZH 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    D_out = (D - ksize[0] + 2 * paddings[0]) / strides[0] + 1
    H_out = (H - ksize[1] + 2 * paddings[1]) / strides[1] + 1
    W_out = (W - ksize[2] + 2 * paddings[2]) / strides[2] + 1
    out = np.zeros((N, C, D_out, H_out, W_out))
    for k in xrange(D_out):
        d_start = np.max((k * strides[0] - paddings[0], 0))
        d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D))
        for i in xrange(H_out):
            h_start = np.max((i * strides[0] - paddings[0], 0))
            h_end = np.min((i * strides[0] + ksize[0] - paddings[0], H))
            for j in xrange(W_out):
                w_start = np.max((j * strides[1] - paddings[1], 0))
                w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W))
                x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end]

                out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3, 4)) / (
                    (d_end - d_start) * (h_end - h_start) * (w_end - w_start))
    return out


class TestPool3d_Op(OpTest):
    def setUp(self):
57
        self.use_cudnn = False
C
fix bug  
chengduoZH 已提交
58
        self.init_test_case()
C
chengduoZH 已提交
59 60 61 62
        self.init_global_pool()
        self.init_op_type()
        self.init_pool_type()

C
fix bug  
chengduoZH 已提交
63 64
        if self.global_pool:
            self.paddings = [0 for _ in range(len(self.paddings))]
C
chengduoZH 已提交
65 66
        input = np.random.random(self.shape).astype("float32")
        output = self.pool3D_forward_naive(input, self.ksize, self.strides,
C
fix bug  
chengduoZH 已提交
67 68
                                           self.paddings,
                                           self.global_pool).astype("float32")
69
        self.inputs = {'X': input}
C
chengduoZH 已提交
70 71 72 73 74

        self.attrs = {
            'strides': self.strides,
            'paddings': self.paddings,
            'ksize': self.ksize,
C
chengduoZH 已提交
75 76
            'pooling_type': self.pool_type,
            'global_pooling': self.global_pool,
77 78
            'use_cudnn': self.use_cudnn,
            'data_format': 'AnyLayout'  # TODO(dzhwinter) : should be fix latter
C
chengduoZH 已提交
79 80
        }

Y
Yu Yang 已提交
81
        self.outputs = {'Out': output.astype('float32')}
C
chengduoZH 已提交
82 83

    def test_check_output(self):
84 85 86 87 88
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
C
chengduoZH 已提交
89 90

    def test_check_grad(self):
91 92 93 94 95
        if self.use_cudnn and self.pool_type != "max":
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place, set(['X']), 'Out', max_relative_error=0.07)
        elif self.pool_type != "max":
96
            self.check_grad(set(['X']), 'Out', max_relative_error=0.07)
C
chengduoZH 已提交
97

C
fix bug  
chengduoZH 已提交
98
    def init_test_case(self):
C
chengduoZH 已提交
99 100 101 102 103
        self.shape = [2, 3, 5, 5, 5]
        self.ksize = [3, 3, 3]
        self.strides = [1, 1, 1]
        self.paddings = [0, 0, 0]

C
chengduoZH 已提交
104 105 106 107 108 109 110 111 112 113
    def init_op_type(self):
        self.op_type = "pool3d"

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

    def init_global_pool(self):
        self.global_pool = True

C
chengduoZH 已提交
114 115

class TestCase1(TestPool3d_Op):
C
fix bug  
chengduoZH 已提交
116
    def init_test_case(self):
C
chengduoZH 已提交
117 118 119 120
        self.op_type = "pool3d"
        self.shape = [2, 3, 7, 7, 7]
        self.ksize = [3, 3, 3]
        self.strides = [1, 1, 1]
C
chengduoZH 已提交
121
        self.paddings = [0, 0, 0]
C
chengduoZH 已提交
122

C
chengduoZH 已提交
123
    def init_op_type(self):
C
chengduoZH 已提交
124
        self.op_type = "pool3d"
C
chengduoZH 已提交
125 126

    def init_pool_type(self):
C
chengduoZH 已提交
127 128
        self.pool_type = "avg"
        self.pool3D_forward_naive = avg_pool3D_forward_naive
C
chengduoZH 已提交
129 130 131 132 133 134 135

    def init_global_pool(self):
        self.global_pool = False


class TestCase2(TestPool3d_Op):
    def init_test_case(self):
C
chengduoZH 已提交
136 137 138 139 140
        self.shape = [2, 3, 7, 7, 7]
        self.ksize = [3, 3, 3]
        self.strides = [1, 1, 1]
        self.paddings = [1, 1, 1]

C
chengduoZH 已提交
141 142 143 144 145 146 147 148 149 150
    def init_op_type(self):
        self.op_type = "pool3d"

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

    def init_global_pool(self):
        self.global_pool = False

C
chengduoZH 已提交
151 152

class TestCase3(TestPool3d_Op):
C
chengduoZH 已提交
153
    def init_op_type(self):
C
chengduoZH 已提交
154
        self.op_type = "pool3d"
C
chengduoZH 已提交
155 156

    def init_pool_type(self):
C
chengduoZH 已提交
157 158 159 160
        self.pool_type = "max"
        self.pool3D_forward_naive = max_pool3D_forward_naive


C
chengduoZH 已提交
161 162
class TestCase4(TestCase1):
    def init_op_type(self):
C
chengduoZH 已提交
163
        self.op_type = "pool3d"
C
chengduoZH 已提交
164 165

    def init_pool_type(self):
C
chengduoZH 已提交
166 167
        self.pool_type = "max"
        self.pool3D_forward_naive = max_pool3D_forward_naive
C
chengduoZH 已提交
168 169


C
chengduoZH 已提交
170 171
class TestCase5(TestCase2):
    def init_op_type(self):
C
chengduoZH 已提交
172
        self.op_type = "pool3d"
C
chengduoZH 已提交
173 174

    def init_pool_type(self):
C
chengduoZH 已提交
175 176
        self.pool_type = "max"
        self.pool3D_forward_naive = max_pool3D_forward_naive
C
chengduoZH 已提交
177 178


179 180
#--------------------test pool3d--------------------
class TestCUDNNCase1(TestPool3d_Op):
C
chengduoZH 已提交
181
    def init_op_type(self):
182 183
        self.use_cudnn = True
        self.op_type = "pool3d"
C
chengduoZH 已提交
184 185


186
class TestCUDNNCase2(TestCase1):
C
chengduoZH 已提交
187
    def init_op_type(self):
188 189
        self.use_cudnn = True
        self.op_type = "pool3d"
C
chengduoZH 已提交
190 191


192
class TestCUDNNCase3(TestCase2):
C
chengduoZH 已提交
193
    def init_op_type(self):
194 195
        self.use_cudnn = True
        self.op_type = "pool3d"
C
chengduoZH 已提交
196 197


198
class TestCUDNNCase4(TestCase3):
C
chengduoZH 已提交
199
    def init_op_type(self):
200 201
        self.use_cudnn = True
        self.op_type = "pool3d"
C
chengduoZH 已提交
202 203


204
class TestCUDNNCase5(TestCase4):
C
chengduoZH 已提交
205
    def init_op_type(self):
206 207
        self.use_cudnn = True
        self.op_type = "pool3d"
C
chengduoZH 已提交
208 209


210
class TestCUDNNCase6(TestCase5):
C
chengduoZH 已提交
211
    def init_op_type(self):
212 213
        self.use_cudnn = True
        self.op_type = "pool3d"
C
chengduoZH 已提交
214

C
chengduoZH 已提交
215 216 217

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