test_conv2d_transpose_op.py 3.4 KB
Newer Older
Z
deconv  
zchen0211 已提交
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


Z
zchen0211 已提交
6
def conv2dtranspose_forward_naive(input_, filter_, conv2dtranspose_param):
Z
deconv  
zchen0211 已提交
7 8 9 10
    in_n, in_c, in_h, in_w = input_.shape
    f_c, out_c, f_h, f_w = filter_.shape
    assert in_c == f_c

Z
zchen0211 已提交
11
    stride, pad = conv2dtranspose_param['stride'], conv2dtranspose_param['pad']
Z
deconv  
zchen0211 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    out_h = (in_h - 1) * stride[0] + f_h
    out_w = (in_w - 1) * stride[1] + f_w

    out = np.zeros((in_n, out_c, out_h, out_w))

    for n in range(in_n):
        for i in range(in_h):
            for j in range(in_w):
                input_masked = input_[n, :, i, j]  # (c)
                input_masked = np.reshape(input_masked, (in_c, 1, 1))
                input_masked = np.tile(input_masked, (1, f_h, f_w))

                for k in range(out_c):
                    tmp_out = np.sum(input_masked * filter_[:, k, :, :], axis=0)
                    i1, i2 = i * stride[0], i * stride[0] + f_h
                    j1, j2 = j * stride[0], j * stride[0] + f_w
                    out[n, k, i1:i2, j1:j2] += tmp_out

C
chengduoZH 已提交
30
    out = out[:, :, pad[0]:out_h - pad[0], pad[1]:out_w - pad[1]]
Z
deconv  
zchen0211 已提交
31 32 33
    return out


Z
zchen0211 已提交
34
class TestConv2dTransposeOp(OpTest):
Z
deconv  
zchen0211 已提交
35
    def setUp(self):
Z
zchen0211 已提交
36
        # init as conv transpose
Z
deconv  
zchen0211 已提交
37 38 39
        self.init_op_type()
        self.init_test_case()

Z
zchen0211 已提交
40
        conv2dtranspose_param = {'stride': self.stride, 'pad': self.pad}
Z
deconv  
zchen0211 已提交
41 42
        input_ = np.random.random(self.input_size).astype("float32")
        filter_ = np.random.random(self.filter_size).astype("float32")
Y
Yu Yang 已提交
43 44
        output = conv2dtranspose_forward_naive(
            input_, filter_, conv2dtranspose_param).astype('float32')
Z
deconv  
zchen0211 已提交
45 46 47 48 49

        self.inputs = {'Input': input_, 'Filter': filter_}
        self.attrs = {
            'strides': self.stride,
            'paddings': self.pad,
Z
zchen0211 已提交
50
            'dilations': self.dilations
Z
deconv  
zchen0211 已提交
51 52 53 54 55 56
        }
        self.outputs = {'Output': output}

    def test_check_output(self):
        self.check_output()

Z
zchen0211 已提交
57 58 59 60
    def test_check_grad_no_input(self):
        self.check_grad(
            ['Filter'],
            'Output',
C
chengduoZH 已提交
61
            max_relative_error=0.02,
Z
zchen0211 已提交
62 63 64 65 66 67
            no_grad_set=set(['Input']))

    def test_check_grad_no_filter(self):
        self.check_grad(
            ['Input'],
            'Output',
C
chengduoZH 已提交
68
            max_relative_error=0.02,
Z
zchen0211 已提交
69
            no_grad_set=set(['Filter']))
Z
deconv  
zchen0211 已提交
70

Z
zchen0211 已提交
71 72
    def test_check_grad(self):
        self.check_grad(
C
chengduoZH 已提交
73 74 75 76 77 78 79 80 81 82 83 84
            set(['Input', 'Filter']), 'Output', max_relative_error=0.02)

    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
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3]

    def init_op_type(self):
        self.op_type = "conv2d_transpose"
Z
deconv  
zchen0211 已提交
85

Z
zchen0211 已提交
86

C
chengduoZH 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
class TestWithPad(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.dilations = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3]


class TestWithStride(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [2, 2]
        self.dilations = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3]


C
chengduoZH 已提交
107
# ------------ test_cudnn ------------
Z
zchen0211 已提交
108
class TestCudnn(TestConv2dTransposeOp):
Z
deconv  
zchen0211 已提交
109
    def init_op_type(self):
Z
zchen0211 已提交
110
        self.op_type = "conv2d_transpose_cudnn"
Z
zchen0211 已提交
111

Z
deconv  
zchen0211 已提交
112 113 114

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