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

C
chengduoZH 已提交
17 18
import unittest
import numpy as np
19

20
import paddle.fluid.core as core
21
import paddle.fluid as fluid
22
from op_test import OpTest
C
chengduoZH 已提交
23 24


C
chengduoZH 已提交
25
def conv3dtranspose_forward_naive(input_, filter_, attrs):
26 27 28 29 30 31 32 33
    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 已提交
34
    in_n, in_c, in_d, in_h, in_w = input_.shape
35 36
    f_c, f_out_c, f_d, f_h, f_w = filter_.shape
    groups = attrs['groups']
C
chengduoZH 已提交
37
    assert in_c == f_c
38
    out_c = f_out_c * groups
M
minqiyang 已提交
39
    sub_in_c = in_c // groups
C
chengduoZH 已提交
40

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

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    def _get_padding_with_SAME(input_shape, kernel_size, kernel_stride):
        padding = []
        for input_size, filter_size, stride_size in zip(
                input_shape, kernel_size, kernel_stride):
            out_size = int((input_size + stride_size - 1) / stride_size)
            pad_sum = np.max((
                (out_size - 1) * stride_size + filter_size - input_size, 0))
            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":
61 62
        dilations = [1, 1, 1]
        input_data_shape = input_.shape[2:5]
63 64 65 66 67 68 69 70 71 72
        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 已提交
73 74 75 76 77 78
    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 已提交
79 80 81 82 83 84
    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):
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                    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 已提交
102

103 104 105 106
    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]
    if attrs['data_format'] == 'NHWC':
        out = np.transpose(out, [0, 2, 3, 4, 1])
C
chengduoZH 已提交
107 108 109
    return out


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

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

C
chengduoZH 已提交
139 140 141
        self.outputs = {'Output': output}

    def test_check_output(self):
142 143 144 145 146
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
C
chengduoZH 已提交
147 148

    def test_check_grad(self):
149 150 151 152 153 154 155 156 157 158
        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 已提交
159 160

    def test_check_grad_no_filter(self):
161 162 163 164 165 166 167
        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']))
168
        elif self.check_no_filter:
169 170 171 172 173
            self.check_grad(
                ['Input'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Filter']))
C
chengduoZH 已提交
174 175

    def test_check_grad_no_input(self):
176 177 178 179 180 181 182
        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']))
183
        elif self.check_no_input:
184 185 186 187 188
            self.check_grad(
                ['Filter'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Input']))
C
chengduoZH 已提交
189 190 191 192 193

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

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


C
cnn 已提交
203
class TestWithSymmetricPad(TestConv3DTransposeOp):
C
chengduoZH 已提交
204
    def init_test_case(self):
205
        self.check_no_input = True
C
chengduoZH 已提交
206 207 208
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
209
        self.groups = 1
C
chengduoZH 已提交
210 211 212 213 214
        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
cnn 已提交
215
class TestWithAsymmetricPad(TestConv3DTransposeOp):
216 217 218 219 220 221 222 223 224 225
    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
        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
cnn 已提交
226
class TestWithSAMEPad(TestConv3DTransposeOp):
227
    def init_test_case(self):
228 229
        self.stride = [1, 1, 2]
        self.dilations = [1, 2, 1]
230
        self.groups = 1
231
        self.input_size = [2, 3, 5, 5, 6]  # NCDHW
232
        f_c = self.input_size[1]
233
        self.filter_size = [f_c, 6, 3, 3, 4]
234 235 236
        self.padding_algorithm = 'SAME'


C
cnn 已提交
237
class TestWithVALIDPad(TestConv3DTransposeOp):
238
    def init_test_case(self):
239
        self.stride = [2, 1, 1]
240 241 242 243
        self.dilations = [1, 1, 1]
        self.groups = 1
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
244
        self.filter_size = [f_c, 6, 3, 4, 3]
245 246 247
        self.padding_algorithm = 'VALID'


C
cnn 已提交
248
class TestWithStride(TestConv3DTransposeOp):
249
    def init_test_case(self):
250
        self.check_no_filter = True
251
        self.pad = [1, 1, 1]
252
        self.stride = [2, 2, 2]
253
        self.dilations = [1, 1, 1]
254 255
        self.groups = 1
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
256
        f_c = self.input_size[1]
257
        self.filter_size = [f_c, 6, 3, 3, 3]
258 259


C
cnn 已提交
260
class TestWithGroups(TestConv3DTransposeOp):
C
chengduoZH 已提交
261 262
    def init_test_case(self):
        self.pad = [1, 1, 1]
263
        self.stride = [1, 1, 1]
C
chengduoZH 已提交
264
        self.dilations = [1, 1, 1]
265 266
        self.groups = 2
        self.input_size = [2, 4, 5, 5, 5]  # NCHW
C
chengduoZH 已提交
267
        f_c = self.input_size[1]
268
        self.filter_size = [f_c, 3, 3, 3, 3]
C
chengduoZH 已提交
269 270


C
cnn 已提交
271
class TestWithDilation(TestConv3DTransposeOp):
C
chengduoZH 已提交
272 273 274 275
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [2, 2, 2]
276
        self.groups = 1
C
chengduoZH 已提交
277 278 279 280 281
        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
cnn 已提交
282
class Test_NHWC(TestConv3DTransposeOp):
283 284 285 286 287 288 289 290 291 292 293
    def init_test_case(self):
        self.pad = [0, 0, 0]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
        self.input_size = [2, 5, 5, 5, 3]  # NDHWC
        f_c = self.input_size[-1]
        self.filter_size = [f_c, 6, 3, 3, 3]
        self.data_format = 'NHWC'


C
chengduoZH 已提交
294
# ------------ test_cudnn ------------
295 296
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
C
cnn 已提交
297
class TestCUDNN(TestConv3DTransposeOp):
C
chengduoZH 已提交
298
    def init_op_type(self):
299 300
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
301 302


303 304
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
305
class TestCUDNNWithSymmetricPad(TestWithSymmetricPad):
C
chengduoZH 已提交
306 307 308 309
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
310
        self.groups = 1
C
chengduoZH 已提交
311 312 313 314 315
        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):
316 317
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
318 319


320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNWithAsymmetricPad(TestWithAsymmetricPad):
    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
        self.input_size = [2, 3, 4, 4, 4]  # NCDHW
        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):
    def init_test_case(self):
341 342
        self.stride = [1, 1, 2]
        self.dilations = [1, 2, 1]
343 344 345
        self.groups = 1
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
346
        self.filter_size = [f_c, 6, 3, 4, 3]
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
        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):
    def init_test_case(self):
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        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"


371 372
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
373
class TestCUDNNWithStride(TestWithStride):
C
chengduoZH 已提交
374 375 376 377
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
378
        self.groups = 1
C
chengduoZH 已提交
379 380 381 382 383
        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):
384 385
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
386 387


388 389
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
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"


405 406
# Please Don't remove the following code.
# Currently, CI use cudnn V5.0 which not support dilation conv.
407
# class TestCUDNNWithDilation(TestWithDilation):
C
chengduoZH 已提交
408 409 410 411 412 413 414 415 416
#     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):
417
#         self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
418

419 420 421

@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
C
cnn 已提交
422
class TestCUDNN_NHWC(TestConv3DTransposeOp):
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    def init_test_case(self):
        self.pad = [0, 0, 0]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
        self.input_size = [2, 5, 5, 5, 3]  # NDHWC
        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):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.groups = 1
        self.input_size = [2, 5, 5, 5, 3]  # NDHWC
        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):
    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
        self.input_size = [2, 5, 5, 5, 3]  # NDHWC
        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):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
        self.groups = 1
        self.input_size = [2, 5, 5, 5, 3]  # NCDHW
        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):
    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, 5, 5, 5, 4]  # NCHW
        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 已提交
510 511
if __name__ == '__main__':
    unittest.main()