|
|
|
|
@ -28,19 +28,20 @@ inline BoxCodeType GetBoxCodeType(const std::string& type) {
|
|
|
|
|
PADDLE_THROW("Not support type %s.", type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
template <typename DeviceContext, typename T>
|
|
|
|
|
class BoxCoderKernel : public framework::OpKernel<T> {
|
|
|
|
|
public:
|
|
|
|
|
void EncodeCenterSize(const framework::Tensor& target_box,
|
|
|
|
|
const framework::Tensor& prior_box,
|
|
|
|
|
const framework::Tensor& prior_box_var,
|
|
|
|
|
void EncodeCenterSize(const framework::Tensor* target_box,
|
|
|
|
|
const framework::Tensor* prior_box,
|
|
|
|
|
const framework::Tensor* prior_box_var,
|
|
|
|
|
const bool normalized, T* output) const {
|
|
|
|
|
int64_t row = target_box.dims()[0];
|
|
|
|
|
int64_t col = prior_box.dims()[0];
|
|
|
|
|
int64_t len = prior_box.dims()[1];
|
|
|
|
|
auto* target_box_data = target_box.data<T>();
|
|
|
|
|
auto* prior_box_data = prior_box.data<T>();
|
|
|
|
|
auto* prior_box_var_data = prior_box_var.data<T>();
|
|
|
|
|
int64_t row = target_box->dims()[0];
|
|
|
|
|
int64_t col = prior_box->dims()[0];
|
|
|
|
|
int64_t len = prior_box->dims()[1];
|
|
|
|
|
auto* target_box_data = target_box->data<T>();
|
|
|
|
|
auto* prior_box_data = prior_box->data<T>();
|
|
|
|
|
const T* prior_box_var_data = nullptr;
|
|
|
|
|
if (prior_box_var) prior_box_var_data = prior_box_var->data<T>();
|
|
|
|
|
|
|
|
|
|
for (int64_t i = 0; i < row; ++i) {
|
|
|
|
|
for (int64_t j = 0; j < col; ++j) {
|
|
|
|
|
@ -65,30 +66,35 @@ class BoxCoderKernel : public framework::OpKernel<T> {
|
|
|
|
|
(normalized == false);
|
|
|
|
|
|
|
|
|
|
size_t offset = i * col * len + j * len;
|
|
|
|
|
output[offset] = (target_box_center_x - prior_box_center_x) /
|
|
|
|
|
prior_box_width / prior_box_var_data[j * len];
|
|
|
|
|
output[offset + 1] = (target_box_center_y - prior_box_center_y) /
|
|
|
|
|
prior_box_height / prior_box_var_data[j * len + 1];
|
|
|
|
|
output[offset] =
|
|
|
|
|
(target_box_center_x - prior_box_center_x) / prior_box_width;
|
|
|
|
|
output[offset + 1] =
|
|
|
|
|
(target_box_center_y - prior_box_center_y) / prior_box_height;
|
|
|
|
|
output[offset + 2] =
|
|
|
|
|
std::log(std::fabs(target_box_width / prior_box_width)) /
|
|
|
|
|
prior_box_var_data[j * len + 2];
|
|
|
|
|
std::log(std::fabs(target_box_width / prior_box_width));
|
|
|
|
|
output[offset + 3] =
|
|
|
|
|
std::log(std::fabs(target_box_height / prior_box_height)) /
|
|
|
|
|
prior_box_var_data[j * len + 3];
|
|
|
|
|
std::log(std::fabs(target_box_height / prior_box_height));
|
|
|
|
|
if (prior_box_var) {
|
|
|
|
|
output[offset] /= prior_box_var_data[j * len];
|
|
|
|
|
output[offset + 1] /= prior_box_var_data[j * len + 1];
|
|
|
|
|
output[offset + 2] /= prior_box_var_data[j * len + 2];
|
|
|
|
|
output[offset + 3] /= prior_box_var_data[j * len + 3];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void DecodeCenterSize(const framework::Tensor& target_box,
|
|
|
|
|
const framework::Tensor& prior_box,
|
|
|
|
|
const framework::Tensor& prior_box_var,
|
|
|
|
|
void DecodeCenterSize(const framework::Tensor* target_box,
|
|
|
|
|
const framework::Tensor* prior_box,
|
|
|
|
|
const framework::Tensor* prior_box_var,
|
|
|
|
|
const bool normalized, T* output) const {
|
|
|
|
|
int64_t row = target_box.dims()[0];
|
|
|
|
|
int64_t col = prior_box.dims()[0];
|
|
|
|
|
int64_t len = prior_box.dims()[1];
|
|
|
|
|
int64_t row = target_box->dims()[0];
|
|
|
|
|
int64_t col = prior_box->dims()[0];
|
|
|
|
|
int64_t len = prior_box->dims()[1];
|
|
|
|
|
|
|
|
|
|
auto* target_box_data = target_box.data<T>();
|
|
|
|
|
auto* prior_box_data = prior_box.data<T>();
|
|
|
|
|
auto* prior_box_var_data = prior_box_var.data<T>();
|
|
|
|
|
auto* target_box_data = target_box->data<T>();
|
|
|
|
|
auto* prior_box_data = prior_box->data<T>();
|
|
|
|
|
const T* prior_box_var_data = nullptr;
|
|
|
|
|
if (prior_box_var) prior_box_var_data = prior_box_var->data<T>();
|
|
|
|
|
|
|
|
|
|
for (int64_t i = 0; i < row; ++i) {
|
|
|
|
|
for (int64_t j = 0; j < col; ++j) {
|
|
|
|
|
@ -103,19 +109,32 @@ class BoxCoderKernel : public framework::OpKernel<T> {
|
|
|
|
|
T prior_box_center_y =
|
|
|
|
|
(prior_box_data[j * len + 3] + prior_box_data[j * len + 1]) / 2;
|
|
|
|
|
|
|
|
|
|
T target_box_center_x = prior_box_var_data[j * len] *
|
|
|
|
|
T target_box_center_x = 0, target_box_center_y = 0;
|
|
|
|
|
T target_box_width = 0, target_box_height = 0;
|
|
|
|
|
if (prior_box_var) {
|
|
|
|
|
target_box_center_x = prior_box_var_data[j * len] *
|
|
|
|
|
target_box_data[offset] * prior_box_width +
|
|
|
|
|
prior_box_center_x;
|
|
|
|
|
T target_box_center_y = prior_box_var_data[j * len + 1] *
|
|
|
|
|
target_box_center_y = prior_box_var_data[j * len + 1] *
|
|
|
|
|
target_box_data[offset + 1] *
|
|
|
|
|
prior_box_height +
|
|
|
|
|
prior_box_center_y;
|
|
|
|
|
T target_box_width = std::exp(prior_box_var_data[j * len + 2] *
|
|
|
|
|
target_box_width = std::exp(prior_box_var_data[j * len + 2] *
|
|
|
|
|
target_box_data[offset + 2]) *
|
|
|
|
|
prior_box_width;
|
|
|
|
|
T target_box_height = std::exp(prior_box_var_data[j * len + 3] *
|
|
|
|
|
target_box_height = std::exp(prior_box_var_data[j * len + 3] *
|
|
|
|
|
target_box_data[offset + 3]) *
|
|
|
|
|
prior_box_height;
|
|
|
|
|
} else {
|
|
|
|
|
target_box_center_x =
|
|
|
|
|
target_box_data[offset] * prior_box_width + prior_box_center_x;
|
|
|
|
|
target_box_center_y = target_box_data[offset + 1] * prior_box_height +
|
|
|
|
|
prior_box_center_y;
|
|
|
|
|
target_box_width =
|
|
|
|
|
std::exp(target_box_data[offset + 2]) * prior_box_width;
|
|
|
|
|
target_box_height =
|
|
|
|
|
std::exp(target_box_data[offset + 3]) * prior_box_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output[offset] = target_box_center_x - target_box_width / 2;
|
|
|
|
|
output[offset + 1] = target_box_center_y - target_box_height / 2;
|
|
|
|
|
@ -147,10 +166,10 @@ class BoxCoderKernel : public framework::OpKernel<T> {
|
|
|
|
|
bool normalized = context.Attr<bool>("box_normalized");
|
|
|
|
|
T* output = output_box->data<T>();
|
|
|
|
|
if (code_type == BoxCodeType::kEncodeCenterSize) {
|
|
|
|
|
EncodeCenterSize(*target_box, *prior_box, *prior_box_var, normalized,
|
|
|
|
|
EncodeCenterSize(target_box, prior_box, prior_box_var, normalized,
|
|
|
|
|
output);
|
|
|
|
|
} else if (code_type == BoxCodeType::kDecodeCenterSize) {
|
|
|
|
|
DecodeCenterSize(*target_box, *prior_box, *prior_box_var, normalized,
|
|
|
|
|
DecodeCenterSize(target_box, prior_box, prior_box_var, normalized,
|
|
|
|
|
output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|