提交 85b61a05 编写于 作者: Z zhangwen31

[op][arm][kernel]fix: yolo_box updated with paddle-fluid, attr "scale_x_y" and...

[op][arm][kernel]fix: yolo_box updated with paddle-fluid, attr "scale_x_y" and "clip_bbox" supported
上级 80452148
...@@ -34,9 +34,12 @@ inline void get_yolo_box(float* box, ...@@ -34,9 +34,12 @@ inline void get_yolo_box(float* box,
int index, int index,
int stride, int stride,
int img_height, int img_height,
int img_width) { int img_width,
box[0] = (i + sigmoid(x[index])) * img_width / grid_size; float scale,
box[1] = (j + sigmoid(x[index + stride])) * img_height / grid_size; float bias) {
box[0] = (i + sigmoid(x[index]) * scale + bias) * img_width / grid_size;
box[1] =
(j + sigmoid(x[index + stride]) * scale + bias) * img_height / grid_size;
box[2] = std::exp(x[index + 2 * stride]) * anchors[2 * an_idx] * img_width / box[2] = std::exp(x[index + 2 * stride]) * anchors[2 * an_idx] * img_width /
input_size; input_size;
box[3] = std::exp(x[index + 3 * stride]) * anchors[2 * an_idx + 1] * box[3] = std::exp(x[index + 3 * stride]) * anchors[2 * an_idx + 1] *
...@@ -57,21 +60,25 @@ inline void calc_detection_box(float* boxes, ...@@ -57,21 +60,25 @@ inline void calc_detection_box(float* boxes,
float* box, float* box,
const int box_idx, const int box_idx,
const int img_height, const int img_height,
const int img_width) { const int img_width,
bool clip_bbox) {
boxes[box_idx] = box[0] - box[2] / 2; boxes[box_idx] = box[0] - box[2] / 2;
boxes[box_idx + 1] = box[1] - box[3] / 2; boxes[box_idx + 1] = box[1] - box[3] / 2;
boxes[box_idx + 2] = box[0] + box[2] / 2; boxes[box_idx + 2] = box[0] + box[2] / 2;
boxes[box_idx + 3] = box[1] + box[3] / 2; boxes[box_idx + 3] = box[1] + box[3] / 2;
boxes[box_idx] = boxes[box_idx] > 0 ? boxes[box_idx] : static_cast<float>(0); if (clip_bbox) {
boxes[box_idx + 1] = boxes[box_idx] =
boxes[box_idx + 1] > 0 ? boxes[box_idx + 1] : static_cast<float>(0); boxes[box_idx] > 0 ? boxes[box_idx] : static_cast<float>(0);
boxes[box_idx + 2] = boxes[box_idx + 2] < img_width - 1 boxes[box_idx + 1] =
? boxes[box_idx + 2] boxes[box_idx + 1] > 0 ? boxes[box_idx + 1] : static_cast<float>(0);
: static_cast<float>(img_width - 1); boxes[box_idx + 2] = boxes[box_idx + 2] < img_width - 1
boxes[box_idx + 3] = boxes[box_idx + 3] < img_height - 1 ? boxes[box_idx + 2]
? boxes[box_idx + 3] : static_cast<float>(img_width - 1);
: static_cast<float>(img_height - 1); boxes[box_idx + 3] = boxes[box_idx + 3] < img_height - 1
? boxes[box_idx + 3]
: static_cast<float>(img_height - 1);
}
} }
inline void calc_label_score(float* scores, inline void calc_label_score(float* scores,
...@@ -94,7 +101,10 @@ void yolobox(lite::Tensor* X, ...@@ -94,7 +101,10 @@ void yolobox(lite::Tensor* X,
std::vector<int> anchors, std::vector<int> anchors,
int class_num, int class_num,
float conf_thresh, float conf_thresh,
int downsample_ratio) { int downsample_ratio,
bool clip_bbox,
float scale,
float bias) {
const int n = X->dims()[0]; const int n = X->dims()[0];
const int h = X->dims()[2]; const int h = X->dims()[2];
const int w = X->dims()[3]; const int w = X->dims()[3];
...@@ -111,8 +121,10 @@ void yolobox(lite::Tensor* X, ...@@ -111,8 +121,10 @@ void yolobox(lite::Tensor* X,
int* ImgSize_data = ImgSize->mutable_data<int>(); int* ImgSize_data = ImgSize->mutable_data<int>();
float* Boxes_data = Boxes->mutable_data<float>(); float* Boxes_data = Boxes->mutable_data<float>();
memset(Boxes_data, 0, Boxes->numel() * sizeof(float));
float* Scores_data = Scores->mutable_data<float>(); float* Scores_data = Scores->mutable_data<float>();
memset(Scores_data, 0, Scores->numel() * sizeof(float));
float box[4]; float box[4];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
...@@ -142,9 +154,12 @@ void yolobox(lite::Tensor* X, ...@@ -142,9 +154,12 @@ void yolobox(lite::Tensor* X,
box_idx, box_idx,
stride, stride,
img_height, img_height,
img_width); img_width,
scale,
bias);
box_idx = (i * b_num + j * stride + k * w + l) * 4; box_idx = (i * b_num + j * stride + k * w + l) * 4;
calc_detection_box(Boxes_data, box, box_idx, img_height, img_width); calc_detection_box(
Boxes_data, box, box_idx, img_height, img_width, clip_bbox);
int label_idx = int label_idx =
get_entry_index(i, j, k * w + l, an_num, an_stride, stride, 5); get_entry_index(i, j, k * w + l, an_num, an_stride, stride, 5);
......
...@@ -29,7 +29,10 @@ void yolobox(lite::Tensor* X, ...@@ -29,7 +29,10 @@ void yolobox(lite::Tensor* X,
std::vector<int> anchors, std::vector<int> anchors,
int class_num, int class_num,
float conf_thresh, float conf_thresh,
int downsample_ratio); int downsample_ratio,
bool clip_bbox,
float scale,
float bias);
} // namespace math } // namespace math
} // namespace arm } // namespace arm
......
...@@ -32,6 +32,9 @@ void YoloBoxCompute::Run() { ...@@ -32,6 +32,9 @@ void YoloBoxCompute::Run() {
int class_num = param.class_num; int class_num = param.class_num;
float conf_thresh = param.conf_thresh; float conf_thresh = param.conf_thresh;
int downsample_ratio = param.downsample_ratio; int downsample_ratio = param.downsample_ratio;
bool clip_bbox = param.clip_bbox;
float scale_x_y = param.scale_x_y;
float bias = -0.5 * (scale_x_y - 1.);
Boxes->clear(); Boxes->clear();
Scores->clear(); Scores->clear();
lite::arm::math::yolobox(X, lite::arm::math::yolobox(X,
...@@ -41,7 +44,10 @@ void YoloBoxCompute::Run() { ...@@ -41,7 +44,10 @@ void YoloBoxCompute::Run() {
anchors, anchors,
class_num, class_num,
conf_thresh, conf_thresh,
downsample_ratio); downsample_ratio,
clip_bbox,
scale_x_y,
bias);
} }
} // namespace arm } // namespace arm
......
...@@ -152,7 +152,7 @@ __global__ void KeYoloBoxFw(const T* input, ...@@ -152,7 +152,7 @@ __global__ void KeYoloBoxFw(const T* input,
scores, input, label_idx, score_idx, class_num, conf, grid_num); scores, input, label_idx, score_idx, class_num, conf, grid_num);
} }
} }
// fixme: yolo box has updated, check arm kernel to get more info
void YoloBoxCompute::Run() { void YoloBoxCompute::Run() {
auto& param = this->Param<param_t>(); auto& param = this->Param<param_t>();
auto& ctx = this->ctx_->template As<CUDAContext>(); auto& ctx = this->ctx_->template As<CUDAContext>();
......
...@@ -21,6 +21,7 @@ namespace lite { ...@@ -21,6 +21,7 @@ namespace lite {
namespace subgraph { namespace subgraph {
namespace xpu { namespace xpu {
// fixme: yolo box has updated, check arm kernel to get more info
int YoloBoxConverter(void* ctx, OpLite* op, KernelBase* kernel) { int YoloBoxConverter(void* ctx, OpLite* op, KernelBase* kernel) {
CHECK(ctx != nullptr); CHECK(ctx != nullptr);
CHECK(op != nullptr); CHECK(op != nullptr);
......
...@@ -264,6 +264,8 @@ struct YoloBoxParam : ParamBase { ...@@ -264,6 +264,8 @@ struct YoloBoxParam : ParamBase {
int class_num{0}; int class_num{0};
float conf_thresh{0.f}; float conf_thresh{0.f};
int downsample_ratio{0}; int downsample_ratio{0};
bool clip_bbox{true};
float scale_x_y{1.0f};
}; };
// For Scale Op // For Scale Op
......
...@@ -72,6 +72,8 @@ bool YoloBoxOp::AttachImpl(const cpp::OpDesc& op_desc, lite::Scope* scope) { ...@@ -72,6 +72,8 @@ bool YoloBoxOp::AttachImpl(const cpp::OpDesc& op_desc, lite::Scope* scope) {
param_.class_num = op_desc.GetAttr<int>("class_num"); param_.class_num = op_desc.GetAttr<int>("class_num");
param_.conf_thresh = op_desc.GetAttr<float>("conf_thresh"); param_.conf_thresh = op_desc.GetAttr<float>("conf_thresh");
param_.downsample_ratio = op_desc.GetAttr<int>("downsample_ratio"); param_.downsample_ratio = op_desc.GetAttr<int>("downsample_ratio");
param_.clip_bbox = op_desc.GetAttr<bool>("clip_bbox");
param_.scale_x_y = op_desc.GetAttr<float>("scale_x_y");
return true; return true;
} }
......
...@@ -100,6 +100,8 @@ class YoloBoxComputeTester : public arena::TestCase { ...@@ -100,6 +100,8 @@ class YoloBoxComputeTester : public arena::TestCase {
int class_num_ = 0; int class_num_ = 0;
float conf_thresh_ = 0.f; float conf_thresh_ = 0.f;
int downsample_ratio_ = 0; int downsample_ratio_ = 0;
bool clip_bbox_ = true;
float scale_x_y_ = 1.0;
DDim _dims0_{{1, 255, 13, 13}}; DDim _dims0_{{1, 255, 13, 13}};
DDim _dims1_{{1, 2}}; DDim _dims1_{{1, 2}};
...@@ -212,6 +214,8 @@ class YoloBoxComputeTester : public arena::TestCase { ...@@ -212,6 +214,8 @@ class YoloBoxComputeTester : public arena::TestCase {
op_desc->SetAttr("class_num", class_num_); op_desc->SetAttr("class_num", class_num_);
op_desc->SetAttr("conf_thresh", conf_thresh_); op_desc->SetAttr("conf_thresh", conf_thresh_);
op_desc->SetAttr("downsample_ratio", downsample_ratio_); op_desc->SetAttr("downsample_ratio", downsample_ratio_);
op_desc->SetAttr("clip_bbox", clip_bbox_);
op_desc->SetAttr("scale_x_y", scale_x_y_);
} }
void PrepareData() override { void PrepareData() override {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册