Skip to content
1 change: 1 addition & 0 deletions fastdeploy/vision/detection/ppdet/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 7 additions & 1 deletion fastdeploy/vision/detection/ppdet/ppdet_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
65 changes: 41 additions & 24 deletions fastdeploy/vision/detection/ppdet/preprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -45,21 +45,23 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
for (const auto& op : cfg["Preprocess"]) {
std::string op_name = op["type"].as<std::string>();
if (op_name == "NormalizeImage") {
auto mean = op["mean"].as<std::vector<float>>();
auto std = op["std"].as<std::vector<float>>();
bool is_scale = true;
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);
if (!disable_normalize_) {
auto mean = op["mean"].as<std::vector<float>>();
auto std = op["std"].as<std::vector<float>>();
bool is_scale = true;
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));
}
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>>();
Expand Down Expand Up @@ -104,10 +106,12 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig(
return false;
}
}
if (has_permute) {
// permute = cast<float> + HWC2CHW
processors_.push_back(std::make_shared<Cast>("float"));
processors_.push_back(std::make_shared<HWC2CHW>());
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
Expand Down Expand Up @@ -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
13 changes: 12 additions & 1 deletion fastdeploy/vision/detection/ppdet/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion python/fastdeploy/vision/detection/ppdet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ def run(self, 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:
def __init__(self):
Expand Down Expand Up @@ -500,4 +512,4 @@ def __init__(self,
self._model = C.vision.detection.RTMDet(
model_file, params_file, config_file, self._runtime_option,
model_format)
assert self.initialized, "RTMDet model initialize failed."
assert self.initialized, "RTMDet model initialize failed."