|
18 | 18 |
|
19 | 19 | namespace {
|
20 | 20 |
|
21 |
| -void colArgMax(const cv::Mat& src, cv::Mat& dst_locs, cv::Mat& dst_values) { |
| 21 | +void colArgMax(const cv::Mat& src, cv::Mat& dst_locs, cv::Mat& dst_values, bool apply_softmax = false, float eps = 1e-6f) { |
22 | 22 | dst_locs = cv::Mat::zeros(src.rows, 1, CV_32S);
|
23 | 23 | dst_values = cv::Mat::zeros(src.rows, 1, CV_32F);
|
24 | 24 |
|
25 | 25 | for (int row = 0; row < src.rows; ++row) {
|
26 | 26 | const float* ptr_row = src.ptr<float>(row);
|
27 | 27 | int max_val_idx = 0;
|
28 |
| - dst_values.at<float>(row) = ptr_row[max_val_idx]; |
| 28 | + float max_val = ptr_row[0]; |
29 | 29 | for (int col = 1; col < src.cols; ++col) {
|
30 |
| - if (ptr_row[col] > ptr_row[max_val_idx]) { |
| 30 | + if (ptr_row[col] > max_val) { |
31 | 31 | max_val_idx = col;
|
32 | 32 | dst_locs.at<int>(row) = max_val_idx;
|
33 |
| - dst_values.at<float>(row) = ptr_row[col]; |
| 33 | + max_val = ptr_row[col]; |
34 | 34 | }
|
35 | 35 | }
|
36 |
| - } |
37 |
| -} |
38 |
| - |
39 |
| -cv::Mat softmax_row(const cv::Mat& src) { |
40 |
| - cv::Mat result = src.clone(); |
41 | 36 |
|
42 |
| - for (int row = 0; row < result.rows; ++row) { |
43 |
| - float* ptr_row = result.ptr<float>(row); |
44 |
| - float max_val = ptr_row[0]; |
45 |
| - for (int col = 1; col < result.cols; ++col) { |
46 |
| - if (ptr_row[col] > max_val) { |
47 |
| - max_val = ptr_row[col]; |
| 37 | + if (apply_softmax) { |
| 38 | + float sum = 0.0f; |
| 39 | + for (int col = 0; col < src.cols; ++col) { |
| 40 | + sum += exp(ptr_row[col] - max_val); |
48 | 41 | }
|
| 42 | + dst_values.at<float>(row) = exp(ptr_row[max_val_idx] - max_val) / (sum + eps); |
49 | 43 | }
|
50 |
| - float sum = 0.0f; |
51 |
| - for (int col = 0; col < result.cols; col++) { |
52 |
| - ptr_row[col] = exp(ptr_row[col] - max_val); |
53 |
| - sum += ptr_row[col]; |
54 |
| - } |
55 |
| - for (int col = 0; col < result.cols; ++col) { |
56 |
| - ptr_row[col] /= sum; |
| 44 | + else { |
| 45 | + dst_values.at<float>(row) = max_val; |
57 | 46 | }
|
58 | 47 | }
|
59 |
| - |
60 |
| - return result; |
61 | 48 | }
|
62 | 49 |
|
63 |
| -DetectedKeypoints decode_simcc(const cv::Mat& simcc_x_input, |
64 |
| - const cv::Mat& simcc_y_input, |
| 50 | +DetectedKeypoints decode_simcc(const cv::Mat& simcc_x, |
| 51 | + const cv::Mat& simcc_y, |
65 | 52 | const cv::Point2f& extra_scale = cv::Point2f(1.f, 1.f),
|
66 |
| - float simcc_split_ratio = 2.0f, |
67 |
| - bool apply_softmax = false) { |
68 |
| - cv::Mat simcc_x = simcc_x_input; |
69 |
| - cv::Mat simcc_y = simcc_y_input; |
70 |
| - |
71 |
| - if (apply_softmax) { |
72 |
| - simcc_x = softmax_row(simcc_x); |
73 |
| - simcc_x = softmax_row(simcc_y); |
74 |
| - } |
75 |
| - |
| 53 | + bool apply_softmax = false, |
| 54 | + float simcc_split_ratio = 2.0f |
| 55 | + ) { |
76 | 56 | cv::Mat x_locs, max_val_x;
|
77 |
| - colArgMax(simcc_x, x_locs, max_val_x); |
| 57 | + colArgMax(simcc_x, x_locs, max_val_x, apply_softmax); |
78 | 58 |
|
79 | 59 | cv::Mat y_locs, max_val_y;
|
80 |
| - colArgMax(simcc_y, y_locs, max_val_y); |
| 60 | + colArgMax(simcc_y, y_locs, max_val_y, apply_softmax); |
81 | 61 |
|
82 | 62 | std::vector<cv::Point2f> keypoints(x_locs.rows);
|
83 | 63 | cv::Mat scores = cv::Mat::zeros(x_locs.rows, 1, CV_32F);
|
|
0 commit comments