|
|
|
@ -32,24 +32,22 @@ class Unpool2dOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
"The format of input tensor is NCHW. Where N is batch size, C is the "
|
|
|
|
|
"number of channels, H and W is the height and width of feature.");
|
|
|
|
|
AddOutput("Out",
|
|
|
|
|
"(Tensor) The output tensor of unpool operator."
|
|
|
|
|
"The format of output tensor is also NCHW."
|
|
|
|
|
"Where N is batch size, C is "
|
|
|
|
|
"the number of channels, H and W is the height and "
|
|
|
|
|
"width of feature.");
|
|
|
|
|
"(Tensor) The output tensor of unpool operator."
|
|
|
|
|
"The format of output tensor is also NCHW."
|
|
|
|
|
"Where N is batch size, C is "
|
|
|
|
|
"the number of channels, H and W is the height and "
|
|
|
|
|
"width of feature.");
|
|
|
|
|
AddAttr<std::vector<int>>(
|
|
|
|
|
"ksize",
|
|
|
|
|
"(vector), the unpooling window size(height, width) "
|
|
|
|
|
"of unpooling operator.");
|
|
|
|
|
AddAttr<std::vector<int>>(
|
|
|
|
|
"strides",
|
|
|
|
|
"(vector, default:{1, 1}), "
|
|
|
|
|
"strides (height, width) of unpooling operator.")
|
|
|
|
|
AddAttr<std::vector<int>>("strides",
|
|
|
|
|
"(vector, default:{1, 1}), "
|
|
|
|
|
"strides (height, width) of unpooling operator.")
|
|
|
|
|
.SetDefault({1, 1});
|
|
|
|
|
AddAttr<std::vector<int>>(
|
|
|
|
|
"paddings",
|
|
|
|
|
"(vector defalut:{0,0}), "
|
|
|
|
|
"paddings (height, width) of unpooling operator.")
|
|
|
|
|
AddAttr<std::vector<int>>("paddings",
|
|
|
|
|
"(vector defalut:{0,0}), "
|
|
|
|
|
"paddings (height, width) of unpooling operator.")
|
|
|
|
|
.SetDefault({0, 0});
|
|
|
|
|
AddAttr<std::string>(
|
|
|
|
|
"unpooling_type",
|
|
|
|
@ -75,71 +73,71 @@ int OutputSize(int input_size, int ksize, int padding, int stride) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class UnpoolOp : public framework::OperatorWithKernel {
|
|
|
|
|
protected:
|
|
|
|
|
framework::OpKernelType GetKernelType(
|
|
|
|
|
const framework::ExecutionContext& ctx) const override {
|
|
|
|
|
protected:
|
|
|
|
|
framework::OpKernelType GetKernelType(
|
|
|
|
|
const framework::ExecutionContext& ctx) const override {
|
|
|
|
|
return framework::OpKernelType(
|
|
|
|
|
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
|
|
|
|
|
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
|
|
|
|
|
ctx.device_context());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp"
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp"
|
|
|
|
|
"should not be null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Indices"), "Input(Indices) of UnpoolOp"
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Indices"), "Input(Indices) of UnpoolOp"
|
|
|
|
|
"should not be null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
|
|
|
|
"Output(Out) of UnpoolOp should not be null.");
|
|
|
|
|
auto in_x_dims = ctx->GetInputDim("X");
|
|
|
|
|
auto in_y_dims = ctx->GetInputDim("Indices");
|
|
|
|
|
std::string unpooling_type =
|
|
|
|
|
auto in_x_dims = ctx->GetInputDim("X");
|
|
|
|
|
auto in_y_dims = ctx->GetInputDim("Indices");
|
|
|
|
|
std::string unpooling_type =
|
|
|
|
|
ctx->Attrs().Get<std::string>("unpooling_type");
|
|
|
|
|
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
|
|
|
|
|
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
|
|
|
|
|
std::vector<int> paddings =
|
|
|
|
|
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
|
|
|
|
|
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
|
|
|
|
|
std::vector<int> paddings =
|
|
|
|
|
ctx->Attrs().Get<std::vector<int>>("paddings");
|
|
|
|
|
PADDLE_ENFORCE(in_x_dims.size() == 4,
|
|
|
|
|
PADDLE_ENFORCE(in_x_dims.size() == 4,
|
|
|
|
|
"Unpooling intput must be of 4-dimensional.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims);
|
|
|
|
|
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
|
|
|
|
|
for (size_t i = 0; i < ksize.size(); ++i) {
|
|
|
|
|
output_shape.push_back(
|
|
|
|
|
OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
|
|
|
|
|
}
|
|
|
|
|
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
|
|
|
|
|
}
|
|
|
|
|
PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims);
|
|
|
|
|
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
|
|
|
|
|
for (size_t i = 0; i < ksize.size(); ++i) {
|
|
|
|
|
output_shape.push_back(
|
|
|
|
|
OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
|
|
|
|
|
}
|
|
|
|
|
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class UnpoolOpGrad : public framework::OperatorWithKernel {
|
|
|
|
|
protected:
|
|
|
|
|
framework::OpKernelType GetKernelType(
|
|
|
|
|
const framework::ExecutionContext& ctx) const override {
|
|
|
|
|
return framework::OpKernelType(
|
|
|
|
|
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
|
|
|
|
|
ctx.device_context());
|
|
|
|
|
}
|
|
|
|
|
protected:
|
|
|
|
|
framework::OpKernelType GetKernelType(
|
|
|
|
|
const framework::ExecutionContext& ctx) const override {
|
|
|
|
|
return framework::OpKernelType(
|
|
|
|
|
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
|
|
|
|
|
ctx.device_context());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
|
|
|
|
|
"Input(X@GRAD) should not be null.");
|
|
|
|
|
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
|
|
|
|
|
}
|
|
|
|
|
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} // namespace operators
|
|
|
|
|
} // namespace paddle
|
|
|
|
|
} // namespace operators
|
|
|
|
|
} // namespace paddle
|
|
|
|
|
|
|
|
|
|
namespace ops = paddle::operators;
|
|
|
|
|
REGISTER_OP(unpool, ops::UnpoolOp, ops::Unpool2dOpMaker, unpool_grad,
|
|
|
|
|
ops::UnpoolOpGrad);
|
|
|
|
|
REGISTER_OP_CPU_KERNEL(
|
|
|
|
|
unpool, ops::UnpoolKernel<paddle::platform::CPUPlace, float>,
|
|
|
|
|
ops::UnpoolKernel<paddle::platform::CPUPlace, double>);
|
|
|
|
|
unpool, ops::UnpoolKernel<paddle::platform::CPUPlace, float>,
|
|
|
|
|
ops::UnpoolKernel<paddle::platform::CPUPlace, double>);
|
|
|
|
|
REGISTER_OP_CPU_KERNEL(
|
|
|
|
|
unpool_grad, ops::UnpoolGradKernel<paddle::platform::CPUPlace, float>,
|
|
|
|
|
ops::UnpoolGradKernel<paddle::platform::CPUPlace, double>);
|
|
|
|
|
unpool_grad, ops::UnpoolGradKernel<paddle::platform::CPUPlace, float>,
|
|
|
|
|
ops::UnpoolGradKernel<paddle::platform::CPUPlace, double>);
|
|
|
|
|
|
|
|
|
|