Skip to content

Commit 99a6c5d

Browse files
committed
change output shape to [2, layer_height, layer_width, num_priors, 4]
1 parent 7297e6f commit 99a6c5d

File tree

3 files changed

+72
-77
lines changed

3 files changed

+72
-77
lines changed

paddle/operators/prior_box_op.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,12 @@ class PriorBoxOp : public framework::OperatorWithKernel {
9393
const int layer_height = input_dims[2];
9494
const int layer_width = input_dims[3];
9595

96-
std::vector<int64_t> dim_vec(3);
97-
// Since all images in a batch has same height and width, we only need to
98-
// generate one set of priors which can be shared across all images.
99-
dim_vec[0] = 1;
100-
// 2 channels. First channel stores the mean of each prior coordinate.
101-
// Second channel stores the variance of each prior coordinate.
102-
dim_vec[1] = 2;
103-
dim_vec[2] = layer_width * layer_height * num_priors * 4;
104-
PADDLE_ENFORCE_GT(dim_vec[2], 0,
105-
"output_dim[2] must larger than 0."
106-
"check your data dims");
96+
std::vector<int64_t> dim_vec(5);
97+
dim_vec[0] = 2;
98+
dim_vec[1] = layer_height;
99+
dim_vec[2] = layer_width;
100+
dim_vec[3] = num_priors;
101+
dim_vec[4] = 4;
107102
auto output_dim = framework::make_ddim(dim_vec);
108103
ctx->SetOutputDim("Out", output_dim);
109104
}
@@ -130,7 +125,8 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
130125
"the input image data of PriorBoxOp, The format is NCHW.");
131126
AddOutput("Out",
132127
"(Tensor, default Tensor<float>), the output prior boxes of "
133-
"PriorBoxOp.");
128+
"PriorBoxOp. The format is [2, layer_height, layer_width, "
129+
"num_priors, 4]");
134130
AddAttr<std::vector<int>>("min_sizes", "(vector<int>) ",
135131
"List of min sizes of generated prior boxes.");
136132
AddAttr<std::vector<int>>("max_sizes", "(vector<int>) ",

paddle/operators/prior_box_op.h

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License. */
1515
#pragma once
1616
#include "paddle/framework/op_registry.h"
1717
#include "paddle/operators/math/math_function.h"
18-
// #include "paddle/operators/strided_memcpy.h"
1918

2019
namespace paddle {
2120
namespace operators {
@@ -94,50 +93,52 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
9493
num_priors += max_sizes.size();
9594
}
9695

97-
int dim = layer_height * layer_width * num_priors * 4;
98-
9996
T* output_data = nullptr;
10097
framework::Tensor output_cpu;
98+
framework::Tensor* output_tensor;
10199
out->mutable_data<T>(ctx.GetPlace());
102100
if (platform::is_gpu_place(ctx.GetPlace())) {
103-
output_data =
104-
output_cpu.mutable_data<T>(out->dims(), platform::CPUPlace());
101+
output_cpu.mutable_data<T>(out->dims(), platform::CPUPlace());
102+
output_tensor = &output_cpu;
105103
} else {
106-
output_data = out->mutable_data<T>(ctx.GetPlace());
104+
output_tensor = out;
107105
}
108106

109-
int idx = 0;
107+
auto e_out = framework::EigenTensor<T, 5>::From(*output_tensor);
110108
for (int h = 0; h < layer_height; ++h) {
111109
for (int w = 0; w < layer_width; ++w) {
112110
float center_x = (w + offset) * step_width;
113111
float center_y = (h + offset) * step_height;
114112
float box_width, box_height;
113+
int idx = 0;
115114
for (size_t s = 0; s < min_sizes.size(); ++s) {
116115
int min_size = min_sizes[s];
117116
// first prior: aspect_ratio = 1, size = min_size
118117
box_width = box_height = min_size;
119118
// xmin
120-
output_data[idx++] = (center_x - box_width / 2.) / img_width;
119+
e_out(0, h, w, idx, 0) = (center_x - box_width / 2.) / img_width;
121120
// ymin
122-
output_data[idx++] = (center_y - box_height / 2.) / img_height;
121+
e_out(0, h, w, idx, 1) = (center_y - box_height / 2.) / img_height;
123122
// xmax
124-
output_data[idx++] = (center_x + box_width / 2.) / img_width;
123+
e_out(0, h, w, idx, 2) = (center_x + box_width / 2.) / img_width;
125124
// ymax
126-
output_data[idx++] = (center_y + box_height / 2.) / img_height;
125+
e_out(0, h, w, idx, 3) = (center_y + box_height / 2.) / img_height;
127126

127+
idx++;
128128
if (max_sizes.size() > 0) {
129129
int max_size = max_sizes[s];
130130
// second prior: aspect_ratio = 1,
131131
// size = sqrt(min_size * max_size)
132132
box_width = box_height = sqrt(min_size * max_size);
133133
// xmin
134-
output_data[idx++] = (center_x - box_width / 2.) / img_width;
134+
e_out(0, h, w, idx, 0) = (center_x - box_width / 2.) / img_width;
135135
// ymin
136-
output_data[idx++] = (center_y - box_height / 2.) / img_height;
136+
e_out(0, h, w, idx, 1) = (center_y - box_height / 2.) / img_height;
137137
// xmax
138-
output_data[idx++] = (center_x + box_width / 2.) / img_width;
138+
e_out(0, h, w, idx, 2) = (center_x + box_width / 2.) / img_width;
139139
// ymax
140-
output_data[idx++] = (center_y + box_height / 2.) / img_height;
140+
e_out(0, h, w, idx, 3) = (center_y + box_height / 2.) / img_height;
141+
idx++;
141142
}
142143

143144
// rest of priors
@@ -149,40 +150,46 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
149150
box_width = min_size * sqrt(ar);
150151
box_height = min_size / sqrt(ar);
151152
// xmin
152-
output_data[idx++] = (center_x - box_width / 2.) / img_width;
153+
e_out(0, h, w, idx, 0) = (center_x - box_width / 2.) / img_width;
153154
// ymin
154-
output_data[idx++] = (center_y - box_height / 2.) / img_height;
155+
e_out(0, h, w, idx, 1) = (center_y - box_height / 2.) / img_height;
155156
// xmax
156-
output_data[idx++] = (center_x + box_width / 2.) / img_width;
157+
e_out(0, h, w, idx, 2) = (center_x + box_width / 2.) / img_width;
157158
// ymax
158-
output_data[idx++] = (center_y + box_height / 2.) / img_height;
159+
e_out(0, h, w, idx, 3) = (center_y + box_height / 2.) / img_height;
160+
idx++;
159161
}
160162
}
161163
}
162164
}
163165

164166
// clip the prior's coordidate such that it is within [0, 1]
165167
if (clip) {
166-
for (int d = 0; d < dim; ++d) {
167-
output_data[d] = std::min<T>(std::max<T>(output_data[d], 0.), 1.);
168+
for (int h = 0; h < layer_height; ++h) {
169+
for (int w = 0; w < layer_width; ++w) {
170+
for (int i = 0; i < num_priors; ++i) {
171+
for (int j = 0; j < 4; ++j) {
172+
e_out(0, h, w, i, j) =
173+
std::min<T>(std::max<T>(e_out(0, h, w, i, j), 0.), 1.);
174+
}
175+
}
176+
}
168177
}
169-
}
170178

171-
// set the variance.
172-
auto output_stride = framework::stride(out->dims());
173-
output_data += output_stride[1];
174-
if (variances.size() == 1) {
175-
for (int i = 0; i < dim; ++i) {
176-
output_data[i] = variances[0];
179+
// set the variance.
180+
auto output_stride = framework::stride(out->dims());
181+
output_data += output_stride[1];
182+
if (variances.size() == 1) {
183+
variances.resize(4);
184+
variances[1] = variances[0];
185+
variances[2] = variances[0];
186+
variances[3] = variances[0];
177187
}
178-
} else {
179-
int count = 0;
180188
for (int h = 0; h < layer_height; ++h) {
181189
for (int w = 0; w < layer_width; ++w) {
182190
for (int i = 0; i < num_priors; ++i) {
183191
for (int j = 0; j < 4; ++j) {
184-
output_data[count] = variances[j];
185-
++count;
192+
e_out(1, h, w, i, j) = variances[j];
186193
}
187194
}
188195
}

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

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,30 @@ def init_test_input(self):
8181
self.layer_h)).astype('float32')
8282

8383
def init_test_output(self):
84-
dim = self.layer_w * self.layer_h * self.num_priors * 4
85-
out_dim = (1, 2, dim)
84+
out_dim = (2, self.layer_h, self.layer_w, self.num_priors, 4)
8685
output = np.zeros(out_dim).astype('float32')
8786

8887
idx = 0
8988
for h in range(self.layer_h):
9089
for w in range(self.layer_w):
9190
center_x = (w + self.offset) * self.step_w
9291
center_y = (h + self.offset) * self.step_h
92+
idx = 0
9393
for s in range(len(self.min_sizes)):
9494
min_size = self.min_sizes[s]
9595
# first prior: aspect_ratio = 1, size = min_size
9696
box_width = box_height = min_size
9797
# xmin
98-
output[0, 0, idx] = (
98+
output[0, h, w, idx, 0] = (
9999
center_x - box_width / 2.) / self.image_w
100-
idx += 1
101100
# ymin
102-
output[0, 0, idx] = (
101+
output[0, h, w, idx, 1] = (
103102
center_y - box_height / 2.) / self.image_h
104-
idx += 1
105103
# xmax
106-
output[0, 0, idx] = (
104+
output[0, h, w, idx, 2] = (
107105
center_x + box_width / 2.) / self.image_w
108-
idx += 1
109106
# ymax
110-
output[0, 0, idx] = (
107+
output[0, h, w, idx, 3] = (
111108
center_y + box_height / 2.) / self.image_h
112109
idx += 1
113110

@@ -117,19 +114,16 @@ def init_test_output(self):
117114
# size = sqrt(min_size * max_size)
118115
box_width = box_height = math.sqrt(min_size * max_size)
119116
# xmin
120-
output[0, 0, idx] = (
117+
output[0, h, w, idx, 0] = (
121118
center_x - box_width / 2.) / self.image_w
122-
idx += 1
123119
# ymin
124-
output[0, 0, idx] = (
120+
output[0, h, w, idx, 1] = (
125121
center_y - box_height / 2.) / self.image_h
126-
idx += 1
127122
# xmax
128-
output[0, 0, idx] = (
123+
output[0, h, w, idx, 2] = (
129124
center_x + box_width / 2.) / self.image_w
130-
idx += 1
131125
# ymax
132-
output[0, 0, idx] = (
126+
output[0, h, w, idx, 3] = (
133127
center_y + box_height / 2.) / self.image_h
134128
idx += 1
135129

@@ -141,37 +135,35 @@ def init_test_output(self):
141135
box_width = min_size * math.sqrt(ar)
142136
box_height = min_size / math.sqrt(ar)
143137
# xmin
144-
output[0, 0, idx] = (
138+
output[0, h, w, idx, 0] = (
145139
center_x - box_width / 2.) / self.image_w
146-
idx += 1
147140
# ymin
148-
output[0, 0, idx] = (
141+
output[0, h, w, idx, 1] = (
149142
center_y - box_height / 2.) / self.image_h
150-
idx += 1
151143
# xmax
152-
output[0, 0, idx] = (
144+
output[0, h, w, idx, 2] = (
153145
center_x + box_width / 2.) / self.image_w
154-
idx += 1
155146
# ymax
156-
output[0, 0, idx] = (
147+
output[0, h, w, idx, 3] = (
157148
center_y + box_height / 2.) / self.image_h
158149
idx += 1
159150
# clip the prior's coordidate such that it is within[0, 1]
160151
if self.clip:
161-
for d in range(dim):
162-
output[0, 0, d] = min(max(output[0, 0, d], 0), 1)
163-
# set the variance.
164-
if len(self.variances) == 1:
165-
for i in range(dim):
166-
output[0, 1, i] = self.variances[0]
167-
else:
168-
count = 0
169152
for h in range(self.layer_h):
170153
for w in range(self.layer_w):
171154
for i in range(self.num_priors):
172155
for j in range(4):
173-
output[0, 1, count] = self.variances[j]
174-
count += 1
156+
output[0, h, w, i, j] = min(
157+
max(output[0, h, w, i, j], 0), 1)
158+
# set the variance.
159+
for h in range(self.layer_h):
160+
for w in range(self.layer_w):
161+
for i in range(self.num_priors):
162+
for j in range(4):
163+
if len(self.variances) == 1:
164+
output[1, h, w, i, j] = self.variances[0]
165+
else:
166+
output[1, h, w, i, j] = self.variances[j]
175167
self.output = output.astype('float32')
176168

177169

0 commit comments

Comments
 (0)