segmenter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <omp.h>
  23. #include "include/paddlex/paddlex.h"
  24. #include "include/paddlex/visualize.h"
  25. using namespace std::chrono;
  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. int main(int argc, char** argv) {
  36. // 解析命令行参数
  37. google::ParseCommandLineFlags(&argc, &argv, true);
  38. if (FLAGS_model_dir == "") {
  39. std::cerr << "--model_dir need to be defined" << std::endl;
  40. return -1;
  41. }
  42. if (FLAGS_image == "" & FLAGS_image_list == "") {
  43. std::cerr << "--image or --image_list need to be defined" << std::endl;
  44. return -1;
  45. }
  46. // 加载模型
  47. PaddleX::Model model;
  48. model.Init(FLAGS_model_dir, FLAGS_use_gpu, FLAGS_use_trt, FLAGS_gpu_id, FLAGS_key, FLAGS_batch_size);
  49. double total_running_time_s = 0.0;
  50. double total_imread_time_s = 0.0;
  51. int imgs = 1;
  52. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  53. // 进行预测
  54. if (FLAGS_image_list != "") {
  55. std::ifstream inf(FLAGS_image_list);
  56. if (!inf) {
  57. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  58. return -1;
  59. }
  60. std::string image_path;
  61. std::vector<std::string> image_paths;
  62. while (getline(inf, image_path)) {
  63. image_paths.push_back(image_path);
  64. }
  65. imgs = image_paths.size();
  66. for(int i = 0; i < image_paths.size(); i += FLAGS_batch_size){
  67. auto start = system_clock::now();
  68. int im_vec_size = std::min((int)image_paths.size(), i + FLAGS_batch_size);
  69. std::vector<cv::Mat> im_vec(im_vec_size - i);
  70. std::vector<PaddleX::SegResult> results(im_vec_size - i, PaddleX::SegResult());
  71. #pragma omp parallel for num_threads(im_vec_size - i)
  72. for(int j = i; j < im_vec_size; ++j){
  73. im_vec[j - i] = std::move(cv::imread(image_paths[j], 1));
  74. }
  75. auto imread_end = system_clock::now();
  76. model.predict(im_vec, results);
  77. auto imread_duration = duration_cast<microseconds>(imread_end - start);
  78. total_imread_time_s += double(imread_duration.count()) * microseconds::period::num / microseconds::period::den;
  79. auto end = system_clock::now();
  80. auto duration = duration_cast<microseconds>(end - start);
  81. total_running_time_s += double(duration.count()) * microseconds::period::num / microseconds::period::den;
  82. // 可视化
  83. for(int j = 0; j < im_vec_size - i; ++j) {
  84. cv::Mat vis_img =
  85. PaddleX::Visualize(im_vec[j], results[j], model.labels, colormap);
  86. std::string save_path =
  87. PaddleX::generate_save_path(FLAGS_save_dir, image_paths[i + j]);
  88. cv::imwrite(save_path, vis_img);
  89. std::cout << "Visualized output saved as " << save_path << std::endl;
  90. }
  91. }
  92. } else {
  93. auto start = system_clock::now();
  94. PaddleX::SegResult result;
  95. cv::Mat im = cv::imread(FLAGS_image, 1);
  96. model.predict(im, &result);
  97. auto end = system_clock::now();
  98. auto duration = duration_cast<microseconds>(end - start);
  99. total_running_time_s += double(duration.count()) * microseconds::period::num / microseconds::period::den;
  100. // 可视化
  101. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  102. std::string save_path =
  103. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  104. cv::imwrite(save_path, vis_img);
  105. result.clear();
  106. std::cout << "Visualized output saved as " << save_path << std::endl;
  107. }
  108. std::cout << "Total running time: "
  109. << total_running_time_s
  110. << " s, average running time: "
  111. << total_running_time_s / imgs
  112. << " s/img, total read img time: "
  113. << total_imread_time_s
  114. << " s, average read img time: "
  115. << total_imread_time_s / imgs
  116. << " s, batch_size = "
  117. << FLAGS_batch_size
  118. << std::endl;
  119. return 0;
  120. }