paddlex.cpp 30 KB

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