test_pool3d_api.py 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

F
From00 已提交
15
import paddle
16 17
import unittest
import numpy as np
F
From00 已提交
18
import paddle.fluid as fluid
19
import paddle.fluid.core as core
F
From00 已提交
20
from paddle.fluid.framework import _test_eager_guard
21
from paddle.nn.functional import avg_pool3d, max_pool3d
F
From00 已提交
22
from paddle.fluid.framework import _test_eager_guard
23
from test_pool3d_op import avg_pool3D_forward_naive, max_pool3D_forward_naive, pool3D_forward_naive
24 25


C
cnn 已提交
26
class TestPool3D_API(unittest.TestCase):
27

28 29 30 31 32 33 34 35
    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()):
36 37 38
            input = fluid.data(name="input",
                               shape=[2, 3, 32, 32, 32],
                               dtype="float32")
39 40 41
            result = avg_pool3d(input, kernel_size=2, stride=2, padding=0)

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

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

    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")

60 61 62 63 64 65
            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")
66

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

69 70 71
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(kernel_size=2,
                                                      stride=None,
                                                      padding="SAME")
72
            result = avg_pool3d_dg(input)
73
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
74

D
Double_V 已提交
75 76 77 78
    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)
79 80 81 82 83 84 85 86 87 88 89 90 91
            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 已提交
92

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

95 96 97 98 99
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(kernel_size=2,
                                                      stride=None,
                                                      padding=1,
                                                      ceil_mode=False,
                                                      exclusive=True)
D
Double_V 已提交
100
            result = avg_pool3d_dg(input)
101
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
102 103 104 105 106

    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)
107 108 109 110 111 112 113 114 115 116 117
            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 已提交
118

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

121 122 123 124
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(kernel_size=2,
                                                      stride=None,
                                                      padding=0,
                                                      ceil_mode=True)
D
Double_V 已提交
125
            result = avg_pool3d_dg(input)
126
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
127

128 129
    def check_max_static_results(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
130 131 132
            input = fluid.data(name="input",
                               shape=[2, 3, 32, 32, 32],
                               dtype="float32")
133 134 135
            result = max_pool3d(input, kernel_size=2, stride=2, padding=0)

            input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32")
136 137 138 139 140
            result_np = pool3D_forward_naive(input_np,
                                             ksize=[2, 2, 2],
                                             strides=[2, 2, 2],
                                             paddings=[0, 0, 0],
                                             pool_type='max')
141 142 143 144 145

            exe = fluid.Executor(place)
            fetches = exe.run(fluid.default_main_program(),
                              feed={"input": input_np},
                              fetch_list=[result])
146
            np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05)
147 148 149 150 151 152 153

    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)

154 155 156 157 158
            result_np = pool3D_forward_naive(input_np,
                                             ksize=[2, 2, 2],
                                             strides=[2, 2, 2],
                                             paddings=[0, 0, 0],
                                             pool_type='max')
159

160
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
161 162 163
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(kernel_size=2,
                                                      stride=None,
                                                      padding=0)
164
            result = max_pool3d_dg(input)
165
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
166

D
Double_V 已提交
167 168 169 170 171
    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(
                np.transpose(input_np, [0, 2, 3, 4, 1]))
172 173 174 175 176 177 178 179 180 181 182 183
            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')
D
Double_V 已提交
184

185 186 187 188
            np.testing.assert_allclose(np.transpose(result.numpy(),
                                                    [0, 4, 1, 2, 3]),
                                       result_np,
                                       rtol=1e-05)
D
Double_V 已提交
189 190 191 192 193

    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)
194 195 196 197 198 199 200 201 202 203 204
            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 已提交
205

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

208 209 210 211
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(kernel_size=2,
                                                      stride=None,
                                                      padding=0,
                                                      ceil_mode=True)
D
Double_V 已提交
212
            result = max_pool3d_dg(input)
213
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
D
Double_V 已提交
214 215 216 217 218

    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)
219 220 221 222 223 224 225 226 227 228 229
            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 已提交
230

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

233 234 235 236
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(kernel_size=2,
                                                      stride=None,
                                                      padding=1,
                                                      ceil_mode=False)
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_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)
244 245 246 247 248 249 250 251 252 253 254 255
            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")
256

257
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
258 259 260
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(kernel_size=2,
                                                      stride=2,
                                                      padding=0)
261
            result = max_pool3d_dg(input)
262
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
263 264 265 266 267 268 269 270

    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)

271 272 273 274 275
            result_np = pool3D_forward_naive(input_np,
                                             ksize=[2, 2, 2],
                                             strides=[2, 2, 2],
                                             paddings=[0, 0, 0],
                                             pool_type='max')
276

277
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
278 279 280
            max_pool3d_dg = paddle.nn.layer.MaxPool3D(kernel_size=2,
                                                      stride=2,
                                                      padding=0)
281
            result = max_pool3d_dg(input)
282
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
283 284 285

            padding = [0, 0, 0, 0, 0, 0]
            result = max_pool3d(input, kernel_size=2, stride=2, padding=padding)
286
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
287 288 289 290 291 292

    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
293 294 295 296 297 298 299 300 301 302 303
            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')
304

305
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
306 307 308
            avg_pool3d_dg = paddle.nn.layer.AvgPool3D(kernel_size=2,
                                                      stride=2,
                                                      padding=0)
309
            result = avg_pool3d_dg(input)
310
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
311 312

            padding = [0, 0, 0, 0, 0, 0]
313 314 315 316 317
            result = avg_pool3d(input,
                                kernel_size=2,
                                stride=2,
                                padding=padding,
                                divisor_override=8)
318
            np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
319 320 321 322 323 324 325 326 327 328 329

    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 已提交
330 331
            self.check_max_dygraph_ndhwc_results(place)
            self.check_max_dygraph_ceilmode_results(place)
332

333
    def test_dygraph_api(self):
F
From00 已提交
334 335 336
        with _test_eager_guard():
            self.test_pool3d()

337

C
cnn 已提交
338
class TestPool3DError_API(unittest.TestCase):
339

340
    def test_error_api(self):
341

342 343
        def run1():
            with fluid.dygraph.guard():
344 345
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
346 347
                input_pd = fluid.dygraph.to_variable(input_np)
                padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]]
348 349 350 351
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding=padding)
352 353 354 355 356

        self.assertRaises(ValueError, run1)

        def run2():
            with fluid.dygraph.guard():
357 358
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
359 360
                input_pd = fluid.dygraph.to_variable(input_np)
                padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]]
361 362 363 364 365
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding=padding,
                                    data_format='NCDHW')
366 367 368 369 370

        self.assertRaises(ValueError, run2)

        def run3():
            with fluid.dygraph.guard():
371 372
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
373 374
                input_pd = fluid.dygraph.to_variable(input_np)
                padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]]
375 376 377 378 379
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding=padding,
                                    data_format='NDHWC')
380 381 382 383 384

        self.assertRaises(ValueError, run3)

        def run4():
            with fluid.dygraph.guard():
385 386
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
387
                input_pd = fluid.dygraph.to_variable(input_np)
388 389 390 391 392
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding=0,
                                    data_format='NNNN')
393 394 395 396 397

        self.assertRaises(ValueError, run4)

        def run5():
            with fluid.dygraph.guard():
398 399
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
400
                input_pd = fluid.dygraph.to_variable(input_np)
401 402 403 404 405
                res_pd = max_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding=0,
                                    data_format='NNNN')
406 407 408 409 410

        self.assertRaises(ValueError, run5)

        def run6():
            with fluid.dygraph.guard():
411 412
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
413
                input_pd = fluid.dygraph.to_variable(input_np)
414 415 416 417 418
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding="padding",
                                    data_format='NNNN')
419 420 421 422 423

        self.assertRaises(ValueError, run6)

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

        self.assertRaises(ValueError, run7)

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

        self.assertRaises(ValueError, run8)

        def run9():
            with fluid.dygraph.guard():
451 452
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
453
                input_pd = fluid.dygraph.to_variable(input_np)
454 455 456 457 458 459
                res_pd = max_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding="VALID",
                                    ceil_mode=True,
                                    data_format='NNNN')
460 461 462

        self.assertRaises(ValueError, run9)

D
Double_V 已提交
463 464
        def run10():
            with fluid.dygraph.guard():
465 466
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
D
Double_V 已提交
467
                input_pd = fluid.dygraph.to_variable(input_np)
468 469 470 471 472 473
                res_pd = max_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding=0,
                                    data_format='NDHWC',
                                    return_mask=True)
D
Double_V 已提交
474 475 476

        self.assertRaises(ValueError, run10)

D
Double_V 已提交
477 478
        def run_kernel_out_of_range():
            with fluid.dygraph.guard():
479 480
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
D
Double_V 已提交
481
                input_pd = fluid.dygraph.to_variable(input_np)
482 483 484 485 486
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=-1,
                                    stride=2,
                                    padding="VALID",
                                    ceil_mode=True)
D
Double_V 已提交
487 488 489 490 491

        self.assertRaises(ValueError, run_kernel_out_of_range)

        def run_size_out_of_range():
            with fluid.dygraph.guard():
492 493
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    np.float32)
D
Double_V 已提交
494
                input_pd = fluid.dygraph.to_variable(input_np)
495 496 497 498 499
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=0,
                                    padding="VALID",
                                    ceil_mode=True)
D
Double_V 已提交
500 501 502

        self.assertRaises(ValueError, run_size_out_of_range)

503
    def test_dygraph_api(self):
F
From00 已提交
504 505 506
        with _test_eager_guard():
            self.test_error_api()

507 508 509

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