Skip to content

Commit 142f632

Browse files
committed
update code
1 parent f020f4b commit 142f632

File tree

3 files changed

+57
-74
lines changed

3 files changed

+57
-74
lines changed

paddle/operators/prior_box_op.cc

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ class PriorBoxOp : public framework::OperatorWithKernel {
2323

2424
void InferShape(framework::InferShapeContext* ctx) const override {
2525
PADDLE_ENFORCE(ctx->HasInput("Input"),
26-
"Input(X) of PriorBoxOp should not be null.");
26+
"Input(Input) of PriorBoxOp should not be null.");
2727
PADDLE_ENFORCE(ctx->HasInput("Image"),
28-
"Input(Offset) of PriorBoxOp should not be null.");
28+
"Input(Image) of PriorBoxOp should not be null.");
2929

3030
auto image_dims = ctx->GetInputDim("Image");
3131
auto input_dims = ctx->GetInputDim("Input");
32-
PADDLE_ENFORCE(image_dims.size() == 4, "The format of image is NCHW.");
33-
PADDLE_ENFORCE(input_dims.size() == 4, "The format of input is NCHW.");
32+
PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW.");
33+
PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
3434

3535
PADDLE_ENFORCE_LT(input_dims[2], image_dims[2],
3636
"The height of input must smaller than image.");
@@ -45,7 +45,7 @@ class PriorBoxOp : public framework::OperatorWithKernel {
4545
bool flip = ctx->Attrs().Get<bool>("flip");
4646

4747
PADDLE_ENFORCE_GT(min_sizes.size(), 0,
48-
"Size of min_size must be at least 1.");
48+
"Size of min_sizes must be at least 1.");
4949
for (size_t i = 0; i < min_sizes.size(); ++i) {
5050
PADDLE_ENFORCE_GT(min_sizes[i], 0, "min_sizes[%d] must be positive.", i);
5151
}
@@ -56,7 +56,7 @@ class PriorBoxOp : public framework::OperatorWithKernel {
5656
int num_priors = aspect_ratios_vec.size() * min_sizes.size();
5757
if (max_sizes.size() > 0) {
5858
PADDLE_ENFORCE_EQ(max_sizes.size(), min_sizes.size(),
59-
"The length of min_size and max_size must be equal.");
59+
"The number of min_size and max_size must be equal.");
6060
for (size_t i = 0; i < min_sizes.size(); ++i) {
6161
PADDLE_ENFORCE_GT(max_sizes[i], min_sizes[i],
6262
"max_size[%d] must be greater than min_size[%d].", i,
@@ -65,13 +65,10 @@ class PriorBoxOp : public framework::OperatorWithKernel {
6565
}
6666
}
6767

68-
if (variances.size() > 1) {
69-
PADDLE_ENFORCE_EQ(variances.size(), 4,
70-
"Must and only provide 4 variance.");
71-
for (size_t i = 0; i < variances.size(); ++i) {
72-
PADDLE_ENFORCE_GT(variances[i], 0.0,
73-
"variance[%d] must be greater than 0.", i);
74-
}
68+
PADDLE_ENFORCE_EQ(variances.size(), 4, "Must and only provide 4 variance.");
69+
for (size_t i = 0; i < variances.size(); ++i) {
70+
PADDLE_ENFORCE_GT(variances[i], 0.0,
71+
"variance[%d] must be greater than 0.", i);
7572
}
7673

7774
const float step_h = ctx->Attrs().Get<float>("step_h");
@@ -95,19 +92,19 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
9592
: OpProtoAndCheckerMaker(proto, op_checker) {
9693
AddInput("Input",
9794
"(Tensor, default Tensor<float>), "
98-
"the input feature data of PriorBoxOp, The format is NCHW.");
95+
"the input feature data of PriorBoxOp, The layout is NCHW.");
9996
AddInput("Image",
10097
"(Tensor, default Tensor<float>), "
101-
"the input image data of PriorBoxOp, The format is NCHW.");
98+
"the input image data of PriorBoxOp, The layout is NCHW.");
10299
AddOutput("Boxes",
103100
"(Tensor, default Tensor<float>), the output prior boxes of "
104-
"PriorBoxOp. The format is [layer_height, layer_width, "
101+
"PriorBoxOp. The layout is [layer_height, layer_width, "
105102
"num_priors, 4]. layer_height is the height of input, "
106103
"layer_width is the width of input, num_priors is the box "
107104
"count of each position.");
108105
AddOutput("Variances",
109106
"(Tensor, default Tensor<float>), the expanded variances of "
110-
"PriorBoxOp. The format is [layer_height, layer_width, "
107+
"PriorBoxOp. The layout is [layer_height, layer_width, "
111108
"num_priors, 4]. layer_height is the height of input, "
112109
"layer_width is the width of input, num_priors is the box "
113110
"count of each position.");
@@ -117,12 +114,10 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
117114
"List of max sizes of generated prior boxes.");
118115
AddAttr<std::vector<float>>(
119116
"aspect_ratios", "(vector<float>) ",
120-
"List of aspect ratios of generated prior boxes.")
121-
.SetDefault({});
117+
"List of aspect ratios of generated prior boxes.");
122118
AddAttr<std::vector<float>>(
123119
"variances", "(vector<float>) ",
124-
"List of variances to be encoded in prior boxes.")
125-
.SetDefault({0.1});
120+
"List of variances to be encoded in prior boxes.");
126121
AddAttr<bool>("flip", "(bool) ", "Whether to flip aspect ratios.")
127122
.SetDefault(true);
128123
AddAttr<bool>("clip", "(bool) ", "Whether to clip out-of-boundary boxes.")

paddle/operators/prior_box_op.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
7070
std::vector<float> aspect_ratios;
7171
ExpandAspectRatios(input_aspect_ratio, flip, aspect_ratios);
7272

73-
auto step_w = ctx.Attr<float>("step_w");
74-
auto step_h = ctx.Attr<float>("step_h");
75-
auto offset = ctx.Attr<float>("offset");
73+
T step_w = static_cast<T>(ctx.Attr<float>("step_w"));
74+
T step_h = static_cast<T>(ctx.Attr<float>("step_h"));
75+
T offset = static_cast<T>(ctx.Attr<float>("offset"));
7676

7777
auto img_width = image->dims()[3];
7878
auto img_height = image->dims()[2];
7979

8080
auto layer_width = input->dims()[3];
8181
auto layer_height = input->dims()[2];
8282

83-
float step_width, step_height;
83+
T step_width, step_height;
8484
if (step_w == 0 || step_h == 0) {
85-
step_width = static_cast<float>(img_width) / layer_width;
86-
step_height = static_cast<float>(img_height) / layer_height;
85+
step_width = static_cast<T>(img_width) / layer_width;
86+
step_height = static_cast<T>(img_height) / layer_height;
8787
} else {
8888
step_width = step_w;
8989
step_height = step_h;
@@ -100,9 +100,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
100100
auto e_boxes = framework::EigenTensor<T, 4>::From(*boxes);
101101
for (int h = 0; h < layer_height; ++h) {
102102
for (int w = 0; w < layer_width; ++w) {
103-
float center_x = (w + offset) * step_width;
104-
float center_y = (h + offset) * step_height;
105-
float box_width, box_height;
103+
T center_x = (w + offset) * step_width;
104+
T center_y = (h + offset) * step_height;
105+
T box_width, box_height;
106106
int idx = 0;
107107
for (size_t s = 0; s < min_sizes.size(); ++s) {
108108
int min_size = min_sizes[s];

python/paddle/v2/fluid/tests/test_prior_box_op.py

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import unittest
216
import numpy as np
317
import sys
@@ -86,65 +100,39 @@ def init_test_output(self):
86100
idx = 0
87101
for h in range(self.layer_h):
88102
for w in range(self.layer_w):
89-
center_x = (w + self.offset) * self.step_w
90-
center_y = (h + self.offset) * self.step_h
103+
c_x = (w + self.offset) * self.step_w
104+
c_y = (h + self.offset) * self.step_h
91105
idx = 0
92106
for s in range(len(self.min_sizes)):
93107
min_size = self.min_sizes[s]
94-
# first prior: aspect_ratio = 1, size = min_size
95-
box_width = box_height = min_size
96-
# xmin
97-
out_boxes[h, w, idx, 0] = (
98-
center_x - box_width / 2.) / self.image_w
99-
# ymin
100-
out_boxes[h, w, idx, 1] = (
101-
center_y - box_height / 2.) / self.image_h
102-
# xmax
103-
out_boxes[h, w, idx, 2] = (
104-
center_x + box_width / 2.) / self.image_w
105-
# ymax
106-
out_boxes[h, w, idx, 3] = (
107-
center_y + box_height / 2.) / self.image_h
108+
c_w = c_h = min_size / 2.
109+
out_boxes[h, w, idx, :] = [
110+
(c_x - c_w) / self.image_w, (c_y - c_h) / self.image_h,
111+
(c_x + c_w) / self.image_w, (c_y + c_h) / self.image_h
112+
]
108113
idx += 1
109114

110115
if len(self.max_sizes) > 0:
111116
max_size = self.max_sizes[s]
112117
# second prior: aspect_ratio = 1,
113-
# size = sqrt(min_size * max_size)
114-
box_width = box_height = math.sqrt(min_size * max_size)
115-
# xmin
116-
out_boxes[h, w, idx, 0] = (
117-
center_x - box_width / 2.) / self.image_w
118-
# ymin
119-
out_boxes[h, w, idx, 1] = (
120-
center_y - box_height / 2.) / self.image_h
121-
# xmax
122-
out_boxes[h, w, idx, 2] = (
123-
center_x + box_width / 2.) / self.image_w
124-
# ymax
125-
out_boxes[h, w, idx, 3] = (
126-
center_y + box_height / 2.) / self.image_h
118+
c_w = c_h = math.sqrt(min_size * max_size) / 2
119+
out_boxes[h, w, idx, :] = [(c_x - c_w) / self.image_w,
120+
(c_y - c_h) / self.image_h,
121+
(c_x + c_w) / self.image_w,
122+
(c_y + c_h) / self.image_h]
127123
idx += 1
128124

129125
# rest of priors
130126
for r in range(len(self.real_aspect_ratios)):
131127
ar = self.real_aspect_ratios[r]
132128
if math.fabs(ar - 1.) < 1e-6:
133129
continue
134-
box_width = min_size * math.sqrt(ar)
135-
box_height = min_size / math.sqrt(ar)
136-
# xmin
137-
out_boxes[h, w, idx, 0] = (
138-
center_x - box_width / 2.) / self.image_w
139-
# ymin
140-
out_boxes[h, w, idx, 1] = (
141-
center_y - box_height / 2.) / self.image_h
142-
# xmax
143-
out_boxes[h, w, idx, 2] = (
144-
center_x + box_width / 2.) / self.image_w
145-
# ymax
146-
out_boxes[h, w, idx, 3] = (
147-
center_y + box_height / 2.) / self.image_h
130+
c_w = min_size * math.sqrt(ar) / 2
131+
c_h = (min_size / math.sqrt(ar)) / 2
132+
out_boxes[h, w, idx, :] = [(c_x - c_w) / self.image_w,
133+
(c_y - c_h) / self.image_h,
134+
(c_x + c_w) / self.image_w,
135+
(c_y + c_h) / self.image_h]
148136
idx += 1
149137
# clip the prior's coordidate such that it is within[0, 1]
150138
if self.clip:

0 commit comments

Comments
 (0)