test_pool3d_api.py 19.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import unittest
16

17
import numpy as np
18 19 20 21 22
from test_pool3d_op import (
    avg_pool3D_forward_naive,
    max_pool3D_forward_naive,
    pool3D_forward_naive,
)
23

24 25 26 27 28
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.nn.functional import avg_pool3d, max_pool3d

29

C
cnn 已提交
30
class TestPool3D_API(unittest.TestCase):
31 32 33 34 35 36 37 38
    def setUp(self):
        np.random.seed(123)
        self.places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.places.append(fluid.CUDAPlace(0))

    def check_avg_static_results(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
39 40 41
            input = fluid.data(
                name="input", shape=[2, 3, 32, 32, 32], dtype="float32"
            )
42 43 44
            result = avg_pool3d(input, kernel_size=2, stride=2, padding=0)

            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
45 46 47 48 49 50 51
            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='avg',
            )
52 53

            exe = fluid.Executor(place)
54 55 56 57 58
            fetches = exe.run(
                fluid.default_main_program(),
                feed={"input": input_np},
                fetch_list=[result],
            )
59
            np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05)
60 61 62 63 64 65 66

    def check_avg_dygraph_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            result = avg_pool3d(input, kernel_size=2, stride=2, padding="SAME")

67 68 69 70 71 72 73 74
            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='avg',
                padding_algorithm="SAME",
            )
75

76
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
77

78 79 80
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(
                kernel_size=2, stride=None, padding="SAME"
            )
81
            result = avg_pool3d_dg(input)
82
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
83

D
Double_V 已提交
84 85 86 87
    def check_avg_dygraph_padding_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            result = avg_pool3d(
                input,
                kernel_size=2,
                stride=2,
                padding=1,
                ceil_mode=False,
                exclusive=True,
            )

            result_np = avg_pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[1, 1, 1],
                ceil_mode=False,
                exclusive=False,
            )
D
Double_V 已提交
105

106
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
107

108 109 110 111 112 113 114
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(
                kernel_size=2,
                stride=None,
                padding=1,
                ceil_mode=False,
                exclusive=True,
            )
D
Double_V 已提交
115
            result = avg_pool3d_dg(input)
116
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
117 118 119 120 121

    def check_avg_dygraph_ceilmode_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
122 123 124 125 126 127 128 129 130 131 132
            result = avg_pool3d(
                input, kernel_size=2, stride=2, padding=0, ceil_mode=True
            )

            result_np = avg_pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                ceil_mode=True,
            )
D
Double_V 已提交
133

134
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
135

136 137 138
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(
                kernel_size=2, stride=None, padding=0, ceil_mode=True
            )
D
Double_V 已提交
139
            result = avg_pool3d_dg(input)
140
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
141

142 143
    def check_max_static_results(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
144 145 146
            input = fluid.data(
                name="input", shape=[2, 3, 32, 32, 32], dtype="float32"
            )
147 148 149
            result = max_pool3d(input, kernel_size=2, stride=2, padding=0)

            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
150 151 152 153 154 155 156
            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='max',
            )
157 158

            exe = fluid.Executor(place)
159 160 161 162 163
            fetches = exe.run(
                fluid.default_main_program(),
                feed={"input": input_np},
                fetch_list=[result],
            )
164
            np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05)
165 166 167 168 169 170 171

    def check_max_dygraph_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            result = max_pool3d(input, kernel_size=2, stride=2, padding=0)

172 173 174 175 176 177 178
            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='max',
            )
179

180
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
181 182 183
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(
                kernel_size=2, stride=None, padding=0
            )
184
            result = max_pool3d_dg(input)
185
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
186

D
Double_V 已提交
187 188 189 190
    def check_max_dygraph_ndhwc_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                np.transpose(input_np, [0, 2, 3, 4, 1])
            )
            result = max_pool3d(
                input,
                kernel_size=2,
                stride=2,
                padding=0,
                data_format="NDHWC",
                return_mask=False,
            )

            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='max',
            )

            np.testing.assert_allclose(
                np.transpose(result.numpy(), [0, 4, 1, 2, 3]),
                result_np,
                rtol=1e-05,
            )
D
Double_V 已提交
215 216 217 218 219

    def check_max_dygraph_ceilmode_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
220 221 222 223 224 225 226 227 228 229 230
            result = max_pool3d(
                input, kernel_size=2, stride=2, padding=0, ceil_mode=True
            )

            result_np = max_pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                ceil_mode=True,
            )
D
Double_V 已提交
231

232
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
233

234 235 236
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(
                kernel_size=2, stride=None, padding=0, ceil_mode=True
            )
D
Double_V 已提交
237
            result = max_pool3d_dg(input)
238
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
239 240 241 242 243

    def check_max_dygraph_padding_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
244 245 246 247 248 249 250 251 252 253 254
            result = max_pool3d(
                input, kernel_size=2, stride=2, padding=1, ceil_mode=False
            )

            result_np = max_pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[1, 1, 1],
                ceil_mode=False,
            )
D
Double_V 已提交
255

256
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
257

258 259 260
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(
                kernel_size=2, stride=None, padding=1, ceil_mode=False
            )
D
Double_V 已提交
261
            result = max_pool3d_dg(input)
262
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
263

264 265 266 267
    def check_max_dygraph_stride_is_none(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
            result, indices = max_pool3d(
                input,
                kernel_size=2,
                stride=None,
                padding="SAME",
                return_mask=True,
            )

            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='max',
                padding_algorithm="SAME",
            )
284

285
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
286 287 288
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(
                kernel_size=2, stride=2, padding=0
            )
289
            result = max_pool3d_dg(input)
290
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
291 292 293 294 295 296 297 298

    def check_max_dygraph_padding(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            padding = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
            result = max_pool3d(input, kernel_size=2, stride=2, padding=padding)

299 300 301 302 303 304 305
            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='max',
            )
306

307
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
308 309 310
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(
                kernel_size=2, stride=2, padding=0
            )
311
            result = max_pool3d_dg(input)
312
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
313 314 315

            padding = [0, 0, 0, 0, 0, 0]
            result = max_pool3d(input, kernel_size=2, stride=2, padding=padding)
316
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
317 318 319 320 321 322

    def check_avg_divisor(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            padding = 0
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            result = avg_pool3d(
                input,
                kernel_size=2,
                stride=2,
                padding=padding,
                divisor_override=8,
            )

            result_np = pool3D_forward_naive(
                input_np,
                ksize=[2, 2, 2],
                strides=[2, 2, 2],
                paddings=[0, 0, 0],
                pool_type='avg',
            )
338

339
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
340 341 342
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(
                kernel_size=2, stride=2, padding=0
            )
343
            result = avg_pool3d_dg(input)
344
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
345 346

            padding = [0, 0, 0, 0, 0, 0]
347 348 349 350 351 352 353
            result = avg_pool3d(
                input,
                kernel_size=2,
                stride=2,
                padding=padding,
                divisor_override=8,
            )
354
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
355 356 357 358 359 360 361 362 363 364 365

    def test_pool3d(self):
        for place in self.places:

            self.check_max_dygraph_results(place)
            self.check_avg_dygraph_results(place)
            self.check_max_static_results(place)
            self.check_avg_static_results(place)
            self.check_max_dygraph_stride_is_none(place)
            self.check_max_dygraph_padding(place)
            self.check_avg_divisor(place)
D
Double_V 已提交
366 367
            self.check_max_dygraph_ndhwc_results(place)
            self.check_max_dygraph_ceilmode_results(place)
368 369


C
cnn 已提交
370
class TestPool3DError_API(unittest.TestCase):
371 372 373
    def test_error_api(self):
        def run1():
            with fluid.dygraph.guard():
374
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
375 376
                    np.float32
                )
377 378
                input_pd = fluid.dygraph.to_variable(input_np)
                padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]]
379 380 381
                res_pd = avg_pool3d(
                    input_pd, kernel_size=2, stride=2, padding=padding
                )
382 383 384 385 386

        self.assertRaises(ValueError, run1)

        def run2():
            with fluid.dygraph.guard():
387
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
388 389
                    np.float32
                )
390 391
                input_pd = fluid.dygraph.to_variable(input_np)
                padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]]
392 393 394 395 396 397 398
                res_pd = avg_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding=padding,
                    data_format='NCDHW',
                )
399 400 401 402 403

        self.assertRaises(ValueError, run2)

        def run3():
            with fluid.dygraph.guard():
404
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
405 406
                    np.float32
                )
407 408
                input_pd = fluid.dygraph.to_variable(input_np)
                padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]]
409 410 411 412 413 414 415
                res_pd = avg_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding=padding,
                    data_format='NDHWC',
                )
416 417 418 419 420

        self.assertRaises(ValueError, run3)

        def run4():
            with fluid.dygraph.guard():
421
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
422 423
                    np.float32
                )
424
                input_pd = fluid.dygraph.to_variable(input_np)
425 426 427 428 429 430 431
                res_pd = avg_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding=0,
                    data_format='NNNN',
                )
432 433 434 435 436

        self.assertRaises(ValueError, run4)

        def run5():
            with fluid.dygraph.guard():
437
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
438 439
                    np.float32
                )
440
                input_pd = fluid.dygraph.to_variable(input_np)
441 442 443 444 445 446 447
                res_pd = max_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding=0,
                    data_format='NNNN',
                )
448 449 450 451 452

        self.assertRaises(ValueError, run5)

        def run6():
            with fluid.dygraph.guard():
453
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
454 455
                    np.float32
                )
456
                input_pd = fluid.dygraph.to_variable(input_np)
457 458 459 460 461 462 463
                res_pd = avg_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding="padding",
                    data_format='NNNN',
                )
464 465 466 467 468

        self.assertRaises(ValueError, run6)

        def run7():
            with fluid.dygraph.guard():
469
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
470 471
                    np.float32
                )
472
                input_pd = fluid.dygraph.to_variable(input_np)
473 474 475 476 477 478 479
                res_pd = max_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding="padding",
                    data_format='NNNN',
                )
480 481 482 483 484

        self.assertRaises(ValueError, run7)

        def run8():
            with fluid.dygraph.guard():
485
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
486 487
                    np.float32
                )
488
                input_pd = fluid.dygraph.to_variable(input_np)
489 490 491 492 493 494 495 496
                res_pd = avg_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding="VALID",
                    ceil_mode=True,
                    data_format='NNNN',
                )
497 498 499 500 501

        self.assertRaises(ValueError, run8)

        def run9():
            with fluid.dygraph.guard():
502
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
503 504
                    np.float32
                )
505
                input_pd = fluid.dygraph.to_variable(input_np)
506 507 508 509 510 511 512 513
                res_pd = max_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding="VALID",
                    ceil_mode=True,
                    data_format='NNNN',
                )
514 515 516

        self.assertRaises(ValueError, run9)

D
Double_V 已提交
517 518
        def run10():
            with fluid.dygraph.guard():
519
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
520 521
                    np.float32
                )
D
Double_V 已提交
522
                input_pd = fluid.dygraph.to_variable(input_np)
523 524 525 526 527 528 529 530
                res_pd = max_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=2,
                    padding=0,
                    data_format='NDHWC',
                    return_mask=True,
                )
D
Double_V 已提交
531 532 533

        self.assertRaises(ValueError, run10)

D
Double_V 已提交
534 535
        def run_kernel_out_of_range():
            with fluid.dygraph.guard():
536
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
537 538
                    np.float32
                )
D
Double_V 已提交
539
                input_pd = fluid.dygraph.to_variable(input_np)
540 541 542 543 544 545 546
                res_pd = avg_pool3d(
                    input_pd,
                    kernel_size=-1,
                    stride=2,
                    padding="VALID",
                    ceil_mode=True,
                )
D
Double_V 已提交
547 548 549 550 551

        self.assertRaises(ValueError, run_kernel_out_of_range)

        def run_size_out_of_range():
            with fluid.dygraph.guard():
552
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
553 554
                    np.float32
                )
D
Double_V 已提交
555
                input_pd = fluid.dygraph.to_variable(input_np)
556 557 558 559 560 561 562
                res_pd = avg_pool3d(
                    input_pd,
                    kernel_size=2,
                    stride=0,
                    padding="VALID",
                    ceil_mode=True,
                )
D
Double_V 已提交
563 564 565

        self.assertRaises(ValueError, run_size_out_of_range)

566 567 568

if __name__ == '__main__':
    unittest.main()