paddlex.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 <math.h>
  15. #include <omp.h>
  16. #include <algorithm>
  17. #include <fstream>
  18. #include <cstring>
  19. #include "include/paddlex/paddlex.h"
  20. #include <opencv2/core/core.hpp>
  21. #include <opencv2/highgui/highgui.hpp>
  22. #include <opencv2/imgproc/imgproc.hpp>
  23. namespace PaddleX {
  24. void Model::create_predictor(const std::string& model_dir,
  25. bool use_gpu,
  26. bool use_trt,
  27. bool use_mkl,
  28. int mkl_thread_num,
  29. int gpu_id,
  30. std::string key,
  31. bool use_ir_optim) {
  32. paddle::AnalysisConfig config;
  33. std::string model_file = model_dir + OS_PATH_SEP + "__model__";
  34. std::string params_file = model_dir + OS_PATH_SEP + "__params__";
  35. std::string yaml_file = model_dir + OS_PATH_SEP + "model.yml";
  36. std::string yaml_input = "";
  37. #ifdef WITH_ENCRYPTION
  38. if (key != "") {
  39. model_file = model_dir + OS_PATH_SEP + "__model__.encrypted";
  40. params_file = model_dir + OS_PATH_SEP + "__params__.encrypted";
  41. yaml_file = model_dir + OS_PATH_SEP + "model.yml.encrypted";
  42. paddle_security_load_model(
  43. &config, key.c_str(), model_file.c_str(), params_file.c_str());
  44. yaml_input = decrypt_file(yaml_file.c_str(), key.c_str());
  45. }
  46. #endif
  47. if (yaml_input == "") {
  48. // read yaml file
  49. std::ifstream yaml_fin(yaml_file);
  50. yaml_fin.seekg(0, std::ios::end);
  51. size_t yaml_file_size = yaml_fin.tellg();
  52. yaml_input.assign(yaml_file_size, ' ');
  53. yaml_fin.seekg(0);
  54. yaml_fin.read(&yaml_input[0], yaml_file_size);
  55. }
  56. // load yaml file
  57. if (!load_config(yaml_input)) {
  58. std::cerr << "Parse file 'model.yml' failed!" << std::endl;
  59. exit(-1);
  60. }
  61. if (key == "") {
  62. config.SetModel(model_file, params_file);
  63. }
  64. if (use_mkl && !use_gpu) {
  65. if (name != "HRNet" && name != "DeepLabv3p" && name != "PPYOLO") {
  66. config.EnableMKLDNN();
  67. config.SetCpuMathLibraryNumThreads(mkl_thread_num);
  68. } else {
  69. std::cerr << "HRNet/DeepLabv3p/PPYOLO are not supported "
  70. << "for the use of mkldnn" << std::endl;
  71. }
  72. }
  73. if (use_gpu) {
  74. config.EnableUseGpu(100, gpu_id);
  75. } else {
  76. config.DisableGpu();
  77. }
  78. config.SwitchUseFeedFetchOps(false);
  79. config.SwitchSpecifyInputNames(true);
  80. // enable graph Optim
  81. #if defined(__arm__) || defined(__aarch64__)
  82. config.SwitchIrOptim(false);
  83. #else
  84. config.SwitchIrOptim(use_ir_optim);
  85. #endif
  86. // enable Memory Optim
  87. config.EnableMemoryOptim();
  88. if (use_trt && use_gpu) {
  89. config.EnableTensorRtEngine(
  90. 1 << 20 /* workspace_size*/,
  91. 32 /* max_batch_size*/,
  92. 20 /* min_subgraph_size*/,
  93. paddle::AnalysisConfig::Precision::kFloat32 /* precision*/,
  94. true /* use_static*/,
  95. false /* use_calib_mode*/);
  96. }
  97. predictor_ = std::move(CreatePaddlePredictor(config));
  98. }
  99. bool Model::load_config(const std::string& yaml_input) {
  100. YAML::Node config = YAML::Load(yaml_input);
  101. type = config["_Attributes"]["model_type"].as<std::string>();
  102. name = config["Model"].as<std::string>();
  103. std::string version = config["version"].as<std::string>();
  104. if (version[0] == '0') {
  105. std::cerr << "[Init] Version of the loaded model is lower than 1.0.0, "
  106. << "deployment cannot be done, please refer to "
  107. << "https://github.com/PaddlePaddle/PaddleX/blob/develop/docs"
  108. << "/tutorials/deploy/upgrade_version.md "
  109. << "to transfer version." << std::endl;
  110. return false;
  111. }
  112. bool to_rgb = true;
  113. if (config["TransformsMode"].IsDefined()) {
  114. std::string mode = config["TransformsMode"].as<std::string>();
  115. if (mode == "BGR") {
  116. to_rgb = false;
  117. } else if (mode != "RGB") {
  118. std::cerr << "[Init] Only 'RGB' or 'BGR' is supported for TransformsMode"
  119. << std::endl;
  120. return false;
  121. }
  122. }
  123. // build data preprocess stream
  124. transforms_.Init(config["Transforms"], to_rgb);
  125. // read label list
  126. labels.clear();
  127. for (const auto& item : config["_Attributes"]["labels"]) {
  128. int index = labels.size();
  129. labels[index] = item.as<std::string>();
  130. }
  131. if (config["_init_params"]["input_channel"].IsDefined()) {
  132. input_channel_ = config["_init_params"]["input_channel"].as<int>();
  133. } else {
  134. input_channel_ = 3;
  135. }
  136. return true;
  137. }
  138. bool Model::preprocess(const cv::Mat& input_im, ImageBlob* blob) {
  139. cv::Mat im = input_im.clone();
  140. if (!transforms_.Run(&im, blob)) {
  141. return false;
  142. }
  143. return true;
  144. }
  145. // use openmp
  146. bool Model::preprocess(const std::vector<cv::Mat>& input_im_batch,
  147. std::vector<ImageBlob>* blob_batch,
  148. int thread_num) {
  149. int batch_size = input_im_batch.size();
  150. bool success = true;
  151. thread_num = std::min(thread_num, batch_size);
  152. #pragma omp parallel for num_threads(thread_num)
  153. for (int i = 0; i < input_im_batch.size(); ++i) {
  154. cv::Mat im = input_im_batch[i].clone();
  155. if (!transforms_.Run(&im, &(*blob_batch)[i])) {
  156. success = false;
  157. }
  158. }
  159. return success;
  160. }
  161. bool Model::predict(const cv::Mat& im, ClsResult* result) {
  162. inputs_.clear();
  163. if (type == "detector") {
  164. std::cerr << "Loading model is a 'detector', DetResult should be passed to "
  165. "function predict()!"
  166. "to function predict()!" << std::endl;
  167. return false;
  168. }
  169. // im preprocess
  170. if (!preprocess(im, &inputs_)) {
  171. std::cerr << "Preprocess failed!" << std::endl;
  172. return false;
  173. }
  174. // predict
  175. auto in_tensor = predictor_->GetInputTensor("image");
  176. int h = inputs_.new_im_size_[0];
  177. int w = inputs_.new_im_size_[1];
  178. in_tensor->Reshape({1, input_channel_, h, w});
  179. in_tensor->copy_from_cpu(inputs_.im_data_.data());
  180. predictor_->ZeroCopyRun();
  181. // get result
  182. auto output_names = predictor_->GetOutputNames();
  183. auto output_tensor = predictor_->GetOutputTensor(output_names[0]);
  184. std::vector<int> output_shape = output_tensor->shape();
  185. int size = 1;
  186. for (const auto& i : output_shape) {
  187. size *= i;
  188. }
  189. outputs_.resize(size);
  190. output_tensor->copy_to_cpu(outputs_.data());
  191. // postprocess
  192. auto ptr = std::max_element(std::begin(outputs_), std::end(outputs_));
  193. result->category_id = std::distance(std::begin(outputs_), ptr);
  194. result->score = *ptr;
  195. result->category = labels[result->category_id];
  196. return true;
  197. }
  198. bool Model::predict(const std::vector<cv::Mat>& im_batch,
  199. std::vector<ClsResult>* results,
  200. int thread_num) {
  201. for (auto& inputs : inputs_batch_) {
  202. inputs.clear();
  203. }
  204. if (type == "detector") {
  205. std::cerr << "Loading model is a 'detector', DetResult should be passed to "
  206. "function predict()!" << std::endl;
  207. return false;
  208. } else if (type == "segmenter") {
  209. std::cerr << "Loading model is a 'segmenter', SegResult should be passed "
  210. "to function predict()!" << std::endl;
  211. return false;
  212. }
  213. inputs_batch_.assign(im_batch.size(), ImageBlob());
  214. // preprocess
  215. if (!preprocess(im_batch, &inputs_batch_, thread_num)) {
  216. std::cerr << "Preprocess failed!" << std::endl;
  217. return false;
  218. }
  219. // predict
  220. int batch_size = im_batch.size();
  221. auto in_tensor = predictor_->GetInputTensor("image");
  222. int h = inputs_batch_[0].new_im_size_[0];
  223. int w = inputs_batch_[0].new_im_size_[1];
  224. in_tensor->Reshape({batch_size, input_channel_, h, w});
  225. std::vector<float> inputs_data(batch_size * input_channel_ * h * w);
  226. for (int i = 0; i < batch_size; ++i) {
  227. std::copy(inputs_batch_[i].im_data_.begin(),
  228. inputs_batch_[i].im_data_.end(),
  229. inputs_data.begin() + i * input_channel_ * h * w);
  230. }
  231. in_tensor->copy_from_cpu(inputs_data.data());
  232. // in_tensor->copy_from_cpu(inputs_.im_data_.data());
  233. predictor_->ZeroCopyRun();
  234. // get result
  235. auto output_names = predictor_->GetOutputNames();
  236. auto output_tensor = predictor_->GetOutputTensor(output_names[0]);
  237. std::vector<int> output_shape = output_tensor->shape();
  238. int size = 1;
  239. for (const auto& i : output_shape) {
  240. size *= i;
  241. }
  242. outputs_.resize(size);
  243. output_tensor->copy_to_cpu(outputs_.data());
  244. // postprocess
  245. (*results).clear();
  246. (*results).resize(batch_size);
  247. int single_batch_size = size / batch_size;
  248. for (int i = 0; i < batch_size; ++i) {
  249. auto start_ptr = std::begin(outputs_);
  250. auto end_ptr = std::begin(outputs_);
  251. std::advance(start_ptr, i * single_batch_size);
  252. std::advance(end_ptr, (i + 1) * single_batch_size);
  253. auto ptr = std::max_element(start_ptr, end_ptr);
  254. (*results)[i].category_id = std::distance(start_ptr, ptr);
  255. (*results)[i].score = *ptr;
  256. (*results)[i].category = labels[(*results)[i].category_id];
  257. }
  258. return true;
  259. }
  260. bool Model::predict(const cv::Mat& im, DetResult* result) {
  261. inputs_.clear();
  262. result->clear();
  263. if (type == "classifier") {
  264. std::cerr << "Loading model is a 'classifier', ClsResult should be passed "
  265. "to function predict()!" << std::endl;
  266. return false;
  267. } else if (type == "segmenter") {
  268. std::cerr << "Loading model is a 'segmenter', SegResult should be passed "
  269. "to function predict()!" << std::endl;
  270. return false;
  271. }
  272. // preprocess
  273. if (!preprocess(im, &inputs_)) {
  274. std::cerr << "Preprocess failed!" << std::endl;
  275. return false;
  276. }
  277. int h = inputs_.new_im_size_[0];
  278. int w = inputs_.new_im_size_[1];
  279. auto im_tensor = predictor_->GetInputTensor("image");
  280. im_tensor->Reshape({1, input_channel_, h, w});
  281. im_tensor->copy_from_cpu(inputs_.im_data_.data());
  282. if (name == "YOLOv3" || name == "PPYOLO") {
  283. auto im_size_tensor = predictor_->GetInputTensor("im_size");
  284. im_size_tensor->Reshape({1, 2});
  285. im_size_tensor->copy_from_cpu(inputs_.ori_im_size_.data());
  286. } else if (name == "FasterRCNN" || name == "MaskRCNN") {
  287. auto im_info_tensor = predictor_->GetInputTensor("im_info");
  288. auto im_shape_tensor = predictor_->GetInputTensor("im_shape");
  289. im_info_tensor->Reshape({1, 3});
  290. im_shape_tensor->Reshape({1, 3});
  291. float ori_h = static_cast<float>(inputs_.ori_im_size_[0]);
  292. float ori_w = static_cast<float>(inputs_.ori_im_size_[1]);
  293. float new_h = static_cast<float>(inputs_.new_im_size_[0]);
  294. float new_w = static_cast<float>(inputs_.new_im_size_[1]);
  295. float im_info[] = {new_h, new_w, inputs_.scale};
  296. float im_shape[] = {ori_h, ori_w, 1.0};
  297. im_info_tensor->copy_from_cpu(im_info);
  298. im_shape_tensor->copy_from_cpu(im_shape);
  299. }
  300. // predict
  301. predictor_->ZeroCopyRun();
  302. std::vector<float> output_box;
  303. auto output_names = predictor_->GetOutputNames();
  304. auto output_box_tensor = predictor_->GetOutputTensor(output_names[0]);
  305. std::vector<int> output_box_shape = output_box_tensor->shape();
  306. int size = 1;
  307. for (const auto& i : output_box_shape) {
  308. size *= i;
  309. }
  310. output_box.resize(size);
  311. output_box_tensor->copy_to_cpu(output_box.data());
  312. if (size < 6) {
  313. std::cerr << "[WARNING] There's no object detected." << std::endl;
  314. return true;
  315. }
  316. int num_boxes = size / 6;
  317. // box postprocess
  318. for (int i = 0; i < num_boxes; ++i) {
  319. Box box;
  320. box.category_id = static_cast<int>(round(output_box[i * 6]));
  321. box.category = labels[box.category_id];
  322. box.score = output_box[i * 6 + 1];
  323. float xmin = output_box[i * 6 + 2];
  324. float ymin = output_box[i * 6 + 3];
  325. float xmax = output_box[i * 6 + 4];
  326. float ymax = output_box[i * 6 + 5];
  327. float w = xmax - xmin + 1;
  328. float h = ymax - ymin + 1;
  329. box.coordinate = {xmin, ymin, w, h};
  330. result->boxes.push_back(std::move(box));
  331. }
  332. // mask postprocess
  333. if (name == "MaskRCNN") {
  334. std::vector<float> output_mask;
  335. auto output_mask_tensor = predictor_->GetOutputTensor(output_names[1]);
  336. std::vector<int> output_mask_shape = output_mask_tensor->shape();
  337. int masks_size = 1;
  338. for (const auto& i : output_mask_shape) {
  339. masks_size *= i;
  340. }
  341. int mask_pixels = output_mask_shape[2] * output_mask_shape[3];
  342. int classes = output_mask_shape[1];
  343. output_mask.resize(masks_size);
  344. output_mask_tensor->copy_to_cpu(output_mask.data());
  345. result->mask_resolution = output_mask_shape[2];
  346. for (int i = 0; i < result->boxes.size(); ++i) {
  347. Box* box = &result->boxes[i];
  348. box->mask.shape = {static_cast<int>(box->coordinate[2]),
  349. static_cast<int>(box->coordinate[3])};
  350. auto begin_mask =
  351. output_mask.data() + (i * classes + box->category_id) * mask_pixels;
  352. cv::Mat bin_mask(result->mask_resolution,
  353. result->mask_resolution,
  354. CV_32FC1,
  355. begin_mask);
  356. cv::resize(bin_mask,
  357. bin_mask,
  358. cv::Size(box->mask.shape[0], box->mask.shape[1]));
  359. cv::threshold(bin_mask, bin_mask, 0.5, 1, cv::THRESH_BINARY);
  360. auto mask_int_begin = reinterpret_cast<float*>(bin_mask.data);
  361. auto mask_int_end =
  362. mask_int_begin + box->mask.shape[0] * box->mask.shape[1];
  363. box->mask.data.assign(mask_int_begin, mask_int_end);
  364. }
  365. }
  366. return true;
  367. }
  368. bool Model::predict(const std::vector<cv::Mat>& im_batch,
  369. std::vector<DetResult>* results,
  370. int thread_num) {
  371. for (auto& inputs : inputs_batch_) {
  372. inputs.clear();
  373. }
  374. if (type == "classifier") {
  375. std::cerr << "Loading model is a 'classifier', ClsResult should be passed "
  376. "to function predict()!" << std::endl;
  377. return false;
  378. } else if (type == "segmenter") {
  379. std::cerr << "Loading model is a 'segmenter', SegResult should be passed "
  380. "to function predict()!" << std::endl;
  381. return false;
  382. }
  383. inputs_batch_.assign(im_batch.size(), ImageBlob());
  384. int batch_size = im_batch.size();
  385. // preprocess
  386. if (!preprocess(im_batch, &inputs_batch_, thread_num)) {
  387. std::cerr << "Preprocess failed!" << std::endl;
  388. return false;
  389. }
  390. // RCNN model padding
  391. if (batch_size > 1) {
  392. if (name == "FasterRCNN" || name == "MaskRCNN") {
  393. int max_h = -1;
  394. int max_w = -1;
  395. for (int i = 0; i < batch_size; ++i) {
  396. max_h = std::max(max_h, inputs_batch_[i].new_im_size_[0]);
  397. max_w = std::max(max_w, inputs_batch_[i].new_im_size_[1]);
  398. // std::cout << "(" << inputs_batch_[i].new_im_size_[0]
  399. // << ", " << inputs_batch_[i].new_im_size_[1]
  400. // << ")" << std::endl;
  401. }
  402. thread_num = std::min(thread_num, batch_size);
  403. #pragma omp parallel for num_threads(thread_num)
  404. for (int i = 0; i < batch_size; ++i) {
  405. int h = inputs_batch_[i].new_im_size_[0];
  406. int w = inputs_batch_[i].new_im_size_[1];
  407. int c = im_batch[i].channels();
  408. if (max_h != h || max_w != w) {
  409. std::vector<float> temp_buffer(c * max_h * max_w);
  410. float* temp_ptr = temp_buffer.data();
  411. float* ptr = inputs_batch_[i].im_data_.data();
  412. for (int cur_channel = c - 1; cur_channel >= 0; --cur_channel) {
  413. int ori_pos = cur_channel * h * w + (h - 1) * w;
  414. int des_pos = cur_channel * max_h * max_w + (h - 1) * max_w;
  415. int last_pos = cur_channel * h * w;
  416. for (; ori_pos >= last_pos; ori_pos -= w, des_pos -= max_w) {
  417. memcpy(temp_ptr + des_pos, ptr + ori_pos, w * sizeof(float));
  418. }
  419. }
  420. inputs_batch_[i].im_data_.swap(temp_buffer);
  421. inputs_batch_[i].new_im_size_[0] = max_h;
  422. inputs_batch_[i].new_im_size_[1] = max_w;
  423. }
  424. }
  425. }
  426. }
  427. int h = inputs_batch_[0].new_im_size_[0];
  428. int w = inputs_batch_[0].new_im_size_[1];
  429. auto im_tensor = predictor_->GetInputTensor("image");
  430. im_tensor->Reshape({batch_size, input_channel_, h, w});
  431. std::vector<float> inputs_data(batch_size * input_channel_ * h * w);
  432. for (int i = 0; i < batch_size; ++i) {
  433. std::copy(inputs_batch_[i].im_data_.begin(),
  434. inputs_batch_[i].im_data_.end(),
  435. inputs_data.begin() + i * input_channel_ * h * w);
  436. }
  437. im_tensor->copy_from_cpu(inputs_data.data());
  438. if (name == "YOLOv3" || name == "PPYOLO") {
  439. auto im_size_tensor = predictor_->GetInputTensor("im_size");
  440. im_size_tensor->Reshape({batch_size, 2});
  441. std::vector<int> inputs_data_size(batch_size * 2);
  442. for (int i = 0; i < batch_size; ++i) {
  443. std::copy(inputs_batch_[i].ori_im_size_.begin(),
  444. inputs_batch_[i].ori_im_size_.end(),
  445. inputs_data_size.begin() + 2 * i);
  446. }
  447. im_size_tensor->copy_from_cpu(inputs_data_size.data());
  448. } else if (name == "FasterRCNN" || name == "MaskRCNN") {
  449. auto im_info_tensor = predictor_->GetInputTensor("im_info");
  450. auto im_shape_tensor = predictor_->GetInputTensor("im_shape");
  451. im_info_tensor->Reshape({batch_size, 3});
  452. im_shape_tensor->Reshape({batch_size, 3});
  453. std::vector<float> im_info(3 * batch_size);
  454. std::vector<float> im_shape(3 * batch_size);
  455. for (int i = 0; i < batch_size; ++i) {
  456. float ori_h = static_cast<float>(inputs_batch_[i].ori_im_size_[0]);
  457. float ori_w = static_cast<float>(inputs_batch_[i].ori_im_size_[1]);
  458. float new_h = static_cast<float>(inputs_batch_[i].new_im_size_[0]);
  459. float new_w = static_cast<float>(inputs_batch_[i].new_im_size_[1]);
  460. im_info[i * 3] = new_h;
  461. im_info[i * 3 + 1] = new_w;
  462. im_info[i * 3 + 2] = inputs_batch_[i].scale;
  463. im_shape[i * 3] = ori_h;
  464. im_shape[i * 3 + 1] = ori_w;
  465. im_shape[i * 3 + 2] = 1.0;
  466. }
  467. im_info_tensor->copy_from_cpu(im_info.data());
  468. im_shape_tensor->copy_from_cpu(im_shape.data());
  469. }
  470. // predict
  471. predictor_->ZeroCopyRun();
  472. // get all box
  473. std::vector<float> output_box;
  474. auto output_names = predictor_->GetOutputNames();
  475. auto output_box_tensor = predictor_->GetOutputTensor(output_names[0]);
  476. std::vector<int> output_box_shape = output_box_tensor->shape();
  477. int size = 1;
  478. for (const auto& i : output_box_shape) {
  479. size *= i;
  480. }
  481. output_box.resize(size);
  482. output_box_tensor->copy_to_cpu(output_box.data());
  483. if (size < 6) {
  484. std::cerr << "[WARNING] There's no object detected." << std::endl;
  485. return true;
  486. }
  487. auto lod_vector = output_box_tensor->lod();
  488. int num_boxes = size / 6;
  489. // box postprocess
  490. (*results).clear();
  491. (*results).resize(batch_size);
  492. for (int i = 0; i < lod_vector[0].size() - 1; ++i) {
  493. for (int j = lod_vector[0][i]; j < lod_vector[0][i + 1]; ++j) {
  494. Box box;
  495. box.category_id = static_cast<int>(round(output_box[j * 6]));
  496. box.category = labels[box.category_id];
  497. box.score = output_box[j * 6 + 1];
  498. float xmin = output_box[j * 6 + 2];
  499. float ymin = output_box[j * 6 + 3];
  500. float xmax = output_box[j * 6 + 4];
  501. float ymax = output_box[j * 6 + 5];
  502. float w = xmax - xmin + 1;
  503. float h = ymax - ymin + 1;
  504. box.coordinate = {xmin, ymin, w, h};
  505. (*results)[i].boxes.push_back(std::move(box));
  506. }
  507. }
  508. // mask postprocess
  509. if (name == "MaskRCNN") {
  510. std::vector<float> output_mask;
  511. auto output_mask_tensor = predictor_->GetOutputTensor(output_names[1]);
  512. std::vector<int> output_mask_shape = output_mask_tensor->shape();
  513. int masks_size = 1;
  514. for (const auto& i : output_mask_shape) {
  515. masks_size *= i;
  516. }
  517. int mask_pixels = output_mask_shape[2] * output_mask_shape[3];
  518. int classes = output_mask_shape[1];
  519. output_mask.resize(masks_size);
  520. output_mask_tensor->copy_to_cpu(output_mask.data());
  521. int mask_idx = 0;
  522. for (int i = 0; i < lod_vector[0].size() - 1; ++i) {
  523. (*results)[i].mask_resolution = output_mask_shape[2];
  524. for (int j = 0; j < (*results)[i].boxes.size(); ++j) {
  525. Box* box = &(*results)[i].boxes[i];
  526. int category_id = box->category_id;
  527. box->mask.shape = {static_cast<int>(box->coordinate[2]),
  528. static_cast<int>(box->coordinate[3])};
  529. auto begin_mask =
  530. output_mask.data() + (i * classes + box->category_id) * mask_pixels;
  531. cv::Mat bin_mask(output_mask_shape[2],
  532. output_mask_shape[2],
  533. CV_32FC1,
  534. begin_mask);
  535. cv::resize(bin_mask,
  536. bin_mask,
  537. cv::Size(box->mask.shape[0], box->mask.shape[1]));
  538. cv::threshold(bin_mask, bin_mask, 0.5, 1, cv::THRESH_BINARY);
  539. auto mask_int_begin = reinterpret_cast<float*>(bin_mask.data);
  540. auto mask_int_end =
  541. mask_int_begin + box->mask.shape[0] * box->mask.shape[1];
  542. box->mask.data.assign(mask_int_begin, mask_int_end);
  543. mask_idx++;
  544. }
  545. }
  546. }
  547. return true;
  548. }
  549. bool Model::predict(const cv::Mat& im, SegResult* result) {
  550. result->clear();
  551. inputs_.clear();
  552. if (type == "classifier") {
  553. std::cerr << "Loading model is a 'classifier', ClsResult should be passed "
  554. "to function predict()!" << std::endl;
  555. return false;
  556. } else if (type == "detector") {
  557. std::cerr << "Loading model is a 'detector', DetResult should be passed to "
  558. "function predict()!" << std::endl;
  559. return false;
  560. }
  561. // preprocess
  562. if (!preprocess(im, &inputs_)) {
  563. std::cerr << "Preprocess failed!" << std::endl;
  564. return false;
  565. }
  566. int h = inputs_.new_im_size_[0];
  567. int w = inputs_.new_im_size_[1];
  568. auto im_tensor = predictor_->GetInputTensor("image");
  569. im_tensor->Reshape({1, input_channel_, h, w});
  570. im_tensor->copy_from_cpu(inputs_.im_data_.data());
  571. // predict
  572. predictor_->ZeroCopyRun();
  573. // get labelmap
  574. auto output_names = predictor_->GetOutputNames();
  575. auto output_label_tensor = predictor_->GetOutputTensor(output_names[0]);
  576. std::vector<int> output_label_shape = output_label_tensor->shape();
  577. int size = 1;
  578. for (const auto& i : output_label_shape) {
  579. size *= i;
  580. result->label_map.shape.push_back(i);
  581. }
  582. result->label_map.data.resize(size);
  583. output_label_tensor->copy_to_cpu(result->label_map.data.data());
  584. // get scoremap
  585. auto output_score_tensor = predictor_->GetOutputTensor(output_names[1]);
  586. std::vector<int> output_score_shape = output_score_tensor->shape();
  587. size = 1;
  588. for (const auto& i : output_score_shape) {
  589. size *= i;
  590. result->score_map.shape.push_back(i);
  591. }
  592. result->score_map.data.resize(size);
  593. output_score_tensor->copy_to_cpu(result->score_map.data.data());
  594. // get origin image result
  595. std::vector<uint8_t> label_map(result->label_map.data.begin(),
  596. result->label_map.data.end());
  597. cv::Mat mask_label(result->label_map.shape[1],
  598. result->label_map.shape[2],
  599. CV_8UC1,
  600. label_map.data());
  601. cv::Mat mask_score(result->score_map.shape[2],
  602. result->score_map.shape[3],
  603. CV_32FC1,
  604. result->score_map.data.data());
  605. int idx = 1;
  606. int len_postprocess = inputs_.im_size_before_resize_.size();
  607. for (std::vector<std::string>::reverse_iterator iter =
  608. inputs_.reshape_order_.rbegin();
  609. iter != inputs_.reshape_order_.rend();
  610. ++iter) {
  611. if (*iter == "padding") {
  612. auto before_shape = inputs_.im_size_before_resize_[len_postprocess - idx];
  613. inputs_.im_size_before_resize_.pop_back();
  614. auto padding_w = before_shape[0];
  615. auto padding_h = before_shape[1];
  616. mask_label = mask_label(cv::Rect(0, 0, padding_h, padding_w));
  617. mask_score = mask_score(cv::Rect(0, 0, padding_h, padding_w));
  618. } else if (*iter == "resize") {
  619. auto before_shape = inputs_.im_size_before_resize_[len_postprocess - idx];
  620. inputs_.im_size_before_resize_.pop_back();
  621. auto resize_w = before_shape[0];
  622. auto resize_h = before_shape[1];
  623. cv::resize(mask_label,
  624. mask_label,
  625. cv::Size(resize_h, resize_w),
  626. 0,
  627. 0,
  628. cv::INTER_NEAREST);
  629. cv::resize(mask_score,
  630. mask_score,
  631. cv::Size(resize_h, resize_w),
  632. 0,
  633. 0,
  634. cv::INTER_LINEAR);
  635. }
  636. ++idx;
  637. }
  638. result->label_map.data.assign(mask_label.begin<uint8_t>(),
  639. mask_label.end<uint8_t>());
  640. result->label_map.shape = {mask_label.rows, mask_label.cols};
  641. result->score_map.data.assign(mask_score.begin<float>(),
  642. mask_score.end<float>());
  643. result->score_map.shape = {mask_score.rows, mask_score.cols};
  644. return true;
  645. }
  646. bool Model::predict(const std::vector<cv::Mat>& im_batch,
  647. std::vector<SegResult>* results,
  648. int thread_num) {
  649. for (auto& inputs : inputs_batch_) {
  650. inputs.clear();
  651. }
  652. if (type == "classifier") {
  653. std::cerr << "Loading model is a 'classifier', ClsResult should be passed "
  654. "to function predict()!" << std::endl;
  655. return false;
  656. } else if (type == "detector") {
  657. std::cerr << "Loading model is a 'detector', DetResult should be passed to "
  658. "function predict()!" << std::endl;
  659. return false;
  660. }
  661. // preprocess
  662. inputs_batch_.assign(im_batch.size(), ImageBlob());
  663. if (!preprocess(im_batch, &inputs_batch_, thread_num)) {
  664. std::cerr << "Preprocess failed!" << std::endl;
  665. return false;
  666. }
  667. int batch_size = im_batch.size();
  668. (*results).clear();
  669. (*results).resize(batch_size);
  670. int h = inputs_batch_[0].new_im_size_[0];
  671. int w = inputs_batch_[0].new_im_size_[1];
  672. auto im_tensor = predictor_->GetInputTensor("image");
  673. im_tensor->Reshape({batch_size, input_channel_, h, w});
  674. std::vector<float> inputs_data(batch_size * input_channel_ * h * w);
  675. for (int i = 0; i < batch_size; ++i) {
  676. std::copy(inputs_batch_[i].im_data_.begin(),
  677. inputs_batch_[i].im_data_.end(),
  678. inputs_data.begin() + i * input_channel_ * h * w);
  679. }
  680. im_tensor->copy_from_cpu(inputs_data.data());
  681. // im_tensor->copy_from_cpu(inputs_.im_data_.data());
  682. // predict
  683. predictor_->ZeroCopyRun();
  684. // get labelmap
  685. auto output_names = predictor_->GetOutputNames();
  686. auto output_label_tensor = predictor_->GetOutputTensor(output_names[0]);
  687. std::vector<int> output_label_shape = output_label_tensor->shape();
  688. int size = 1;
  689. for (const auto& i : output_label_shape) {
  690. size *= i;
  691. }
  692. std::vector<int64_t> output_labels(size, 0);
  693. output_label_tensor->copy_to_cpu(output_labels.data());
  694. auto output_labels_iter = output_labels.begin();
  695. int single_batch_size = size / batch_size;
  696. for (int i = 0; i < batch_size; ++i) {
  697. (*results)[i].label_map.data.resize(single_batch_size);
  698. (*results)[i].label_map.shape.push_back(1);
  699. for (int j = 1; j < output_label_shape.size(); ++j) {
  700. (*results)[i].label_map.shape.push_back(output_label_shape[j]);
  701. }
  702. std::copy(output_labels_iter + i * single_batch_size,
  703. output_labels_iter + (i + 1) * single_batch_size,
  704. (*results)[i].label_map.data.data());
  705. }
  706. // get scoremap
  707. auto output_score_tensor = predictor_->GetOutputTensor(output_names[1]);
  708. std::vector<int> output_score_shape = output_score_tensor->shape();
  709. size = 1;
  710. for (const auto& i : output_score_shape) {
  711. size *= i;
  712. }
  713. std::vector<float> output_scores(size, 0);
  714. output_score_tensor->copy_to_cpu(output_scores.data());
  715. auto output_scores_iter = output_scores.begin();
  716. int single_batch_score_size = size / batch_size;
  717. for (int i = 0; i < batch_size; ++i) {
  718. (*results)[i].score_map.data.resize(single_batch_score_size);
  719. (*results)[i].score_map.shape.push_back(1);
  720. for (int j = 1; j < output_score_shape.size(); ++j) {
  721. (*results)[i].score_map.shape.push_back(output_score_shape[j]);
  722. }
  723. std::copy(output_scores_iter + i * single_batch_score_size,
  724. output_scores_iter + (i + 1) * single_batch_score_size,
  725. (*results)[i].score_map.data.data());
  726. }
  727. // get origin image result
  728. for (int i = 0; i < batch_size; ++i) {
  729. std::vector<uint8_t> label_map((*results)[i].label_map.data.begin(),
  730. (*results)[i].label_map.data.end());
  731. cv::Mat mask_label((*results)[i].label_map.shape[1],
  732. (*results)[i].label_map.shape[2],
  733. CV_8UC1,
  734. label_map.data());
  735. cv::Mat mask_score((*results)[i].score_map.shape[2],
  736. (*results)[i].score_map.shape[3],
  737. CV_32FC1,
  738. (*results)[i].score_map.data.data());
  739. int idx = 1;
  740. int len_postprocess = inputs_batch_[i].im_size_before_resize_.size();
  741. for (std::vector<std::string>::reverse_iterator iter =
  742. inputs_batch_[i].reshape_order_.rbegin();
  743. iter != inputs_batch_[i].reshape_order_.rend();
  744. ++iter) {
  745. if (*iter == "padding") {
  746. auto before_shape =
  747. inputs_batch_[i].im_size_before_resize_[len_postprocess - idx];
  748. inputs_batch_[i].im_size_before_resize_.pop_back();
  749. auto padding_w = before_shape[0];
  750. auto padding_h = before_shape[1];
  751. mask_label = mask_label(cv::Rect(0, 0, padding_h, padding_w));
  752. mask_score = mask_score(cv::Rect(0, 0, padding_h, padding_w));
  753. } else if (*iter == "resize") {
  754. auto before_shape =
  755. inputs_batch_[i].im_size_before_resize_[len_postprocess - idx];
  756. inputs_batch_[i].im_size_before_resize_.pop_back();
  757. auto resize_w = before_shape[0];
  758. auto resize_h = before_shape[1];
  759. cv::resize(mask_label,
  760. mask_label,
  761. cv::Size(resize_h, resize_w),
  762. 0,
  763. 0,
  764. cv::INTER_NEAREST);
  765. cv::resize(mask_score,
  766. mask_score,
  767. cv::Size(resize_h, resize_w),
  768. 0,
  769. 0,
  770. cv::INTER_LINEAR);
  771. }
  772. ++idx;
  773. }
  774. (*results)[i].label_map.data.assign(mask_label.begin<uint8_t>(),
  775. mask_label.end<uint8_t>());
  776. (*results)[i].label_map.shape = {mask_label.rows, mask_label.cols};
  777. (*results)[i].score_map.data.assign(mask_score.begin<float>(),
  778. mask_score.end<float>());
  779. (*results)[i].score_map.shape = {mask_score.rows, mask_score.cols};
  780. }
  781. return true;
  782. }
  783. } // namespace PaddleX