paddlex.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include "include/paddlex/paddlex.h"
  15. #include <iostream>
  16. #include <fstream>
  17. using namespace paddle::lite_api;
  18. namespace PaddleX {
  19. void Model::create_predictor(const std::string& model_dir,
  20. const std::string& cfg_dir,
  21. int thread_num) {
  22. MobileConfig config;
  23. config.set_model_from_file(model_dir);
  24. config.set_threads(thread_num);
  25. load_config(cfg_dir);
  26. predictor_ = CreatePaddlePredictor<MobileConfig>(config);
  27. }
  28. bool Model::load_config(const std::string& cfg_dir) {
  29. YAML::Node config = YAML::LoadFile(cfg_dir);
  30. type = config["_Attributes"]["model_type"].as<std::string>();
  31. name = config["Model"].as<std::string>();
  32. bool to_rgb = true;
  33. if (config["TransformsMode"].IsDefined()) {
  34. std::string mode = config["TransformsMode"].as<std::string>();
  35. if (mode == "BGR") {
  36. to_rgb = false;
  37. } else if (mode != "RGB") {
  38. std::cerr << "[Init] Only 'RGB' or 'BGR' is supported for TransformsMode"
  39. << std::endl;
  40. return false;
  41. }
  42. }
  43. // 构建数据处理流
  44. transforms_.Init(config["Transforms"], to_rgb);
  45. // 读入label lis
  46. for (const auto& item : config["_Attributes"]["labels"]) {
  47. int index = labels.size();
  48. labels[index] = item.as<std::string>();
  49. }
  50. return true;
  51. }
  52. bool Model::preprocess(cv::Mat* input_im, ImageBlob* inputs) {
  53. if (!transforms_.Run(input_im, inputs)) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool Model::predict(const cv::Mat& im, ClsResult* result) {
  59. inputs_.clear();
  60. if (type == "detector") {
  61. std::cerr << "Loading model is a 'detector', DetResult should be passed to "
  62. "function predict()!"
  63. << std::endl;
  64. return false;
  65. } else if (type == "segmenter") {
  66. std::cerr << "Loading model is a 'segmenter', SegResult should be passed "
  67. "to function predict()!"
  68. << std::endl;
  69. return false;
  70. }
  71. // 处理输入图像
  72. inputs_.input_tensor_ = std::move(predictor_->GetInput(0));
  73. cv::Mat im_clone = im.clone();
  74. if (!preprocess(&im_clone, &inputs_)) {
  75. std::cerr << "Preprocess failed!" << std::endl;
  76. return false;
  77. }
  78. ;
  79. predictor_->Run();
  80. std::unique_ptr<const Tensor> output_tensor(std::move(predictor_->GetOutput(0)));
  81. const float *outputs_data = output_tensor->mutable_data<float>();
  82. // 对模型输出结果进行后处理
  83. auto ptr = std::max_element(outputs_data, outputs_data+sizeof(outputs_data));
  84. result->category_id = std::distance(outputs_data, ptr);
  85. result->score = *ptr;
  86. result->category = labels[result->category_id];
  87. //for (int i=0;i<sizeof(outputs_data);i++){
  88. // std::cout << labels[i] << std::endl;
  89. // std::cout << outputs_[i] << std::endl;
  90. // }
  91. }
  92. bool Model::predict(const cv::Mat& im, DetResult* result) {
  93. inputs_.clear();
  94. result->clear();
  95. if (type == "classifier") {
  96. std::cerr << "Loading model is a 'classifier', ClsResult should be passed "
  97. "to function predict()!" << std::endl;
  98. return false;
  99. } else if (type == "segmenter") {
  100. std::cerr << "Loading model is a 'segmenter', SegResult should be passed "
  101. "to function predict()!" << std::endl;
  102. return false;
  103. }
  104. inputs_.input_tensor_ = std::move(predictor_->GetInput(0));
  105. cv::Mat im_clone = im.clone();
  106. if (!preprocess(&im_clone, &inputs_)) {
  107. std::cerr << "Preprocess failed!" << std::endl;
  108. return false;
  109. }
  110. int h = inputs_.new_im_size_[0];
  111. int w = inputs_.new_im_size_[1];
  112. if (name == "YOLOv3") {
  113. std::unique_ptr<Tensor> im_size_tensor(std::move(predictor_->GetInput(1)));
  114. const std::vector<int64_t> IM_SIZE_SHAPE = {1,2};
  115. im_size_tensor->Resize(IM_SIZE_SHAPE);
  116. auto *im_size_data = im_size_tensor->mutable_data<int>();
  117. memcpy(im_size_data, inputs_.ori_im_size_.data(), 1*2*sizeof(int));
  118. }
  119. predictor_->Run();
  120. auto output_names = predictor_->GetOutputNames();
  121. auto output_box_tensor = predictor_->GetTensor(output_names[0]);
  122. const float *output_box = output_box_tensor->mutable_data<float>();
  123. std::vector<int64_t> output_box_shape = output_box_tensor->shape();
  124. int size = 1;
  125. for (const auto& i : output_box_shape) {
  126. size *= i;
  127. }
  128. int num_boxes = size / 6;
  129. for (int i = 0; i < num_boxes; ++i) {
  130. Box box;
  131. box.category_id = static_cast<int>(round(output_box[i * 6]));
  132. box.category = labels[box.category_id];
  133. box.score = output_box[i * 6 + 1];
  134. float xmin = output_box[i * 6 + 2];
  135. float ymin = output_box[i * 6 + 3];
  136. float xmax = output_box[i * 6 + 4];
  137. float ymax = output_box[i * 6 + 5];
  138. float w = xmax - xmin + 1;
  139. float h = ymax - ymin + 1;
  140. box.coordinate = {xmin, ymin, w, h};
  141. result->boxes.push_back(std::move(box));
  142. }
  143. return true;
  144. }
  145. bool Model::predict(const cv::Mat& im, SegResult* result) {
  146. result->clear();
  147. inputs_.clear();
  148. if (type == "classifier") {
  149. std::cerr << "Loading model is a 'classifier', ClsResult should be passed "
  150. "to function predict()!" << std::endl;
  151. return false;
  152. } else if (type == "detector") {
  153. std::cerr << "Loading model is a 'detector', DetResult should be passed to "
  154. "function predict()!" << std::endl;
  155. return false;
  156. }
  157. inputs_.input_tensor_ = std::move(predictor_->GetInput(0));
  158. cv::Mat im_clone = im.clone();
  159. if (!preprocess(&im_clone, &inputs_)) {
  160. std::cerr << "Preprocess failed!" << std::endl;
  161. return false;
  162. }
  163. std::cout << "Preprocess is done" << std::endl;
  164. predictor_->Run();
  165. auto output_names = predictor_->GetOutputNames();
  166. auto output_label_tensor = predictor_->GetTensor(output_names[0]);
  167. std::cout << "output0" << output_names[0] << std::endl;
  168. std::cout << "output1" << output_names[1] << std::endl;
  169. const int64_t *label_data = output_label_tensor->mutable_data<int64_t>();
  170. std::vector<int64_t> output_label_shape = output_label_tensor->shape();
  171. int size = 1;
  172. for (const auto& i : output_label_shape) {
  173. size *= i;
  174. result->label_map.shape.push_back(i);
  175. }
  176. result->label_map.data.resize(size);
  177. memcpy(result->label_map.data.data(), label_data, size*sizeof(int64_t));
  178. auto output_score_tensor = predictor_->GetTensor(output_names[1]);
  179. const float *score_data = output_score_tensor->mutable_data<float>();
  180. std::vector<int64_t> output_score_shape = output_score_tensor->shape();
  181. size = 1;
  182. for (const auto& i : output_score_shape) {
  183. size *= i;
  184. result->score_map.shape.push_back(i);
  185. }
  186. result->score_map.data.resize(size);
  187. memcpy(result->score_map.data.data(), score_data, size*sizeof(float));
  188. std::vector<uint8_t> label_map(result->label_map.data.begin(),
  189. result->label_map.data.end());
  190. cv::Mat mask_label(result->label_map.shape[1],
  191. result->label_map.shape[2],
  192. CV_8UC1,
  193. label_map.data());
  194. cv::Mat mask_score(result->score_map.shape[2],
  195. result->score_map.shape[3],
  196. CV_32FC1,
  197. result->score_map.data.data());
  198. int idx = 1;
  199. int len_postprocess = inputs_.im_size_before_resize_.size();
  200. for (std::vector<std::string>::reverse_iterator iter =
  201. inputs_.reshape_order_.rbegin();
  202. iter != inputs_.reshape_order_.rend();
  203. ++iter) {
  204. if (*iter == "padding") {
  205. auto before_shape = inputs_.im_size_before_resize_[len_postprocess - idx];
  206. inputs_.im_size_before_resize_.pop_back();
  207. auto padding_w = before_shape[0];
  208. auto padding_h = before_shape[1];
  209. mask_label = mask_label(cv::Rect(0, 0, padding_h, padding_w));
  210. mask_score = mask_score(cv::Rect(0, 0, padding_h, padding_w));
  211. } else if (*iter == "resize") {
  212. auto before_shape = inputs_.im_size_before_resize_[len_postprocess - idx];
  213. inputs_.im_size_before_resize_.pop_back();
  214. auto resize_w = before_shape[0];
  215. auto resize_h = before_shape[1];
  216. cv::resize(mask_label,
  217. mask_label,
  218. cv::Size(resize_h, resize_w),
  219. 0,
  220. 0,
  221. cv::INTER_NEAREST);
  222. cv::resize(mask_score,
  223. mask_score,
  224. cv::Size(resize_h, resize_w),
  225. 0,
  226. 0,
  227. cv::INTER_LINEAR);
  228. }
  229. ++idx;
  230. }
  231. result->label_map.data.assign(mask_label.begin<uint8_t>(),
  232. mask_label.end<uint8_t>());
  233. result->label_map.shape = {mask_label.rows, mask_label.cols};
  234. result->score_map.data.assign(mask_score.begin<float>(),
  235. mask_score.end<float>());
  236. result->score_map.shape = {mask_score.rows, mask_score.cols};
  237. return true;
  238. }
  239. } // namespce of PaddleX