test_conv2d_op.py 5.9 KB
Newer Older
1 2
import unittest
import numpy as np
D
dzhwinter 已提交
3 4

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


C
chengduoZH 已提交
8 9 10 11 12 13 14
def conv2d_forward_naive(input, filter, group, conv_param):
    in_n, in_c, in_h, in_w = input.shape
    out_c, f_c, f_h, f_w = filter.shape
    assert f_c * group == in_c
    assert np.mod(out_c, group) == 0
    sub_out_c = out_c / group

C
chengduoZH 已提交
15 16 17 18
    stride, pad, dilation = conv_param['stride'], conv_param['pad'], conv_param[
        'dilation']
    out_h = 1 + (in_h + 2 * pad[0] - (dilation[0] * (f_h - 1) + 1)) / stride[0]
    out_w = 1 + (in_w + 2 * pad[1] - (dilation[1] * (f_w - 1) + 1)) / stride[1]
C
chengduoZH 已提交
19 20
    out = np.zeros((in_n, out_c, out_h, out_w))

武毅 已提交
21 22
    d_bolck_h = (dilation[0] * (f_h - 1) + 1)
    d_bolck_w = (dilation[1] * (f_w - 1) + 1)
C
chengduoZH 已提交
23

C
chengduoZH 已提交
24
    input_pad = np.pad(input, ((0, ), (0, ), (pad[0], ), (pad[1], )),
C
chengduoZH 已提交
25 26
                       mode='constant',
                       constant_values=0)
C
chengduoZH 已提交
27 28 29 30 31

    filter_dilation = np.zeros((out_c, f_c, d_bolck_h, d_bolck_w))
    filter_dilation[:, :, 0:d_bolck_h:dilation[0], 0:d_bolck_w:dilation[
        1]] = filter

C
chengduoZH 已提交
32 33 34
    for i in range(out_h):
        for j in range(out_w):
            for g in range(group):
C
chengduoZH 已提交
35 36
                input_pad_masked = \
                    input_pad[:, g * f_c:(g + 1) * f_c,
C
chengduoZH 已提交
37 38
                    i * stride[0]:i * stride[0] + d_bolck_h,
                    j * stride[1]:j * stride[1] + d_bolck_w]
C
chengduoZH 已提交
39

C
chengduoZH 已提交
40 41
                f_sub = filter_dilation[g * sub_out_c:(g + 1) *
                                        sub_out_c, :, :, :]
C
chengduoZH 已提交
42
                for k in range(sub_out_c):
C
chengduoZH 已提交
43 44 45
                    out[:, g * sub_out_c + k, i, j] = \
                        np.sum(input_pad_masked * f_sub[k, :, :, :],
                               axis=(1, 2, 3))
C
chengduoZH 已提交
46 47 48 49

    return out


H
hedaoyuan 已提交
50
class TestConv2dOp(OpTest):
51
    def setUp(self):
D
dzhwinter 已提交
52
        core.use_cuda()
C
chengduoZH 已提交
53 54
        self.init_op_type()
        self.init_group()
C
chengduoZH 已提交
55
        self.init_dilation()
C
chengduoZH 已提交
56
        self.init_test_case()
C
chengduoZH 已提交
57

C
chengduoZH 已提交
58 59 60 61 62
        conv2d_param = {
            'stride': self.stride,
            'pad': self.pad,
            'dilation': self.dilations
        }
C
chengduoZH 已提交
63 64
        input = np.random.random(self.input_size).astype("float32")
        filter = np.random.random(self.filter_size).astype("float32")
Y
Yu Yang 已提交
65 66
        output = conv2d_forward_naive(input, filter, self.groups,
                                      conv2d_param).astype('float32')
67

H
hedaoyuan 已提交
68
        self.inputs = {'Input': input, 'Filter': filter}
H
hedaoyuan 已提交
69
        self.attrs = {
C
chengduoZH 已提交
70 71
            'strides': self.stride,
            'paddings': self.pad,
C
chengduoZH 已提交
72
            'groups': self.groups,
C
chengduoZH 已提交
73
            'dilations': self.dilations
H
hedaoyuan 已提交
74
        }
75 76
        self.outputs = {'Output': output}

H
hedaoyuan 已提交
77 78 79
    def test_check_output(self):
        self.check_output()

H
hedaoyuan 已提交
80
    def test_check_grad(self):
81
        self.check_grad(
C
chengduoZH 已提交
82
            set(['Input', 'Filter']), 'Output', max_relative_error=0.02)
H
hedaoyuan 已提交
83

84
    def test_check_grad_no_filter(self):
85 86 87
        self.check_grad(
            ['Input'],
            'Output',
C
chengduoZH 已提交
88
            max_relative_error=0.02,
89
            no_grad_set=set(['Filter']))
90 91

    def test_check_grad_no_input(self):
92 93 94
        self.check_grad(
            ['Filter'],
            'Output',
C
chengduoZH 已提交
95
            max_relative_error=0.02,
96
            no_grad_set=set(['Input']))
97

C
chengduoZH 已提交
98 99 100 101 102 103 104 105
    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] / self.groups
        self.filter_size = [6, f_c, 3, 3]

C
chengduoZH 已提交
106 107 108
    def init_dilation(self):
        self.dilations = [1, 1]

C
chengduoZH 已提交
109
    def init_group(self):
H
hedaoyuan 已提交
110 111
        self.groups = 1

C
chengduoZH 已提交
112
    def init_op_type(self):
武毅 已提交
113 114
        self.op_type = "conv2d"

H
hedaoyuan 已提交
115

C
chengduoZH 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
class TestWithPad(TestConv2dOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] / self.groups
        self.filter_size = [6, f_c, 3, 3]


class TestWithStride(TestConv2dOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [2, 2]
        self.input_size = [2, 3, 6, 6]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] / self.groups
        self.filter_size = [6, f_c, 3, 3]


H
hedaoyuan 已提交
136
class TestWithGroup(TestConv2dOp):
C
chengduoZH 已提交
137
    def init_group(self):
H
hedaoyuan 已提交
138 139
        self.groups = 3

武毅 已提交
140

C
chengduoZH 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
class TestWith1x1(TestConv2dOp):
    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] / self.groups
        self.filter_size = [6, f_c, 1, 1]

    def init_group(self):
        self.groups = 3


C
chengduoZH 已提交
154 155 156 157 158 159 160 161
class TestWithDilation(TestConv2dOp):
    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.input_size = [2, 3, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] / self.groups
        self.filter_size = [6, f_c, 3, 3]
C
chengduoZH 已提交
162

C
chengduoZH 已提交
163 164
    def init_dilation(self):
        self.dilations = [2, 2]
C
chengduoZH 已提交
165

C
chengduoZH 已提交
166
    def init_group(self):
C
chengduoZH 已提交
167
        self.groups = 3
武毅 已提交
168

C
chengduoZH 已提交
169 170 171

#----------------Conv2dCudnn----------------
class TestCudnn(TestConv2dOp):
C
chengduoZH 已提交
172
    def init_op_type(self):
D
dzhwinter 已提交
173
        core.use_cudnn()
武毅 已提交
174
        self.op_type = "conv2d_cudnn"
C
chengduoZH 已提交
175 176


C
chengduoZH 已提交
177 178
class TestCudnnWithPad(TestWithPad):
    def init_op_type(self):
D
dzhwinter 已提交
179
        core.use_cudnn()
武毅 已提交
180
        self.op_type = "conv2d_cudnn"
C
chengduoZH 已提交
181 182


C
chengduoZH 已提交
183
class TestCudnnWithStride(TestWithStride):
C
chengduoZH 已提交
184
    def init_op_type(self):
D
dzhwinter 已提交
185
        core.use_cudnn()
武毅 已提交
186
        self.op_type = "conv2d_cudnn"
武毅 已提交
187

C
chengduoZH 已提交
188

C
chengduoZH 已提交
189 190
class TestCudnnWithGroup(TestWithGroup):
    def init_op_type(self):
D
dzhwinter 已提交
191
        core.use_cudnn()
武毅 已提交
192
        self.op_type = "conv2d_cudnn"
C
chengduoZH 已提交
193

武毅 已提交
194

C
chengduoZH 已提交
195
class TestCudnnWith1x1(TestWith1x1):
C
chengduoZH 已提交
196
    def init_op_type(self):
D
dzhwinter 已提交
197
        core.use_cudnn()
武毅 已提交
198
        self.op_type = "conv2d_cudnn"
C
chengduoZH 已提交
199

武毅 已提交
200

C
chengduoZH 已提交
201 202 203 204 205
#  cudnn v5 does not support dilation conv.
# class TestCudnnWithDilation(TestWithDilation):
#     def init_op_type(self):
#         self.op_type = "conv_cudnn"

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