20
20
namespace PaddleDetection {
21
21
22
22
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 ;
29
29
}
30
30
31
31
template <typename _Tp>
32
32
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 };
35
35
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
+ }
40
40
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
+ }
44
44
45
- return 0 ;
45
+ return 0 ;
46
46
}
47
47
48
48
// 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];
66
63
}
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 ]);
71
72
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;
76
77
77
- return result_item;
78
+ return result_item;
78
79
}
79
80
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) {
90
87
std::vector<std::vector<PaddleDetection::ObjectResult>> bbox_results;
91
88
bbox_results.resize (num_class);
92
89
int in_h = im_shape[0 ], in_w = im_shape[1 ];
93
90
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]) ;
96
93
for (int idx = 0 ; idx < feature_h * feature_w; idx++) {
97
94
const float *scores = outs[i] + (idx * num_class);
98
95
@@ -107,24 +104,25 @@ void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult>* results,
107
104
}
108
105
}
109
106
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));
114
112
}
115
113
}
116
114
}
117
115
for (int i = 0 ; i < (int )bbox_results.size (); i++) {
118
116
PaddleDetection::nms (bbox_results[i], nms_threshold);
119
117
120
118
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);
126
124
}
127
125
}
128
126
}
129
127
130
- } // namespace PaddleDetection
128
+ } // namespace PaddleDetection
0 commit comments