Exemple #1
0
 def test_upper_case(self):
     with fluid.dygraph.guard():
         data = np.random.random((3, 32, 32, 5)).astype('float32')
         x = fluid.dygraph.to_variable(data)
         pool2d = fluid.dygraph.Pool2D(pool_size=2,
                                       pool_type='MAX',
                                       pool_stride=1,
                                       pool_padding=[0, 0],
                                       global_pooling=False,
                                       data_format='nhwc')
         out1 = pool2d(x)
         out2 = pool2D_forward_naive(data, [2, 2], [1, 1],
                                     paddings=[0, 0],
                                     pool_type='max',
                                     data_format='NHWC')
         self.assertTrue(np.allclose(out1.numpy(), out2))
Exemple #2
0
    def check_avg_dygraph_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            result = avg_pool2d(input, kernel_size=2, stride=2, padding=0)

            result_np = pool2D_forward_naive(
                input_np,
                ksize=[2, 2],
                strides=[2, 2],
                paddings=[0, 0],
                pool_type='avg')
            self.assertTrue(np.allclose(result.numpy(), result_np))

            avg_pool2d_dg = paddle.nn.layer.AvgPool2D(
                kernel_size=2, stride=2, padding=0)
            result = avg_pool2d_dg(input)
            self.assertTrue(np.allclose(result.numpy(), result_np))
    def setUp(self):
        self.set_npu()
        self.op_type = "pool2d"
        self.init_kernel_type()
        self.init_data_type()
        self.init_test_case()
        self.padding_algorithm = "EXPLICIT"
        self.init_paddings()
        self.init_global_pool()
        self.init_kernel_type()
        self.init_pool_type()
        self.init_ceil_mode()
        self.init_exclusive()
        self.init_adaptive()
        self.init_data_format()
        self.init_shape()

        input = np.random.random(self.shape).astype(self.dtype)
        if self.pool_type == "max":
            input = np.array([x for x in range(np.prod(self.shape))
                              ]).reshape(self.shape).astype(self.dtype)
        output = pool2D_forward_naive(
            input, self.ksize, self.strides, self.paddings, self.global_pool,
            self.ceil_mode, self.exclusive, self.adaptive, self.data_format,
            self.pool_type, self.padding_algorithm).astype(self.dtype)
        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)}

        self.attrs = {
            'strides': self.strides,
            'paddings': self.paddings,
            'ksize': self.ksize,
            'pooling_type': self.pool_type,
            'global_pooling': self.global_pool,
            'use_cudnn': False,
            'use_mkldnn': False,
            'ceil_mode': self.ceil_mode,
            'data_format': self.data_format,
            'exclusive': self.exclusive,
            'adaptive': self.adaptive,
            "padding_algorithm": self.padding_algorithm,
        }

        self.outputs = {'Out': output}
Exemple #4
0
    def check_avg_static_results(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            input = fluid.data(
                name="input", shape=[2, 3, 32, 32], dtype="float32")
            result = avg_pool2d(input, kernel_size=2, stride=2, padding=0)

            input_np = np.random.random([2, 3, 32, 32]).astype("float32")
            result_np = pool2D_forward_naive(
                input_np,
                ksize=[2, 2],
                strides=[2, 2],
                paddings=[0, 0],
                pool_type='avg')

            exe = fluid.Executor(place)
            fetches = exe.run(fluid.default_main_program(),
                              feed={"input": input_np},
                              fetch_list=[result])
            self.assertTrue(np.allclose(fetches[0], result_np))
    def check_max_dygraph_nhwc_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(
                np.transpose(input_np, [0, 2, 3, 1]))
            result = max_pool2d(input,
                                kernel_size=2,
                                stride=2,
                                padding=0,
                                return_indices=False,
                                data_format="NHWC")

            result_np = pool2D_forward_naive(input_np,
                                             ksize=[2, 2],
                                             strides=[2, 2],
                                             paddings=[0, 0],
                                             pool_type='max')
            self.assertTrue(
                np.allclose(np.transpose(result.numpy(), [0, 3, 1, 2]),
                            result_np))
    def check_max_dygraph_padding(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            padding = [[0, 0], [0, 0], [0, 0], [0, 0]]
            result = max_pool2d(input,
                                kernel_size=2,
                                stride=2,
                                padding=padding,
                                return_indices=False)

            result_np = pool2D_forward_naive(input_np,
                                             ksize=[2, 2],
                                             strides=[2, 2],
                                             paddings=[0, 0],
                                             pool_type='max')
            self.assertTrue(np.allclose(result.numpy(), result_np))

            max_pool2d_dg = paddle.nn.layer.MaxPool2d(kernel_size=2,
                                                      stride=2,
                                                      padding=0)
            result = max_pool2d_dg(input)
            self.assertTrue(np.allclose(result.numpy(), result_np))
Exemple #7
0
    def setUp(self):
        self.place = paddle.device.MLUPlace(0)
        self.__class__.use_mlu = True
        self.op_type = "pool2d"
        self.init_data_type()
        self.init_test_case()
        self.padding_algorithm = "EXPLICIT"
        self.init_paddings()
        self.init_global_pool()
        self.init_pool_type()
        self.init_ceil_mode()
        self.init_exclusive()
        self.init_adaptive()
        self.init_data_format()
        self.init_shape()

        input = np.random.random(self.shape).astype(self.dtype)
        output = pool2D_forward_naive(
            input, self.ksize, self.strides, self.paddings, self.global_pool,
            self.ceil_mode, self.exclusive, self.adaptive, self.data_format,
            self.pool_type, self.padding_algorithm).astype(self.dtype)
        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)}

        self.attrs = {
            'strides': self.strides,
            'paddings': self.paddings,
            'ksize': self.ksize,
            'pooling_type': self.pool_type,
            'global_pooling': self.global_pool,
            'ceil_mode': self.ceil_mode,
            'data_format': self.data_format,
            'exclusive': self.exclusive,
            'adaptive': self.adaptive,
            "padding_algorithm": self.padding_algorithm,
        }

        self.outputs = {'Out': output}
Exemple #8
0
    def check_max_dygraph_stride_is_none(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            result, indices = max_pool2d(
                input,
                kernel_size=2,
                stride=None,
                padding="SAME",
                return_mask=True)

            result_np = pool2D_forward_naive(
                input_np,
                ksize=[2, 2],
                strides=[2, 2],
                paddings=[0, 0],
                pool_type='max',
                padding_algorithm="SAME")
            self.assertTrue(np.allclose(result.numpy(), result_np))

            max_pool2d_dg = paddle.nn.layer.MaxPool2D(
                kernel_size=2, stride=2, padding=0)
            result = max_pool2d_dg(input)
            self.assertTrue(np.allclose(result.numpy(), result_np))
Exemple #9
0
    def test_api(self):
        x_NHWC = np.random.random([2, 5, 5, 3]).astype("float32")
        x_NCHW = np.random.random([2, 3, 5, 5]).astype("float32")

        input_NHWC = fluid.layers.data(name="input_NHWC",
                                       shape=[2, 5, 5, 3],
                                       append_batch_size=False,
                                       dtype="float32")

        input_NCHW = fluid.layers.data(name="input_NCHW",
                                       shape=[2, 3, 5, 5],
                                       append_batch_size=False,
                                       dtype="float32")

        input_NHWC_negetive = fluid.layers.data(name="input_NHWC_negetive",
                                                shape=[2, -1, 5, 3],
                                                append_batch_size=False,
                                                dtype="float32")

        input_NCHW_negetive = fluid.layers.data(name="input_NCHW_negetive",
                                                shape=[2, 3, -1, -1],
                                                append_batch_size=False,
                                                dtype="float32")

        ksize = [3, 3]
        out_1 = fluid.layers.pool2d(input=input_NHWC,
                                    pool_size=ksize,
                                    pool_type="max",
                                    pool_padding=[1, 1],
                                    data_format="NHWC")

        out_2 = fluid.layers.pool2d(input=input_NHWC,
                                    pool_size=ksize,
                                    pool_type="avg",
                                    pool_padding=[[0, 0], [1, 1], [1, 1],
                                                  [0, 0]],
                                    data_format="NHWC")

        out_3 = fluid.layers.pool2d(input=input_NCHW,
                                    pool_size=ksize,
                                    pool_type="avg",
                                    pool_padding=[[0, 0], [0, 0], [1, 1],
                                                  [1, 1]],
                                    data_format="NCHW")

        out_4 = fluid.layers.pool2d(input=input_NCHW,
                                    pool_size=ksize,
                                    pool_type="avg",
                                    pool_padding=[1, 2, 1, 0],
                                    data_format="NCHW")
        # test VALID
        out_5 = fluid.layers.pool2d(input=input_NCHW,
                                    pool_size=ksize,
                                    pool_type="avg",
                                    pool_padding="VALID",
                                    data_format="NCHW")

        out_6 = fluid.layers.pool2d(input=input_NHWC,
                                    pool_size=ksize,
                                    pool_type="max",
                                    pool_padding="VALID",
                                    data_format="NHWC")

        # test SAME
        out_7 = fluid.layers.pool2d(input=input_NCHW,
                                    pool_size=[4, 4],
                                    pool_type="avg",
                                    pool_padding="SAME",
                                    data_format="NCHW")

        out_8 = fluid.layers.pool2d(input=input_NHWC,
                                    pool_size=[4, 4],
                                    pool_type="max",
                                    pool_padding="SAME",
                                    data_format="NHWC")

        # test negetive
        out_9 = fluid.layers.pool2d(input=input_NHWC_negetive,
                                    pool_size=ksize,
                                    pool_type="avg",
                                    pool_padding=[0, 0],
                                    data_format="NHWC")
        assert out_9.shape == (2, -1, 3, 3)

        out_10 = fluid.layers.pool2d(input=input_NCHW_negetive,
                                     pool_size=ksize,
                                     pool_type="avg",
                                     pool_padding=[0, 0],
                                     data_format="NCHW")
        assert out_10.shape == (2, 3, -1, -1)

        exe = fluid.Executor(place=fluid.MLUPlace(0))
        [res_1, res_2, res_3, res_4, res_5, res_6, res_7,
         res_8] = exe.run(fluid.default_main_program(),
                          feed={
                              "input_NHWC": x_NHWC,
                              "input_NCHW": x_NCHW,
                              "input_NHWC_negetive": x_NHWC,
                              "input_NCHW_negetive": x_NCHW
                          },
                          fetch_list=[
                              out_1, out_2, out_3, out_4, out_5, out_6, out_7,
                              out_8
                          ])

        assert np.allclose(
            res_1,
            pool2D_forward_naive(x=x_NHWC,
                                 ksize=ksize,
                                 pool_type="max",
                                 strides=[1, 1],
                                 paddings=[1, 1],
                                 data_format="NHWC"))

        assert np.allclose(
            res_2,
            pool2D_forward_naive(x=x_NHWC,
                                 ksize=ksize,
                                 pool_type="avg",
                                 strides=[1, 1],
                                 paddings=[1, 1, 1, 1],
                                 data_format="NHWC"))
        assert np.allclose(res_3,
                           pool2D_forward_naive(x=x_NCHW,
                                                ksize=ksize,
                                                pool_type="avg",
                                                strides=[1, 1],
                                                paddings=[1, 1, 1, 1],
                                                data_format="NCHW"),
                           rtol=0.07,
                           atol=1e-05)

        assert np.allclose(res_4,
                           pool2D_forward_naive(x=x_NCHW,
                                                ksize=ksize,
                                                pool_type="avg",
                                                strides=[1, 1],
                                                paddings=[1, 2, 1, 0],
                                                data_format="NCHW"),
                           rtol=0.07,
                           atol=1e-05)

        # VALID
        assert np.allclose(
            res_5,
            pool2D_forward_naive(
                x=x_NCHW,
                ksize=ksize,
                pool_type="avg",
                strides=[1, 1],
                paddings=[10, 20],  # any ele is ok
                padding_algorithm="VALID",
                data_format="NCHW"),
            rtol=0.07,
            atol=1e-05)
        assert np.allclose(
            res_6,
            pool2D_forward_naive(x=x_NHWC,
                                 ksize=ksize,
                                 pool_type="max",
                                 strides=[1, 1],
                                 paddings=[10, 20],
                                 padding_algorithm="VALID",
                                 data_format="NHWC"))
        # SAME
        assert np.allclose(res_7,
                           pool2D_forward_naive(x=x_NCHW,
                                                ksize=[4, 4],
                                                pool_type="avg",
                                                strides=[1, 1],
                                                paddings=[10, 20],
                                                padding_algorithm="SAME",
                                                data_format="NCHW"),
                           rtol=0.07,
                           atol=1e-05)

        assert np.allclose(
            res_8,
            pool2D_forward_naive(x=x_NHWC,
                                 ksize=[4, 4],
                                 pool_type="max",
                                 strides=[1, 1],
                                 paddings=[10, 20],
                                 padding_algorithm="SAME",
                                 data_format="NHWC"))