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 20
import paddle.fluid.core as core
from op_test import OpTest
F
From00 已提交
21
from paddle.fluid.framework import _test_eager_guard
22
from paddle.nn.functional import avg_pool3d, max_pool3d
F
From00 已提交
23
from paddle.fluid.framework import _test_eager_guard
D
Double_V 已提交
24
from test_pool3d_op import adaptive_start_index, adaptive_end_index, pool3D_forward_naive, avg_pool3D_forward_naive, max_pool3D_forward_naive
25 26


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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)

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

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

D
Double_V 已提交
168 169 170 171 172
    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]))
173 174 175 176 177 178 179 180 181 182 183 184
            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 已提交
185

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

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

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

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

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

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

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

241 242 243 244
    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)
245 246 247 248 249 250 251 252 253 254 255 256
            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")
257

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

    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)

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

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

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

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

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

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

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

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

338

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

341
    def test_error_api(self):
342

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

        self.assertRaises(ValueError, run1)

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

        self.assertRaises(ValueError, run2)

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

        self.assertRaises(ValueError, run3)

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

        self.assertRaises(ValueError, run4)

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

        self.assertRaises(ValueError, run5)

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

        self.assertRaises(ValueError, run6)

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

        self.assertRaises(ValueError, run7)

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

        self.assertRaises(ValueError, run8)

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

        self.assertRaises(ValueError, run9)

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

        self.assertRaises(ValueError, run10)

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

        self.assertRaises(ValueError, run_kernel_out_of_range)

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

        self.assertRaises(ValueError, run_size_out_of_range)

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

508 509 510

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