test_conv2d_op.py 3.4 KB
Newer Older
1 2
import unittest
import numpy as np
H
hedaoyuan 已提交
3
from op_test import OpTest
4 5


C
chengduoZH 已提交
6 7 8 9 10 11 12 13
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

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

C
chengduoZH 已提交
18
    input_pad = np.pad(input, ((0, ), (0, ), (pad[0], ), (pad[1], )),
C
chengduoZH 已提交
19 20 21 22 23
                       mode='constant',
                       constant_values=0)
    for i in range(out_h):
        for j in range(out_w):
            for g in range(group):
C
chengduoZH 已提交
24 25 26 27 28
                input_pad_masked = \
                    input_pad[:, g * f_c:(g + 1) * f_c,
                    i * stride[0]:i * stride[0] + f_h,
                    j * stride[1]:j * stride[1] + f_w]

C
chengduoZH 已提交
29 30
                f_sub = filter[g * sub_out_c:(g + 1) * sub_out_c, :, :, :]
                for k in range(sub_out_c):
C
chengduoZH 已提交
31 32 33
                    out[:, g * sub_out_c + k, i, j] = \
                        np.sum(input_pad_masked * f_sub[k, :, :, :],
                               axis=(1, 2, 3))
C
chengduoZH 已提交
34 35 36 37

    return out


H
hedaoyuan 已提交
38
class TestConv2dOp(OpTest):
39
    def setUp(self):
C
chengduoZH 已提交
40 41 42
        self.init_op_type()
        self.init_group()
        self.init_test_case()
C
chengduoZH 已提交
43

C
chengduoZH 已提交
44 45 46
        conv2d_param = {'stride': self.stride, 'pad': self.pad}
        input = np.random.random(self.input_size).astype("float32")
        filter = np.random.random(self.filter_size).astype("float32")
C
chengduoZH 已提交
47
        output = conv2d_forward_naive(input, filter, self.groups, conv2d_param)
48

H
hedaoyuan 已提交
49
        self.inputs = {'Input': input, 'Filter': filter}
H
hedaoyuan 已提交
50
        self.attrs = {
C
chengduoZH 已提交
51 52
            'strides': self.stride,
            'paddings': self.pad,
C
chengduoZH 已提交
53
            'groups': self.groups,
C
chengduoZH 已提交
54
            'dilations': self.dilations
H
hedaoyuan 已提交
55
        }
56 57
        self.outputs = {'Output': output}

H
hedaoyuan 已提交
58 59 60
    def test_check_output(self):
        self.check_output()

H
hedaoyuan 已提交
61
    def test_check_grad(self):
62 63
        self.check_grad(
            set(['Input', 'Filter']), 'Output', max_relative_error=0.05)
H
hedaoyuan 已提交
64

65
    def test_check_grad_no_filter(self):
66 67 68 69 70
        self.check_grad(
            ['Input'],
            'Output',
            max_relative_error=0.05,
            no_grad_set=set(['Filter']))
71 72

    def test_check_grad_no_input(self):
73 74 75 76 77
        self.check_grad(
            ['Filter'],
            'Output',
            max_relative_error=0.05,
            no_grad_set=set(['Input']))
78

C
chengduoZH 已提交
79
    def init_test_case(self):
C
chengduoZH 已提交
80 81
        # self.groups = 1
        # self.op_type = "conv2d"
C
chengduoZH 已提交
82 83 84 85 86 87 88 89 90
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.dilations = [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]

    def init_group(self):
H
hedaoyuan 已提交
91 92
        self.groups = 1

C
chengduoZH 已提交
93
    def init_op_type(self):
武毅 已提交
94 95
        self.op_type = "conv2d"

H
hedaoyuan 已提交
96 97

class TestWithGroup(TestConv2dOp):
C
chengduoZH 已提交
98
    def init_group(self):
H
hedaoyuan 已提交
99 100
        self.groups = 3

C
chengduoZH 已提交
101 102
    def init_op_type(self):
        self.op_type = "conv2d"
H
hedaoyuan 已提交
103

武毅 已提交
104

C
chengduoZH 已提交
105 106 107
class TestCudnn(TestConv2dOp):
    def init_group(self):
        self.groups = 1
武毅 已提交
108

C
chengduoZH 已提交
109
    def init_op_type(self):
武毅 已提交
110 111
        self.op_type = "conv_cudnn"

C
chengduoZH 已提交
112 113 114

class TestCudnnWithGroup(TestConv2dOp):
    def init_group(self):
武毅 已提交
115 116
        self.groups = 3

C
chengduoZH 已提交
117 118 119
    def init_op_type(self):
        self.op_type = "conv_cudnn"

武毅 已提交
120

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