test_conv3d_transpose_op.py 9.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.

C
chengduoZH 已提交
15 16
import unittest
import numpy as np
17

18
import paddle.fluid.core as core
19
from .op_test import OpTest
C
chengduoZH 已提交
20 21


C
chengduoZH 已提交
22
def conv3dtranspose_forward_naive(input_, filter_, attrs):
C
chengduoZH 已提交
23
    in_n, in_c, in_d, in_h, in_w = input_.shape
24 25
    f_c, f_out_c, f_d, f_h, f_w = filter_.shape
    groups = attrs['groups']
C
chengduoZH 已提交
26
    assert in_c == f_c
27 28
    out_c = f_out_c * groups
    sub_in_c = in_c / groups
C
chengduoZH 已提交
29

C
chengduoZH 已提交
30 31 32 33 34 35 36 37 38
    stride, pad, dilations = attrs['strides'], attrs['paddings'], attrs[
        'dilations']

    d_bolck_d = dilations[0] * (f_d - 1) + 1
    d_bolck_h = dilations[1] * (f_h - 1) + 1
    d_bolck_w = dilations[2] * (f_w - 1) + 1
    out_d = (in_d - 1) * stride[0] + d_bolck_d
    out_h = (in_h - 1) * stride[1] + d_bolck_h
    out_w = (in_w - 1) * stride[2] + d_bolck_w
C
chengduoZH 已提交
39 40 41 42 43 44
    out = np.zeros((in_n, out_c, out_d, out_h, out_w))

    for n in range(in_n):
        for d in range(in_d):
            for i in range(in_h):
                for j in range(in_w):
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
                    for g in range(groups):
                        input_masked = input_[n, g * sub_in_c:(g + 1
                                                               ) * sub_in_c, d,
                                              i, j]  # (c)
                        input_masked = np.reshape(input_masked,
                                                  (sub_in_c, 1, 1, 1))
                        input_masked = np.tile(input_masked, (1, f_d, 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)
                            d1, d2 = d * stride[0], d * stride[0] + d_bolck_d
                            i1, i2 = i * stride[1], i * stride[1] + d_bolck_h
                            j1, j2 = j * stride[2], j * stride[2] + d_bolck_w
                            out[n, g * f_out_c + k, d1:d2:dilations[0], i1:i2:
                                dilations[1], j1:j2:dilations[2]] += tmp_out
C
chengduoZH 已提交
62

C
chengduoZH 已提交
63 64
    out = out[:, :, pad[0]:out_d - pad[0], pad[1]:out_h - pad[1], pad[2]:out_w -
              pad[2]]
C
chengduoZH 已提交
65 66 67 68 69 70
    return out


class TestConv3dTransposeOp(OpTest):
    def setUp(self):
        # init as conv transpose
71
        self.use_cudnn = False
C
chengduoZH 已提交
72 73 74 75 76 77 78 79 80 81
        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,
82
            'dilations': self.dilations,
83
            'groups': self.groups,
84 85
            'use_cudnn': self.use_cudnn,
            'data_format': 'AnyLayout'  # TODO(dzhwinter) : should be fix latter
C
chengduoZH 已提交
86
        }
C
chengduoZH 已提交
87 88 89 90

        output = conv3dtranspose_forward_naive(input_, filter_,
                                               self.attrs).astype("float32")

C
chengduoZH 已提交
91 92 93
        self.outputs = {'Output': output}

    def test_check_output(self):
94 95 96 97 98
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
C
chengduoZH 已提交
99 100

    def test_check_grad(self):
101 102 103 104 105 106 107 108 109 110
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place,
                set(['Input', 'Filter']),
                'Output',
                max_relative_error=0.03)
        else:
            self.check_grad(
                set(['Input', 'Filter']), 'Output', max_relative_error=0.03)
C
chengduoZH 已提交
111 112

    def test_check_grad_no_filter(self):
113 114 115 116 117 118 119 120 121 122 123 124 125
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place, ['Input'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Filter']))
        else:
            self.check_grad(
                ['Input'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Filter']))
C
chengduoZH 已提交
126 127

    def test_check_grad_no_input(self):
128 129 130 131 132 133 134 135 136 137 138 139 140
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place, ['Filter'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Input']))
        else:
            self.check_grad(
                ['Filter'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Input']))
C
chengduoZH 已提交
141 142 143 144 145

    def init_test_case(self):
        self.pad = [0, 0, 0]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
146
        self.groups = 1
C
chengduoZH 已提交
147
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
C
chengduoZH 已提交
148 149 150 151
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
C
chengduoZH 已提交
152
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
153 154


C
chengduoZH 已提交
155 156 157 158 159
class TestWithPad(TestConv3dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
160
        self.groups = 1
C
chengduoZH 已提交
161 162 163 164 165
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


166 167 168 169 170 171 172 173 174 175 176
class TestWithGroups(TestConv3dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 2
        self.input_size = [2, 4, 5, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 3, 3, 3, 3]


C
chengduoZH 已提交
177 178 179 180 181
class TestWithStride(TestConv3dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
182
        self.groups = 1
C
chengduoZH 已提交
183 184 185 186 187
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


C
chengduoZH 已提交
188 189 190 191 192
class TestWithDilation(TestConv3dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [2, 2, 2]
193
        self.groups = 1
C
chengduoZH 已提交
194 195 196 197 198
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


C
chengduoZH 已提交
199
# ------------ test_cudnn ------------
200 201
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
202
class TestCUDNN(TestConv3dTransposeOp):
C
chengduoZH 已提交
203
    def init_op_type(self):
204 205
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
206 207


208 209
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
210
class TestCUDNNWithPad(TestWithPad):
C
chengduoZH 已提交
211 212 213 214
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
215
        self.groups = 1
C
chengduoZH 已提交
216 217 218 219 220
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
221 222
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
223 224


225 226
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
227
class TestCUDNNWithStride(TestWithStride):
C
chengduoZH 已提交
228 229 230 231
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
232
        self.groups = 1
C
chengduoZH 已提交
233 234 235 236 237
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
238 239
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
240 241


242 243
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
class TestCUDNNWithGroups(TestWithGroups):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 2
        self.input_size = [2, 4, 5, 5, 5]  # NCHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 3, 3, 3, 3]

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


259 260
# Please Don't remove the following code.
# Currently, CI use cudnn V5.0 which not support dilation conv.
261
# class TestCUDNNWithDilation(TestWithDilation):
C
chengduoZH 已提交
262 263 264 265 266 267 268 269 270
#     def init_test_case(self):
#         self.pad = [1, 1, 1]
#         self.stride = [2, 2, 2]
#         self.dilations = [2, 2, 2]
#         self.input_size = [2, 3, 5, 5, 5]  # NCDHW
#         f_c = self.input_size[1]
#         self.filter_size = [f_c, 6, 3, 3, 3]
#
#     def init_op_type(self):
271
#         self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
272

C
chengduoZH 已提交
273 274
if __name__ == '__main__':
    unittest.main()