Skip to content

Commit 428d6f9

Browse files
committed
Fix debug mode in prior_box_op (#15702)
test=release/1.3 * Fix debug mode in prior_box_op * Refine code
1 parent af4c9b5 commit 428d6f9

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

paddle/fluid/operators/detection/density_prior_box_op.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class DensityPriorBoxOpKernel : public framework::OpKernel<T> {
7272
#ifdef PADDLE_WITH_MKLML
7373
#pragma omp parallel for
7474
#endif
75-
for (int i = 0; i < fixed_ratios.size(); i++) {
75+
for (size_t i = 0; i < fixed_ratios.size(); i++) {
7676
sqrt_fixed_ratios.push_back(sqrt(fixed_ratios[i]));
7777
}
7878

@@ -115,11 +115,10 @@ class DensityPriorBoxOpKernel : public framework::OpKernel<T> {
115115
}
116116
}
117117
if (clip) {
118-
platform::Transform<platform::CPUDeviceContext> trans;
119-
ClipFunctor<T> clip_func;
120-
trans(ctx.template device_context<platform::CPUDeviceContext>(),
121-
boxes->data<T>(), boxes->data<T>() + boxes->numel(),
122-
boxes->data<T>(), clip_func);
118+
T* dt = boxes->data<T>();
119+
std::transform(dt, dt + boxes->numel(), dt, [](T v) -> T {
120+
return std::min<T>(std::max<T>(v, 0.), 1.);
121+
});
123122
}
124123
framework::Tensor var_t;
125124
var_t.mutable_data<T>(
@@ -141,7 +140,7 @@ class DensityPriorBoxOpKernel : public framework::OpKernel<T> {
141140
#pragma omp parallel for collapse(2)
142141
#endif
143142
for (int i = 0; i < box_num; ++i) {
144-
for (int j = 0; j < variances.size(); ++j) {
143+
for (size_t j = 0; j < variances.size(); ++j) {
145144
e_vars(i, j) = variances[j];
146145
}
147146
}

paddle/fluid/operators/detection/prior_box_op.h

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratior,
4646
}
4747
}
4848

49-
template <typename T>
50-
struct ClipFunctor {
51-
HOSTDEVICE inline T operator()(T in) const {
52-
return std::min<T>(std::max<T>(in, 0.), 1.);
53-
}
54-
};
55-
5649
template <typename T>
5750
class PriorBoxOpKernel : public framework::OpKernel<T> {
5851
public:
@@ -101,31 +94,30 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
10194
boxes->mutable_data<T>(ctx.GetPlace());
10295
vars->mutable_data<T>(ctx.GetPlace());
10396

104-
auto e_boxes = framework::EigenTensor<T, 4>::From(*boxes);
97+
T* b_t = boxes->data<T>();
10598
for (int h = 0; h < feature_height; ++h) {
10699
for (int w = 0; w < feature_width; ++w) {
107100
T center_x = (w + offset) * step_width;
108101
T center_y = (h + offset) * step_height;
109102
T box_width, box_height;
110-
int idx = 0;
111103
for (size_t s = 0; s < min_sizes.size(); ++s) {
112104
auto min_size = min_sizes[s];
113105
if (min_max_aspect_ratios_order) {
114106
box_width = box_height = min_size / 2.;
115-
e_boxes(h, w, idx, 0) = (center_x - box_width) / img_width;
116-
e_boxes(h, w, idx, 1) = (center_y - box_height) / img_height;
117-
e_boxes(h, w, idx, 2) = (center_x + box_width) / img_width;
118-
e_boxes(h, w, idx, 3) = (center_y + box_height) / img_height;
119-
idx++;
107+
b_t[0] = (center_x - box_width) / img_width;
108+
b_t[1] = (center_y - box_height) / img_height;
109+
b_t[2] = (center_x + box_width) / img_width;
110+
b_t[3] = (center_y + box_height) / img_height;
111+
b_t += 4;
120112
if (max_sizes.size() > 0) {
121113
auto max_size = max_sizes[s];
122114
// square prior with size sqrt(minSize * maxSize)
123115
box_width = box_height = sqrt(min_size * max_size) / 2.;
124-
e_boxes(h, w, idx, 0) = (center_x - box_width) / img_width;
125-
e_boxes(h, w, idx, 1) = (center_y - box_height) / img_height;
126-
e_boxes(h, w, idx, 2) = (center_x + box_width) / img_width;
127-
e_boxes(h, w, idx, 3) = (center_y + box_height) / img_height;
128-
idx++;
116+
b_t[0] = (center_x - box_width) / img_width;
117+
b_t[1] = (center_y - box_height) / img_height;
118+
b_t[2] = (center_x + box_width) / img_width;
119+
b_t[3] = (center_y + box_height) / img_height;
120+
b_t += 4;
129121
}
130122
// priors with different aspect ratios
131123
for (size_t r = 0; r < aspect_ratios.size(); ++r) {
@@ -135,45 +127,44 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
135127
}
136128
box_width = min_size * sqrt(ar) / 2.;
137129
box_height = min_size / sqrt(ar) / 2.;
138-
e_boxes(h, w, idx, 0) = (center_x - box_width) / img_width;
139-
e_boxes(h, w, idx, 1) = (center_y - box_height) / img_height;
140-
e_boxes(h, w, idx, 2) = (center_x + box_width) / img_width;
141-
e_boxes(h, w, idx, 3) = (center_y + box_height) / img_height;
142-
idx++;
130+
b_t[0] = (center_x - box_width) / img_width;
131+
b_t[1] = (center_y - box_height) / img_height;
132+
b_t[2] = (center_x + box_width) / img_width;
133+
b_t[3] = (center_y + box_height) / img_height;
134+
b_t += 4;
143135
}
144136
} else {
145137
// priors with different aspect ratios
146138
for (size_t r = 0; r < aspect_ratios.size(); ++r) {
147139
float ar = aspect_ratios[r];
148140
box_width = min_size * sqrt(ar) / 2.;
149141
box_height = min_size / sqrt(ar) / 2.;
150-
e_boxes(h, w, idx, 0) = (center_x - box_width) / img_width;
151-
e_boxes(h, w, idx, 1) = (center_y - box_height) / img_height;
152-
e_boxes(h, w, idx, 2) = (center_x + box_width) / img_width;
153-
e_boxes(h, w, idx, 3) = (center_y + box_height) / img_height;
154-
idx++;
142+
b_t[0] = (center_x - box_width) / img_width;
143+
b_t[1] = (center_y - box_height) / img_height;
144+
b_t[2] = (center_x + box_width) / img_width;
145+
b_t[3] = (center_y + box_height) / img_height;
146+
b_t += 4;
155147
}
156148
if (max_sizes.size() > 0) {
157149
auto max_size = max_sizes[s];
158150
// square prior with size sqrt(minSize * maxSize)
159151
box_width = box_height = sqrt(min_size * max_size) / 2.;
160-
e_boxes(h, w, idx, 0) = (center_x - box_width) / img_width;
161-
e_boxes(h, w, idx, 1) = (center_y - box_height) / img_height;
162-
e_boxes(h, w, idx, 2) = (center_x + box_width) / img_width;
163-
e_boxes(h, w, idx, 3) = (center_y + box_height) / img_height;
164-
idx++;
152+
b_t[0] = (center_x - box_width) / img_width;
153+
b_t[1] = (center_y - box_height) / img_height;
154+
b_t[2] = (center_x + box_width) / img_width;
155+
b_t[3] = (center_y + box_height) / img_height;
156+
b_t += 4;
165157
}
166158
}
167159
}
168160
}
169161
}
170162

171163
if (clip) {
172-
platform::Transform<platform::CPUDeviceContext> trans;
173-
ClipFunctor<T> clip_func;
174-
trans(ctx.template device_context<platform::CPUDeviceContext>(),
175-
boxes->data<T>(), boxes->data<T>() + boxes->numel(),
176-
boxes->data<T>(), clip_func);
164+
T* dt = boxes->data<T>();
165+
std::transform(dt, dt + boxes->numel(), dt, [](T v) -> T {
166+
return std::min<T>(std::max<T>(v, 0.), 1.);
167+
});
177168
}
178169

179170
framework::Tensor var_t;

0 commit comments

Comments
 (0)