test_conv2d_transpose_op.py 6.7 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.
Z
deconv  
zchen0211 已提交
14 15
import unittest
import numpy as np
16 17

import paddle.v2.fluid.core as core
Z
deconv  
zchen0211 已提交
18 19 20
from op_test import OpTest


C
chengduoZH 已提交
21
def conv2dtranspose_forward_naive(input_, filter_, attrs):
Z
deconv  
zchen0211 已提交
22 23 24 25
    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

C
chengduoZH 已提交
26 27 28 29 30 31
    stride, pad, dilations = attrs['strides'], attrs['paddings'], attrs[
        'dilations']
    d_bolck_h = dilations[0] * (f_h - 1) + 1
    d_bolck_w = dilations[1] * (f_w - 1) + 1
    out_h = (in_h - 1) * stride[0] + d_bolck_h
    out_w = (in_w - 1) * stride[1] + d_bolck_w
Z
deconv  
zchen0211 已提交
32 33 34 35 36 37 38 39 40 41 42 43

    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)
C
chengduoZH 已提交
44 45 46
                    i1, i2 = i * stride[0], i * stride[0] + d_bolck_h
                    j1, j2 = j * stride[0], j * stride[0] + d_bolck_h
                    out[n, k, i1:i2:dilations[0], j1:j2:dilations[1]] += tmp_out
Z
deconv  
zchen0211 已提交
47

C
chengduoZH 已提交
48
    out = out[:, :, pad[0]:out_h - pad[0], pad[1]:out_w - pad[1]]
Z
deconv  
zchen0211 已提交
49 50 51
    return out


Z
zchen0211 已提交
52
class TestConv2dTransposeOp(OpTest):
Z
deconv  
zchen0211 已提交
53
    def setUp(self):
Z
zchen0211 已提交
54
        # init as conv transpose
55
        self.use_cudnn = False
Z
deconv  
zchen0211 已提交
56 57 58 59 60 61 62 63 64 65
        self.init_op_type()
        self.init_test_case()

        input_ = np.random.random(self.input_size).astype("float32")
        filter_ = np.random.random(self.filter_size).astype("float32")

        self.inputs = {'Input': input_, 'Filter': filter_}
        self.attrs = {
            'strides': self.stride,
            'paddings': self.pad,
66 67 68
            'dilations': self.dilations,
            'use_cudnn': self.use_cudnn,
            'data_format': 'AnyLayout'  # TODO(dzhwinter) : should be fix latter
Z
deconv  
zchen0211 已提交
69
        }
C
chengduoZH 已提交
70 71 72 73

        output = conv2dtranspose_forward_naive(input_, filter_,
                                               self.attrs).astype('float32')

Z
deconv  
zchen0211 已提交
74 75 76
        self.outputs = {'Output': output}

    def test_check_output(self):
77 78 79 80 81
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
Z
deconv  
zchen0211 已提交
82

Z
zchen0211 已提交
83
    def test_check_grad_no_input(self):
84 85 86 87 88 89 90 91 92 93 94 95 96
        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']))
Z
zchen0211 已提交
97 98

    def test_check_grad_no_filter(self):
99 100 101 102 103 104 105 106 107 108 109 110 111
        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']))
Z
deconv  
zchen0211 已提交
112

Z
zchen0211 已提交
113
    def test_check_grad(self):
114 115 116 117 118 119 120 121 122 123
        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)
C
chengduoZH 已提交
124 125 126 127 128 129 130 131 132 133 134

    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 已提交
135

Z
zchen0211 已提交
136

C
chengduoZH 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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 已提交
157 158 159 160 161 162 163 164 165 166
class TestWithDilation(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.dilations = [2, 2]
        self.input_size = [2, 3, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3]


C
chengduoZH 已提交
167
# ------------ test_cudnn ------------
168
class TestCUDNN(TestConv2dTransposeOp):
Z
deconv  
zchen0211 已提交
169
    def init_op_type(self):
170 171
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"
Z
zchen0211 已提交
172

Z
deconv  
zchen0211 已提交
173

174
class TestCUDNNWithPad(TestWithPad):
C
chengduoZH 已提交
175 176 177 178 179 180 181 182 183
    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]

    def init_op_type(self):
184 185
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"
C
chengduoZH 已提交
186 187


188
class TestCUDNNWithStride(TestWithStride):
C
chengduoZH 已提交
189 190 191 192 193 194 195 196 197
    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]

    def init_op_type(self):
198 199
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"
C
chengduoZH 已提交
200 201 202


# #cudnn v5 does not support dilation conv.
203
# class TestCUDNNWithDilation(TestWithDilation):
C
chengduoZH 已提交
204 205 206 207 208 209 210 211 212
#     def init_test_case(self):
#         self.pad = [1, 1]
#         self.stride = [2, 2]
#         self.dilations = [2, 2]
#         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):
213
#         self.op_type = "conv2d_transpose"
C
chengduoZH 已提交
214

Z
deconv  
zchen0211 已提交
215 216
if __name__ == '__main__':
    unittest.main()