提交 6f61b5df 编写于 作者: C chengduoZH

fix unit test

上级 84a2512b
......@@ -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<size_t>(in_X->dims().size()) - 2);
for (size_t i = 0; i < ksize.size(); ++i)
ksize[i] = static_cast<int>(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.");
}
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册