[Model] Update PPDet Preprocess (#1006)

* 更新导航文档

* 更新导航文档

* 更新导航文档

* 更新导航文档

* 更新PPDet PreProcess

* 更新PPDet PreProcess

* 更新PPDet pybind and python

* 更新

* 更新ppdet
This commit is contained in:
Zheng-Bicheng
2022-12-29 21:15:23 +08:00
committed by GitHub
parent dd5759bd99
commit d275c3ba02
5 changed files with 74 additions and 27 deletions

View File

@@ -68,6 +68,7 @@ class FASTDEPLOY_DECL PPYOLOE : public PPDetBase {
valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT}; valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
valid_timvx_backends = {Backend::LITE}; valid_timvx_backends = {Backend::LITE};
valid_kunlunxin_backends = {Backend::LITE}; valid_kunlunxin_backends = {Backend::LITE};
valid_rknpu_backends = {Backend::RKNPU2};
valid_ascend_backends = {Backend::LITE}; valid_ascend_backends = {Backend::LITE};
initialized = Initialize(); initialized = Initialize();
} }

View File

@@ -31,7 +31,13 @@ void BindPPDet(pybind11::module& m) {
outputs[i].StopSharing(); outputs[i].StopSharing();
} }
return outputs; return outputs;
}); })
.def("disable_normalize", [](vision::detection::PaddleDetPreprocessor& self) {
self.DisableNormalize();
})
.def("disable_permute", [](vision::detection::PaddleDetPreprocessor& self) {
self.DisablePermute();
});;
pybind11::class_<vision::detection::PaddleDetPostprocessor>( pybind11::class_<vision::detection::PaddleDetPostprocessor>(
m, "PaddleDetPostprocessor") m, "PaddleDetPostprocessor")

View File

@@ -22,19 +22,19 @@ namespace vision {
namespace detection { namespace detection {
PaddleDetPreprocessor::PaddleDetPreprocessor(const std::string& config_file) { PaddleDetPreprocessor::PaddleDetPreprocessor(const std::string& config_file) {
FDASSERT(BuildPreprocessPipelineFromConfig(config_file), this->config_file_ = config_file;
FDASSERT(BuildPreprocessPipelineFromConfig(),
"Failed to create PaddleDetPreprocessor."); "Failed to create PaddleDetPreprocessor.");
initialized_ = true; initialized_ = true;
} }
bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig( bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig() {
const std::string& config_file) {
processors_.clear(); processors_.clear();
YAML::Node cfg; YAML::Node cfg;
try { try {
cfg = YAML::LoadFile(config_file); cfg = YAML::LoadFile(config_file_);
} catch (YAML::BadFile& e) { } catch (YAML::BadFile& e) {
FDERROR << "Failed to load yaml file " << config_file FDERROR << "Failed to load yaml file " << config_file_
<< ", maybe you should check this file." << std::endl; << ", maybe you should check this file." << std::endl;
return false; return false;
} }
@@ -45,21 +45,23 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
for (const auto& op : cfg["Preprocess"]) { for (const auto& op : cfg["Preprocess"]) {
std::string op_name = op["type"].as<std::string>(); std::string op_name = op["type"].as<std::string>();
if (op_name == "NormalizeImage") { if (op_name == "NormalizeImage") {
auto mean = op["mean"].as<std::vector<float>>(); if (!disable_normalize_) {
auto std = op["std"].as<std::vector<float>>(); auto mean = op["mean"].as<std::vector<float>>();
bool is_scale = true; auto std = op["std"].as<std::vector<float>>();
if (op["is_scale"]) { bool is_scale = true;
is_scale = op["is_scale"].as<bool>(); if (op["is_scale"]) {
is_scale = op["is_scale"].as<bool>();
}
std::string norm_type = "mean_std";
if (op["norm_type"]) {
norm_type = op["norm_type"].as<std::string>();
}
if (norm_type != "mean_std") {
std::fill(mean.begin(), mean.end(), 0.0);
std::fill(std.begin(), std.end(), 1.0);
}
processors_.push_back(std::make_shared<Normalize>(mean, std, is_scale));
} }
std::string norm_type = "mean_std";
if (op["norm_type"]) {
norm_type = op["norm_type"].as<std::string>();
}
if (norm_type != "mean_std") {
std::fill(mean.begin(), mean.end(), 0.0);
std::fill(std.begin(), std.end(), 1.0);
}
processors_.push_back(std::make_shared<Normalize>(mean, std, is_scale));
} else if (op_name == "Resize") { } else if (op_name == "Resize") {
bool keep_ratio = op["keep_ratio"].as<bool>(); bool keep_ratio = op["keep_ratio"].as<bool>();
auto target_size = op["target_size"].as<std::vector<int>>(); auto target_size = op["target_size"].as<std::vector<int>>();
@@ -104,10 +106,12 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
return false; return false;
} }
} }
if (has_permute) { if (!disable_permute_) {
// permute = cast<float> + HWC2CHW if (has_permute) {
processors_.push_back(std::make_shared<Cast>("float")); // permute = cast<float> + HWC2CHW
processors_.push_back(std::make_shared<HWC2CHW>()); processors_.push_back(std::make_shared<Cast>("float"));
processors_.push_back(std::make_shared<HWC2CHW>());
}
} }
// Fusion will improve performance // Fusion will improve performance
@@ -202,7 +206,20 @@ bool PaddleDetPreprocessor::Run(std::vector<FDMat>* images,
return true; return true;
} }
void PaddleDetPreprocessor::DisableNormalize() {
this->disable_normalize_ = true;
// the DisableNormalize function will be invalid if the configuration file is loaded during preprocessing
if (!BuildPreprocessPipelineFromConfig()) {
FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl;
}
}
void PaddleDetPreprocessor::DisablePermute() {
this->disable_permute_ = true;
// the DisablePermute function will be invalid if the configuration file is loaded during preprocessing
if (!BuildPreprocessPipelineFromConfig()) {
FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl;
}
}
} // namespace detection } // namespace detection
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -39,10 +39,21 @@ class FASTDEPLOY_DECL PaddleDetPreprocessor {
*/ */
bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs); bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs);
/// This function will disable normalize in preprocessing step.
void DisableNormalize();
/// This function will disable hwc2chw in preprocessing step.
void DisablePermute();
private: private:
bool BuildPreprocessPipelineFromConfig(const std::string& config_file); bool BuildPreprocessPipelineFromConfig();
std::vector<std::shared_ptr<Processor>> processors_; std::vector<std::shared_ptr<Processor>> processors_;
bool initialized_ = false; bool initialized_ = false;
// for recording the switch of hwc2chw
bool disable_permute_ = false;
// for recording the switch of normalize
bool disable_normalize_ = false;
// read config file
std::string config_file_;
}; };
} // namespace detection } // namespace detection

View File

@@ -36,6 +36,18 @@ class PaddleDetPreprocessor:
""" """
return self._preprocessor.run(input_ims) return self._preprocessor.run(input_ims)
def disable_normalize(self):
"""
This function will disable normalize in preprocessing step.
"""
self._preprocessor.disable_normalize()
def disable_permute(self):
"""
This function will disable hwc2chw in preprocessing step.
"""
self._preprocessor.disable_permute()
class PaddleDetPostprocessor: class PaddleDetPostprocessor:
def __init__(self): def __init__(self):
@@ -500,4 +512,4 @@ class RTMDet(PPYOLOE):
self._model = C.vision.detection.RTMDet( self._model = C.vision.detection.RTMDet(
model_file, params_file, config_file, self._runtime_option, model_file, params_file, config_file, self._runtime_option,
model_format) model_format)
assert self.initialized, "RTMDet model initialize failed." assert self.initialized, "RTMDet model initialize failed."