roi_pool_op.cc 6.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaox 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You 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
sneaxiy 已提交
15
#include <memory>
16

17
#include "paddle/fluid/framework/infershape_utils.h"
18
#include "paddle/fluid/framework/op_registry.h"
19
#include "paddle/fluid/framework/op_version_registry.h"
20 21
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/ternary.h"
W
wanghaox 已提交
22 23 24 25

namespace paddle {
namespace operators {

W
wanghaox 已提交
26
using Tensor = framework::Tensor;
27
using LoDTensor = framework::LoDTensor;
W
wanghaox 已提交
28

W
wanghaox 已提交
29
class ROIPoolOp : public framework::OperatorWithKernel {
W
wanghaox 已提交
30 31 32 33
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
34
  framework::OpKernelType GetExpectedKernelType(
W
wanghaox 已提交
35
      const framework::ExecutionContext& ctx) const override {
36 37 38
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
W
wanghaox 已提交
39 40 41
  }
};

W
wanghaox 已提交
42
class ROIPoolGradOp : public framework::OperatorWithKernel {
W
wanghaox 已提交
43 44 45 46
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
47 48 49 50 51 52 53 54
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")),
                   "Input",
                   framework::GradVarName("Out"),
                   "roi_pool");
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("X")),
                   "Output",
                   framework::GradVarName("X"),
                   "roi_pool");
W
wanghaox 已提交
55 56 57 58
    ctx->SetOutputsDim(framework::GradVarName("X"), ctx->GetInputsDim("X"));
  }

 protected:
59
  framework::OpKernelType GetExpectedKernelType(
W
wanghaox 已提交
60
      const framework::ExecutionContext& ctx) const override {
61 62 63
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
W
wanghaox 已提交
64 65 66
  }
};

W
wanghaox 已提交
67
class ROIPoolOpMaker : public framework::OpProtoAndCheckerMaker {
W
wanghaox 已提交
68
 public:
Y
Yu Yang 已提交
69
  void Make() override {
W
wanghaox 已提交
70 71
    AddInput("X",
             "(Tensor), "
W
wanghaox 已提交
72 73 74 75 76 77
             "the input of ROIPoolOp. "
             "The format of input tensor is NCHW. Where N is batch size, "
             "C is the number of input channels, "
             "H is the height of the feature, and "
             "W is the width of the feature.");
    AddInput("ROIs",
78
             "(LoDTensor), "
W
wanghaox 已提交
79
             "ROIs (Regions of Interest) to pool over. "
80
             "should be a 2-D LoDTensor of shape (num_rois, 4)"
W
wopeizl 已提交
81
             "given as [[x1, y1, x2, y2], ...]. "
W
wanghaox 已提交
82 83 84
             "Where batch_id is the id of the data, "
             "(x1, y1) is the top left coordinates, and "
             "(x2, y2) is the bottom right coordinates.");
85 86
    AddInput("RoisNum", "(Tensor), The number of RoIs in each image.")
        .AsDispensable();
W
wanghaox 已提交
87 88
    AddOutput("Out",
              "(Tensor), "
W
wanghaox 已提交
89 90
              "The output of ROIPoolOp is a 4-D tensor with shape "
              "(num_rois, channels, pooled_h, pooled_w).");
W
wanghaox 已提交
91 92 93 94
    AddOutput("Argmax",
              "(Tensor), "
              "Argmaxes corresponding to indices in X used "
              "for gradient computation. Only output "
P
peizhilin 已提交
95
              "if arg \"is_test\" is false.")
96
        .AsIntermediate();
W
wanghaox 已提交
97
    AddAttr<float>("spatial_scale",
W
wanghaox 已提交
98 99 100 101
                   "(float, default 1.0), "
                   "Multiplicative spatial scale factor "
                   "to translate ROI coords from their input scale "
                   "to the scale used when pooling.")
102
        .SetDefault(1.0);
W
wanghaox 已提交
103
    AddAttr<int>("pooled_height",
W
wanghaox 已提交
104 105
                 "(int, default 1), "
                 "The pooled output height.")
106
        .SetDefault(1);
W
wanghaox 已提交
107
    AddAttr<int>("pooled_width",
W
wanghaox 已提交
108 109
                 "(int, default 1), "
                 "The pooled output width.")
110
        .SetDefault(1);
W
wanghaox 已提交
111
    AddComment(R"DOC(
Y
yi.wu 已提交
112
**ROIPool Operator**
W
wanghaox 已提交
113

Y
yi.wu 已提交
114 115 116 117 118
Region of interest pooling (also known as RoI pooling) is to perform
is to perform max pooling on inputs of nonuniform sizes to obtain
fixed-size feature maps (e.g. 7*7).

The operator has three steps:
Y
yi.wu 已提交
119

Y
yi.wu 已提交
120 121
1. Dividing each region proposal into equal-sized sections with
   the pooled_width and pooled_height
Y
update  
yi.wu 已提交
122

Y
yi.wu 已提交
123
2. Finding the largest value in each section
Y
update  
yi.wu 已提交
124

Y
yi.wu 已提交
125 126
3. Copying these max values to the output buffer

127
ROI Pooling for Faster-RCNN. The link below is a further introduction:
W
wanghaox 已提交
128 129 130 131 132
https://stackoverflow.com/questions/43430056/what-is-roi-layer-in-fast-rcnn
    )DOC");
  }
};

H
hong 已提交
133 134
template <typename T>
class ROIPoolGradMaker : public framework::SingleGradOpMaker<T> {
S
sneaxiy 已提交
135
 public:
H
hong 已提交
136
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
S
sneaxiy 已提交
137 138

 protected:
139
  void Apply(GradOpPtr<T> op) const override {
S
sneaxiy 已提交
140
    op->SetType("roi_pool_grad");
H
hong 已提交
141 142
    op->SetInput("X", this->Input("X"));
    op->SetInput("ROIs", this->Input("ROIs"));
143
    op->SetInput("RoisNum", this->Input("RoisNum"));
H
hong 已提交
144 145 146 147
    op->SetInput("Argmax", this->Output("Argmax"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
S
sneaxiy 已提交
148 149 150
  }
};

W
wanghaox 已提交
151 152 153 154
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
155 156
DECLARE_INFER_SHAPE_FUNCTOR(roi_pool,
                            RoiPoolInferShapeFunctor,
157 158
                            PD_INFER_META(phi::RoiPoolInferMeta));

159 160 161
REGISTER_OPERATOR(roi_pool,
                  ops::ROIPoolOp,
                  ops::ROIPoolOpMaker,
H
hong 已提交
162
                  ops::ROIPoolGradMaker<paddle::framework::OpDesc>,
163 164
                  ops::ROIPoolGradMaker<paddle::imperative::OpBase>,
                  RoiPoolInferShapeFunctor);
165
REGISTER_OPERATOR(roi_pool_grad, ops::ROIPoolGradOp);
166

167
REGISTER_OP_VERSION(roi_pool)
168 169 170 171 172 173 174
    .AddCheckpoint(
        R"ROC(
              Incompatible upgrade of input [RpnRoisLod])ROC",
        paddle::framework::compatible::OpVersionDesc().DeleteInput(
            "RpnRoisLod",
            "Delete RpnRoisLod due to incorrect input name and "
            "it is not used in object detection models yet."))
175 176 177 178 179 180
    .AddCheckpoint(
        R"ROC(
              Upgrade roi_pool add a new input [RoisNum])ROC",
        paddle::framework::compatible::OpVersionDesc().NewInput(
            "RoisNum",
            "The number of RoIs in each image. RoisNum is dispensable."));