提交 4b06d8db 编写于 作者: C chengduoZH

fix globalPooling type (int => bool)

上级 30a586df
...@@ -35,7 +35,7 @@ class PoolOp : public framework::OperatorWithKernel { ...@@ -35,7 +35,7 @@ class PoolOp : public framework::OperatorWithKernel {
auto in_x = ctx.Input<Tensor>("X"); auto in_x = ctx.Input<Tensor>("X");
auto out = ctx.Output<Tensor>("Out"); auto out = ctx.Output<Tensor>("Out");
int global_pooling = Attr<int>("globalPooling"); bool global_pooling = Attr<bool>("globalPooling");
std::string pooling_type = Attr<std::string>("poolingType"); std::string pooling_type = Attr<std::string>("poolingType");
std::vector<int> ksize = Attr<std::vector<int>>("ksize"); std::vector<int> ksize = Attr<std::vector<int>>("ksize");
std::vector<int> strides = Attr<std::vector<int>>("strides"); std::vector<int> strides = Attr<std::vector<int>>("strides");
...@@ -45,6 +45,15 @@ class PoolOp : public framework::OperatorWithKernel { ...@@ -45,6 +45,15 @@ class PoolOp : public framework::OperatorWithKernel {
"pooling_type should be 'max' or 'avg'"); "pooling_type should be 'max' or 'avg'");
PADDLE_ENFORCE(in_x->dims().size() == 4 || in_x->dims().size() == 5, PADDLE_ENFORCE(in_x->dims().size() == 4 || in_x->dims().size() == 5,
"Pooling intput should be 4-D or 5-D"); "Pooling intput should be 4-D or 5-D");
if (global_pooling) {
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]);
}
PADDLE_ENFORCE(in_x->dims().size() == static_cast<size_t>(ksize.size() + 2),
"Input size and Pooling size should be consistent.");
PADDLE_ENFORCE(ksize.size() == 2 || ksize.size() == 3, PADDLE_ENFORCE(ksize.size() == 2 || ksize.size() == 3,
"Pooling size should be 2 elements. or 3 elements."); "Pooling size should be 2 elements. or 3 elements.");
PADDLE_ENFORCE_EQ(ksize.size(), strides.size(), PADDLE_ENFORCE_EQ(ksize.size(), strides.size(),
...@@ -52,12 +61,6 @@ class PoolOp : public framework::OperatorWithKernel { ...@@ -52,12 +61,6 @@ class PoolOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ(ksize.size(), paddings.size(), PADDLE_ENFORCE_EQ(ksize.size(), paddings.size(),
"paddings size and pooling size should be the same."); "paddings size and pooling size should be the same.");
if (global_pooling == 1) {
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]);
}
std::vector<int64_t> output_shape({in_x->dims()[0], in_x->dims()[1]}); std::vector<int64_t> output_shape({in_x->dims()[0], in_x->dims()[1]});
for (size_t i = 0; i < ksize.size(); ++i) { for (size_t i = 0; i < ksize.size(); ++i) {
output_shape.push_back(OutputSizePool(in_x->dims()[i + 2], ksize[i], output_shape.push_back(OutputSizePool(in_x->dims()[i + 2], ksize[i],
...@@ -103,15 +106,16 @@ class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -103,15 +106,16 @@ class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker {
"poolingType of pooling operator." "poolingType of pooling operator."
"str constant equal to 'max' or 'avg'"); "str constant equal to 'max' or 'avg'");
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>(
"ksize", "pooling size(height, width) of pooling operator.") "ksize",
.AddCustomChecker(GreaterThanChecker_pool({0, 0})); "Pooling size(depth, height, width) of pooling operator."
AddAttr<int>( "If globalPooling = true, ksize is ignored and need not be specified.");
AddAttr<bool>(
"globalPooling", "globalPooling",
"whether to use the globalPooling." "whether to use the globalPooling."
"int constant equal to 0 or 1" "int constant equal to false or true"
"default 0" "default false"
"If globalPooling = 1, ksize is ignored and need not be specified.") "If globalPooling = true, ksize is ignored and need not be specified.")
.SetDefault(0); .SetDefault(false);
AddAttr<std::vector<int>>("strides", AddAttr<std::vector<int>>("strides",
"strides(height, width) of pooling operator." "strides(height, width) of pooling operator."
"default {1,1}") "default {1,1}")
...@@ -177,15 +181,16 @@ class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -177,15 +181,16 @@ class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker {
"poolingType of pooling operator." "poolingType of pooling operator."
"str constant equal to 'max' or 'avg'"); "str constant equal to 'max' or 'avg'");
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>(
"ksize", "pooling size(depth, height, width) of pooling operator.") "ksize",
.AddCustomChecker(GreaterThanChecker_pool({0, 0, 0})); "pooling size(depth, height, width) of pooling operator."
AddAttr<int>( "If globalPooling = true, ksize is ignored and need not be specified.");
AddAttr<bool>(
"globalPooling", "globalPooling",
"whether to use the globalPooling." "whether to use the globalPooling."
"int constant equal to 0 or 1" "int constant equal to false or true"
"default 0" "default false"
"If globalPooling = 1, ksize is ignored and need not be specified.") "If globalPooling = true, ksize is ignored and need not be specified.")
.SetDefault(0); .SetDefault(false);
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>(
"strides", "strides",
"strides(depth, height, width) of pooling operator." "strides(depth, height, width) of pooling operator."
......
...@@ -31,12 +31,12 @@ class PoolKernel : public framework::OpKernel { ...@@ -31,12 +31,12 @@ class PoolKernel : public framework::OpKernel {
const Tensor* in_x = context.Input<Tensor>("X"); const Tensor* in_x = context.Input<Tensor>("X");
Tensor* out = context.Output<Tensor>("Out"); Tensor* out = context.Output<Tensor>("Out");
int global_pooling = context.Attr<int>("globalPooling"); bool global_pooling = context.Attr<bool>("globalPooling");
std::string pooling_type = context.Attr<std::string>("poolingType"); std::string pooling_type = context.Attr<std::string>("poolingType");
std::vector<int> ksize = context.Attr<std::vector<int>>("ksize"); std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
std::vector<int> strides = context.Attr<std::vector<int>>("strides"); std::vector<int> strides = context.Attr<std::vector<int>>("strides");
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings"); std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
if (global_pooling == 1) { if (global_pooling) {
for (size_t i = 0; i < ksize.size(); ++i) { for (size_t i = 0; i < ksize.size(); ++i) {
ksize[i] = static_cast<int>(in_x->dims()[i + 2]); ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
} }
...@@ -92,13 +92,13 @@ class PoolGradKernel : public framework::OpKernel { ...@@ -92,13 +92,13 @@ class PoolGradKernel : public framework::OpKernel {
context.Input<Tensor>(framework::GradVarName("Out")); context.Input<Tensor>(framework::GradVarName("Out"));
Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X")); Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X"));
int global_pooling = context.Attr<int>("globalPooling"); bool global_pooling = context.Attr<bool>("globalPooling");
std::string pooling_type = context.Attr<std::string>("poolingType"); std::string pooling_type = context.Attr<std::string>("poolingType");
std::vector<int> ksize = context.Attr<std::vector<int>>("ksize"); std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
std::vector<int> strides = context.Attr<std::vector<int>>("strides"); std::vector<int> strides = context.Attr<std::vector<int>>("strides");
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings"); std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
if (global_pooling == 1) { if (global_pooling) {
for (size_t i = 0; i < ksize.size(); ++i) for (size_t i = 0; i < ksize.size(); ++i)
ksize[i] = static_cast<int>(in_x->dims()[i + 2]); ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册