det_model.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "model_deploy/ppdet/include/det_model.h"
  15. #include "model_deploy/ppdet/include/det_standard_config.h"
  16. namespace PaddleDeploy {
  17. bool DetModel::GenerateTransformsConfig(const YAML::Node& src) {
  18. assert(src["Preprocess"].IsDefined());
  19. assert(src["arch"].IsDefined());
  20. std::string model_arch = src["arch"].as<std::string>();
  21. yaml_config_["transforms"]["BGR2RGB"] = YAML::Null;
  22. for (const auto& op : src["Preprocess"]) {
  23. assert(op["type"].IsDefined());
  24. std::string op_name = op["type"].as<std::string>();
  25. if (op_name == "Normalize") {
  26. DetNormalize(op, &yaml_config_);
  27. } else if (op_name == "NormalizeImage") {
  28. DetNormalize(op, &yaml_config_);
  29. } else if (op_name == "Permute") {
  30. DetPermute(op, &yaml_config_);
  31. } else if (op_name == "Resize") {
  32. DetResize(op, &yaml_config_, model_arch);
  33. } else if (op_name == "PadStride") {
  34. DetPadStride(op, &yaml_config_);
  35. } else {
  36. std::cerr << "Unexpected transforms op name: '"
  37. << op_name << "'" << std::endl;
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. bool DetModel::YamlConfigInit(const std::string& cfg_file,
  44. const std::string key) {
  45. YAML::Node det_config;
  46. if ("" == key) {
  47. det_config = YAML::LoadFile(cfg_file);
  48. } else {
  49. #ifdef PADDLEX_DEPLOY_ENCRYPTION
  50. std::string cfg = decrypt_file(cfg_file.c_str(), key.c_str());
  51. det_config = YAML::Load(cfg);
  52. #else
  53. std::cerr << "Don't open encryption on compile" << std::endl;
  54. return false;
  55. #endif // PADDLEX_DEPLOY_ENCRYPTION
  56. }
  57. yaml_config_["model_format"] = "Paddle";
  58. // arch support value:YOLO, SSD, RetinaNet, RCNN, Face
  59. if (!det_config["arch"].IsDefined()) {
  60. std::cerr << "Fail to find arch in PaddleDection yaml file" << std::endl;
  61. return false;
  62. } else if (!det_config["label_list"].IsDefined()) {
  63. std::cerr << "Fail to find label_list in "
  64. << "PaddleDection yaml file"
  65. << std::endl;
  66. return false;
  67. }
  68. yaml_config_["model_name"] = det_config["arch"].as<std::string>();
  69. yaml_config_["toolkit"] = "PaddleDetection";
  70. if (det_config["version"].IsDefined()) {
  71. yaml_config_["version"] = det_config["version"].as<std::string>();
  72. } else if (det_config["use_python_inference"].IsDefined()) {
  73. yaml_config_["version"] = "0.5";
  74. } else if (!det_config["use_python_inference"].IsDefined()) {
  75. yaml_config_["version"] = "2.0";
  76. }
  77. int i = 0;
  78. for (const auto& label : det_config["label_list"]) {
  79. yaml_config_["labels"][i] = label.as<std::string>();
  80. i++;
  81. }
  82. // Generate Standard Transforms Configuration
  83. if (!GenerateTransformsConfig(det_config)) {
  84. std::cerr << "Fail to generate standard configuration "
  85. << "of tranforms" << std::endl;
  86. return false;
  87. }
  88. return true;
  89. }
  90. bool DetModel::PreprocessInit() {
  91. preprocess_ = std::make_shared<DetPreprocess>();
  92. if (!preprocess_->Init(yaml_config_))
  93. return false;
  94. return true;
  95. }
  96. bool DetModel::PostprocessInit() {
  97. postprocess_ = std::make_shared<DetPostprocess>();
  98. if (!postprocess_->Init(yaml_config_))
  99. return false;
  100. return true;
  101. }
  102. } // namespace PaddleDeploy