test_conv2d_op.py 7.5 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
14 15
import unittest
import numpy as np
D
dzhwinter 已提交
16 17

import paddle.v2.fluid.core as core
H
hedaoyuan 已提交
18
from op_test import OpTest
19 20


C
chengduoZH 已提交
21 22 23 24 25 26 27
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 已提交
28 29 30 31
    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 已提交
32 33
    out = np.zeros((in_n, out_c, out_h, out_w))

武毅 已提交
34 35
    d_bolck_h = (dilation[0] * (f_h - 1) + 1)
    d_bolck_w = (dilation[1] * (f_w - 1) + 1)
C
chengduoZH 已提交
36

C
chengduoZH 已提交
37
    input_pad = np.pad(input, ((0, ), (0, ), (pad[0], ), (pad[1], )),
C
chengduoZH 已提交
38 39
                       mode='constant',
                       constant_values=0)
C
chengduoZH 已提交
40 41 42 43 44

    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 已提交
45 46 47
    for i in range(out_h):
        for j in range(out_w):
            for g in range(group):
C
chengduoZH 已提交
48 49
                input_pad_masked = \
                    input_pad[:, g * f_c:(g + 1) * f_c,
C
chengduoZH 已提交
50 51
                    i * stride[0]:i * stride[0] + d_bolck_h,
                    j * stride[1]:j * stride[1] + d_bolck_w]
C
chengduoZH 已提交
52

C
chengduoZH 已提交
53 54
                f_sub = filter_dilation[g * sub_out_c:(g + 1) *
                                        sub_out_c, :, :, :]
C
chengduoZH 已提交
55
                for k in range(sub_out_c):
C
chengduoZH 已提交
56 57 58
                    out[:, g * sub_out_c + k, i, j] = \
                        np.sum(input_pad_masked * f_sub[k, :, :, :],
                               axis=(1, 2, 3))
C
chengduoZH 已提交
59 60 61 62

    return out


H
hedaoyuan 已提交
63
class TestConv2dOp(OpTest):
64
    def setUp(self):
65
        self.use_cudnn = False
C
chengduoZH 已提交
66 67
        self.init_op_type()
        self.init_group()
C
chengduoZH 已提交
68
        self.init_dilation()
C
chengduoZH 已提交
69
        self.init_test_case()
C
chengduoZH 已提交
70

C
chengduoZH 已提交
71 72 73 74 75
        conv2d_param = {
            'stride': self.stride,
            'pad': self.pad,
            'dilation': self.dilations
        }
C
chengduoZH 已提交
76 77
        input = np.random.random(self.input_size).astype("float32")
        filter = np.random.random(self.filter_size).astype("float32")
Y
Yu Yang 已提交
78 79
        output = conv2d_forward_naive(input, filter, self.groups,
                                      conv2d_param).astype('float32')
80

H
hedaoyuan 已提交
81
        self.inputs = {'Input': input, 'Filter': filter}
H
hedaoyuan 已提交
82
        self.attrs = {
C
chengduoZH 已提交
83 84
            'strides': self.stride,
            'paddings': self.pad,
C
chengduoZH 已提交
85
            'groups': self.groups,
86 87
            'dilations': self.dilations,
            'use_cudnn': self.use_cudnn
H
hedaoyuan 已提交
88
        }
89 90
        self.outputs = {'Output': output}

H
hedaoyuan 已提交
91
    def test_check_output(self):
92 93 94 95 96
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
H
hedaoyuan 已提交
97

H
hedaoyuan 已提交
98
    def test_check_grad(self):
99 100 101 102 103 104 105 106 107 108
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place,
                set(['Input', 'Filter']),
                'Output',
                max_relative_error=0.02)
        else:
            self.check_grad(
                set(['Input', 'Filter']), 'Output', max_relative_error=0.02)
H
hedaoyuan 已提交
109

110
    def test_check_grad_no_filter(self):
111 112 113 114 115 116 117 118 119 120 121 122 123
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place, ['Input'],
                'Output',
                max_relative_error=0.02,
                no_grad_set=set(['Filter']))
        else:
            self.check_grad(
                ['Input'],
                'Output',
                max_relative_error=0.02,
                no_grad_set=set(['Filter']))
124 125

    def test_check_grad_no_input(self):
126 127 128 129 130 131 132 133 134 135 136 137 138
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place, ['Filter'],
                'Output',
                max_relative_error=0.02,
                no_grad_set=set(['Input']))
        else:
            self.check_grad(
                ['Filter'],
                'Output',
                max_relative_error=0.02,
                no_grad_set=set(['Input']))
139

C
chengduoZH 已提交
140 141 142 143 144 145 146 147
    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 已提交
148 149 150
    def init_dilation(self):
        self.dilations = [1, 1]

C
chengduoZH 已提交
151
    def init_group(self):
H
hedaoyuan 已提交
152 153
        self.groups = 1

C
chengduoZH 已提交
154
    def init_op_type(self):
武毅 已提交
155 156
        self.op_type = "conv2d"

H
hedaoyuan 已提交
157

C
chengduoZH 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
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 已提交
178
class TestWithGroup(TestConv2dOp):
C
chengduoZH 已提交
179
    def init_group(self):
H
hedaoyuan 已提交
180 181
        self.groups = 3

武毅 已提交
182

C
chengduoZH 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195
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 已提交
196 197 198 199 200 201 202 203
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 已提交
204

C
chengduoZH 已提交
205 206
    def init_dilation(self):
        self.dilations = [2, 2]
C
chengduoZH 已提交
207

C
chengduoZH 已提交
208
    def init_group(self):
C
chengduoZH 已提交
209
        self.groups = 3
武毅 已提交
210

C
chengduoZH 已提交
211

212 213
#----------------Conv2dCUDNN----------------
class TestCUDNN(TestConv2dOp):
C
chengduoZH 已提交
214
    def init_op_type(self):
215 216
        self.use_cudnn = True
        self.op_type = "conv2d"
C
chengduoZH 已提交
217 218


219
class TestCUDNNWithPad(TestWithPad):
C
chengduoZH 已提交
220
    def init_op_type(self):
221 222
        self.use_cudnn = True
        self.op_type = "conv2d"
C
chengduoZH 已提交
223 224


225
class TestCUDNNWithStride(TestWithStride):
C
chengduoZH 已提交
226
    def init_op_type(self):
227 228
        self.use_cudnn = True
        self.op_type = "conv2d"
武毅 已提交
229

C
chengduoZH 已提交
230

231
class TestCUDNNWithGroup(TestWithGroup):
C
chengduoZH 已提交
232
    def init_op_type(self):
233 234
        self.use_cudnn = True
        self.op_type = "conv2d"
C
chengduoZH 已提交
235

武毅 已提交
236

237
class TestCUDNNWith1x1(TestWith1x1):
C
chengduoZH 已提交
238
    def init_op_type(self):
239 240
        self.use_cudnn = True
        self.op_type = "conv2d"
C
chengduoZH 已提交
241

武毅 已提交
242

C
chengduoZH 已提交
243
#  cudnn v5 does not support dilation conv.
244
# class TestCUDNNWithDilation(TestWithDilation):
C
chengduoZH 已提交
245 246 247
#     def init_op_type(self):
#         self.op_type = "conv_cudnn"

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