segmenter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "include/paddlex/visualize.h"
  25. using namespace std::chrono; // NOLINT
  26. DEFINE_string(model_dir, "", "Path of inference model");
  27. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  28. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  29. DEFINE_int32(gpu_id, 0, "GPU card id");
  30. DEFINE_string(key, "", "key of encryption");
  31. DEFINE_string(image, "", "Path of test image file");
  32. DEFINE_string(image_list, "", "Path of test image list file");
  33. DEFINE_string(save_dir, "output", "Path to save visualized image");
  34. DEFINE_int32(batch_size, 1, "Batch size of infering");
  35. DEFINE_int32(thread_num,
  36. omp_get_num_procs(),
  37. "Number of preprocessing threads");
  38. int main(int argc, char** argv) {
  39. // 解析命令行参数
  40. google::ParseCommandLineFlags(&argc, &argv, true);
  41. if (FLAGS_model_dir == "") {
  42. std::cerr << "--model_dir need to be defined" << std::endl;
  43. return -1;
  44. }
  45. if (FLAGS_image == "" & FLAGS_image_list == "") {
  46. std::cerr << "--image or --image_list need to be defined" << std::endl;
  47. return -1;
  48. }
  49. // 加载模型
  50. PaddleX::Model model;
  51. model.Init(FLAGS_model_dir,
  52. FLAGS_use_gpu,
  53. FLAGS_use_trt,
  54. FLAGS_gpu_id,
  55. FLAGS_key,
  56. FLAGS_batch_size);
  57. double total_running_time_s = 0.0;
  58. double total_imread_time_s = 0.0;
  59. int imgs = 1;
  60. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  61. // 进行预测
  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. std::string image_path;
  69. std::vector<std::string> image_paths;
  70. while (getline(inf, image_path)) {
  71. image_paths.push_back(image_path);
  72. }
  73. imgs = image_paths.size();
  74. for (int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  75. auto start = system_clock::now();
  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::SegResult> results(im_vec_size - i,
  80. PaddleX::SegResult());
  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. // 可视化
  98. for (int j = 0; j < im_vec_size - i; ++j) {
  99. cv::Mat vis_img =
  100. PaddleX::Visualize(im_vec[j], results[j], model.labels, colormap);
  101. std::string save_path =
  102. PaddleX::generate_save_path(FLAGS_save_dir, image_paths[i + j]);
  103. cv::imwrite(save_path, vis_img);
  104. std::cout << "Visualized output saved as " << save_path << std::endl;
  105. }
  106. }
  107. } else {
  108. auto start = system_clock::now();
  109. PaddleX::SegResult result;
  110. cv::Mat im = cv::imread(FLAGS_image, 1);
  111. model.predict(im, &result);
  112. auto end = system_clock::now();
  113. auto duration = duration_cast<microseconds>(end - start);
  114. total_running_time_s += static_cast<double>(duration.count()) *
  115. microseconds::period::num /
  116. microseconds::period::den;
  117. // 可视化
  118. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  119. std::string save_path =
  120. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  121. cv::imwrite(save_path, vis_img);
  122. result.clear();
  123. std::cout << "Visualized output saved as " << save_path << std::endl;
  124. }
  125. std::cout << "Total running time: " << total_running_time_s
  126. << " s, average running time: " << total_running_time_s / imgs
  127. << " s/img, total read img time: " << total_imread_time_s
  128. << " s, average read img time: " << total_imread_time_s / imgs
  129. << " s, batch_size = " << FLAGS_batch_size << std::endl;
  130. return 0;
  131. }