From 6f61b5df7dd55f3f4ee5b7f42c7e31e9f84ea1be Mon Sep 17 00:00:00 2001 From: chengduoZH Date: Fri, 22 Sep 2017 10:49:59 +0800 Subject: [PATCH] fix unit test --- paddle/operators/pool_op.cc | 13 +++--- .../v2/framework/tests/test_pool2d_op.py | 40 +++++++++++++++++-- .../v2/framework/tests/test_pool3d_op.py | 40 ++++++++++++++++--- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/paddle/operators/pool_op.cc b/paddle/operators/pool_op.cc index c23adf63bf8..bcda072a2d3 100644 --- a/paddle/operators/pool_op.cc +++ b/paddle/operators/pool_op.cc @@ -46,20 +46,19 @@ class PoolOp : public framework::OperatorWithKernel { PADDLE_ENFORCE(pooling_type == "max" || pooling_type == "ave", "pooling_type should be 'max' or 'ave'"); - PADDLE_ENFORCE(ksize.size() == 2 || ksize.size() == 3, - "Pooling ksize should be 2-D or 3-D"); + PADDLE_ENFORCE(in_X->dims().size() == 4 || in_X->dims().size() == 5, + "Pooling intput should be 4-D or 5-D"); if (global_pooling == 1) { - for (size_t i = 0; i < ksize.size(); ++i) ksize[i] = in_X->dims()[i + 2]; + ksize.resize(static_cast(in_X->dims().size()) - 2); + for (size_t i = 0; i < ksize.size(); ++i) + ksize[i] = static_cast(in_X->dims()[i + 2]); } + if (ksize.size() == 2) { - PADDLE_ENFORCE_EQ(in_X->dims().size(), 4, - "Pool2DOp intput should be 4-D."); PADDLE_ENFORCE_EQ(strides.size(), 2, "Pool2DOp strides should be 2-D."); PADDLE_ENFORCE_EQ(paddings.size(), 2, "Pool2DOp paddings should be 2-D."); } else { - PADDLE_ENFORCE_EQ(in_X->dims().size(), 5, - "Pool3DOp intput should be 5-D."); PADDLE_ENFORCE_EQ(strides.size(), 3, "Pool3DOp strides should be 3-D."); PADDLE_ENFORCE_EQ(paddings.size(), 3, "Pool3DOp paddings should be 3-D."); } diff --git a/python/paddle/v2/framework/tests/test_pool2d_op.py b/python/paddle/v2/framework/tests/test_pool2d_op.py index 2a8fedc0379..0efddc28814 100644 --- a/python/paddle/v2/framework/tests/test_pool2d_op.py +++ b/python/paddle/v2/framework/tests/test_pool2d_op.py @@ -3,9 +3,11 @@ import numpy as np from op_test import OpTest -def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0]): +def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, H, W = x.shape + if global_pool == 1: + ksize = [H, W] H_out = (H - ksize[0] + 2 * paddings[0]) / strides[0] + 1 W_out = (W - ksize[1] + 2 * paddings[1]) / strides[1] + 1 out = np.zeros((N, C, H_out, W_out)) @@ -21,9 +23,11 @@ def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0]): return out -def ave_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0]): +def ave_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, H, W = x.shape + if global_pool == 1: + ksize = [H, W] H_out = (H - ksize[0] + 2 * paddings[0]) / strides[0] + 1 W_out = (W - ksize[1] + 2 * paddings[1]) / strides[1] + 1 out = np.zeros((N, C, H_out, W_out)) @@ -46,7 +50,7 @@ class TestPool2d_Op(OpTest): self.op_type = "pool2d" input = np.random.random(self.shape).astype("float32") output = self.pool2D_forward_naive(input, self.ksize, self.strides, - self.paddings) + self.paddings, self.global_pool) self.inputs = {'X': input} self.attrs = { @@ -54,6 +58,7 @@ class TestPool2d_Op(OpTest): 'paddings': self.paddings, 'ksize': self.ksize, 'poolingType': self.pool_type, + 'globalPooling': self.global_pool, } self.outputs = {'Out': output} @@ -66,6 +71,7 @@ class TestPool2d_Op(OpTest): self.check_grad(set(['X']), 'Out', max_relative_error=0.07) def initTestCase(self): + self.global_pool = 0 self.pool_type = "ave" self.pool2D_forward_naive = ave_pool2D_forward_naive self.shape = [2, 3, 5, 5] @@ -74,8 +80,21 @@ class TestPool2d_Op(OpTest): self.paddings = [0, 0] +class TestCase1(TestPool2d_Op): + def initTestCase(self): + self.global_pool = 0 + self.op_type = "pool2d" + self.pool_type = "ave" + self.pool2D_forward_naive = ave_pool2D_forward_naive + self.shape = [2, 3, 5, 5] + self.ksize = [3, 3] + self.strides = [1, 1] + self.paddings = [1, 1] + + class TestCase2(TestPool2d_Op): def initTestCase(self): + self.global_pool = 1 self.op_type = "pool2d" self.pool_type = "ave" self.pool2D_forward_naive = ave_pool2D_forward_naive @@ -85,8 +104,21 @@ class TestCase2(TestPool2d_Op): self.paddings = [1, 1] -class TestCase1(TestPool2d_Op): +class TestCase3(TestPool2d_Op): + def initTestCase(self): + self.global_pool = 0 + self.op_type = "pool2d" + self.pool_type = "max" + self.pool2D_forward_naive = max_pool2D_forward_naive + self.shape = [2, 3, 5, 5] + self.ksize = [3, 3] + self.strides = [1, 1] + self.paddings = [1, 1] + + +class TestCase4(TestPool2d_Op): def initTestCase(self): + self.global_pool = 1 self.op_type = "pool2d" self.pool_type = "max" self.pool2D_forward_naive = max_pool2D_forward_naive diff --git a/python/paddle/v2/framework/tests/test_pool3d_op.py b/python/paddle/v2/framework/tests/test_pool3d_op.py index 907ee0c0fe6..4ba3d754d53 100644 --- a/python/paddle/v2/framework/tests/test_pool3d_op.py +++ b/python/paddle/v2/framework/tests/test_pool3d_op.py @@ -3,9 +3,11 @@ import numpy as np from op_test import OpTest -def max_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0]): +def max_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, D, H, W = x.shape + if global_pool == 1: + ksize = [D, H, W] D_out = (D - ksize[0] + 2 * paddings[0]) / strides[0] + 1 H_out = (H - ksize[1] + 2 * paddings[1]) / strides[1] + 1 W_out = (W - ksize[2] + 2 * paddings[2]) / strides[2] + 1 @@ -19,16 +21,17 @@ def max_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0]): for j in xrange(W_out): w_start = np.max((j * strides[1] - paddings[1], 0)) w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W)) - x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end] out[:, :, k, i, j] = np.max(x_masked, axis=(2, 3, 4)) return out -def ave_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0]): +def ave_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, D, H, W = x.shape + if global_pool == 1: + ksize = [D, H, W] D_out = (D - ksize[0] + 2 * paddings[0]) / strides[0] + 1 H_out = (H - ksize[1] + 2 * paddings[1]) / strides[1] + 1 W_out = (W - ksize[2] + 2 * paddings[2]) / strides[2] + 1 @@ -42,7 +45,6 @@ def ave_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0]): for j in xrange(W_out): w_start = np.max((j * strides[1] - paddings[1], 0)) w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W)) - x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end] out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3, 4)) / ( @@ -56,7 +58,7 @@ class TestPool3d_Op(OpTest): self.op_type = "pool3d" input = np.random.random(self.shape).astype("float32") output = self.pool3D_forward_naive(input, self.ksize, self.strides, - self.paddings) + self.paddings, self.global_pool) self.inputs = {'X': input} self.attrs = { @@ -64,6 +66,7 @@ class TestPool3d_Op(OpTest): 'paddings': self.paddings, 'ksize': self.ksize, 'poolingType': self.pool_type, + 'globalPooling': self.global_pool, } self.outputs = {'Out': output} @@ -76,6 +79,7 @@ class TestPool3d_Op(OpTest): self.check_grad(set(['X']), 'Out', max_relative_error=0.07) def initTestCase(self): + self.global_pool = 0 self.pool_type = "ave" self.pool3D_forward_naive = ave_pool3D_forward_naive self.shape = [2, 3, 5, 5, 5] @@ -86,6 +90,7 @@ class TestPool3d_Op(OpTest): class TestCase1(TestPool3d_Op): def initTestCase(self): + self.global_pool = 0 self.op_type = "pool3d" self.pool_type = "ave" self.pool3D_forward_naive = ave_pool3D_forward_naive @@ -97,6 +102,31 @@ class TestCase1(TestPool3d_Op): class TestCase2(TestPool3d_Op): def initTestCase(self): + self.global_pool = 1 + self.op_type = "pool3d" + self.pool_type = "ave" + self.pool3D_forward_naive = ave_pool3D_forward_naive + self.shape = [2, 3, 7, 7, 7] + self.ksize = [3, 3, 3] + self.strides = [1, 1, 1] + self.paddings = [1, 1, 1] + + +class TestCase3(TestPool3d_Op): + def initTestCase(self): + self.global_pool = 0 + self.op_type = "pool3d" + self.pool_type = "max" + self.pool3D_forward_naive = max_pool3D_forward_naive + self.shape = [2, 3, 5, 5, 5] + self.ksize = [3, 3, 3] + self.strides = [1, 1, 1] + self.paddings = [1, 1, 1] + + +class TestCase4(TestPool3d_Op): + def initTestCase(self): + self.global_pool = 1 self.op_type = "pool3d" self.pool_type = "max" self.pool3D_forward_naive = max_pool3D_forward_naive -- GitLab