unpool_op.cc 5.8 KB
Newer Older
S
sweetsky0901 已提交
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2 3 4 5 6 7 8 9 10 11 12 13

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Indicesou may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
S
sweetsky0901 已提交
14 15 16 17 18 19 20

#include "paddle/operators/unpool_op.h"
namespace paddle {
namespace operators {

class Unpool2dOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
21
  Unpool2dOpMaker(OpProto* proto, OpAttrChecker* op_checker)
S
sweetsky0901 已提交
22
      : OpProtoAndCheckerMaker(proto, op_checker) {
S
sweetsky0901 已提交
23 24
    AddInput(
        "X",
S
sweetsky0901 已提交
25 26 27
        "(Tensor) The input tensor of unpool operator. "
        "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.");
S
sweetsky0901 已提交
28 29
    AddInput(
        "Indices",
S
sweetsky0901 已提交
30 31 32
        "(Tensor) The input tensor of the indices given out by MaxPool2d. "
        "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.");
S
sweetsky0901 已提交
33
    AddOutput("Out",
S
sweetsky0901 已提交
34 35 36 37 38
              "(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.");
S
sweetsky0901 已提交
39 40
    AddAttr<std::vector<int>>(
        "ksize",
S
sweetsky0901 已提交
41
        "(vector), the unpooling window size(height, width) "
S
sweetsky0901 已提交
42
        "of unpooling operator.");
S
sweetsky0901 已提交
43 44 45
    AddAttr<std::vector<int>>("strides",
                              "(vector, default:{1, 1}), "
                              "strides (height, width) of unpooling operator.")
S
sweetsky0901 已提交
46
        .SetDefault({1, 1});
S
sweetsky0901 已提交
47 48 49
    AddAttr<std::vector<int>>("paddings",
                              "(vector defalut:{0,0}), "
                              "paddings (height, width) of unpooling operator.")
S
sweetsky0901 已提交
50
        .SetDefault({0, 0});
S
sweetsky0901 已提交
51 52
    AddAttr<std::string>(
        "unpooling_type",
S
sweetsky0901 已提交
53 54
        "(string), unpooling type, can be \"max\" for max-unpooling ")
        .InEnum({"max"});
S
sweetsky0901 已提交
55
    AddComment(R"DOC(
S
sweetsky0901 已提交
56 57 58
        "Input shape: $(N, C_{in}, H_{in}, W_{in})$
        Output shape: $(N, C_{out}, H_{out}, W_{out})$
        Where
S
sweetsky0901 已提交
59 60 61 62
          $$
            H_{out} = (H_{in}−1) * strides[0] − 2 * paddings[0] + ksize[0] \\
            W_{out} = (W_{in}−1) * strides[1] − 2 * paddings[1] + ksize[1]
          $$
S
sweetsky0901 已提交
63 64
        Paper: http://www.matthewzeiler.com/wp-content/uploads/2017
        /07/iccv2011.pdf
S
sweetsky0901 已提交
65 66 67 68 69
        )DOC");
  }
};

int OutputSize(int input_size, int ksize, int padding, int stride) {
S
sweetsky0901 已提交
70
  int output_size = (input_size - 1) * stride - 2 * padding + ksize;
S
sweetsky0901 已提交
71 72 73 74
  return output_size;
}

class UnpoolOp : public framework::OperatorWithKernel {
S
sweetsky0901 已提交
75 76
 protected:
  framework::OpKernelType GetKernelType(
S
sweetsky0901 已提交
77 78 79
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
S
sweetsky0901 已提交
80
        ctx.device_context());
S
sweetsky0901 已提交
81
  }
S
sweetsky0901 已提交
82

S
sweetsky0901 已提交
83 84 85
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
S
sweetsky0901 已提交
86 87 88 89 90
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of UnpoolOp"
                   "should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Indices"),
                   "Input(Indices) of UnpoolOp"
S
sweetsky0901 已提交
91
                   "should not be null.");
S
sweetsky0901 已提交
92
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
S
sweetsky0901 已提交
93
                   "Output(Out) of UnpoolOp should not be null.");
S
sweetsky0901 已提交
94 95
    auto in_x_dims = ctx->GetInputDim("X");
    auto in_y_dims = ctx->GetInputDim("Indices");
S
sweetsky0901 已提交
96 97
    std::string unpooling_type =
        ctx->Attrs().Get<std::string>("unpooling_type");
S
sweetsky0901 已提交
98 99
    std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
    std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
S
sweetsky0901 已提交
100
    std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
S
sweetsky0901 已提交
101
    PADDLE_ENFORCE(in_x_dims.size() == 4,
S
sweetsky0901 已提交
102
                   "Unpooling intput must be of 4-dimensional.");
S
sweetsky0901 已提交
103 104 105 106
    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(
S
sweetsky0901 已提交
107
          OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
S
sweetsky0901 已提交
108 109 110
    }
    ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
  }
S
sweetsky0901 已提交
111 112 113
};

class UnpoolOpGrad : public framework::OperatorWithKernel {
S
sweetsky0901 已提交
114 115
 protected:
  framework::OpKernelType GetKernelType(
S
sweetsky0901 已提交
116 117
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
S
sweetsky0901 已提交
118 119
        framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
        ctx.device_context());
S
sweetsky0901 已提交
120
  }
S
sweetsky0901 已提交
121

S
sweetsky0901 已提交
122 123 124 125 126
 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")),
S
sweetsky0901 已提交
127
                   "Input(X@GRAD) should not be null.");
S
sweetsky0901 已提交
128 129
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }
S
sweetsky0901 已提交
130
};
S
sweetsky0901 已提交
131 132
}  // namespace operators
}  // namespace paddle
S
sweetsky0901 已提交
133 134

namespace ops = paddle::operators;
S
sweetsky0901 已提交
135
REGISTER_OP(unpool, ops::UnpoolOp, ops::Unpool2dOpMaker, unpool_grad,
S
sweetsky0901 已提交
136
            ops::UnpoolOpGrad);
S
sweetsky0901 已提交
137
REGISTER_OP_CPU_KERNEL(
Q
QI JUN 已提交
138 139 140 141 142 143
    unpool, ops::UnpoolKernel<paddle::platform::CPUDeviceContext, float>,
    ops::UnpoolKernel<paddle::platform::CPUDeviceContext, double>);
REGISTER_OP_CPU_KERNEL(
    unpool_grad,
    ops::UnpoolGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::UnpoolGradKernel<paddle::platform::CPUDeviceContext, double>);