diff --git a/paddle/operators/math/pooling.cc b/paddle/operators/math/pooling.cc index 5ce748ff08b1045011b6efb7497d28331d067b15..72b260e72985990ed666de0cd03ddf4c1af82246 100644 --- a/paddle/operators/math/pooling.cc +++ b/paddle/operators/math/pooling.cc @@ -137,19 +137,23 @@ class Pool2dBackwardFunctor { template class Pool2dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool2dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool2dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool2dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; template class Pool3dForwardFunctor { @@ -302,19 +306,23 @@ class Pool3dBackwardFunctor { template class Pool3dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool3dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool3dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool3dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; } // namespace math } // namespace operators } // namespace paddle diff --git a/paddle/operators/math/pooling.cu b/paddle/operators/math/pooling.cu index 09e6bd9000a4e76af4aded60a69bd40950177254..619fd64b97980d53ec952ab995fbcbf3dc17a3ce 100644 --- a/paddle/operators/math/pooling.cu +++ b/paddle/operators/math/pooling.cu @@ -190,19 +190,23 @@ class Pool2dBackwardFunctor { template class Pool2dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool2dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool2dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool2dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; template __global__ void KernelPool3DForward( @@ -414,19 +418,23 @@ class Pool3dBackwardFunctor { template class Pool3dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool3dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool3dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool3dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; } // namespace math } // namespace operators diff --git a/paddle/operators/math/pooling.h b/paddle/operators/math/pooling.h index 627ece2ca4023070c63684f48b2546ca8a4f17bb..b975ad31af89c4c8bb70a1699ebd3c999b0f925c 100644 --- a/paddle/operators/math/pooling.h +++ b/paddle/operators/math/pooling.h @@ -33,6 +33,18 @@ class maxPool { DEVICE inline T initial() { return static_cast(-FLT_MAX); } DEVICE inline void process(T& y, const T& x) { y = y > x ? y : x; } DEVICE inline void finalize(T& y, const T& poo_size) {} +}; + +template +class avgPool { + public: + DEVICE inline T initial() { return static_cast(0); } + DEVICE inline void process(T& y, const T& x) { y += x; } + DEVICE inline void finalize(T& y, const T& poo_size) { y /= poo_size; } +}; +template +class maxPoolGrad { + public: DEVICE inline void gradProcess(const T& x, const T& y, const T& dy, T& dx, T scale) { dx += dy * (x == y); @@ -40,11 +52,8 @@ class maxPool { }; template -class avePool { +class avgPoolGrad { public: - DEVICE inline T initial() { return static_cast(0); } - DEVICE inline void process(T& y, const T& x) { y += x; } - DEVICE inline void finalize(T& y, const T& poo_size) { y /= poo_size; } DEVICE inline void gradProcess(const T& x, const T& y, const T& dy, T& dx, T scale) { dx += (scale * dy); diff --git a/paddle/operators/pool_op.cc b/paddle/operators/pool_op.cc index c57922373f826fa447d93b46eaf07d7c40adadd4..e51249beb913a8a75c3f2868c26019343102c226 100644 --- a/paddle/operators/pool_op.cc +++ b/paddle/operators/pool_op.cc @@ -41,8 +41,8 @@ class PoolOp : public framework::OperatorWithKernel { std::vector strides = Attr>("strides"); std::vector paddings = Attr>("paddings"); - PADDLE_ENFORCE(pooling_type == "max" || pooling_type == "ave", - "pooling_type should be 'max' or 'ave'"); + PADDLE_ENFORCE(pooling_type == "max" || pooling_type == "avg", + "pooling_type should be 'max' or 'avg'"); PADDLE_ENFORCE(in_X->dims().size() == 4 || in_X->dims().size() == 5, "Pooling intput should be 4-D or 5-D"); @@ -99,7 +99,7 @@ class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker { AddAttr("poolingType", "poolingType of pooling operator." - "str constant equal to 'max' or 'ave'"); + "str constant equal to 'max' or 'avg'"); AddAttr>( "ksize", "pooling size(height, width) of pooling operator."); AddAttr( @@ -140,7 +140,7 @@ class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker { AddAttr("poolingType", "poolingType of pooling operator." - "str constant equal to 'max' or 'ave'"); + "str constant equal to 'max' or 'avg'"); AddAttr>( "ksize", "pooling size(depth, height, width) of pooling operator."); AddAttr( diff --git a/paddle/operators/pool_op.h b/paddle/operators/pool_op.h index 5a40f76172b363949d4fd8fcdd1ca39c02d453d3..6f1d05272cd1e66f8858d19454fe7dc08ce7b45d 100644 --- a/paddle/operators/pool_op.h +++ b/paddle/operators/pool_op.h @@ -52,11 +52,11 @@ class PoolKernel : public framework::OpKernel { pool2d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool2dForwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPool, T> pool2d_forward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPool pool_process; pool2d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); } @@ -69,11 +69,11 @@ class PoolKernel : public framework::OpKernel { paddle::operators::math::pool::maxPool pool_process; pool3d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool3dForwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPool, T> pool3d_forward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPool pool_process; pool3d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); } @@ -112,16 +112,16 @@ class PoolGradKernel : public framework::OpKernel { case 2: { if (pooling_type == "max") { paddle::operators::math::Pool2dBackwardFunctor< - Place, paddle::operators::math::pool::maxPool, T> + Place, paddle::operators::math::pool::maxPoolGrad, T> pool2d_backward; - paddle::operators::math::pool::maxPool pool_process; + paddle::operators::math::pool::maxPoolGrad pool_process; pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool2dBackwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPoolGrad, T> pool2d_backward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPoolGrad pool_process; pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); } @@ -129,16 +129,16 @@ class PoolGradKernel : public framework::OpKernel { case 3: { if (pooling_type == "max") { paddle::operators::math::Pool3dBackwardFunctor< - Place, paddle::operators::math::pool::maxPool, T> + Place, paddle::operators::math::pool::maxPoolGrad, T> pool3d_backward; - paddle::operators::math::pool::maxPool pool_process; + paddle::operators::math::pool::maxPoolGrad pool_process; pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool3dBackwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPoolGrad, T> pool3d_backward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPoolGrad pool_process; pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); } diff --git a/python/paddle/v2/framework/tests/test_pool2d_op.py b/python/paddle/v2/framework/tests/test_pool2d_op.py index 0efddc28814f05b47b9402dc483bf9498c907276..e8e5218073e002f257bbf7c2eb9efeeeba9bac1d 100644 --- a/python/paddle/v2/framework/tests/test_pool2d_op.py +++ b/python/paddle/v2/framework/tests/test_pool2d_op.py @@ -23,7 +23,7 @@ def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): return out -def ave_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): +def avg_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, H, W = x.shape if global_pool == 1: @@ -72,8 +72,8 @@ class TestPool2d_Op(OpTest): def initTestCase(self): self.global_pool = 0 - self.pool_type = "ave" - self.pool2D_forward_naive = ave_pool2D_forward_naive + self.pool_type = "avg" + self.pool2D_forward_naive = avg_pool2D_forward_naive self.shape = [2, 3, 5, 5] self.ksize = [3, 3] self.strides = [1, 1] @@ -84,8 +84,8 @@ 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.pool_type = "avg" + self.pool2D_forward_naive = avg_pool2D_forward_naive self.shape = [2, 3, 5, 5] self.ksize = [3, 3] self.strides = [1, 1] @@ -96,8 +96,8 @@ 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 + self.pool_type = "avg" + self.pool2D_forward_naive = avg_pool2D_forward_naive self.shape = [2, 3, 5, 5] self.ksize = [3, 3] self.strides = [1, 1] diff --git a/python/paddle/v2/framework/tests/test_pool3d_op.py b/python/paddle/v2/framework/tests/test_pool3d_op.py index 4ba3d754d53966c769e4553e1b9bb011cd3cf8b0..2a3de89a70978bea14a4b7f9d926ed2cf7d4f0ba 100644 --- a/python/paddle/v2/framework/tests/test_pool3d_op.py +++ b/python/paddle/v2/framework/tests/test_pool3d_op.py @@ -27,7 +27,7 @@ def max_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): return out -def ave_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): +def avg_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, D, H, W = x.shape if global_pool == 1: @@ -80,8 +80,8 @@ class TestPool3d_Op(OpTest): def initTestCase(self): self.global_pool = 0 - self.pool_type = "ave" - self.pool3D_forward_naive = ave_pool3D_forward_naive + self.pool_type = "avg" + self.pool3D_forward_naive = avg_pool3D_forward_naive self.shape = [2, 3, 5, 5, 5] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] @@ -92,8 +92,8 @@ 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 + self.pool_type = "avg" + self.pool3D_forward_naive = avg_pool3D_forward_naive self.shape = [2, 3, 7, 7, 7] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] @@ -104,8 +104,8 @@ 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.pool_type = "avg" + self.pool3D_forward_naive = avg_pool3D_forward_naive self.shape = [2, 3, 7, 7, 7] self.ksize = [3, 3, 3] self.strides = [1, 1, 1]