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. PaddleDeploy::Model* model = PaddleDeploy::CreateModel(FLAGS_model_type);
  34. // model init
  35. model->Init(FLAGS_cfg_file);
  36. // inference engine init
  37. PaddleDeploy::PaddleEngineConfig engine_config;
  38. engine_config.model_filename = FLAGS_model_filename;
  39. engine_config.params_filename = FLAGS_params_filename;
  40. engine_config.use_gpu = FLAGS_use_gpu;
  41. engine_config.gpu_id = FLAGS_gpu_id;
  42. engine_config.use_trt = FLAGS_use_trt;
  43. if (FLAGS_use_trt) {
  44. engine_config.precision = 0;
  45. }
  46. model->PaddleEngineInit(engine_config);
  47. // Mini-batch
  48. std::vector<std::string> image_paths;
  49. if (FLAGS_image_list != "") {
  50. std::ifstream inf(FLAGS_image_list);
  51. if (!inf) {
  52. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  53. return -1;
  54. }
  55. std::string image_path;
  56. while (getline(inf, image_path)) {
  57. image_paths.push_back(image_path);
  58. }
  59. }
  60. // infer
  61. std::vector<PaddleDeploy::Result> results;
  62. for (int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  63. // Read image
  64. int im_vec_size =
  65. std::min(static_cast<int>(image_paths.size()), i + FLAGS_batch_size);
  66. std::vector<cv::Mat> im_vec(im_vec_size - i);
  67. #pragma omp parallel for num_threads(im_vec_size - i)
  68. for (int j = i; j < im_vec_size; ++j) {
  69. im_vec[j - i] = std::move(cv::imread(image_paths[j], 1));
  70. }
  71. model->Predict(im_vec, &results);
  72. std::cout << i / FLAGS_batch_size << " group -----" << std::endl;
  73. for (auto j = 0; j < results.size(); ++j) {
  74. std::cout << "Result for sample " << j << std::endl;
  75. std::cout << results[j] << std::endl;
  76. }
  77. }
  78. delete model;
  79. return 0;
  80. }