paddlex.cpp 28 KB

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