Skip to content

Commit c0e34ee

Browse files
committed
add roi align
1 parent 5fc3052 commit c0e34ee

File tree

3 files changed

+664
-0
lines changed

3 files changed

+664
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software
7+
distributed under the License is distributed on an "AS IS" BASIS,
8+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
See the License for the specific language governing permissions and
10+
limitations under the License. */
11+
12+
#include "paddle/fluid/operators/roi_align_op.h"
13+
14+
namespace paddle {
15+
namespace operators {
16+
17+
using Tensor = framework::Tensor;
18+
using LoDTensor = framework::LoDTensor;
19+
20+
class ROIAlignOp : public framework::OperatorWithKernel {
21+
public:
22+
using framework::OperatorWithKernel::OperatorWithKernel;
23+
24+
void InferShape(framework::InferShapeContext* ctx) const override {
25+
PADDLE_ENFORCE(ctx->HasInput("X"),
26+
"Input(X) of ROIAlignOp should not be null.");
27+
PADDLE_ENFORCE(ctx->HasInput("ROIs"),
28+
"Input(ROIs) of ROIAlignOp should not be null.");
29+
PADDLE_ENFORCE(ctx->HasOutput("Out"),
30+
"Output(Out) of ROIAlignOp should not be null.");
31+
auto input_dims = ctx->GetInputDim("X");
32+
auto rois_dims = ctx->GetInputDim("ROIs");
33+
34+
PADDLE_ENFORCE(input_dims.size() == 4,
35+
"The format of input tensor is NCHW.");
36+
PADDLE_ENFORCE(rois_dims.size() == 2,
37+
"ROIs should be a 2-D LoDTensor of shape (num_rois, 4)"
38+
"given as [[x1, y1, x2, y2], …].");
39+
PADDLE_ENFORCE(rois_dims[1] == 4,
40+
"ROIs should be a 2-D LoDTensor of shape (num_rois, 4)"
41+
"given as [[x1, y1, x2, y2], …].");
42+
int pooled_height = ctx->Attrs().Get<int>("pooled_height");
43+
int pooled_width = ctx->Attrs().Get<int>("pooled_width");
44+
float spatial_scale = ctx->Attrs().Get<float>("spatial_scale");
45+
46+
PADDLE_ENFORCE_GT(pooled_height, 0,
47+
"The pooled output height must greater than 0");
48+
PADDLE_ENFORCE_GT(pooled_width, 0,
49+
"The pooled output width must greater than 0");
50+
PADDLE_ENFORCE_GT(spatial_scale, 0.0f,
51+
"The spatial scale must greater than 0");
52+
53+
auto out_dims = input_dims;
54+
out_dims[0] = rois_dims[0];
55+
out_dims[1] = input_dims[1];
56+
out_dims[2] = pooled_height;
57+
out_dims[3] = pooled_width;
58+
59+
ctx->SetOutputDim("Out", out_dims);
60+
}
61+
62+
protected:
63+
framework::OpKernelType GetExpectedKernelType(
64+
const framework::ExecutionContext& ctx) const override {
65+
return framework::OpKernelType(
66+
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
67+
ctx.device_context());
68+
}
69+
};
70+
71+
class ROIAlignGradOp : public framework::OperatorWithKernel {
72+
public:
73+
using framework::OperatorWithKernel::OperatorWithKernel;
74+
75+
void InferShape(framework::InferShapeContext* ctx) const override {
76+
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
77+
"The GRAD@Out of ROIAlignGradOp should not be null.");
78+
PADDLE_ENFORCE(ctx->HasOutputs(framework::GradVarName("X")),
79+
"The GRAD@X of ROIAlignGradOp should not be null.");
80+
ctx->SetOutputsDim(framework::GradVarName("X"), ctx->GetInputsDim("X"));
81+
}
82+
83+
protected:
84+
framework::OpKernelType GetExpectedKernelType(
85+
const framework::ExecutionContext& ctx) const override {
86+
return framework::OpKernelType(
87+
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
88+
ctx.device_context());
89+
}
90+
};
91+
92+
class ROIAlignOpMaker : public framework::OpProtoAndCheckerMaker {
93+
public:
94+
void Make() override {
95+
AddInput("X",
96+
"(Tensor), "
97+
"the input of ROIAlignOp. "
98+
"The format of input tensor is NCHW. Where N is batch size, "
99+
"C is the number of input channels, "
100+
"H is the height of the feature, and "
101+
"W is the width of the feature.");
102+
AddInput("ROIs",
103+
"(LoDTensor), "
104+
"ROIs (Regions of Interest) to pool over. "
105+
"should be a 2-D LoDTensor of shape (num_rois, 4)"
106+
"given as [[x1, y1, x2, y2], …]. "
107+
"Where batch_id is the id of the data, "
108+
"(x1, y1) is the top left coordinates, and "
109+
"(x2, y2) is the bottom right coordinates.");
110+
AddOutput("Out",
111+
"(Tensor), "
112+
"The output of ROIAlignOp is a 4-D tensor with shape "
113+
"(num_rois, channels, pooled_h, pooled_w).");
114+
AddAttr<float>("spatial_scale",
115+
"(float, default 1.0), "
116+
"Multiplicative spatial scale factor "
117+
"to translate ROI coords from their input scale "
118+
"to the scale used when pooling.")
119+
.SetDefault(1.0);
120+
AddAttr<int>("pooled_height",
121+
"(int, default 1), "
122+
"The pooled output height.")
123+
.SetDefault(1);
124+
AddAttr<int>("pooled_width",
125+
"(int, default 1), "
126+
"The pooled output width.")
127+
.SetDefault(1);
128+
AddAttr<int>("sampling_ratio",
129+
"(int,default -1),"
130+
"number of sampling points in the interpolation grid"
131+
"If <=0, then grid points are adaptive to roi_width "
132+
"and pooled_w, likewise for height")
133+
.SetDefault(-1);
134+
AddComment(R"DOC(
135+
)DOC");
136+
}
137+
};
138+
139+
} // namespace operators
140+
} // namespace paddle
141+
142+
namespace ops = paddle::operators;
143+
REGISTER_OPERATOR(roi_align, ops::ROIAlignOp, ops::ROIAlignOpMaker,
144+
paddle::framework::DefaultGradOpDescMaker<true>);
145+
REGISTER_OPERATOR(roi_align_grad, ops::ROIAlignGradOp);
146+
REGISTER_OP_CPU_KERNEL(
147+
roi_align,
148+
ops::CPUROIAlignOpKernel<paddle::platform::CPUDeviceContext, float>,
149+
ops::CPUROIAlignOpKernel<paddle::platform::CPUDeviceContext, double>);
150+
REGISTER_OP_CPU_KERNEL(
151+
roi_align_grad,
152+
ops::CPUROIAlignGradOpKernel<paddle::platform::CPUDeviceContext, float>,
153+
ops::CPUROIAlignGradOpKernel<paddle::platform::CPUDeviceContext, double>);

0 commit comments

Comments
 (0)