paddlex.cpp 30 KB

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