model_infer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2021 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 <glog/logging.h>
  15. #include <omp.h>
  16. #include <memory>
  17. #include <string>
  18. #include <fstream>
  19. #include "model_deploy/common/include/paddle_deploy.h"
  20. DEFINE_string(model_filename, "", "Path of det inference model");
  21. DEFINE_string(params_filename, "", "Path of det inference params");
  22. DEFINE_string(cfg_file, "", "Path of yaml file");
  23. DEFINE_string(model_type, "", "model type");
  24. DEFINE_string(image, "", "Path of test image file");
  25. DEFINE_string(image_list, "", "Path of test image file");
  26. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  27. DEFINE_int32(gpu_id, 0, "GPU card id");
  28. DEFINE_bool(use_mkl, true, "Infering with mkl");
  29. DEFINE_int32(batch_size, 1, "Batch size of infering");
  30. DEFINE_int32(thread_num, 1, "thread num of preprocessing");
  31. DEFINE_int32(mkl_thread_num, 8, "thread num of mkldnn");
  32. int main(int argc, char** argv) {
  33. // Parsing command-line
  34. google::ParseCommandLineFlags(&argc, &argv, true);
  35. std::cout << "ParseCommandLineFlags:FLAGS_model_type="
  36. << FLAGS_model_type << " model_filename="
  37. << FLAGS_model_filename << std::endl;
  38. // create model
  39. std::shared_ptr<PaddleDeploy::Model> model =
  40. PaddleDeploy::CreateModel(FLAGS_model_type);
  41. if (!model) {
  42. std::cout << "no model_type: " << FLAGS_model_type
  43. << " model=" << model << std::endl;
  44. return 0;
  45. }
  46. std::cout << "start model init " << std::endl;
  47. // model init
  48. model->Init(FLAGS_cfg_file);
  49. std::cout << "start engine init " << std::endl;
  50. // inference engine in
  51. model->PaddleEngineInit(FLAGS_model_filename,
  52. FLAGS_params_filename,
  53. FLAGS_use_gpu,
  54. FLAGS_gpu_id,
  55. FLAGS_use_mkl,
  56. FLAGS_mkl_thread_num);
  57. // Mini-batch
  58. std::vector<std::string> image_paths;
  59. if (FLAGS_image_list != "") {
  60. std::ifstream inf(FLAGS_image_list);
  61. if (!inf) {
  62. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  63. return -1;
  64. }
  65. std::string image_path;
  66. while (getline(inf, image_path)) {
  67. image_paths.push_back(image_path);
  68. }
  69. } else if (FLAGS_image != "") {
  70. image_paths.push_back(FLAGS_image);
  71. } else {
  72. std::cerr << "image_list or image should be defined" << std::endl;
  73. return -1;
  74. }
  75. std::cout << "start model predict " << image_paths.size() << std::endl;
  76. // infer
  77. std::vector<PaddleDeploy::Result> results;
  78. for (int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  79. // Read image
  80. int im_vec_size =
  81. std::min(static_cast<int>(image_paths.size()), i + FLAGS_batch_size);
  82. std::vector<cv::Mat> im_vec(im_vec_size - i);
  83. #pragma omp parallel for num_threads(im_vec_size - i)
  84. for (int j = i; j < im_vec_size; ++j) {
  85. im_vec[j - i] = std::move(cv::imread(image_paths[j], 1));
  86. }
  87. model->Predict(im_vec, &results, FLAGS_thread_num);
  88. std::cout << i / FLAGS_batch_size << " group" << std::endl;
  89. for (auto j = 0; j < results.size(); ++j) {
  90. std::cout << "Result for sample " << j << std::endl;
  91. std::cout << results[j] << std::endl;
  92. }
  93. }
  94. return 0;
  95. }