|
|
@ -22,6 +22,9 @@ limitations under the License. */
|
|
|
|
namespace paddle {
|
|
|
|
namespace paddle {
|
|
|
|
namespace operators {
|
|
|
|
namespace operators {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr int kPriorBoxFLOAT = 1;
|
|
|
|
|
|
|
|
constexpr int kPriorBoxDOUBLE = 2;
|
|
|
|
|
|
|
|
|
|
|
|
inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratior,
|
|
|
|
inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratior,
|
|
|
|
bool flip,
|
|
|
|
bool flip,
|
|
|
|
std::vector<float>* output_aspect_ratior) {
|
|
|
|
std::vector<float>* output_aspect_ratior) {
|
|
|
@ -46,7 +49,7 @@ inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratior,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
template <typename T, typename K>
|
|
|
|
class PriorBoxOpKernel : public framework::OpKernel<T> {
|
|
|
|
class PriorBoxOpKernel : public framework::OpKernel<T> {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
void Compute(const framework::ExecutionContext& ctx) const override {
|
|
|
|
void Compute(const framework::ExecutionContext& ctx) const override {
|
|
|
@ -67,9 +70,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
|
|
|
|
std::vector<float> aspect_ratios;
|
|
|
|
std::vector<float> aspect_ratios;
|
|
|
|
ExpandAspectRatios(input_aspect_ratio, flip, &aspect_ratios);
|
|
|
|
ExpandAspectRatios(input_aspect_ratio, flip, &aspect_ratios);
|
|
|
|
|
|
|
|
|
|
|
|
T step_w = static_cast<T>(ctx.Attr<float>("step_w"));
|
|
|
|
K step_w = static_cast<K>(ctx.Attr<float>("step_w"));
|
|
|
|
T step_h = static_cast<T>(ctx.Attr<float>("step_h"));
|
|
|
|
K step_h = static_cast<K>(ctx.Attr<float>("step_h"));
|
|
|
|
T offset = static_cast<T>(ctx.Attr<float>("offset"));
|
|
|
|
K offset = static_cast<K>(ctx.Attr<float>("offset"));
|
|
|
|
|
|
|
|
|
|
|
|
auto img_width = image->dims()[3];
|
|
|
|
auto img_width = image->dims()[3];
|
|
|
|
auto img_height = image->dims()[2];
|
|
|
|
auto img_height = image->dims()[2];
|
|
|
@ -77,10 +80,10 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
|
|
|
|
auto feature_width = input->dims()[3];
|
|
|
|
auto feature_width = input->dims()[3];
|
|
|
|
auto feature_height = input->dims()[2];
|
|
|
|
auto feature_height = input->dims()[2];
|
|
|
|
|
|
|
|
|
|
|
|
T step_width, step_height;
|
|
|
|
K step_width, step_height;
|
|
|
|
if (step_w == 0 || step_h == 0) {
|
|
|
|
if (step_w == 0 || step_h == 0) {
|
|
|
|
step_width = static_cast<T>(img_width) / feature_width;
|
|
|
|
step_width = static_cast<K>(img_width) / feature_width;
|
|
|
|
step_height = static_cast<T>(img_height) / feature_height;
|
|
|
|
step_height = static_cast<K>(img_height) / feature_height;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
step_width = step_w;
|
|
|
|
step_width = step_w;
|
|
|
|
step_height = step_h;
|
|
|
|
step_height = step_h;
|
|
|
@ -91,15 +94,15 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
|
|
|
|
num_priors += max_sizes.size();
|
|
|
|
num_priors += max_sizes.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
boxes->mutable_data<T>(ctx.GetPlace());
|
|
|
|
boxes->mutable_data<K>(ctx.GetPlace());
|
|
|
|
vars->mutable_data<T>(ctx.GetPlace());
|
|
|
|
vars->mutable_data<K>(ctx.GetPlace());
|
|
|
|
|
|
|
|
|
|
|
|
T* b_t = boxes->data<T>();
|
|
|
|
K* b_t = boxes->data<K>();
|
|
|
|
for (int h = 0; h < feature_height; ++h) {
|
|
|
|
for (int h = 0; h < feature_height; ++h) {
|
|
|
|
for (int w = 0; w < feature_width; ++w) {
|
|
|
|
for (int w = 0; w < feature_width; ++w) {
|
|
|
|
T center_x = (w + offset) * step_width;
|
|
|
|
K center_x = (w + offset) * step_width;
|
|
|
|
T center_y = (h + offset) * step_height;
|
|
|
|
K center_y = (h + offset) * step_height;
|
|
|
|
T box_width, box_height;
|
|
|
|
K box_width, box_height;
|
|
|
|
for (size_t s = 0; s < min_sizes.size(); ++s) {
|
|
|
|
for (size_t s = 0; s < min_sizes.size(); ++s) {
|
|
|
|
auto min_size = min_sizes[s];
|
|
|
|
auto min_size = min_sizes[s];
|
|
|
|
if (min_max_aspect_ratios_order) {
|
|
|
|
if (min_max_aspect_ratios_order) {
|
|
|
@ -161,17 +164,17 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (clip) {
|
|
|
|
if (clip) {
|
|
|
|
T* dt = boxes->data<T>();
|
|
|
|
K* dt = boxes->data<K>();
|
|
|
|
std::transform(dt, dt + boxes->numel(), dt, [](T v) -> T {
|
|
|
|
std::transform(dt, dt + boxes->numel(), dt, [](K v) -> K {
|
|
|
|
return std::min<T>(std::max<T>(v, 0.), 1.);
|
|
|
|
return std::min<K>(std::max<K>(v, 0.), 1.);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
framework::Tensor var_t;
|
|
|
|
framework::Tensor var_t;
|
|
|
|
var_t.mutable_data<T>(
|
|
|
|
var_t.mutable_data<K>(
|
|
|
|
framework::make_ddim({1, static_cast<int>(variances.size())}),
|
|
|
|
framework::make_ddim({1, static_cast<int>(variances.size())}),
|
|
|
|
ctx.GetPlace());
|
|
|
|
ctx.GetPlace());
|
|
|
|
auto var_et = framework::EigenTensor<T, 2>::From(var_t);
|
|
|
|
auto var_et = framework::EigenTensor<K, 2>::From(var_t);
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PADDLE_WITH_MKLML
|
|
|
|
#ifdef PADDLE_WITH_MKLML
|
|
|
|
#pragma omp parallel for
|
|
|
|
#pragma omp parallel for
|
|
|
@ -184,7 +187,7 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
|
|
|
|
auto var_dim = vars->dims();
|
|
|
|
auto var_dim = vars->dims();
|
|
|
|
vars->Resize({box_num, static_cast<int>(variances.size())});
|
|
|
|
vars->Resize({box_num, static_cast<int>(variances.size())});
|
|
|
|
|
|
|
|
|
|
|
|
auto e_vars = framework::EigenMatrix<T, Eigen::RowMajor>::From(*vars);
|
|
|
|
auto e_vars = framework::EigenMatrix<K, Eigen::RowMajor>::From(*vars);
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PADDLE_WITH_MKLML
|
|
|
|
#ifdef PADDLE_WITH_MKLML
|
|
|
|
#pragma omp parallel for collapse(2)
|
|
|
|
#pragma omp parallel for collapse(2)
|
|
|
|