model_infer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_name, "", "Name of inference model");
  21. DEFINE_string(url, "", "url of triton server");
  22. DEFINE_string(model_version, "", "model version of triton server");
  23. DEFINE_string(cfg_file, "", "Path of yaml file");
  24. DEFINE_string(model_type, "", "model type");
  25. DEFINE_string(image, "", "Path of test image file");
  26. DEFINE_string(image_list, "", "Path of test image file");
  27. int main(int argc, char** argv) {
  28. // Parsing command-line
  29. google::ParseCommandLineFlags(&argc, &argv, true);
  30. std::cout << "ParseCommandLineFlags:FLAGS_model_type="
  31. << FLAGS_model_type << " model_name="
  32. << FLAGS_model_name << std::endl;
  33. // create model
  34. PaddleDeploy::Model* model = PaddleDeploy::CreateModel(FLAGS_model_type);
  35. if (!model) {
  36. std::cout << "no model_type: " << FLAGS_model_type
  37. << " model=" << model << std::endl;
  38. return 0;
  39. }
  40. std::cout << "start model init " << std::endl;
  41. // model init
  42. model->Init(FLAGS_cfg_file);
  43. std::cout << "start engine init " << std::endl;
  44. // inference engine init
  45. PaddleDeploy::TritonEngineConfig engine_config;
  46. engine_config.url_ = FLAGS_url;
  47. engine_config.model_name_ = FLAGS_model_name;
  48. engine_config.model_version_ = FLAGS_model_version;
  49. model->TritonEngineInit(engine_config);
  50. // prepare data
  51. std::vector<std::string> image_paths;
  52. if (FLAGS_image_list != "") {
  53. std::ifstream inf(FLAGS_image_list);
  54. if (!inf) {
  55. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  56. return -1;
  57. }
  58. std::string image_path;
  59. while (getline(inf, image_path)) {
  60. image_paths.push_back(image_path);
  61. }
  62. } else if (FLAGS_image != "") {
  63. image_paths.push_back(FLAGS_image);
  64. } else {
  65. std::cerr << "image_list or image should be defined" << std::endl;
  66. return -1;
  67. }
  68. std::cout << "start model predict " << image_paths.size() << std::endl;
  69. // infer
  70. std::vector<PaddleDeploy::Result> results;
  71. std::vector<cv::Mat> imgs;
  72. cv::Mat img;
  73. for (auto i = 0; i < image_paths.size(); ++i) {
  74. img = cv::imread(image_paths[i]);
  75. if (img.empty()) {
  76. std::cerr << "Fail to read image: " << i << std::endl;
  77. return -1;
  78. }
  79. imgs.clear();
  80. imgs.push_back(std::move(img));
  81. model->Predict(imgs, &results);
  82. std::cout << "image: " << image_paths[i] << std::endl;
  83. for (auto j = 0; j < results.size(); ++j) {
  84. std::cout << "Result for sample " << j << std::endl;
  85. std::cout << results[j] << std::endl;
  86. }
  87. }
  88. delete model;
  89. return 0;
  90. }