Skip to content

Commit b3a7649

Browse files
authored
fix picodet cpp infer (PaddlePaddle#5061)
* fix picodet cpp infer
1 parent 2711a60 commit b3a7649

File tree

7 files changed

+863
-974
lines changed

7 files changed

+863
-974
lines changed

deploy/cpp/include/picodet_postprocess.h

+13-14
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,24 @@
1414

1515
#pragma once
1616

17-
#include <string>
18-
#include <vector>
19-
#include <memory>
20-
#include <utility>
17+
#include <cmath>
2118
#include <ctime>
19+
#include <memory>
2220
#include <numeric>
21+
#include <string>
22+
#include <utility>
23+
#include <vector>
2324

2425
#include "include/utils.h"
2526

2627
namespace PaddleDetection {
2728

28-
void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult>* results,
29-
std::vector<const float *> outs,
30-
std::vector<int> fpn_stride,
31-
std::vector<float> im_shape,
32-
std::vector<float> scale_factor,
33-
float score_threshold = 0.3,
34-
float nms_threshold = 0.5,
35-
int num_class = 80,
36-
int reg_max = 7);
29+
void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult> *results,
30+
std::vector<const float *> outs,
31+
std::vector<int> fpn_stride,
32+
std::vector<float> im_shape,
33+
std::vector<float> scale_factor,
34+
float score_threshold = 0.3, float nms_threshold = 0.5,
35+
int num_class = 80, int reg_max = 7);
3736

38-
} // namespace PaddleDetection
37+
} // namespace PaddleDetection

deploy/cpp/src/picodet_postprocess.cc

+62-64
Original file line numberDiff line numberDiff line change
@@ -20,79 +20,76 @@
2020
namespace PaddleDetection {
2121

2222
float fast_exp(float x) {
23-
union {
24-
uint32_t i;
25-
float f;
26-
} v{};
27-
v.i = (1 << 23) * (1.4426950409 * x + 126.93490512f);
28-
return v.f;
23+
union {
24+
uint32_t i;
25+
float f;
26+
} v{};
27+
v.i = (1 << 23) * (1.4426950409 * x + 126.93490512f);
28+
return v.f;
2929
}
3030

3131
template <typename _Tp>
3232
int activation_function_softmax(const _Tp *src, _Tp *dst, int length) {
33-
const _Tp alpha = *std::max_element(src, src + length);
34-
_Tp denominator{0};
33+
const _Tp alpha = *std::max_element(src, src + length);
34+
_Tp denominator{0};
3535

36-
for (int i = 0; i < length; ++i) {
37-
dst[i] = fast_exp(src[i] - alpha);
38-
denominator += dst[i];
39-
}
36+
for (int i = 0; i < length; ++i) {
37+
dst[i] = fast_exp(src[i] - alpha);
38+
denominator += dst[i];
39+
}
4040

41-
for (int i = 0; i < length; ++i) {
42-
dst[i] /= denominator;
43-
}
41+
for (int i = 0; i < length; ++i) {
42+
dst[i] /= denominator;
43+
}
4444

45-
return 0;
45+
return 0;
4646
}
4747

4848
// PicoDet decode
49-
PaddleDetection::ObjectResult disPred2Bbox(const float *&dfl_det, int label, float score,
50-
int x, int y, int stride, std::vector<float> im_shape,
51-
int reg_max) {
52-
float ct_x = (x + 0.5) * stride;
53-
float ct_y = (y + 0.5) * stride;
54-
std::vector<float> dis_pred;
55-
dis_pred.resize(4);
56-
for (int i = 0; i < 4; i++) {
57-
float dis = 0;
58-
float* dis_after_sm = new float[reg_max + 1];
59-
activation_function_softmax(dfl_det + i * (reg_max + 1), dis_after_sm, reg_max + 1);
60-
for (int j = 0; j < reg_max + 1; j++) {
61-
dis += j * dis_after_sm[j];
62-
}
63-
dis *= stride;
64-
dis_pred[i] = dis;
65-
delete[] dis_after_sm;
49+
PaddleDetection::ObjectResult
50+
disPred2Bbox(const float *&dfl_det, int label, float score, int x, int y,
51+
int stride, std::vector<float> im_shape, int reg_max) {
52+
float ct_x = (x + 0.5) * stride;
53+
float ct_y = (y + 0.5) * stride;
54+
std::vector<float> dis_pred;
55+
dis_pred.resize(4);
56+
for (int i = 0; i < 4; i++) {
57+
float dis = 0;
58+
float *dis_after_sm = new float[reg_max + 1];
59+
activation_function_softmax(dfl_det + i * (reg_max + 1), dis_after_sm,
60+
reg_max + 1);
61+
for (int j = 0; j < reg_max + 1; j++) {
62+
dis += j * dis_after_sm[j];
6663
}
67-
int xmin = (int)(std::max)(ct_x - dis_pred[0], .0f);
68-
int ymin = (int)(std::max)(ct_y - dis_pred[1], .0f);
69-
int xmax = (int)(std::min)(ct_x + dis_pred[2], (float)im_shape[0]);
70-
int ymax = (int)(std::min)(ct_y + dis_pred[3], (float)im_shape[1]);
64+
dis *= stride;
65+
dis_pred[i] = dis;
66+
delete[] dis_after_sm;
67+
}
68+
int xmin = (int)(std::max)(ct_x - dis_pred[0], .0f);
69+
int ymin = (int)(std::max)(ct_y - dis_pred[1], .0f);
70+
int xmax = (int)(std::min)(ct_x + dis_pred[2], (float)im_shape[0]);
71+
int ymax = (int)(std::min)(ct_y + dis_pred[3], (float)im_shape[1]);
7172

72-
PaddleDetection::ObjectResult result_item;
73-
result_item.rect = {xmin, ymin, xmax, ymax};
74-
result_item.class_id = label;
75-
result_item.confidence = score;
73+
PaddleDetection::ObjectResult result_item;
74+
result_item.rect = {xmin, ymin, xmax, ymax};
75+
result_item.class_id = label;
76+
result_item.confidence = score;
7677

77-
return result_item;
78+
return result_item;
7879
}
7980

80-
81-
void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult>* results,
82-
std::vector<const float *> outs,
83-
std::vector<int> fpn_stride,
84-
std::vector<float> im_shape,
85-
std::vector<float> scale_factor,
86-
float score_threshold,
87-
float nms_threshold,
88-
int num_class,
89-
int reg_max) {
81+
void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult> *results,
82+
std::vector<const float *> outs,
83+
std::vector<int> fpn_stride,
84+
std::vector<float> im_shape,
85+
std::vector<float> scale_factor, float score_threshold,
86+
float nms_threshold, int num_class, int reg_max) {
9087
std::vector<std::vector<PaddleDetection::ObjectResult>> bbox_results;
9188
bbox_results.resize(num_class);
9289
int in_h = im_shape[0], in_w = im_shape[1];
9390
for (int i = 0; i < fpn_stride.size(); ++i) {
94-
int feature_h = in_h / fpn_stride[i];
95-
int feature_w = in_w / fpn_stride[i];
91+
int feature_h = std::ceil((float)in_h / fpn_stride[i]);
92+
int feature_w = std::ceil((float)in_w / fpn_stride[i]);
9693
for (int idx = 0; idx < feature_h * feature_w; idx++) {
9794
const float *scores = outs[i] + (idx * num_class);
9895

@@ -107,24 +104,25 @@ void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult>* results,
107104
}
108105
}
109106
if (score > score_threshold) {
110-
const float *bbox_pred = outs[i + fpn_stride.size()]
111-
+ (idx * 4 * (reg_max + 1));
112-
bbox_results[cur_label].push_back(disPred2Bbox(bbox_pred,
113-
cur_label, score, col, row, fpn_stride[i], im_shape, reg_max));
107+
const float *bbox_pred =
108+
outs[i + fpn_stride.size()] + (idx * 4 * (reg_max + 1));
109+
bbox_results[cur_label].push_back(
110+
disPred2Bbox(bbox_pred, cur_label, score, col, row, fpn_stride[i],
111+
im_shape, reg_max));
114112
}
115113
}
116114
}
117115
for (int i = 0; i < (int)bbox_results.size(); i++) {
118116
PaddleDetection::nms(bbox_results[i], nms_threshold);
119117

120118
for (auto box : bbox_results[i]) {
121-
box.rect[0] = box.rect[0] / scale_factor[1];
122-
box.rect[2] = box.rect[2] / scale_factor[1];
123-
box.rect[1] = box.rect[1] / scale_factor[0];
124-
box.rect[3] = box.rect[3] / scale_factor[0];
125-
results->push_back(box);
119+
box.rect[0] = box.rect[0] / scale_factor[1];
120+
box.rect[2] = box.rect[2] / scale_factor[1];
121+
box.rect[1] = box.rect[1] / scale_factor[0];
122+
box.rect[3] = box.rect[3] / scale_factor[0];
123+
results->push_back(box);
126124
}
127125
}
128126
}
129127

130-
} // namespace PaddleDetection
128+
} // namespace PaddleDetection

deploy/lite/src/picodet_postprocess.cc

+62-64
Original file line numberDiff line numberDiff line change
@@ -20,79 +20,76 @@
2020
namespace PaddleDetection {
2121

2222
float fast_exp(float x) {
23-
union {
24-
uint32_t i;
25-
float f;
26-
} v{};
27-
v.i = (1 << 23) * (1.4426950409 * x + 126.93490512f);
28-
return v.f;
23+
union {
24+
uint32_t i;
25+
float f;
26+
} v{};
27+
v.i = (1 << 23) * (1.4426950409 * x + 126.93490512f);
28+
return v.f;
2929
}
3030

3131
template <typename _Tp>
3232
int activation_function_softmax(const _Tp *src, _Tp *dst, int length) {
33-
const _Tp alpha = *std::max_element(src, src + length);
34-
_Tp denominator{0};
33+
const _Tp alpha = *std::max_element(src, src + length);
34+
_Tp denominator{0};
3535

36-
for (int i = 0; i < length; ++i) {
37-
dst[i] = fast_exp(src[i] - alpha);
38-
denominator += dst[i];
39-
}
36+
for (int i = 0; i < length; ++i) {
37+
dst[i] = fast_exp(src[i] - alpha);
38+
denominator += dst[i];
39+
}
4040

41-
for (int i = 0; i < length; ++i) {
42-
dst[i] /= denominator;
43-
}
41+
for (int i = 0; i < length; ++i) {
42+
dst[i] /= denominator;
43+
}
4444

45-
return 0;
45+
return 0;
4646
}
4747

4848
// PicoDet decode
49-
PaddleDetection::ObjectResult disPred2Bbox(const float *&dfl_det, int label, float score,
50-
int x, int y, int stride, std::vector<float> im_shape,
51-
int reg_max) {
52-
float ct_x = (x + 0.5) * stride;
53-
float ct_y = (y + 0.5) * stride;
54-
std::vector<float> dis_pred;
55-
dis_pred.resize(4);
56-
for (int i = 0; i < 4; i++) {
57-
float dis = 0;
58-
float* dis_after_sm = new float[reg_max + 1];
59-
activation_function_softmax(dfl_det + i * (reg_max + 1), dis_after_sm, reg_max + 1);
60-
for (int j = 0; j < reg_max + 1; j++) {
61-
dis += j * dis_after_sm[j];
62-
}
63-
dis *= stride;
64-
dis_pred[i] = dis;
65-
delete[] dis_after_sm;
49+
PaddleDetection::ObjectResult
50+
disPred2Bbox(const float *&dfl_det, int label, float score, int x, int y,
51+
int stride, std::vector<float> im_shape, int reg_max) {
52+
float ct_x = (x + 0.5) * stride;
53+
float ct_y = (y + 0.5) * stride;
54+
std::vector<float> dis_pred;
55+
dis_pred.resize(4);
56+
for (int i = 0; i < 4; i++) {
57+
float dis = 0;
58+
float *dis_after_sm = new float[reg_max + 1];
59+
activation_function_softmax(dfl_det + i * (reg_max + 1), dis_after_sm,
60+
reg_max + 1);
61+
for (int j = 0; j < reg_max + 1; j++) {
62+
dis += j * dis_after_sm[j];
6663
}
67-
int xmin = (int)(std::max)(ct_x - dis_pred[0], .0f);
68-
int ymin = (int)(std::max)(ct_y - dis_pred[1], .0f);
69-
int xmax = (int)(std::min)(ct_x + dis_pred[2], (float)im_shape[0]);
70-
int ymax = (int)(std::min)(ct_y + dis_pred[3], (float)im_shape[1]);
64+
dis *= stride;
65+
dis_pred[i] = dis;
66+
delete[] dis_after_sm;
67+
}
68+
int xmin = (int)(std::max)(ct_x - dis_pred[0], .0f);
69+
int ymin = (int)(std::max)(ct_y - dis_pred[1], .0f);
70+
int xmax = (int)(std::min)(ct_x + dis_pred[2], (float)im_shape[0]);
71+
int ymax = (int)(std::min)(ct_y + dis_pred[3], (float)im_shape[1]);
7172

72-
PaddleDetection::ObjectResult result_item;
73-
result_item.rect = {xmin, ymin, xmax, ymax};
74-
result_item.class_id = label;
75-
result_item.confidence = score;
73+
PaddleDetection::ObjectResult result_item;
74+
result_item.rect = {xmin, ymin, xmax, ymax};
75+
result_item.class_id = label;
76+
result_item.confidence = score;
7677

77-
return result_item;
78+
return result_item;
7879
}
7980

80-
81-
void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult>* results,
82-
std::vector<const float *> outs,
83-
std::vector<int> fpn_stride,
84-
std::vector<float> im_shape,
85-
std::vector<float> scale_factor,
86-
float score_threshold,
87-
float nms_threshold,
88-
int num_class,
89-
int reg_max) {
81+
void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult> *results,
82+
std::vector<const float *> outs,
83+
std::vector<int> fpn_stride,
84+
std::vector<float> im_shape,
85+
std::vector<float> scale_factor, float score_threshold,
86+
float nms_threshold, int num_class, int reg_max) {
9087
std::vector<std::vector<PaddleDetection::ObjectResult>> bbox_results;
9188
bbox_results.resize(num_class);
9289
int in_h = im_shape[0], in_w = im_shape[1];
9390
for (int i = 0; i < fpn_stride.size(); ++i) {
94-
int feature_h = in_h / fpn_stride[i];
95-
int feature_w = in_w / fpn_stride[i];
91+
int feature_h = ceil((float)in_h / fpn_stride[i]);
92+
int feature_w = ceil((float)in_w / fpn_stride[i]);
9693
for (int idx = 0; idx < feature_h * feature_w; idx++) {
9794
const float *scores = outs[i] + (idx * num_class);
9895

@@ -107,24 +104,25 @@ void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult>* results,
107104
}
108105
}
109106
if (score > score_threshold) {
110-
const float *bbox_pred = outs[i + fpn_stride.size()]
111-
+ (idx * 4 * (reg_max + 1));
112-
bbox_results[cur_label].push_back(disPred2Bbox(bbox_pred,
113-
cur_label, score, col, row, fpn_stride[i], im_shape, reg_max));
107+
const float *bbox_pred =
108+
outs[i + fpn_stride.size()] + (idx * 4 * (reg_max + 1));
109+
bbox_results[cur_label].push_back(
110+
disPred2Bbox(bbox_pred, cur_label, score, col, row, fpn_stride[i],
111+
im_shape, reg_max));
114112
}
115113
}
116114
}
117115
for (int i = 0; i < (int)bbox_results.size(); i++) {
118116
PaddleDetection::nms(bbox_results[i], nms_threshold);
119117

120118
for (auto box : bbox_results[i]) {
121-
box.rect[0] = box.rect[0] / scale_factor[1];
122-
box.rect[2] = box.rect[2] / scale_factor[1];
123-
box.rect[1] = box.rect[1] / scale_factor[0];
124-
box.rect[3] = box.rect[3] / scale_factor[0];
125-
results->push_back(box);
119+
box.rect[0] = box.rect[0] / scale_factor[1];
120+
box.rect[2] = box.rect[2] / scale_factor[1];
121+
box.rect[1] = box.rect[1] / scale_factor[0];
122+
box.rect[3] = box.rect[3] / scale_factor[0];
123+
results->push_back(box);
126124
}
127125
}
128126
}
129127

130-
} // namespace PaddleDetection
128+
} // namespace PaddleDetection

0 commit comments

Comments
 (0)