classifier.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_bool(use_mkl, true, "Infering with MKL");
  29. DEFINE_int32(mkl_thread_num,
  30. omp_get_num_procs(),
  31. "Number of mkl threads");
  32. DEFINE_int32(gpu_id, 0, "GPU card id");
  33. DEFINE_string(key, "", "key of encryption");
  34. DEFINE_string(image, "", "Path of test image file");
  35. DEFINE_string(image_list, "", "Path of test image list file");
  36. DEFINE_int32(batch_size, 1, "Batch size of infering");
  37. DEFINE_int32(thread_num,
  38. omp_get_num_procs(),
  39. "Number of preprocessing threads");
  40. int main(int argc, char** argv) {
  41. // Parsing command-line
  42. google::ParseCommandLineFlags(&argc, &argv, true);
  43. if (FLAGS_model_dir == "") {
  44. std::cerr << "--model_dir need to be defined" << std::endl;
  45. return -1;
  46. }
  47. if (FLAGS_image == "" & FLAGS_image_list == "") {
  48. std::cerr << "--image or --image_list need to be defined" << std::endl;
  49. return -1;
  50. }
  51. // Load model
  52. PaddleX::Model model;
  53. model.Init(FLAGS_model_dir,
  54. FLAGS_use_gpu,
  55. FLAGS_use_trt,
  56. FLAGS_use_mkl,
  57. FLAGS_mkl_thread_num,
  58. FLAGS_gpu_id,
  59. FLAGS_key);
  60. // Predict
  61. int imgs = 1;
  62. if (FLAGS_image_list != "") {
  63. std::ifstream inf(FLAGS_image_list);
  64. if (!inf) {
  65. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  66. return -1;
  67. }
  68. // Mini-batch predict
  69. std::string image_path;
  70. std::vector<std::string> image_paths;
  71. while (getline(inf, image_path)) {
  72. image_paths.push_back(image_path);
  73. }
  74. imgs = image_paths.size();
  75. for (int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  76. // Read image
  77. int im_vec_size =
  78. std::min(static_cast<int>(image_paths.size()), i + FLAGS_batch_size);
  79. std::vector<cv::Mat> im_vec(im_vec_size - i);
  80. std::vector<PaddleX::ClsResult> results(im_vec_size - i,
  81. PaddleX::ClsResult());
  82. int thread_num = std::min(FLAGS_thread_num, im_vec_size - i);
  83. #pragma omp parallel for num_threads(thread_num)
  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. if (!model.predict(im_vec, &results, thread_num)) {
  88. return -1;
  89. }
  90. for (int j = i; j < im_vec_size; ++j) {
  91. std::cout << "Path:" << image_paths[j]
  92. << ", predict label: " << results[j - i].category
  93. << ", label_id:" << results[j - i].category_id
  94. << ", score: " << results[j - i].score << std::endl;
  95. }
  96. }
  97. } else {
  98. PaddleX::ClsResult result;
  99. cv::Mat im = cv::imread(FLAGS_image, 1);
  100. if (!model.predict(im, &result)) {
  101. return -1;
  102. }
  103. std::cout << "Predict label: " << result.category
  104. << ", label_id:" << result.category_id
  105. << ", score: " << result.score << std::endl;
  106. }
  107. return 0;
  108. }