multi_gpu_model.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #pragma once
  15. #include <memory>
  16. #include <string>
  17. #include <thread> // NOLINT
  18. #include <vector>
  19. #include "model_deploy/common/include/model_factory.h"
  20. namespace PaddleDeploy {
  21. class MultiGPUModel {
  22. private:
  23. std::vector<std::shared_ptr<Model>> models_;
  24. public:
  25. bool Init(const std::string& model_type,
  26. const std::string& cfg_file, size_t gpu_num = 1) {
  27. models_.clear();
  28. for (auto i = 0; i < gpu_num; ++i) {
  29. std::shared_ptr<Model> model =
  30. PaddleDeploy::ModelFactory::CreateObject(model_type);
  31. if (!model) {
  32. std::cerr << "no model_type: " << model_type << std::endl;
  33. return false;
  34. }
  35. std::cerr << i + 1 << " model start init" << std::endl;
  36. if (!model->Init(cfg_file)) {
  37. std::cerr << "model Init error" << std::endl;
  38. return false;
  39. }
  40. models_.push_back(model);
  41. }
  42. return true;
  43. }
  44. bool PaddleEngineInit(const std::string& model_filename,
  45. const std::string& params_filename,
  46. const std::vector<int> gpu_ids) {
  47. if (gpu_ids.size() != models_.size()) {
  48. std::cerr << "Paddle Engine Init gpu_ids != MultiGPUModel Init gpu_num"
  49. << gpu_ids.size() << " != " << models_.size()
  50. << std::endl;
  51. return false;
  52. }
  53. for (auto i = 0; i < gpu_ids.size(); ++i) {
  54. if (!models_[i]->PaddleEngineInit(model_filename,
  55. params_filename,
  56. true, gpu_ids[i],
  57. true)) {
  58. std::cerr << "Paddle Engine Init error:" << gpu_ids[i] << std::endl;
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. bool Predict(const std::vector<cv::Mat>& imgs,
  65. std::vector<Result>* results,
  66. int thread_num = 1) {
  67. results->clear();
  68. int model_num = models_.size();
  69. if (model_num <= 0) {
  70. std::cerr << "Please Init before Predict!" << std::endl;
  71. return false;
  72. }
  73. int imgs_size = imgs.size();
  74. int start = 0;
  75. std::vector<std::thread> threads;
  76. std::vector<std::vector<cv::Mat>> split_imgs;
  77. std::vector<std::vector<Result>> model_results;
  78. for (int i = 0; i < model_num; ++i) {
  79. int img_num = static_cast<int>(imgs_size / model_num);
  80. if (i < imgs_size % model_num) {
  81. img_num += 1;
  82. } else if (img_num <= 0) {
  83. // imgs.size < model_.size
  84. break;
  85. }
  86. std::vector<cv::Mat> new_imgs(imgs.begin() + start,
  87. imgs.begin() + start + img_num);
  88. split_imgs.push_back(new_imgs);
  89. start += img_num;
  90. }
  91. model_results.resize(split_imgs.size());
  92. for (int i = 0; i < split_imgs.size(); ++i) {
  93. threads.push_back(std::thread(&PaddleDeploy::Model::Predict, models_[i],
  94. std::ref(split_imgs[i]),
  95. &model_results[i], thread_num));
  96. }
  97. for (auto& thread : threads) {
  98. if (thread.joinable()) {
  99. thread.join();
  100. }
  101. }
  102. // merge result
  103. for (auto model_result : model_results) {
  104. results->insert(results->end(),
  105. model_result.begin(), model_result.end());
  106. }
  107. return true;
  108. }
  109. };
  110. } // namespace PaddleDeploy