classifier.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <glog/logging.h>
  15. #include <omp.h>
  16. #include <algorithm>
  17. #include <chrono> // NOLINT
  18. #include <fstream>
  19. #include <iostream>
  20. #include <string>
  21. #include <vector>
  22. #include <utility>
  23. #include "include/paddlex/paddlex.h"
  24. using namespace std::chrono; // NOLINT
  25. DEFINE_string(model_dir, "", "Path of inference model");
  26. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  27. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  28. DEFINE_int32(gpu_id, 0, "GPU card id");
  29. DEFINE_string(key, "", "key of encryption");
  30. DEFINE_string(image, "", "Path of test image file");
  31. DEFINE_string(image_list, "", "Path of test image list file");
  32. DEFINE_int32(batch_size, 1, "Batch size of infering");
  33. DEFINE_int32(thread_num,
  34. omp_get_num_procs(),
  35. "Number of preprocessing threads");
  36. DEFINE_bool(use_ir_optim, true, "use ir optimization");
  37. int main(int argc, char** argv) {
  38. // Parsing command-line
  39. google::ParseCommandLineFlags(&argc, &argv, true);
  40. if (FLAGS_model_dir == "") {
  41. std::cerr << "--model_dir need to be defined" << std::endl;
  42. return -1;
  43. }
  44. if (FLAGS_image == "" & FLAGS_image_list == "") {
  45. std::cerr << "--image or --image_list need to be defined" << std::endl;
  46. return -1;
  47. }
  48. // 加载模型
  49. PaddleX::Model model;
  50. model.Init(FLAGS_model_dir,
  51. FLAGS_use_gpu,
  52. FLAGS_use_trt,
  53. FLAGS_gpu_id,
  54. FLAGS_key,
  55. FLAGS_use_ir_optim);
  56. // 进行预测
  57. double total_running_time_s = 0.0;
  58. double total_imread_time_s = 0.0;
  59. int imgs = 1;
  60. if (FLAGS_image_list != "") {
  61. std::ifstream inf(FLAGS_image_list);
  62. if (!inf) {
  63. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  64. return -1;
  65. }
  66. // 多batch预测
  67. std::string image_path;
  68. std::vector<std::string> image_paths;
  69. while (getline(inf, image_path)) {
  70. image_paths.push_back(image_path);
  71. }
  72. imgs = image_paths.size();
  73. for (int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  74. auto start = system_clock::now();
  75. // 读图像
  76. int im_vec_size =
  77. std::min(static_cast<int>(image_paths.size()), i + FLAGS_batch_size);
  78. std::vector<cv::Mat> im_vec(im_vec_size - i);
  79. std::vector<PaddleX::ClsResult> results(im_vec_size - i,
  80. PaddleX::ClsResult());
  81. int thread_num = std::min(FLAGS_thread_num, im_vec_size - i);
  82. #pragma omp parallel for num_threads(thread_num)
  83. for (int j = i; j < im_vec_size; ++j) {
  84. im_vec[j - i] = std::move(cv::imread(image_paths[j], 1));
  85. }
  86. auto imread_end = system_clock::now();
  87. model.predict(im_vec, &results, thread_num);
  88. auto imread_duration = duration_cast<microseconds>(imread_end - start);
  89. total_imread_time_s += static_cast<double>(imread_duration.count()) *
  90. microseconds::period::num /
  91. microseconds::period::den;
  92. auto end = system_clock::now();
  93. auto duration = duration_cast<microseconds>(end - start);
  94. total_running_time_s += static_cast<double>(duration.count()) *
  95. microseconds::period::num /
  96. microseconds::period::den;
  97. for (int j = i; j < im_vec_size; ++j) {
  98. std::cout << "Path:" << image_paths[j]
  99. << ", predict label: " << results[j - i].category
  100. << ", label_id:" << results[j - i].category_id
  101. << ", score: " << results[j - i].score << std::endl;
  102. }
  103. }
  104. } else {
  105. auto start = system_clock::now();
  106. PaddleX::ClsResult result;
  107. cv::Mat im = cv::imread(FLAGS_image, 1);
  108. model.predict(im, &result);
  109. auto end = system_clock::now();
  110. auto duration = duration_cast<microseconds>(end - start);
  111. total_running_time_s += static_cast<double>(duration.count()) *
  112. microseconds::period::num /
  113. microseconds::period::den;
  114. std::cout << "Predict label: " << result.category
  115. << ", label_id:" << result.category_id
  116. << ", score: " << result.score << std::endl;
  117. }
  118. std::cout << "Total running time: " << total_running_time_s
  119. << " s, average running time: " << total_running_time_s / imgs
  120. << " s/img, total read img time: " << total_imread_time_s
  121. << " s, average read time: " << total_imread_time_s / imgs
  122. << " s/img, batch_size = " << FLAGS_batch_size << std::endl;
  123. return 0;
  124. }