Files
paddlepaddle--paddle/paddle/phi/kernels/prior_box_kernel.h
T
2026-07-13 12:40:42 +08:00

63 lines
2.1 KiB
C++

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "paddle/phi/core/dense_tensor.h"
namespace phi {
template <typename T, typename Context>
void PriorBoxKernel(const Context& dev_ctx,
const DenseTensor& input,
const DenseTensor& image,
const std::vector<float>& min_sizes,
const std::vector<float>& max_sizes,
const std::vector<float>& aspect_ratios,
const std::vector<float>& variances,
bool flip,
bool clip,
float step_w,
float step_h,
float offset,
bool min_max_aspect_ratios_order,
DenseTensor* out,
DenseTensor* var);
inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratio,
bool flip,
std::vector<float>* output_aspect_ratio) {
constexpr float epsilon = 1e-6;
output_aspect_ratio->clear();
output_aspect_ratio->push_back(1.0f);
for (size_t i = 0; i < input_aspect_ratio.size(); ++i) {
float ar = input_aspect_ratio[i];
bool already_exist = false;
for (size_t j = 0; j < output_aspect_ratio->size(); ++j) {
if (fabs(ar - output_aspect_ratio->at(j)) < epsilon) {
already_exist = true;
break;
}
}
if (!already_exist) {
output_aspect_ratio->push_back(ar);
if (flip) {
output_aspect_ratio->push_back(1.0f / ar);
}
}
}
}
} // namespace phi