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

from __future__ import print_function
from __future__ import division

F
From00 已提交
18
import paddle
19 20
import unittest
import numpy as np
F
From00 已提交
21
import paddle.fluid as fluid
22 23
import paddle.fluid.core as core
from op_test import OpTest
F
From00 已提交
24
from paddle.fluid.framework import _test_eager_guard
25
from paddle.nn.functional import avg_pool3d, max_pool3d
F
From00 已提交
26
from paddle.fluid.framework import _test_eager_guard
D
Double_V 已提交
27
from test_pool3d_op import adaptive_start_index, adaptive_end_index, pool3D_forward_naive, avg_pool3D_forward_naive, max_pool3D_forward_naive
28 29


C
cnn 已提交
30
class TestPool3D_API(unittest.TestCase):
31

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

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

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

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

64 65 66 67 68 69
            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")
70

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

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

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

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

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

    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)
111 112 113 114 115 116 117 118 119 120 121
            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 已提交
122

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

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

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

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

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

    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)

158 159 160 161 162
            result_np = pool3D_forward_naive(input_np,
                                             ksize=[2, 2, 2],
                                             strides=[2, 2, 2],
                                             paddings=[0, 0, 0],
                                             pool_type='max')
163

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

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

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

    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)
198 199 200 201 202 203 204 205 206 207 208
            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 已提交
209

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

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

    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)
223 224 225 226 227 228 229 230 231 232 233
            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 已提交
234

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

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

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

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

    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)

275 276 277 278 279
            result_np = pool3D_forward_naive(input_np,
                                             ksize=[2, 2, 2],
                                             strides=[2, 2, 2],
                                             paddings=[0, 0, 0],
                                             pool_type='max')
280

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

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

    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
297 298 299 300 301 302 303 304 305 306 307
            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')
308

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

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

    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 已提交
334 335
            self.check_max_dygraph_ndhwc_results(place)
            self.check_max_dygraph_ceilmode_results(place)
336

F
From00 已提交
337 338 339 340
    def test_dygraph_final_state_api(self):
        with _test_eager_guard():
            self.test_pool3d()

341

C
cnn 已提交
342
class TestPool3DError_API(unittest.TestCase):
343

344
    def test_error_api(self):
345

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

        self.assertRaises(ValueError, run1)

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

        self.assertRaises(ValueError, run2)

        def run3():
            with fluid.dygraph.guard():
375 376
                input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype(
                    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 382 383
                res_pd = avg_pool3d(input_pd,
                                    kernel_size=2,
                                    stride=2,
                                    padding=padding,
                                    data_format='NDHWC')
384 385 386 387 388

        self.assertRaises(ValueError, run3)

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

        self.assertRaises(ValueError, run4)

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

        self.assertRaises(ValueError, run5)

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

        self.assertRaises(ValueError, run6)

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

        self.assertRaises(ValueError, run7)

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

        self.assertRaises(ValueError, run8)

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

        self.assertRaises(ValueError, run9)

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

        self.assertRaises(ValueError, run10)

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

        self.assertRaises(ValueError, run_kernel_out_of_range)

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

        self.assertRaises(ValueError, run_size_out_of_range)

F
From00 已提交
507 508 509 510
    def test_dygraph_final_state_api(self):
        with _test_eager_guard():
            self.test_error_api()

511 512 513

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