test_conv2d_transpose_op.py 10.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

Z
deconv  
zchen0211 已提交
17 18
import unittest
import numpy as np
19

20
import paddle.fluid.core as core
21
from op_test import OpTest
Z
deconv  
zchen0211 已提交
22 23


C
chengduoZH 已提交
24
def conv2dtranspose_forward_naive(input_, filter_, attrs):
Z
deconv  
zchen0211 已提交
25
    in_n, in_c, in_h, in_w = input_.shape
Y
Yibing Liu 已提交
26 27
    f_c, f_out_c, f_h, f_w = filter_.shape
    groups = attrs['groups']
Z
deconv  
zchen0211 已提交
28
    assert in_c == f_c
Y
Yibing Liu 已提交
29
    out_c = f_out_c * groups
M
minqiyang 已提交
30
    sub_in_c = in_c // groups
Z
deconv  
zchen0211 已提交
31

C
chengduoZH 已提交
32 33 34 35 36 37
    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
38 39 40 41
    if 'output_size' in attrs:
        output_size = attrs['output_size']
        out_h = output_size[0] + 2 * pad[0]
        out_w = output_size[1] + 2 * pad[1]
Z
deconv  
zchen0211 已提交
42 43 44 45 46 47

    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):
Y
Yibing Liu 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                for g in range(groups):
                    input_masked = input_[n, g * sub_in_c:(g + 1) * sub_in_c, i,
                                          j]  # (c)
                    input_masked = np.reshape(input_masked, (sub_in_c, 1, 1))
                    input_masked = np.tile(input_masked, (1, f_h, f_w))

                    for k in range(f_out_c):
                        tmp_out = np.sum(
                            input_masked *
                            filter_[g * sub_in_c:(g + 1) * sub_in_c, k, :, :],
                            axis=0)
                        i1, i2 = i * stride[0], i * stride[0] + d_bolck_h
                        j1, j2 = j * stride[0], j * stride[0] + d_bolck_h
                        out[n, g * f_out_c + k, i1:i2:dilations[0], j1:j2:
                            dilations[1]] += tmp_out
Z
deconv  
zchen0211 已提交
63

C
chengduoZH 已提交
64
    out = out[:, :, pad[0]:out_h - pad[0], pad[1]:out_w - pad[1]]
Z
deconv  
zchen0211 已提交
65 66 67
    return out


Z
zchen0211 已提交
68
class TestConv2dTransposeOp(OpTest):
Z
deconv  
zchen0211 已提交
69
    def setUp(self):
Z
zchen0211 已提交
70
        # init as conv transpose
J
Jacek Czaja 已提交
71
        self.is_test = False
72
        self.use_cudnn = False
J
Jacek Czaja 已提交
73
        self.use_mkldnn = False
74
        self.output_size = None
J
Jacek Czaja 已提交
75
        self.data_format = "AnyLayout"
Z
deconv  
zchen0211 已提交
76 77 78 79 80 81 82 83 84 85
        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,
Y
Yibing Liu 已提交
86
            'groups': self.groups,
87 88
            'dilations': self.dilations,
            'use_cudnn': self.use_cudnn,
J
Jacek Czaja 已提交
89 90 91
            'is_test': self.is_test,
            'use_mkldnn': self.use_mkldnn,
            'data_format': self.data_format
Z
deconv  
zchen0211 已提交
92
        }
93 94
        if self.output_size is not None:
            self.attrs['output_size'] = self.output_size
C
chengduoZH 已提交
95 96 97 98

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

Z
deconv  
zchen0211 已提交
99 100 101
        self.outputs = {'Output': output}

    def test_check_output(self):
102 103 104 105 106
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
Z
deconv  
zchen0211 已提交
107

Z
zchen0211 已提交
108
    def test_check_grad_no_input(self):
109 110 111 112 113 114 115 116 117 118 119 120 121
        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 已提交
122 123

    def test_check_grad_no_filter(self):
124 125 126 127 128 129 130 131 132 133 134 135 136
        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 已提交
137

Z
zchen0211 已提交
138
    def test_check_grad(self):
139 140 141 142 143 144 145 146 147 148
        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 已提交
149 150 151 152 153

    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.dilations = [1, 1]
Y
Yibing Liu 已提交
154
        self.groups = 1
C
chengduoZH 已提交
155 156 157 158 159 160
        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 已提交
161

Z
zchen0211 已提交
162

C
chengduoZH 已提交
163 164 165 166 167
class TestWithPad(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.dilations = [1, 1]
Y
Yibing Liu 已提交
168
        self.groups = 1
C
chengduoZH 已提交
169 170 171 172 173
        self.input_size = [2, 3, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3]


Y
Yibing Liu 已提交
174 175 176 177 178 179 180 181 182 183 184
class TestWithGroups(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.dilations = [1, 1]
        self.groups = 2
        self.input_size = [2, 4, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 3, 3, 3]


C
chengduoZH 已提交
185 186 187 188 189
class TestWithStride(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [2, 2]
        self.dilations = [1, 1]
Y
Yibing Liu 已提交
190
        self.groups = 1
C
chengduoZH 已提交
191 192 193 194 195
        self.input_size = [2, 3, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3]


C
chengduoZH 已提交
196 197 198 199
class TestWithDilation(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
Y
Yibing Liu 已提交
200
        self.groups = 1
C
chengduoZH 已提交
201 202 203 204 205 206
        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]


207 208 209 210 211 212 213 214 215 216 217 218
class TestWithEvenUpsample(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [2, 2]
        self.stride = [2, 2]
        self.groups = 1
        self.dilations = [1, 1]
        self.output_size = [14, 14]
        self.input_size = [2, 3, 7, 7]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 5, 5]


C
chengduoZH 已提交
219
# ------------ test_cudnn ------------
220 221
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
222
class TestCUDNN(TestConv2dTransposeOp):
Z
deconv  
zchen0211 已提交
223
    def init_op_type(self):
224 225
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"
Z
zchen0211 已提交
226

Z
deconv  
zchen0211 已提交
227

228 229
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
230
class TestCUDNNWithPad(TestWithPad):
C
chengduoZH 已提交
231 232 233
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
Y
Yibing Liu 已提交
234
        self.groups = 1
C
chengduoZH 已提交
235 236 237 238 239 240
        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):
241 242
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"
C
chengduoZH 已提交
243 244


245 246
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
247
class TestCUDNNWithStride(TestWithStride):
C
chengduoZH 已提交
248 249 250
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [2, 2]
Y
Yibing Liu 已提交
251
        self.groups = 1
C
chengduoZH 已提交
252 253 254 255 256 257
        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):
258 259
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"
C
chengduoZH 已提交
260 261


262 263
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
class TestCUDNNWithGroups(TestWithGroups):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.dilations = [1, 1]
        self.groups = 2
        self.input_size = [2, 4, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 3, 3, 3]

    def init_op_type(self):
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"


279 280 281 282 283 284 285 286
class TestDepthwiseConvTranspose(TestConv2dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [2, 2]
        self.dilations = [1, 1]
        self.input_size = [2, 8, 16, 16]  # NCHW
        self.groups = 8
        assert np.mod(self.input_size[1], self.groups) == 0
M
minqiyang 已提交
287
        f_c = self.input_size[1] // self.groups
288 289 290 291
        self.filter_size = [self.input_size[1], f_c, 4, 4]
        self.op_type = "depthwise_conv2d_transpose"


292 293 294 295 296 297 298 299 300
# ------------ test_cudnn ------------
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithEvenUpsample(TestWithEvenUpsample):
    def init_op_type(self):
        self.use_cudnn = True
        self.op_type = "conv2d_transpose"


301 302
# Please Don't remove the following code.
# Currently, CI use cudnn V5.0 which not support dilation conv.
303
# class TestCUDNNWithDilation(TestWithDilation):
C
chengduoZH 已提交
304 305 306 307 308 309 310 311 312
#     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):
313
#         self.op_type = "conv2d_transpose"
C
chengduoZH 已提交
314

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