x_model.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/paddlex/include/x_model.h"
  15. #include "model_deploy/paddlex/include/x_standard_config.h"
  16. #include <fstream>
  17. namespace PaddleDeploy {
  18. bool PaddleXModel::GenerateTransformsConfig(const YAML::Node& src) {
  19. XEssential(src, &yaml_config_);
  20. for (const auto& op : src["Transforms"]) {
  21. std::string op_name = op.begin()->first.as<std::string>();
  22. if (op_name == "Normalize") {
  23. if (src["version"].as<std::string>() >= "2.0.0") {
  24. yaml_config_["transforms"]["Convert"]["dtype"] = "float";
  25. }
  26. XNormalize(op.begin()->second, &yaml_config_);
  27. } else if (op_name == "ResizeByShort") {
  28. XResizeByShort(op.begin()->second, &yaml_config_);
  29. } else if (op_name == "ResizeByLong") {
  30. XResizeByLong(op.begin()->second, &yaml_config_);
  31. } else if (op_name == "Padding") {
  32. if (src["version"].as<std::string>() >= "2.0.0") {
  33. XPaddingV2(op.begin()->second, &yaml_config_);
  34. } else {
  35. XPadding(op.begin()->second, &yaml_config_);
  36. }
  37. } else if (op_name == "CenterCrop") {
  38. XCenterCrop(op.begin()->second, &yaml_config_);
  39. } else if (op_name == "Resize") {
  40. XResize(op.begin()->second, &yaml_config_);
  41. } else {
  42. std::cerr << "Unexpected transforms op name: '"
  43. << op_name << "'" << std::endl;
  44. return false;
  45. }
  46. }
  47. yaml_config_["transforms"]["Permute"] = YAML::Null;
  48. return true;
  49. }
  50. bool PaddleXModel::YamlConfigInit(const std::string& cfg_file,
  51. const std::string key) {
  52. YAML::Node x_config;
  53. if ("" == key) {
  54. x_config = YAML::LoadFile(cfg_file);
  55. } else {
  56. #ifdef PADDLEX_DEPLOY_ENCRYPTION
  57. std::string cfg = decrypt_file(cfg_file.c_str(), key.c_str());
  58. x_config = YAML::Load(cfg);
  59. #else
  60. std::cerr << "Don't open encryption on compile" << std::endl;
  61. return false;
  62. #endif // PADDLEX_DEPLOY_ENCRYPTION
  63. }
  64. yaml_config_["model_format"] = "Paddle";
  65. yaml_config_["toolkit"] = "PaddleX";
  66. yaml_config_["version"] = x_config["version"].as<std::string>();
  67. yaml_config_["model_type"] =
  68. x_config["_Attributes"]["model_type"].as<std::string>();
  69. yaml_config_["model_name"] = x_config["Model"].as<std::string>();
  70. int i = 0;
  71. for (const auto& label : x_config["_Attributes"]["labels"]) {
  72. yaml_config_["labels"][i] = label.as<std::string>();
  73. i++;
  74. }
  75. // Generate Standard Transforms Configuration
  76. if (!GenerateTransformsConfig(x_config)) {
  77. std::cerr << "Fail to generate standard configuration "
  78. << "of tranforms" << std::endl;
  79. return false;
  80. }
  81. return true;
  82. }
  83. bool PaddleXModel::PreprocessInit() {
  84. preprocess_ = std::make_shared<XPreprocess>();
  85. if (!preprocess_->Init(yaml_config_)) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. bool PaddleXModel::PostprocessInit() {
  91. postprocess_ = std::make_shared<XPostprocess>();
  92. if (!postprocess_->Init(yaml_config_)) {
  93. return false;
  94. }
  95. return true;
  96. }
  97. } // namespace PaddleDeploy