batch_infer.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <gflags/gflags.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_list, "", "Path of test image file");
  25. DEFINE_int32(batch_size, 1, "Batch size of infering");
  26. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  27. DEFINE_int32(gpu_id, 0, "GPU card id");
  28. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  29. int main(int argc, char** argv) {
  30. // Parsing command-line
  31. google::ParseCommandLineFlags(&argc, &argv, true);
  32. // create model
  33. std::shared_ptr<PaddleDeploy::Model> model =
  34. PaddleDeploy::CreateModel(FLAGS_model_type);
  35. // model init
  36. model->Init(FLAGS_cfg_file);
  37. // inference engine init
  38. PaddleDeploy::PaddleEngineConfig engine_config;
  39. engine_config.model_filename = FLAGS_model_filename;
  40. engine_config.params_filename = FLAGS_params_filename;
  41. engine_config.use_gpu = FLAGS_use_gpu;
  42. engine_config.gpu_id = FLAGS_gpu_id;
  43. engine_config.use_trt = FLAGS_use_trt;
  44. if (FLAGS_use_trt) {
  45. engine_config.precision = 0;
  46. }
  47. model->PaddleEngineInit(engine_config);
  48. // Mini-batch
  49. std::vector<std::string> image_paths;
  50. if (FLAGS_image_list != "") {
  51. std::ifstream inf(FLAGS_image_list);
  52. if (!inf) {
  53. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  54. return -1;
  55. }
  56. std::string image_path;
  57. while (getline(inf, image_path)) {
  58. image_paths.push_back(image_path);
  59. }
  60. }
  61. // infer
  62. std::vector<PaddleDeploy::Result> results;
  63. for (int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  64. // Read image
  65. int im_vec_size =
  66. std::min(static_cast<int>(image_paths.size()), i + FLAGS_batch_size);
  67. std::vector<cv::Mat> im_vec(im_vec_size - i);
  68. #pragma omp parallel for num_threads(im_vec_size - i)
  69. for (int j = i; j < im_vec_size; ++j) {
  70. im_vec[j - i] = std::move(cv::imread(image_paths[j], 1));
  71. }
  72. model->Predict(im_vec, &results);
  73. std::cout << i / FLAGS_batch_size << " group -----" << std::endl;
  74. for (auto j = 0; j < results.size(); ++j) {
  75. std::cout << "Result for sample " << j << std::endl;
  76. std::cout << results[j] << std::endl;
  77. }
  78. }
  79. return 0;
  80. }