seg_model.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/ppseg/include/seg_model.h"
  15. #include "model_deploy/ppseg/include/seg_standard_config.h"
  16. namespace PaddleDeploy {
  17. bool SegModel::GenerateTransformsConfig(const YAML::Node& src) {
  18. yaml_config_["transforms"]["BGR2RGB"] = YAML::Null;
  19. yaml_config_["transforms"]["Convert"]["dtype"] = "float";
  20. for (const auto& op : src) {
  21. assert(op["type"].IsDefined());
  22. std::string op_name = op["type"].as<std::string>();
  23. if (op_name == "Normalize") {
  24. SegNormalize(op, &yaml_config_);
  25. } else if (op_name == "Resize") {
  26. SegResize(op, &yaml_config_);
  27. } else if (op_name == "Padding") {
  28. SegPadding(op, &yaml_config_);
  29. } else {
  30. std::cerr << "Unexpected transforms op name: '"
  31. << op_name << "'" << std::endl;
  32. return false;
  33. }
  34. }
  35. yaml_config_["transforms"]["Permute"] = YAML::Null;
  36. return true;
  37. }
  38. bool SegModel::YamlConfigInit(const std::string& cfg_file,
  39. const std::string key) {
  40. YAML::Node seg_config;
  41. if ("" == key) {
  42. seg_config = YAML::LoadFile(cfg_file);
  43. } else {
  44. #ifdef PADDLEX_DEPLOY_ENCRYPTION
  45. std::string cfg = decrypt_file(cfg_file.c_str(), key.c_str());
  46. seg_config = YAML::Load(cfg);
  47. #else
  48. std::cerr << "Don't open encryption on compile" << std::endl;
  49. return false;
  50. #endif // PADDLEX_DEPLOY_ENCRYPTION
  51. }
  52. yaml_config_["model_format"] = "Paddle";
  53. yaml_config_["toolkit"] = "PaddleSeg";
  54. yaml_config_["toolkit_version"] = "Unknown";
  55. // Generate Standard Transforms Configuration
  56. if (!GenerateTransformsConfig(seg_config["Deploy"]["transforms"])) {
  57. std::cerr << "Fail to generate standard configuration "
  58. << "of tranforms" << std::endl;
  59. return false;
  60. }
  61. return true;
  62. }
  63. bool SegModel::PreprocessInit() {
  64. preprocess_ = std::make_shared<SegPreprocess>();
  65. if (!preprocess_->Init(yaml_config_))
  66. return false;
  67. return true;
  68. }
  69. bool SegModel::PostprocessInit() {
  70. postprocess_ = std::make_shared<SegPostprocess>();
  71. if (!postprocess_->Init(yaml_config_))
  72. return false;
  73. return true;
  74. }
  75. } // namespace PaddleDeploy