classifier.cpp 4.6 KB

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