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

K
Kaipeng Deng 已提交
18
import paddle
19

K
Kaipeng Deng 已提交
20
paddle.enable_static()
21
import paddle.fluid.core as core
22
import paddle.fluid as fluid
23
from op_test import OpTest
C
chengduoZH 已提交
24 25


C
chengduoZH 已提交
26
def conv3dtranspose_forward_naive(input_, filter_, attrs):
27 28 29 30 31 32 33 34
    padding_algorithm = attrs['padding_algorithm']
    if padding_algorithm not in ["SAME", "VALID", "EXPLICIT"]:
        raise ValueError("Unknown Attr(padding_algorithm): '%s'. "
                         "It can only be 'SAME' or 'VALID'." %
                         str(padding_algorithm))

    if attrs['data_format'] == 'NHWC':
        input_ = np.transpose(input_, [0, 4, 1, 2, 3])
C
chengduoZH 已提交
35
    in_n, in_c, in_d, in_h, in_w = input_.shape
36 37
    f_c, f_out_c, f_d, f_h, f_w = filter_.shape
    groups = attrs['groups']
C
chengduoZH 已提交
38
    assert in_c == f_c
39
    out_c = f_out_c * groups
M
minqiyang 已提交
40
    sub_in_c = in_c // groups
C
chengduoZH 已提交
41

C
chengduoZH 已提交
42 43 44
    stride, pad, dilations = attrs['strides'], attrs['paddings'], attrs[
        'dilations']

45 46
    def _get_padding_with_SAME(input_shape, kernel_size, kernel_stride):
        padding = []
47 48 49
        for input_size, filter_size, stride_size in zip(input_shape,
                                                        kernel_size,
                                                        kernel_stride):
50
            out_size = int((input_size + stride_size - 1) / stride_size)
51 52
            pad_sum = np.max(
                ((out_size - 1) * stride_size + filter_size - input_size, 0))
53 54 55 56 57 58 59 60 61 62
            pad_0 = int(pad_sum / 2)
            pad_1 = int(pad_sum - pad_0)
            padding.append(pad_0)
            padding.append(pad_1)
        return padding

    ksize = filter_.shape[2:5]
    if padding_algorithm == "VALID":
        pad = [0, 0, 0, 0, 0, 0]
    elif padding_algorithm == "SAME":
63 64
        dilations = [1, 1, 1]
        input_data_shape = input_.shape[2:5]
65 66 67 68 69 70 71 72 73 74
        pad = _get_padding_with_SAME(input_data_shape, ksize, stride)

    pad_d_0, pad_d_1 = pad[0], pad[0]
    pad_h_0, pad_h_1 = pad[1], pad[1]
    pad_w_0, pad_w_1 = pad[2], pad[2]
    if len(pad) == 6:
        pad_d_0, pad_d_1 = pad[0], pad[1]
        pad_h_0, pad_h_1 = pad[2], pad[3]
        pad_w_0, pad_w_1 = pad[4], pad[5]

C
chengduoZH 已提交
75 76 77 78 79 80
    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 已提交
81 82 83 84 85 86
    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):
87
                    for g in range(groups):
88 89 90
                        input_masked = input_[n,
                                              g * sub_in_c:(g + 1) * sub_in_c,
                                              d, i, j]  # (c)
91 92 93 94 95
                        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):
96 97 98
                            tmp_out = np.sum(input_masked *
                                             filter_[g * sub_in_c:(g + 1) *
                                                     sub_in_c, k, :, :, :],
99 100 101 102
                                             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
103 104 105
                            out[n, g * f_out_c + k, d1:d2:dilations[0],
                                i1:i2:dilations[1],
                                j1:j2:dilations[2]] += tmp_out
C
chengduoZH 已提交
106

107 108
    out = out[:, :, pad_d_0:out_d - pad_d_1, pad_h_0:out_h - pad_h_1,
              pad_w_0:out_w - pad_w_1]
109 110
    if attrs['data_format'] == 'NHWC':
        out = np.transpose(out, [0, 2, 3, 4, 1])
C
chengduoZH 已提交
111 112 113
    return out


C
cnn 已提交
114
class TestConv3DTransposeOp(OpTest):
115

C
chengduoZH 已提交
116 117
    def setUp(self):
        # init as conv transpose
118
        self.use_cudnn = False
119 120
        self.check_no_input = False
        self.check_no_filter = False
121 122 123
        self.data_format = 'NCHW'
        self.pad = [0, 0, 0]
        self.padding_algorithm = "EXPLICIT"
C
chengduoZH 已提交
124 125 126 127 128 129 130 131 132 133
        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,
134
            'padding_algorithm': self.padding_algorithm,
135
            'dilations': self.dilations,
136
            'groups': self.groups,
137
            'use_cudnn': self.use_cudnn,
138
            'data_format': self.data_format
C
chengduoZH 已提交
139
        }
C
chengduoZH 已提交
140 141 142 143

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

C
chengduoZH 已提交
144 145 146
        self.outputs = {'Output': output}

    def test_check_output(self):
147 148 149 150 151
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
C
chengduoZH 已提交
152 153

    def test_check_grad(self):
154 155
        if self.use_cudnn:
            place = core.CUDAPlace(0)
156 157 158 159
            self.check_grad_with_place(place,
                                       set(['Input', 'Filter']),
                                       'Output',
                                       max_relative_error=0.03)
160
        else:
161 162 163
            self.check_grad(set(['Input', 'Filter']),
                            'Output',
                            max_relative_error=0.03)
C
chengduoZH 已提交
164 165

    def test_check_grad_no_filter(self):
166 167
        if self.use_cudnn:
            place = core.CUDAPlace(0)
168 169 170 171
            self.check_grad_with_place(place, ['Input'],
                                       'Output',
                                       max_relative_error=0.03,
                                       no_grad_set=set(['Filter']))
172
        elif self.check_no_filter:
173 174 175 176
            self.check_grad(['Input'],
                            'Output',
                            max_relative_error=0.03,
                            no_grad_set=set(['Filter']))
C
chengduoZH 已提交
177 178

    def test_check_grad_no_input(self):
179 180
        if self.use_cudnn:
            place = core.CUDAPlace(0)
181 182 183 184
            self.check_grad_with_place(place, ['Filter'],
                                       'Output',
                                       max_relative_error=0.03,
                                       no_grad_set=set(['Input']))
185
        elif self.check_no_input:
186 187 188 189
            self.check_grad(['Filter'],
                            'Output',
                            max_relative_error=0.03,
                            no_grad_set=set(['Input']))
C
chengduoZH 已提交
190 191 192 193 194

    def init_test_case(self):
        self.pad = [0, 0, 0]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
195
        self.groups = 1
C
chengduoZH 已提交
196
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
C
chengduoZH 已提交
197 198 199 200
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
C
chengduoZH 已提交
201
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
202 203


C
cnn 已提交
204
class TestWithSymmetricPad(TestConv3DTransposeOp):
205

C
chengduoZH 已提交
206
    def init_test_case(self):
207
        self.check_no_input = True
C
chengduoZH 已提交
208 209 210
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
211
        self.groups = 1
K
Kaipeng Deng 已提交
212
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
C
chengduoZH 已提交
213 214 215 216
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


C
cnn 已提交
217
class TestWithAsymmetricPad(TestConv3DTransposeOp):
218

219 220 221 222 223
    def init_test_case(self):
        self.pad = [1, 0, 1, 0, 1, 2]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
224
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
225 226 227 228
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


C
cnn 已提交
229
class TestWithSAMEPad(TestConv3DTransposeOp):
230

231
    def init_test_case(self):
232 233
        self.stride = [1, 1, 2]
        self.dilations = [1, 2, 1]
234
        self.groups = 1
K
Kaipeng Deng 已提交
235
        self.input_size = [1, 2, 5, 5, 6]  # NCDHW
236
        f_c = self.input_size[1]
237
        self.filter_size = [f_c, 6, 3, 3, 4]
238 239 240
        self.padding_algorithm = 'SAME'


C
cnn 已提交
241
class TestWithVALIDPad(TestConv3DTransposeOp):
242

243
    def init_test_case(self):
244
        self.stride = [2, 1, 1]
245 246
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
247
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
248
        f_c = self.input_size[1]
249
        self.filter_size = [f_c, 6, 3, 4, 3]
250 251 252
        self.padding_algorithm = 'VALID'


C
cnn 已提交
253
class TestWithStride(TestConv3DTransposeOp):
254

255
    def init_test_case(self):
256
        self.check_no_filter = True
257
        self.pad = [1, 1, 1]
258
        self.stride = [2, 2, 2]
259
        self.dilations = [1, 1, 1]
260
        self.groups = 1
K
Kaipeng Deng 已提交
261
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
262
        f_c = self.input_size[1]
263
        self.filter_size = [f_c, 6, 3, 3, 3]
264 265


C
cnn 已提交
266
class TestWithGroups(TestConv3DTransposeOp):
267

C
chengduoZH 已提交
268 269
    def init_test_case(self):
        self.pad = [1, 1, 1]
270
        self.stride = [1, 1, 1]
C
chengduoZH 已提交
271
        self.dilations = [1, 1, 1]
272
        self.groups = 2
K
Kaipeng Deng 已提交
273
        self.input_size = [1, 2, 5, 5, 5]  # NCHW
C
chengduoZH 已提交
274
        f_c = self.input_size[1]
275
        self.filter_size = [f_c, 3, 3, 3, 3]
C
chengduoZH 已提交
276 277


C
cnn 已提交
278
class TestWithDilation(TestConv3DTransposeOp):
279

C
chengduoZH 已提交
280 281 282 283
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [2, 2, 2]
284
        self.groups = 1
K
Kaipeng Deng 已提交
285
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
C
chengduoZH 已提交
286 287 288 289
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


C
cnn 已提交
290
class Test_NHWC(TestConv3DTransposeOp):
291

292 293 294 295 296
    def init_test_case(self):
        self.pad = [0, 0, 0]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
297
        self.input_size = [1, 5, 5, 5, 2]  # NDHWC
298 299 300 301 302
        f_c = self.input_size[-1]
        self.filter_size = [f_c, 6, 3, 3, 3]
        self.data_format = 'NHWC'


C
chengduoZH 已提交
303
# ------------ test_cudnn ------------
304 305
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
C
cnn 已提交
306
class TestCUDNN(TestConv3DTransposeOp):
307

C
chengduoZH 已提交
308
    def init_op_type(self):
309 310
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
311 312


313 314
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
315
class TestCUDNNWithSymmetricPad(TestWithSymmetricPad):
316

C
chengduoZH 已提交
317 318 319 320
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
321
        self.groups = 1
K
Kaipeng Deng 已提交
322
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
C
chengduoZH 已提交
323 324 325 326
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
327 328
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
329 330


331 332 333
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithAsymmetricPad(TestWithAsymmetricPad):
334

335 336 337 338 339
    def init_test_case(self):
        self.pad = [1, 1, 1, 0, 0, 2]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
340
        self.input_size = [1, 2, 4, 4, 4]  # NCDHW
341 342 343 344 345 346 347 348 349 350 351
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

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


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithSAMEPad(TestWithSAMEPad):
352

353
    def init_test_case(self):
354 355
        self.stride = [1, 1, 2]
        self.dilations = [1, 2, 1]
356
        self.groups = 1
K
Kaipeng Deng 已提交
357
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
358
        f_c = self.input_size[1]
359
        self.filter_size = [f_c, 6, 3, 4, 3]
360 361 362 363 364 365 366 367 368 369
        self.padding_algorithm = 'SAME'

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


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithVALIDPad(TestWithVALIDPad):
370

371 372 373 374
    def init_test_case(self):
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
375
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
376 377 378 379 380 381 382 383 384
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]
        self.padding_algorithm = 'VALID'

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


385 386
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
387
class TestCUDNNWithStride(TestWithStride):
388

C
chengduoZH 已提交
389 390 391 392
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
393
        self.groups = 1
K
Kaipeng Deng 已提交
394
        self.input_size = [1, 2, 5, 5, 5]  # NCDHW
C
chengduoZH 已提交
395 396 397 398
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
399 400
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
401 402


403 404
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
405
class TestCUDNNWithGroups(TestWithGroups):
406

407 408 409 410 411
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 2
K
Kaipeng Deng 已提交
412
        self.input_size = [1, 2, 5, 5, 5]  # NCHW
413 414 415 416 417 418 419 420
        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"


421 422
# Please Don't remove the following code.
# Currently, CI use cudnn V5.0 which not support dilation conv.
423
# class TestCUDNNWithDilation(TestWithDilation):
C
chengduoZH 已提交
424 425 426 427 428 429 430 431 432
#     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):
433
#         self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
434

435 436 437

@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
C
cnn 已提交
438
class TestCUDNN_NHWC(TestConv3DTransposeOp):
439

440 441 442 443 444
    def init_test_case(self):
        self.pad = [0, 0, 0]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
445
        self.input_size = [1, 5, 5, 5, 2]  # NDHWC
446 447 448 449 450 451 452 453 454 455 456 457
        f_c = self.input_size[-1]
        self.filter_size = [f_c, 6, 3, 3, 3]
        self.data_format = 'NHWC'

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


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithSymmetricPad_NHWC(TestWithSymmetricPad):
458

459 460 461 462 463
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
464
        self.input_size = [1, 5, 5, 5, 2]  # NDHWC
465 466 467 468 469 470 471 472 473 474 475 476
        f_c = self.input_size[-1]
        self.filter_size = [f_c, 6, 3, 3, 3]
        self.data_format = 'NHWC'

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


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithAsymmetricPad_NHWC(TestWithAsymmetricPad):
477

478 479 480 481 482
    def init_test_case(self):
        self.pad = [1, 0, 1, 0, 0, 2]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
483
        self.input_size = [1, 5, 5, 5, 2]  # NDHWC
484 485 486 487 488 489 490 491 492 493 494 495
        f_c = self.input_size[-1]
        self.filter_size = [f_c, 6, 3, 3, 3]
        self.data_format = 'NHWC'

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


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithStride_NHWC(TestWithStride):
496

497 498 499 500 501
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
        self.groups = 1
K
Kaipeng Deng 已提交
502
        self.input_size = [1, 5, 5, 5, 2]  # NDHWC
503 504 505 506 507 508 509 510 511 512 513 514
        f_c = self.input_size[-1]
        self.filter_size = [f_c, 6, 3, 3, 3]
        self.data_format = 'NHWC'

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


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithGroups_NHWC(TestWithGroups):
515

516 517 518 519 520
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 2
K
Kaipeng Deng 已提交
521
        self.input_size = [1, 5, 5, 5, 2]  # NDHWC
522 523 524 525 526 527 528 529 530
        f_c = self.input_size[-1]
        self.filter_size = [f_c, 3, 3, 3, 3]
        self.data_format = 'NHWC'

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


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