paddlex.cpp 30 KB

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