output_struct.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <algorithm>
  16. #include <iostream>
  17. #include <ostream>
  18. #include <map>
  19. #include <string>
  20. #include <utility>
  21. #include <vector>
  22. #include <cassert>
  23. #include <functional>
  24. #include <numeric>
  25. #include <iomanip>
  26. namespace PaddleDeploy {
  27. enum Dtype {FLOAT32, INT64, INT32, INT8};
  28. struct DataBlob {
  29. // data
  30. std::vector<char> data;
  31. // data name
  32. std::string name;
  33. // data shape
  34. std::vector<int> shape;
  35. /*
  36. data dtype
  37. 0: FLOAT32
  38. 1: INT64
  39. 2: INT32
  40. 3: UINT8
  41. */
  42. int dtype;
  43. // Lod Info
  44. std::vector<std::vector<size_t>> lod;
  45. DataBlob() {}
  46. explicit DataBlob(const std::string& blob_name) {
  47. name = blob_name;
  48. }
  49. void Resize(const std::vector<int>& new_shape, int data_type) {
  50. assert(data_type >= 0 && data_type < 4);
  51. int unit = 4;
  52. if (data_type == INT64) {
  53. unit = 8;
  54. } else if (data_type == INT8) {
  55. unit = 1;
  56. }
  57. int total_size = std::accumulate(new_shape.begin(),
  58. new_shape.end(), 1, std::multiplies<int>());
  59. data.resize(total_size * unit);
  60. shape.clear();
  61. shape.assign(new_shape.begin(), new_shape.end());
  62. dtype = data_type;
  63. }
  64. };
  65. struct ShapeInfo {
  66. std::vector<std::vector<int>> shapes;
  67. std::vector<std::string> transforms;
  68. bool has_batch_padding = false;
  69. void Insert(const std::string& name, int width, int height) {
  70. transforms.push_back(name);
  71. shapes.push_back({width, height});
  72. }
  73. };
  74. template<typename T>
  75. void cal_mean_std(const std::vector<T>& values, double* mean, double* std) {
  76. *mean = 0;
  77. *std = 0;
  78. for (auto i = 0; i < values.size(); ++i) {
  79. (*mean) += static_cast<double>(values[i]);
  80. }
  81. (*mean) = (*mean) / static_cast<double>(values.size());
  82. if (values.size() < 2) {
  83. *std = 0.0;
  84. } else {
  85. double tmp = 0.0;
  86. for (auto i = 0; i < values.size(); ++i) {
  87. tmp += (values[i] - (*mean)) * (values[i] - (*mean));
  88. }
  89. *std = tmp / (values.size() - 1);
  90. }
  91. }
  92. template <class T>
  93. struct Mask {
  94. // raw data of mask
  95. std::vector<T> data;
  96. // the shape of mask
  97. std::vector<int> shape;
  98. void Resize(const std::vector<int>& new_shape) {
  99. int total_size = std::accumulate(new_shape.begin(),
  100. new_shape.end(), 1, std::multiplies<int>());
  101. data.resize(total_size);
  102. shape.clear();
  103. shape.assign(new_shape.begin(), new_shape.end());
  104. }
  105. void Clear() {
  106. data.clear();
  107. shape.clear();
  108. }
  109. friend std::ostream &operator<<(std::ostream & stream, const Mask<T>& mask) {
  110. double m;
  111. double d;
  112. cal_mean_std<T>(mask.data, &m, &d);
  113. stream << "Mask(mean:\t" << m << "\tstd:\t" << d << ")";
  114. return stream;
  115. }
  116. };
  117. struct Box {
  118. int category_id;
  119. // category label this box belongs to
  120. std::string category;
  121. // confidence score
  122. float score;
  123. std::vector<float> coordinate;
  124. Mask<uint8_t> mask;
  125. friend std::ostream &operator<<(std::ostream & stream, const Box& b) {
  126. stream << "Box(" << b.category_id << "\t" << b.category << "\t" << b.score;
  127. for (auto i = 0; i < b.coordinate.size(); ++i) {
  128. stream << "\t" << b.coordinate[i];
  129. }
  130. stream << ")";
  131. if (b.mask.data.size() != 0) {
  132. stream << "\n" << b.mask;
  133. for (auto shape : b.mask.shape) {
  134. stream << "\t" << shape;
  135. }
  136. }
  137. return stream;
  138. }
  139. };
  140. struct ClasResult {
  141. // target boxes
  142. int category_id;
  143. std::string category;
  144. double score;
  145. friend std::ostream &operator<<(std::ostream & stream, const ClasResult& c) {
  146. stream << "Classify(" << c.category_id << "\t"
  147. << c.category << "\t" << c.score << ")";
  148. return stream;
  149. }
  150. };
  151. struct DetResult {
  152. // target boxes
  153. std::vector<Box> boxes;
  154. int mask_resolution;
  155. void clear() {
  156. boxes.clear();
  157. }
  158. friend std::ostream &operator<<(std::ostream & stream, const DetResult& d) {
  159. for (auto i = 0; i < d.boxes.size(); ++i) {
  160. stream << d.boxes[i];
  161. if (i != d.boxes.size() - 1) {
  162. stream << "\n";
  163. }
  164. }
  165. return stream;
  166. }
  167. };
  168. struct OcrResult {
  169. std::vector<std::vector<std::vector<int>>> boxes;
  170. float cls_score;
  171. float crnn_score;
  172. int label;
  173. std::vector<std::string> str_res;
  174. };
  175. struct SegResult {
  176. // represent label of each pixel on image matrix
  177. Mask<uint8_t> label_map;
  178. // represent score of each pixel on image matrix
  179. Mask<float> score_map;
  180. friend std::ostream &operator<<(std::ostream & stream, const SegResult& s) {
  181. stream << "Score" << s.score_map << "\tLabel" << s.label_map;
  182. return stream;
  183. }
  184. };
  185. struct Result {
  186. std::string model_type;
  187. union {
  188. ClasResult* clas_result;
  189. DetResult* det_result;
  190. SegResult* seg_result;
  191. OcrResult* ocr_result;
  192. };
  193. Result() {
  194. clas_result = nullptr;
  195. }
  196. explicit Result(std::string result_type) {
  197. model_type = result_type;
  198. clas_result = nullptr;
  199. }
  200. friend std::ostream &operator<<(std::ostream & stream, const Result& r) {
  201. std::cout.setf(std::ios::fixed);
  202. std::cout << std::setprecision(8);
  203. if ("det" == r.model_type) {
  204. if (nullptr == r.det_result) {
  205. stream << "det_result is not initialized";
  206. } else {
  207. stream << *(r.det_result);
  208. }
  209. } else if ("clas" == r.model_type) {
  210. if (nullptr == r.clas_result) {
  211. stream << "clas_result is not initialized";
  212. } else {
  213. stream << *(r.clas_result);
  214. }
  215. } else if ("seg" == r.model_type) {
  216. stream << *(r.seg_result);
  217. } else {
  218. stream << "Result is not support to print";
  219. }
  220. return stream;
  221. }
  222. void Clear() {
  223. if ("det" == model_type) {
  224. delete det_result;
  225. det_result = NULL;
  226. } else if ("seg" == model_type) {
  227. delete seg_result;
  228. seg_result = NULL;
  229. } else if ("clas" == model_type) {
  230. delete clas_result;
  231. clas_result = NULL;
  232. } else if ("ocr" == model_type) {
  233. delete ocr_result;
  234. ocr_result = NULL;
  235. }
  236. }
  237. Result(const Result& result) {
  238. Clear();
  239. model_type = result.model_type;
  240. if ("det" == model_type) {
  241. det_result = new DetResult();
  242. *det_result = *(result.det_result);
  243. } else if ("seg" == model_type) {
  244. seg_result = new SegResult();
  245. *seg_result = *(result.seg_result);
  246. } else if ("clas" == model_type) {
  247. clas_result = new ClasResult();
  248. *clas_result = *(result.clas_result);
  249. } else if ("ocr" == model_type) {
  250. ocr_result = new OcrResult();
  251. *ocr_result = *(result.ocr_result);
  252. }
  253. }
  254. Result& operator=(const Result& result) {
  255. Clear();
  256. model_type = result.model_type;
  257. if ("det" == model_type) {
  258. det_result = new DetResult();
  259. *det_result = *(result.det_result);
  260. } else if ("seg" == model_type) {
  261. seg_result = new SegResult();
  262. *seg_result = *(result.seg_result);
  263. } else if ("clas" == model_type) {
  264. clas_result = new ClasResult();
  265. *clas_result = *(result.clas_result);
  266. } else if ("ocr" == model_type) {
  267. ocr_result = new OcrResult();
  268. *ocr_result = *(result.ocr_result);
  269. }
  270. return *this;
  271. }
  272. ~Result() {
  273. Clear();
  274. }
  275. };
  276. } // namespace PaddleDeploy