Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

inference.cpp 10 KB

You have to be logged in to leave a comment. Sign In
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  1. // Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. #include "inference.hpp"
  3. #include <immintrin.h>
  4. #include <iostream>
  5. #include <cstdint>
  6. #include <string>
  7. #include <vector>
  8. #include <cmath>
  9. #define IOU_THRESHOLD 0.45
  10. uint16_t float32_to_float16(float value) {
  11. __m128 input = _mm_set_ss(value);
  12. return _mm_cvtsi128_si32(_mm_cvtps_ph(input, 0));
  13. }
  14. float float16_to_float32(uint16_t half) {
  15. uint16_t sign = (half & 0x8000) >> 15;
  16. uint16_t exponent = (half & 0x7C00) >> 10;
  17. uint16_t mantissa = (half & 0x03FF);
  18. if (exponent == 0) {
  19. if (mantissa == 0) {
  20. return sign ? -0.0f : 0.0f;
  21. }
  22. return std::ldexp(mantissa / 1024.0f, -14) * (sign ? -1.0f : 1.0f);
  23. }
  24. else if (exponent == 31) {
  25. return mantissa ? NAN : (sign ? -INFINITY : INFINITY);
  26. }
  27. float real_value = std::ldexp(1.0f + mantissa / 1024.0f, exponent - 15);
  28. return sign ? -real_value : real_value;
  29. }
  30. void Image::preprocess(cv::Mat* img, std::vector<uint16_t>& triton_data, int input_w, int input_h)
  31. {
  32. int w, h, x, y;
  33. float r_w = input_w / (img->cols*1.0);
  34. float r_h = input_h / (img->rows*1.0);
  35. if (r_h > r_w) {
  36. w = input_w;
  37. h = r_w * img->rows;
  38. x = 0;
  39. y = (input_h - h) / 2;
  40. } else {
  41. w = r_h * img->cols;
  42. h = input_h;
  43. x = (input_w - w) / 2;
  44. y = 0;
  45. }
  46. cv::Mat re(h, w, CV_8UC3);
  47. cv::resize(*img, re, re.size(), 0, 0, cv::INTER_LINEAR);
  48. cv::Mat out(input_h, input_w, CV_8UC3, cv::Scalar(114, 114, 114));
  49. re.copyTo(out(cv::Rect(x, y, re.cols, re.rows)));
  50. cv::cvtColor(out, out, cv::COLOR_BGR2RGB);
  51. unsigned char* data = (unsigned char*)out.data;
  52. int step = out.step;
  53. for (int yy = 0; yy < input_h; ++yy)
  54. {
  55. for (int kk = 0; kk < 3; ++kk)
  56. {
  57. for (int xx = 0; xx < input_w; ++xx)
  58. {
  59. float temp_f = data[yy * step + xx * 3 + kk] / 255.0f;
  60. triton_data[kk * input_w * input_h + yy * input_w + xx] = float32_to_float16(temp_f);
  61. }
  62. }
  63. }
  64. }
  65. int getDetectionsFromTritonRawData(std::vector<float>& detection_results, std::vector<struct detection_struct> &detections, std::vector<std::string>& object_class_list, float confidence_threshold, int image_width, int image_height)
  66. {
  67. const size_t shape[3] = {1, object_class_list.size()+4, 8400};
  68. std::vector<BoundingBox> boxes;
  69. for (size_t i = 0; i < shape[2]; i++)
  70. {
  71. int x = int(detection_results[0 * shape[2] + i]);
  72. int y = int(detection_results[1 * shape[2] + i]);
  73. int w = int(detection_results[2 * shape[2] + i]);
  74. int h = int(detection_results[3 * shape[2] + i]);
  75. for(size_t j =0 ; j < object_class_list.size(); j++)
  76. {
  77. if (detection_results[(4+j) * shape[2] + i] > 0.01)
  78. {
  79. BoundingBox box;
  80. box.x = static_cast<float>(x);
  81. box.y = static_cast<float>(y);
  82. box.w = static_cast<float>(w);
  83. box.h = static_cast<float>(h);
  84. box.score = detection_results[(4 + j) * shape[2] + i];
  85. box.class_id = j;
  86. boxes.push_back(box);
  87. }
  88. }
  89. }
  90. auto nms_boxes = NMS(boxes, IOU_THRESHOLD);
  91. detections.clear();
  92. if(nms_boxes.size()==0)
  93. {
  94. return 0;
  95. }
  96. float scale_x = 0.0;
  97. float scale_y = 0.0;
  98. int x1,y1 = 0;
  99. int x2,y2 = 0;
  100. float shift_factor_x = 0.6;
  101. float shift_factor_y = 0.5;
  102. int offset_shift = (image_width/640.0f)*10;
  103. if (image_width<=640)
  104. {
  105. scale_x = static_cast<float>(image_width - 640.0f ) * 0.5 ;
  106. scale_y = static_cast<float>(image_height - 640.0f) * 0.5 ;
  107. }
  108. for (size_t i = 0; i < nms_boxes.size(); ++i)
  109. {
  110. if (nms_boxes[i].score< confidence_threshold)
  111. {
  112. continue;
  113. }
  114. struct detection_struct tespit_yapi ;
  115. detections.push_back(tespit_yapi);
  116. detections[detections.size() - 1].confidence_score = nms_boxes[i].score;
  117. if (image_width==640)
  118. {
  119. scale_x = static_cast<float>(image_width - 640.0f ) * 0.5 ;
  120. scale_y = static_cast<float>(image_height - 640.0f) * 0.5 ;
  121. x1 = static_cast<int>((nms_boxes[i].x - nms_boxes[i].w/2) + scale_x);
  122. y1 = static_cast<int>((nms_boxes[i].y - nms_boxes[i].h/2) + scale_y) ;
  123. x2 = static_cast<int>((nms_boxes[i].x + nms_boxes[i].w/2) + scale_x);
  124. y2 = static_cast<int>((nms_boxes[i].y + nms_boxes[i].h/2) + scale_y);
  125. }
  126. else if(image_width>=1080)
  127. {
  128. x1 = static_cast<int>((nms_boxes[i].x - nms_boxes[i].w/2) * (image_width/640) );
  129. y1 = static_cast<int>((nms_boxes[i].y - nms_boxes[i].h/2) * (image_width/640) - ((image_width - image_height) / 2.0)) ;
  130. x2 = static_cast<int>((nms_boxes[i].x + nms_boxes[i].w/2) * (image_width/640) );
  131. y2 = static_cast<int>((nms_boxes[i].y + nms_boxes[i].h/2) * (image_width/640)- ((image_width - image_height) / 2.0));
  132. }
  133. float x_center, y_center, width, height;
  134. x_center = (x1 + x2) / 2.0f;
  135. y_center = (y1 + y2) / 2.0f;
  136. width = x2 - x1;
  137. height = y2 - y1;
  138. detections[detections.size() - 1].bbox.x = x_center - width/2 ;
  139. detections[detections.size() - 1].bbox.y = y_center - height/2;
  140. detections[detections.size() - 1].bbox.width = width ;
  141. detections[detections.size() - 1].bbox.height = height;
  142. if (detections[detections.size() - 1].bbox.x <= 0)
  143. detections[detections.size() - 1].bbox.x = offset_shift;
  144. if (detections[detections.size() - 1].bbox.y <= 0)
  145. detections[detections.size() - 1].bbox.y = offset_shift;
  146. if (detections[detections.size() - 1].bbox.x + detections[detections.size() - 1].bbox.width >= image_width)
  147. {
  148. detections[detections.size() - 1].bbox.width -= detections[detections.size() - 1].bbox.x + detections[detections.size() - 1].bbox.width - image_width + offset_shift ;
  149. }
  150. if (detections[detections.size() - 1].bbox.y + detections[detections.size() - 1].bbox.height >= image_height)
  151. {
  152. detections[detections.size() - 1].bbox.height -= detections[detections.size() - 1].bbox.y + detections[detections.size() - 1].bbox.height - image_height + offset_shift ;
  153. }
  154. detections[detections.size() - 1].name = object_class_list[nms_boxes[i].class_id];
  155. detections[detections.size() - 1].class_id = nms_boxes[i].class_id;
  156. }
  157. return 0;
  158. }
  159. float IoU(const BoundingBox& box1, const BoundingBox& box2) {
  160. float x1_min = box1.x - box1.w / 2.0f;
  161. float y1_min = box1.y - box1.h / 2.0f;
  162. float x1_max = box1.x + box1.w / 2.0f;
  163. float y1_max = box1.y + box1.h / 2.0f;
  164. float x2_min = box2.x - box2.w / 2.0f;
  165. float y2_min = box2.y - box2.h / 2.0f;
  166. float x2_max = box2.x + box2.w / 2.0f;
  167. float y2_max = box2.y + box2.h / 2.0f;
  168. float inter_x_min = std::max(x1_min, x2_min);
  169. float inter_y_min = std::max(y1_min, y2_min);
  170. float inter_x_max = std::min(x1_max, x2_max);
  171. float inter_y_max = std::min(y1_max, y2_max);
  172. float inter_width = inter_x_max - inter_x_min;
  173. float inter_height = inter_y_max - inter_y_min;
  174. if (inter_width <= 0 || inter_height <= 0)
  175. return 0.0f;
  176. float inter_area = inter_width * inter_height;
  177. float area1 = (x1_max - x1_min) * (y1_max - y1_min);
  178. float area2 = (x2_max - x2_min) * (y2_max - y2_min);
  179. float union_area = area1 + area2 - inter_area;
  180. return inter_area / union_area;
  181. }
  182. std::vector<BoundingBox> NMS(const std::vector<BoundingBox>& boxes, float iou_threshold) {
  183. std::vector<BoundingBox> result;
  184. std::vector<BoundingBox> sorted_boxes = boxes;
  185. std::sort(sorted_boxes.begin(), sorted_boxes.end(), [](const BoundingBox& a, const BoundingBox& b) {
  186. return a.score > b.score;
  187. });
  188. std::vector<bool> suppressed(sorted_boxes.size(), false);
  189. for (size_t i = 0; i < sorted_boxes.size(); ++i) {
  190. if (suppressed[i])
  191. continue;
  192. result.push_back(sorted_boxes[i]);
  193. for (size_t j = i + 1; j < sorted_boxes.size(); ++j) {
  194. if (suppressed[j])
  195. continue;
  196. if (IoU(sorted_boxes[i], sorted_boxes[j]) > iou_threshold) {
  197. suppressed[j] = true;
  198. }
  199. }
  200. }
  201. return result;
  202. }
  203. TritonCommunication::TritonCommunication(std::string triton_address, std::string model_name, std::string model_version, int image_channel, int image_width, int image_height, int class_count) : options(model_name)
  204. {
  205. triton::client::Error err;
  206. this->triton_url = triton_address;
  207. this->options.model_version_ = model_version;
  208. this->shape = {1, image_channel, image_width, image_height};
  209. this->input_byte_size = image_channel * image_width * image_height * sizeof(uint16_t) ;
  210. this->output_byte_size = (class_count + 4) * 8400 * sizeof(uint16_t);
  211. err = tc::InferenceServerGrpcClient::Create(&(this->client), this->triton_url);
  212. if (!err.IsOk()) {
  213. std::cout<< "Create grpc client error:"<<err.Message()<<std::endl;
  214. }
  215. bool live;
  216. err = client->IsServerReady(&live);
  217. if (!err.IsOk() || !live) {
  218. std::cout<< "Triton server is not live !"<<std::endl;
  219. exit(-1);
  220. }
  221. bool model_ready;
  222. err = client->IsModelReady(&model_ready,model_name,model_version);
  223. if (!err.IsOk() || !model_ready) {
  224. std::cerr << "Model:[" << model_name << "] has not been deployed on Triton Server. Triton Server Address:["<<triton_address <<"]"<<std::endl;
  225. exit(-1);
  226. }
  227. std::cout<<"Triton server is LIVE and model is READY!"<<std::endl;
  228. }
  229. void TritonCommunication::infer(uint16_t* image_data)
  230. {
  231. size_t num_elements = output_byte_size / sizeof(uint16_t);
  232. tc::Error err;
  233. tc::InferInput* input0;
  234. err = tc::InferInput::Create(&input0, "images", shape, "FP16"); // FP16 is the data type of the input tensor.
  235. std::shared_ptr<tc::InferInput> input0_ptr;
  236. input0_ptr.reset(input0);
  237. err = input0_ptr->AppendRaw((const uint8_t*)image_data, this->input_byte_size);
  238. std::vector<tc::InferInput*> inputs = {input0_ptr.get()};
  239. tc::InferResult* results;
  240. err = client->Infer(&results, options, inputs);
  241. results_ptr.reset(results);
  242. float *output0_data;
  243. size_t output0_byte_size;
  244. std::vector<uint16_t> result_fp16_raw_data;
  245. results->RawData("output0", (const uint8_t**)&output0_data, &output0_byte_size); // output0 is a specific name for the output tensor.
  246. result_fp16_raw_data.resize(output0_byte_size/sizeof(uint16_t));
  247. std::memcpy(result_fp16_raw_data.data(), output0_data, output0_byte_size);
  248. std::vector<float> float32_data(num_elements);
  249. for (size_t i = 0; i < num_elements; i++) {
  250. float32_data[i] = float16_to_float32(result_fp16_raw_data[i]);
  251. }
  252. output_raw_data = float32_data;
  253. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...