test_conv2d_op.py 3.5 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")
Y
Yu Yang 已提交
47 48
        output = conv2d_forward_naive(input, filter, self.groups,
                                      conv2d_param).astype('float32')
49

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

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

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

66
    def test_check_grad_no_filter(self):
67 68 69
        self.check_grad(
            ['Input'],
            'Output',
C
chengduoZH 已提交
70
            max_relative_error=0.02,
71
            no_grad_set=set(['Filter']))
72 73

    def test_check_grad_no_input(self):
74 75 76
        self.check_grad(
            ['Filter'],
            'Output',
C
chengduoZH 已提交
77
            max_relative_error=0.02,
78
            no_grad_set=set(['Input']))
79

C
chengduoZH 已提交
80 81 82 83 84 85 86 87 88 89
    def init_test_case(self):
        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 已提交
90 91
        self.groups = 1

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

H
hedaoyuan 已提交
95 96

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

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

武毅 已提交
103

C
chengduoZH 已提交
104 105 106
#----------------Conv2dCudnn----------------


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

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

C
chengduoZH 已提交
114 115 116

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

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

武毅 已提交
122

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