Skip to content

Commit ef90559

Browse files
wanghaoxwanghaox
authored andcommitted
fix some code issues
2 parents 36dd770 + 6ab78ae commit ef90559

File tree

5 files changed

+170
-183
lines changed

5 files changed

+170
-183
lines changed

paddle/capi/examples/model_inference/dense/main.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <paddle/capi.h>
22
#include <time.h>
3+
34
#include "../common/common.h"
45

56
#define CONFIG_BIN "./trainer_config.bin"
@@ -27,20 +28,19 @@ int main() {
2728
CHECK(paddle_arguments_resize(in_args, 1));
2829

2930
// Create input matrix.
30-
paddle_matrix mat = paddle_matrix_create(/* sample_num */ 10,
31+
paddle_matrix mat = paddle_matrix_create(/* sample_num */ 1,
3132
/* size */ 784,
3233
/* useGPU */ false);
3334
srand(time(0));
3435

35-
std::vector<paddle_real> input;
36-
input.resize(784 * 10);
36+
paddle_real* array;
37+
38+
// Get First row.
39+
CHECK(paddle_matrix_get_row(mat, 0, &array));
3740

38-
for (int i = 0; i < input.size(); ++i) {
39-
input[i] = rand() / ((float)RAND_MAX);
41+
for (int i = 0; i < 784; ++i) {
42+
array[i] = rand() / ((float)RAND_MAX);
4043
}
41-
42-
// Set value for the input matrix
43-
CHECK(paddle_matrix_set_value(mat, input.data()));
4444

4545
CHECK(paddle_arguments_set_value(in_args, 0, mat));
4646

@@ -53,17 +53,18 @@ int main() {
5353

5454
CHECK(paddle_arguments_get_value(out_args, 0, prob));
5555

56-
std::std::vector<paddle_real> result;
57-
int height;
58-
int width;
56+
uint64_t height;
57+
uint64_t width;
5958

60-
CHECK(paddle_matrix_get_shape(prob, &height, &width);
61-
result.resize(height * width);
62-
CHECK(paddle_matrix_get_value(prob, result.data()));
59+
CHECK(paddle_matrix_get_shape(prob, &height, &width));
60+
CHECK(paddle_matrix_get_row(prob, 0, &array));
6361

64-
printf("Prob: ");
62+
printf("Prob: \n");
6563
for (int i = 0; i < height * width; ++i) {
66-
printf("%.2f ", result[i]);
64+
printf("%.4f ", array[i]);
65+
if ((i + 1) % width == 0) {
66+
printf("\n");
67+
}
6768
}
6869
printf("\n");
6970

paddle/operators/roi_pool_op.cc

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,47 @@ limitations under the License. */
1717
namespace paddle {
1818
namespace operators {
1919

20-
class RoiPoolOp : public framework::OperatorWithKernel {
20+
class ROIPoolOp : public framework::OperatorWithKernel {
2121
public:
2222
using framework::OperatorWithKernel::OperatorWithKernel;
2323

2424
void InferShape(framework::InferShapeContext* ctx) const override {
2525
PADDLE_ENFORCE(ctx->HasInput("X"),
26-
"Input(X) of RoiPoolOp should not be null.");
27-
PADDLE_ENFORCE(ctx->HasInput("Rois"),
28-
"Input(Rois) of RoiPoolOp should not be null.");
26+
"Input(X) of ROIPoolOp should not be null.");
27+
PADDLE_ENFORCE(ctx->HasInput("ROIs"),
28+
"Input(ROIs) of ROIPoolOp should not be null.");
2929
PADDLE_ENFORCE(ctx->HasOutput("Out"),
30-
"Output(Out) of RoiPoolOp should not be null.");
30+
"Output(Out) of ROIPoolOp should not be null.");
3131
PADDLE_ENFORCE(ctx->HasOutput("Argmax"),
32-
"Output(Argmax) of RoiPoolOp should not be null.");
32+
"Output(Argmax) of ROIPoolOp should not be null.");
3333
auto input_dims = ctx->GetInputDim("X");
34-
35-
// Initialize the output's dims to maximum,
36-
// and re-set to real dims by the value of Rois at kernel
37-
ctx->SetOutputDim("Out", input_dims);
34+
auto rois_dims = ctx->GetInputDim("ROIs");
35+
36+
PADDLE_ENFORCE(input_dims.size() == 4,
37+
"The format of input tensor is NCHW.");
38+
PADDLE_ENFORCE(rois_dims.size() == 2,
39+
"ROIs should be a 2-D tensor of shape (num_rois, 5)"
40+
"given as [[batch_id, x1, y1, x2, y2], …].");
41+
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+
ctx->SetOutputDim("Argmax", out_dims);
3861
}
3962

4063
protected:
@@ -46,7 +69,7 @@ class RoiPoolOp : public framework::OperatorWithKernel {
4669
}
4770
};
4871

49-
class RoiPoolGradOp : public framework::OperatorWithKernel {
72+
class ROIPoolGradOp : public framework::OperatorWithKernel {
5073
public:
5174
using framework::OperatorWithKernel::OperatorWithKernel;
5275

@@ -67,44 +90,51 @@ class RoiPoolGradOp : public framework::OperatorWithKernel {
6790
}
6891
};
6992

70-
class RoiPoolOpMaker : public framework::OpProtoAndCheckerMaker {
93+
class ROIPoolOpMaker : public framework::OpProtoAndCheckerMaker {
7194
public:
72-
RoiPoolOpMaker(framework::OpProto* proto,
95+
ROIPoolOpMaker(framework::OpProto* proto,
7396
framework::OpAttrChecker* op_checker)
7497
: OpProtoAndCheckerMaker(proto, op_checker) {
7598
AddInput("X",
7699
"(Tensor), "
77-
"the input of RoiPoolOp.");
78-
AddInput("Rois",
100+
"the input of ROIPoolOp. "
101+
"The format of input tensor is NCHW. Where N is batch size, "
102+
"C is the number of input channels, "
103+
"H is the height of the feature, and "
104+
"W is the width of the feature.");
105+
AddInput("ROIs",
79106
"(Tensor), "
80-
"RoIs (Regions of Interest) to pool over. "
81-
"Should be a 2-D tensor of shape (num_rois, 5)"
82-
"given as [[batch_id, x1, y1, x2, y2], …].");
107+
"ROIs (Regions of Interest) to pool over. "
108+
"should be a 2-D tensor of shape (num_rois, 5)"
109+
"given as [[batch_id, x1, y1, x2, y2], …]. "
110+
"Where batch_id is the id of the data, "
111+
"(x1, y1) is the top left coordinates, and "
112+
"(x2, y2) is the bottom right coordinates.");
83113
AddOutput("Out",
84114
"(Tensor), "
85-
"RoI pooled output 4-D tensor of shape "
86-
"(num_rois, channels, pooled_h, pooled_w).");
115+
"The output of ROIPoolOp is a 4-D tensor with shape "
116+
"(num_rois, channels, pooled_h, pooled_w).");
87117
AddOutput("Argmax",
88118
"(Tensor), "
89119
"Argmaxes corresponding to indices in X used "
90120
"for gradient computation. Only output "
91121
"if arg “is_test” is false.").AsIntermediate();
92122
AddAttr<float>("spatial_scale",
93-
"(float, default 1.0), "
94-
"Multiplicative spatial scale factor "
95-
"to translate ROI coords from their input scale "
96-
"to the scale used when pooling.")
97-
.SetDefault(1.0);
123+
"(float, default 1.0), "
124+
"Multiplicative spatial scale factor "
125+
"to translate ROI coords from their input scale "
126+
"to the scale used when pooling.")
127+
.SetDefault(1.0);
98128
AddAttr<int>("pooled_height",
99-
"(int, default 1), "
100-
"The pooled output height.")
101-
.SetDefault(1);
129+
"(int, default 1), "
130+
"The pooled output height.")
131+
.SetDefault(1);
102132
AddAttr<int>("pooled_width",
103-
"(int, default 1), "
104-
"The pooled output width.")
105-
.SetDefault(1);
133+
"(int, default 1), "
134+
"The pooled output width.")
135+
.SetDefault(1);
106136
AddComment(R"DOC(
107-
RoiPool operator
137+
ROIPool operator
108138
109139
ROI Pooling for Faster-RCNN. The link below is a further introduction:
110140
https://stackoverflow.com/questions/43430056/what-is-roi-layer-in-fast-rcnn
@@ -116,11 +146,11 @@ ROI Pooling for Faster-RCNN. The link below is a further introduction:
116146
} // namespace paddle
117147

118148
namespace ops = paddle::operators;
119-
REGISTER_OP(roi_pool, ops::RoiPoolOp, ops::RoiPoolOpMaker,
120-
roi_pool_grad, ops::RoiPoolGradOp);
149+
REGISTER_OP(roi_pool, ops::ROIPoolOp, ops::ROIPoolOpMaker,
150+
roi_pool_grad, ops::ROIPoolGradOp);
121151
REGISTER_OP_CPU_KERNEL(
122152
roi_pool,
123-
ops::CPURoiPoolOpKernel<paddle::platform::CPUPlace, float>);
153+
ops::CPUROIPoolOpKernel<paddle::platform::CPUPlace, float>);
124154
REGISTER_OP_CPU_KERNEL(
125155
roi_pool_grad,
126-
ops::CPURoiPoolGradOpKernel<paddle::platform::CPUPlace, float>);
156+
ops::CPUROIPoolGradOpKernel<paddle::platform::CPUPlace, float>);

0 commit comments

Comments
 (0)