paddlex.cpp 9.0 KB

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