Skip to content

Commit

Permalink
[Model] Update PPSeg Preprocess (PaddlePaddle#1007)
Browse files Browse the repository at this point in the history
* 更新PPSeg pybind and python

* 更新PPSeg pybind and python
  • Loading branch information
Zheng-Bicheng authored Dec 29, 2022
1 parent 9795a52 commit dd5759b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ cd ./build/install

## 运行结果展示
ClassifyResult(
label_ids: 153,
scores: 0.684570,
label_ids: 153,
scores: 0.684570,
)

## 注意事项
Expand All @@ -75,4 +75,4 @@ DisablePermute(C++)或`disable_permute(Python),在预处理阶段禁用数据
## 其它文档
- [ResNet50_vd Python 部署](../python)
- [模型预测结果说明](../../../../../../docs/api/vision_results/)
- [转换ResNet50_vd RKNN模型文档](../README.md)
- [转换ResNet50_vd RKNN模型文档](../README.md)
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ python3 infer.py --model_file ./ResNet50_vd_infer/ResNet50_vd_infer_rk3588.rknn

# 运行完成后返回结果如下所示
ClassifyResult(
label_ids: 153,
scores: 0.684570,
label_ids: 153,
scores: 0.684570,
)
```

Expand All @@ -32,4 +32,4 @@ DisablePermute(C++)或`disable_permute(Python),在预处理阶段禁用数据
## 其它文档
- [ResNet50_vd C++部署](../cpp)
- [模型预测结果说明](../../../../../../docs/api/vision_results/)
- [转换ResNet50_vd RKNN模型文档](../README.md)
- [转换ResNet50_vd RKNN模型文档](../README.md)
3 changes: 2 additions & 1 deletion examples/vision/segmentation/paddleseg/rknpu2/cpp/infer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
model.GetPreprocessor().DisableNormalizeAndPermute();
model.GetPreprocessor().DisablePermute();
model.GetPreprocessor().DisableNormalize();

fastdeploy::TimeCounter tc;
tc.Start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def build_option(args):
runtime_option=runtime_option,
model_format=fd.ModelFormat.RKNN)

model.preprocessor.disable_normalize_and_permute()
model.preprocessor.disable_normalize()
model.preprocessor.disable_permute()

# 预测图片分割结果
im = cv2.imread(args.image)
Expand Down
9 changes: 6 additions & 3 deletions fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ void BindPPSeg(pybind11::module& m) {
}
return make_pair(outputs, imgs_info);;
})
.def("disable_normalize_and_permute",
&vision::segmentation::PaddleSegPreprocessor::DisableNormalizeAndPermute)

.def("disable_normalize", [](vision::segmentation::PaddleSegPreprocessor& self) {
self.DisableNormalize();
})
.def("disable_permute", [](vision::segmentation::PaddleSegPreprocessor& self) {
self.DisablePermute();
})
.def_property("is_vertical_screen",
&vision::segmentation::PaddleSegPreprocessor::GetIsVerticalScreen,
&vision::segmentation::PaddleSegPreprocessor::SetIsVerticalScreen);
Expand Down
26 changes: 16 additions & 10 deletions fastdeploy/vision/segmentation/ppseg/preprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool PaddleSegPreprocessor::BuildPreprocessPipelineFromConfig() {
FDASSERT(op.IsMap(),
"Require the transform information in yaml be Map type.");
if (op["type"].as<std::string>() == "Normalize") {
if (!disable_normalize_and_permute_) {
if (!disable_normalize_) {
std::vector<float> mean = {0.5, 0.5, 0.5};
std::vector<float> std = {0.5, 0.5, 0.5};
if (op["mean"]) {
Expand All @@ -55,7 +55,7 @@ bool PaddleSegPreprocessor::BuildPreprocessPipelineFromConfig() {
processors_.push_back(std::make_shared<Normalize>(mean, std));
}
} else if (op["type"].as<std::string>() == "Resize") {
is_contain_resize_op = true;
is_contain_resize_op_ = true;
const auto& target_size = op["target_size"];
int resize_width = target_size[0].as<int>();
int resize_height = target_size[1].as<int>();
Expand All @@ -73,13 +73,13 @@ bool PaddleSegPreprocessor::BuildPreprocessPipelineFromConfig() {
auto input_shape = cfg["Deploy"]["input_shape"];
int input_height = input_shape[2].as<int>();
int input_width = input_shape[3].as<int>();
if (input_height != -1 && input_width != -1 && !is_contain_resize_op) {
is_contain_resize_op = true;
if (input_height != -1 && input_width != -1 && !is_contain_resize_op_) {
is_contain_resize_op_ = true;
processors_.insert(processors_.begin(),
std::make_shared<Resize>(input_width, input_height));
}
}
if (!disable_normalize_and_permute_) {
if (!disable_permute_) {
processors_.push_back(std::make_shared<HWC2CHW>());
}

Expand Down Expand Up @@ -121,7 +121,7 @@ bool PaddleSegPreprocessor::Run(std::vector<FDMat>* images, std::vector<FDTensor
}
size_t img_num = images->size();
// Batch preprocess : resize all images to the largest image shape in batch
if (!is_contain_resize_op && img_num > 1) {
if (!is_contain_resize_op_ && img_num > 1) {
int max_width = 0;
int max_height = 0;
for (size_t i = 0; i < img_num; ++i) {
Expand Down Expand Up @@ -156,14 +156,20 @@ bool PaddleSegPreprocessor::Run(std::vector<FDMat>* images, std::vector<FDTensor
return true;
}

void PaddleSegPreprocessor::DisableNormalizeAndPermute(){
disable_normalize_and_permute_ = true;
// the DisableNormalizeAndPermute function will be invalid if the configuration file is loaded during preprocessing
void PaddleSegPreprocessor::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 PaddleSegPreprocessor::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 segmentation
} // namespace vision
} // namespace fastdeploy
14 changes: 9 additions & 5 deletions fastdeploy/vision/segmentation/ppseg/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class FASTDEPLOY_DECL PaddleSegPreprocessor {
is_vertical_screen_ = value;
}

// This function will disable normalize and hwc2chw in preprocessing step.
void DisableNormalizeAndPermute();
/// This function will disable normalize in preprocessing step.
void DisableNormalize();
/// This function will disable hwc2chw in preprocessing step.
void DisablePermute();

private:
virtual bool BuildPreprocessPipelineFromConfig();
Expand All @@ -61,10 +63,12 @@ class FASTDEPLOY_DECL PaddleSegPreprocessor {
*/
bool is_vertical_screen_ = false;

// for recording the switch of normalize and hwc2chw
bool disable_normalize_and_permute_ = false;
// for recording the switch of hwc2chw
bool disable_permute_ = false;
// for recording the switch of normalize
bool disable_normalize_ = false;

bool is_contain_resize_op = false;
bool is_contain_resize_op_ = false;

bool initialized_ = false;
};
Expand Down
13 changes: 10 additions & 3 deletions python/fastdeploy/vision/segmentation/ppseg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,17 @@ def run(self, input_ims):
"""
return self._preprocessor.run(input_ims)

def disable_normalize_and_permute(self):
"""To disable normalize and hwc2chw in preprocessing step.
def disable_normalize(self):
"""
return self._preprocessor.disable_normalize_and_permute()
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()

@property
def is_vertical_screen(self):
Expand Down

0 comments on commit dd5759b

Please sign in to comment.