paddlex.cpp 28 KB

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