tensorrt_infer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <string>
  16. #include <vector>
  17. #include "model_deploy/common/include/paddle_deploy.h"
  18. DEFINE_string(model_filename, "", "Path of det inference model");
  19. DEFINE_string(params_filename, "", "Path of det inference params");
  20. DEFINE_string(cfg_file, "", "Path of yaml file");
  21. DEFINE_string(model_type, "", "model type");
  22. DEFINE_string(image, "", "Path of test image file");
  23. DEFINE_int32(gpu_id, 0, "GPU card id");
  24. int main(int argc, char** argv) {
  25. // Parsing command-line
  26. google::ParseCommandLineFlags(&argc, &argv, true);
  27. // create model
  28. PaddleDeploy::Model* model = PaddleDeploy::CreateModel(FLAGS_model_type);
  29. // model init
  30. model->Init(FLAGS_cfg_file);
  31. // inference engine init
  32. PaddleDeploy::PaddleEngineConfig engine_config;
  33. engine_config.model_filename = FLAGS_model_filename;
  34. engine_config.params_filename = FLAGS_params_filename;
  35. engine_config.gpu_id = FLAGS_gpu_id;
  36. engine_config.use_gpu = true;
  37. engine_config.use_trt = true;
  38. engine_config.precision = 0;
  39. engine_config.min_subgraph_size = 10;
  40. engine_config.max_workspace_size = 1 << 30;
  41. if ("clas" == FLAGS_model_type) {
  42. // Adjust shape according to the actual model
  43. engine_config.min_input_shape["inputs"] = {1, 3, 224, 224};
  44. engine_config.max_input_shape["inputs"] = {1, 3, 224, 224};
  45. engine_config.optim_input_shape["inputs"] = {1, 3, 224, 224};
  46. } else if ("det" == FLAGS_model_type) {
  47. // Adjust shape according to the actual model
  48. engine_config.min_input_shape["image"] = {1, 3, 608, 608};
  49. engine_config.max_input_shape["image"] = {1, 3, 608, 608};
  50. engine_config.optim_input_shape["image"] = {1, 3, 608, 608};
  51. } else if ("seg" == FLAGS_model_type) {
  52. engine_config.min_input_shape["x"] = {1, 3, 100, 100};
  53. engine_config.max_input_shape["x"] = {1, 3, 2000, 2000};
  54. engine_config.optim_input_shape["x"] = {1, 3, 1024, 1024};
  55. // Additional nodes need to be added, pay attention to the output prompt
  56. }
  57. model->PaddleEngineInit(engine_config);
  58. // prepare data
  59. std::vector<cv::Mat> imgs;
  60. imgs.push_back(std::move(cv::imread(FLAGS_image)));
  61. // predict
  62. std::vector<PaddleDeploy::Result> results;
  63. model->Predict(imgs, &results, 1);
  64. std::cout << results[0] << std::endl;
  65. delete model;
  66. return 0;
  67. }