@@ -15,7 +15,6 @@ limitations under the License. */
15
15
#pragma once
16
16
#include " paddle/framework/op_registry.h"
17
17
#include " paddle/operators/math/math_function.h"
18
- // #include "paddle/operators/strided_memcpy.h"
19
18
20
19
namespace paddle {
21
20
namespace operators {
@@ -94,50 +93,52 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
94
93
num_priors += max_sizes.size ();
95
94
}
96
95
97
- int dim = layer_height * layer_width * num_priors * 4 ;
98
-
99
96
T* output_data = nullptr ;
100
97
framework::Tensor output_cpu;
98
+ framework::Tensor* output_tensor;
101
99
out->mutable_data <T>(ctx.GetPlace ());
102
100
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;
105
103
} else {
106
- output_data = out-> mutable_data <T>(ctx. GetPlace ()) ;
104
+ output_tensor = out;
107
105
}
108
106
109
- int idx = 0 ;
107
+ auto e_out = framework::EigenTensor<T, 5 >:: From (*output_tensor) ;
110
108
for (int h = 0 ; h < layer_height; ++h) {
111
109
for (int w = 0 ; w < layer_width; ++w) {
112
110
float center_x = (w + offset) * step_width;
113
111
float center_y = (h + offset) * step_height;
114
112
float box_width, box_height;
113
+ int idx = 0 ;
115
114
for (size_t s = 0 ; s < min_sizes.size (); ++s) {
116
115
int min_size = min_sizes[s];
117
116
// first prior: aspect_ratio = 1, size = min_size
118
117
box_width = box_height = min_size;
119
118
// 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;
121
120
// 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;
123
122
// 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;
125
124
// 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;
127
126
127
+ idx++;
128
128
if (max_sizes.size () > 0 ) {
129
129
int max_size = max_sizes[s];
130
130
// second prior: aspect_ratio = 1,
131
131
// size = sqrt(min_size * max_size)
132
132
box_width = box_height = sqrt (min_size * max_size);
133
133
// 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;
135
135
// 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;
137
137
// 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;
139
139
// 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++;
141
142
}
142
143
143
144
// rest of priors
@@ -149,40 +150,46 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
149
150
box_width = min_size * sqrt (ar);
150
151
box_height = min_size / sqrt (ar);
151
152
// 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;
153
154
// 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;
155
156
// 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;
157
158
// 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++;
159
161
}
160
162
}
161
163
}
162
164
}
163
165
164
166
// clip the prior's coordidate such that it is within [0, 1]
165
167
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
+ }
168
177
}
169
- }
170
178
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 ];
177
187
}
178
- } else {
179
- int count = 0 ;
180
188
for (int h = 0 ; h < layer_height; ++h) {
181
189
for (int w = 0 ; w < layer_width; ++w) {
182
190
for (int i = 0 ; i < num_priors; ++i) {
183
191
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];
186
193
}
187
194
}
188
195
}
0 commit comments