paddlex.cpp 29 KB

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