model_infer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2021 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 <gflags/gflags.h>
  15. #include <omp.h>
  16. #include <memory>
  17. #include <string>
  18. #include <fstream>
  19. #include "model_deploy/common/include/paddle_deploy.h"
  20. DEFINE_string(model_file, "", "Path of inference model");
  21. DEFINE_string(cfg_file, "", "Path of yaml file");
  22. DEFINE_string(model_type, "", "model type");
  23. DEFINE_string(image, "", "Path of test image file");
  24. DEFINE_string(image_list, "", "Path of test image file");
  25. DEFINE_string(trt_cache_file, "", "Cache path to store optimized trt file");
  26. DEFINE_bool(save_engine, false, "Save Trt Engine");
  27. DEFINE_int32(gpu_id, 0, "GPU card id");
  28. int main(int argc, char** argv) {
  29. // Parsing command-line
  30. google::ParseCommandLineFlags(&argc, &argv, true);
  31. std::cout << "ParseCommandLineFlags:FLAGS_model_type="
  32. << FLAGS_model_type << " model_file="
  33. << FLAGS_model_file << std::endl;
  34. // create model
  35. std::shared_ptr<PaddleDeploy::Model> model =
  36. PaddleDeploy::CreateModel(FLAGS_model_type);
  37. if (!model) {
  38. std::cout << "no model_type: " << FLAGS_model_type
  39. << " model=" << model << std::endl;
  40. return 0;
  41. }
  42. std::cout << "start model init " << std::endl;
  43. // model init
  44. model->Init(FLAGS_cfg_file);
  45. std::cout << "start engine init " << std::endl;
  46. // inference engine init
  47. PaddleDeploy::TensorRTEngineConfig engine_config;
  48. engine_config.model_file_ = FLAGS_model_file;
  49. engine_config.cfg_file_ = FLAGS_cfg_file;
  50. engine_config.gpu_id_ = FLAGS_gpu_id;
  51. engine_config.save_engine_ = FLAGS_save_engine;
  52. engine_config.trt_cache_file_ = FLAGS_trt_cache_file;
  53. model->TensorRTInit(engine_config);
  54. // prepare data
  55. std::vector<std::string> image_paths;
  56. if (FLAGS_image_list != "") {
  57. std::ifstream inf(FLAGS_image_list);
  58. if (!inf) {
  59. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  60. return -1;
  61. }
  62. std::string image_path;
  63. while (getline(inf, image_path)) {
  64. image_paths.push_back(image_path);
  65. }
  66. } else if (FLAGS_image != "") {
  67. image_paths.push_back(FLAGS_image);
  68. } else {
  69. std::cerr << "image_list or image should be defined" << std::endl;
  70. return -1;
  71. }
  72. std::cout << "start model predict " << image_paths.size() << std::endl;
  73. // infer
  74. std::vector<PaddleDeploy::Result> results;
  75. std::vector<cv::Mat> imgs;
  76. cv::Mat img;
  77. for (auto i = 0; i < image_paths.size(); ++i) {
  78. img = cv::imread(image_paths[i]);
  79. if (img.empty()) {
  80. std::cerr << "Fail to read image: " << i << std::endl;
  81. return -1;
  82. }
  83. imgs.clear();
  84. imgs.push_back(std::move(img));
  85. model->Predict(imgs, &results);
  86. std::cout << "image: " << image_paths[i] << std::endl;
  87. for (auto j = 0; j < results.size(); ++j) {
  88. std::cout << "Result for sample " << j << std::endl;
  89. std::cout << results[j] << std::endl;
  90. }
  91. }
  92. return 0;
  93. }