[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_timvx_backends = {Backend::LITE};
valid_kunlunxin_backends = {Backend::LITE};
valid_rknpu_backends = {Backend::RKNPU2};
valid_ascend_backends = {Backend::LITE};
initialized = Initialize();
}

View File

@@ -31,7 +31,13 @@ void BindPPDet(pybind11::module& m) {
outputs[i].StopSharing();
}
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>(
m, "PaddleDetPostprocessor")

View File

@@ -22,19 +22,19 @@ namespace vision {
namespace detection {
PaddleDetPreprocessor::PaddleDetPreprocessor(const std::string& config_file) {
FDASSERT(BuildPreprocessPipelineFromConfig(config_file),
this->config_file_ = config_file;
FDASSERT(BuildPreprocessPipelineFromConfig(),
"Failed to create PaddleDetPreprocessor.");
initialized_ = true;
}
bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
const std::string& config_file) {
bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig() {
processors_.clear();
YAML::Node cfg;
try {
cfg = YAML::LoadFile(config_file);
cfg = YAML::LoadFile(config_file_);
} 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;
return false;
}
@@ -45,6 +45,7 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
for (const auto& op : cfg["Preprocess"]) {
std::string op_name = op["type"].as<std::string>();
if (op_name == "NormalizeImage") {
if (!disable_normalize_) {
auto mean = op["mean"].as<std::vector<float>>();
auto std = op["std"].as<std::vector<float>>();
bool is_scale = true;
@@ -60,6 +61,7 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
std::fill(std.begin(), std.end(), 1.0);
}
processors_.push_back(std::make_shared<Normalize>(mean, std, is_scale));
}
} else if (op_name == "Resize") {
bool keep_ratio = op["keep_ratio"].as<bool>();
auto target_size = op["target_size"].as<std::vector<int>>();
@@ -104,11 +106,13 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
return false;
}
}
if (!disable_permute_) {
if (has_permute) {
// permute = cast<float> + HWC2CHW
processors_.push_back(std::make_shared<Cast>("float"));
processors_.push_back(std::make_shared<HWC2CHW>());
}
}
// Fusion will improve performance
FuseTransforms(&processors_);
@@ -202,7 +206,20 @@ bool PaddleDetPreprocessor::Run(std::vector<FDMat>* images,
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 vision
} // namespace fastdeploy

View File

@@ -39,10 +39,21 @@ class FASTDEPLOY_DECL PaddleDetPreprocessor {
*/
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:
bool BuildPreprocessPipelineFromConfig(const std::string& config_file);
bool BuildPreprocessPipelineFromConfig();
std::vector<std::shared_ptr<Processor>> processors_;
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

View File

@@ -36,6 +36,18 @@ class PaddleDetPreprocessor:
"""
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:
def __init__(self):