-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeshgrid.cpp
More file actions
211 lines (159 loc) · 6.04 KB
/
meshgrid.cpp
File metadata and controls
211 lines (159 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <cmath>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#define print(x) std::cout << x << std::endl
float sigmoid(float x)
{
return static_cast<float>(1.0f / (1.0f + std::exp(-x)));
}
int read_data(std::string fileName, int start_row, int end_row, std::vector<float>& data)
{
std::ifstream inFile(fileName, std::ios::in);
std::string line;
for (int i = 0; i < end_row; i++) {
inFile >> line;
if (i < start_row)
continue;
float value = std::stof(line);
data.push_back(value);
// std::cout << line << std::endl;
}
return 0;
}
int make_anchors(int height, int width, int stride, float offset,
std::vector<std::vector<float>>& anchors)
{
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
std::vector<float> anc(3);
anc[0] = (float)w + offset;
anc[1] = (float)h + offset;
anc[2] = stride;
anchors.push_back(anc);
}
}
return 0;
}
int post_proc()
{
// =================================================================
float conf_thres = 0.4;
int num_anchors = 12096;
int num_classes = 8;
int num_keypoints = 17;
int reg_max = 16;
int filter_size[6] = {72, 128, 36, 64, 18, 32};
int tensor_length = (reg_max * 4 + num_classes) * num_anchors; // 870912
// =================================================================
int res;
std::vector<std::vector<float>> anchors = {};
res = make_anchors(filter_size[0], filter_size[1], 8, 0.5, anchors);
res = make_anchors(filter_size[2], filter_size[3], 16, 0.5, anchors);
res = make_anchors(filter_size[4], filter_size[5], 32, 0.5, anchors);
// for (auto anc: anchors) {
// std::cout << anc[0] << " " << anc[1] << " " << anc[2] << std::endl;
// }
print("anchors size: " << anchors.size());
// =================================================================
std::vector<float> tensor = {};
res = read_data("/home/ubt/Documents/algorithm/yolo/yolov8/outputTensor/v8s-1x72x12096.txt",
0, tensor_length, tensor);
std::cout << "src data: " << tensor.size() << std::endl;
// std::reverse(tensor.begin(), tensor.end());
// for (auto v: tensor) {
// print(v);
// }
// =================================================================
int ttl = 0;
// =================================================================
for (int m = 0; m < num_anchors; m++) {
int idx_max = -1;
float conf_max = 0.0;
std::vector<float> outputs = {};
std::vector<float> exp_total = {0.0, 0.0, 0.0, 0.0}; // [4] x 16
std::vector<int> exp_indices = {};
std::vector<int> conv_weight = {};
int cnt = reg_max, idx_exp = 0;
for (int k = 0; k < 4 * reg_max + num_classes; k++) { // 4 x 16 + 8 = 72
int idx = tensor_length - 1 - k * num_anchors - m;
// print(tensor[idx]);
if (k <= (num_classes - 1)) {
float conf = sigmoid(tensor[idx]);
if (conf > conf_max) {
conf_max = conf;
idx_max = (num_classes - 1) - k;
}
} else if (conf_max < conf_thres) {
break;
}
if (k > (num_classes - 1) && conf_max > conf_thres) {
float data = std::exp(tensor[idx]);
outputs.emplace_back(data);
exp_indices.emplace_back(idx_exp);
conv_weight.emplace_back(cnt - 1);
exp_total[idx_exp] += data;
cnt -= 1;
if (cnt == 0) {
cnt = reg_max;
idx_exp += 1;
}
// exp_total[idx_exp] += data;
// idx_exp += 1;
// if (idx_exp > 3) {
// idx_exp = 0;
// cnt -= 1;
// }
}
}
if (conf_max > conf_thres) {
std::vector<float> coords = {};
for (int i = 0; i < 4; i++) {
float out = 0.0f;
for (int p = 0; p < reg_max; p++) {
int idx = i * reg_max + p;
// [!] Softmax
outputs[idx] = outputs[idx] / exp_total[exp_indices[idx]];
// [!] Conv 1x1
out += outputs[idx] * (float)conv_weight[idx];
}
coords.emplace_back(out);
}
// for (int i = 0; i < 4; i++) {
// std::vector<float> outs = {0.0f, 0.0f, 0.0f, 0.0f};
// for (int p = 0; p < reg_max; p++) {
// int idx = i * reg_max + p;
// // [!] Softmax
// outputs[idx] = outputs[idx] / exp_total[exp_indices[idx]];
// // [!] Conv 1x1
// outs[exp_indices[idx]] += outputs[idx] * (float)conv_weight[idx];
// }
// coords = outs;
// }
coords[0] = (anchors[num_anchors - 1 - m][1] + coords[0]) * anchors[num_anchors - 1 - m][2]; // y2
coords[1] = (anchors[num_anchors - 1 - m][1] + coords[1]) * anchors[num_anchors - 1 - m][2]; // x2
coords[2] = (anchors[num_anchors - 1 - m][0] - coords[2]) * anchors[num_anchors - 1 - m][2]; // y1
coords[3] = (anchors[num_anchors - 1 - m][0] - coords[3]) * anchors[num_anchors - 1 - m][2]; // x1
std::reverse(coords.begin(), coords.end());
for (auto c: coords) {
print(c);
}
print("====");
ttl += 1;
// break;
}
// if (m >= 0)
// break;
}
print("before nms: " << ttl);
// =================================================================
// =================================================================
return 0;
}
int main()
{
int res = post_proc();
return 0;
}