-
Notifications
You must be signed in to change notification settings - Fork 669
Add new model PaddleSeg #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e660031
Support new model PaddleSeg
felixhjh 9612e8c
Fix conflict
felixhjh a5a2eed
PaddleSeg add visulization function
felixhjh 6d57fee
Merge pull request #3 from PaddlePaddle/develop
felixhjh 3f44ce7
fix bug
felixhjh caf961b
Fix BindPPSeg wrong name
felixhjh 13216d0
Fix variable name
felixhjh 934793c
Merge branch 'develop' into ppseg
jiangjiajun 9810140
Update by comments
felixhjh 4dad52d
Merge branch 'ppseg' of https://github.com/felixhjh/FastDeploy into p…
felixhjh aac6ca2
Add ppseg-unet example python version
felixhjh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "fastdeploy/vision.h" | ||
| #include "yaml-cpp/yaml.h" | ||
|
|
||
| int main() { | ||
| namespace vis = fastdeploy::vision; | ||
|
|
||
| std::string model_file = "../resources/models/unet_Cityscapes/model.pdmodel"; | ||
| std::string params_file = | ||
| "../resources/models/unet_Cityscapes/model.pdiparams"; | ||
| std::string config_file = "../resources/models/unet_Cityscapes/deploy.yaml"; | ||
| std::string img_path = "../resources/images/cityscapes_demo.png"; | ||
| std::string vis_path = "../resources/outputs/vis.jpeg"; | ||
|
|
||
| auto model = vis::ppseg::Model(model_file, params_file, config_file); | ||
| if (!model.Initialized()) { | ||
| std::cerr << "Init Failed." << std::endl; | ||
| return -1; | ||
| } | ||
|
|
||
| cv::Mat im = cv::imread(img_path); | ||
| cv::Mat vis_im; | ||
|
|
||
| vis::SegmentationResult res; | ||
| if (!model.Predict(&im, &res)) { | ||
| std::cerr << "Prediction Failed." << std::endl; | ||
| return -1; | ||
| } else { | ||
| std::cout << "Prediction Done!" << std::endl; | ||
| } | ||
|
|
||
| // 输出预测框结果 | ||
| std::cout << res.Str() << std::endl; | ||
|
|
||
| YAML::Node cfg = YAML::LoadFile(config_file); | ||
| int num_classes = 19; | ||
| if (cfg["Deploy"]["num_classes"]) { | ||
| num_classes = cfg["Deploy"]["num_classes"].as<int>(); | ||
| } | ||
|
|
||
| // 可视化预测结果 | ||
| vis::Visualize::VisSegmentation(im, res, &vis_im, num_classes); | ||
| cv::imwrite(vis_path, vis_im); | ||
| std::cout << "Inference Done! Saved: " << vis_path << std::endl; | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import absolute_import | ||
| import logging | ||
| from ... import FastDeployModel, Frontend | ||
| from ... import fastdeploy_main as C | ||
|
|
||
|
|
||
| class Model(FastDeployModel): | ||
| def __init__(self, | ||
| model_file, | ||
| params_file, | ||
| config_file, | ||
| backend_option=None, | ||
| model_format=Frontend.PADDLE): | ||
| super(Model, self).__init__(backend_option) | ||
|
|
||
| assert model_format == Frontend.PADDLE, "PaddleSeg only support model format of Frontend.Paddle now." | ||
| self._model = C.vision.ppseg.Model(model_file, params_file, | ||
| config_file, self._runtime_option, | ||
| model_format) | ||
| assert self.initialized, "PaddleSeg model initialize failed." | ||
|
|
||
| def predict(self, input_image): | ||
| return self._model.predict(input_image) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #include "fastdeploy/vision/ppseg/model.h" | ||
| #include "fastdeploy/vision.h" | ||
| #include "fastdeploy/vision/utils/utils.h" | ||
| #include "yaml-cpp/yaml.h" | ||
|
|
||
| namespace fastdeploy { | ||
| namespace vision { | ||
| namespace ppseg { | ||
|
|
||
| Model::Model(const std::string& model_file, const std::string& params_file, | ||
| const std::string& config_file, const RuntimeOption& custom_option, | ||
| const Frontend& model_format) { | ||
| config_file_ = config_file; | ||
| valid_cpu_backends = {Backend::ORT, Backend::PDINFER}; | ||
| valid_gpu_backends = {Backend::ORT, Backend::PDINFER}; | ||
| runtime_option = custom_option; | ||
| runtime_option.model_format = model_format; | ||
| runtime_option.model_file = model_file; | ||
| runtime_option.params_file = params_file; | ||
| initialized = Initialize(); | ||
| } | ||
|
|
||
| bool Model::Initialize() { | ||
| if (!BuildPreprocessPipelineFromConfig()) { | ||
| FDERROR << "Failed to build preprocess pipeline from configuration file." | ||
| << std::endl; | ||
| return false; | ||
| } | ||
| if (!InitRuntime()) { | ||
| FDERROR << "Failed to initialize fastdeploy backend." << std::endl; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool Model::BuildPreprocessPipelineFromConfig() { | ||
| processors_.clear(); | ||
| YAML::Node cfg; | ||
| processors_.push_back(std::make_shared<BGR2RGB>()); | ||
| try { | ||
| cfg = YAML::LoadFile(config_file_); | ||
| } catch (YAML::BadFile& e) { | ||
| FDERROR << "Failed to load yaml file " << config_file_ | ||
| << ", maybe you should check this file." << std::endl; | ||
| return false; | ||
| } | ||
|
|
||
| if (cfg["Deploy"]["transforms"]) { | ||
| auto preprocess_cfg = cfg["Deploy"]["transforms"]; | ||
| for (const auto& op : preprocess_cfg) { | ||
| FDASSERT(op.IsMap(), | ||
| "Require the transform information in yaml be Map type."); | ||
| if (op["type"].as<std::string>() == "Normalize") { | ||
| std::vector<float> mean = {0.5, 0.5, 0.5}; | ||
| std::vector<float> std = {0.5, 0.5, 0.5}; | ||
| if (op["mean"]) { | ||
| mean = op["mean"].as<std::vector<float>>(); | ||
| } | ||
| if (op["std"]) { | ||
| std = op["std"].as<std::vector<float>>(); | ||
| } | ||
| processors_.push_back(std::make_shared<Normalize>(mean, std)); | ||
|
|
||
| } else if (op["type"].as<std::string>() == "Resize") { | ||
| const auto& target_size = op["target_size"]; | ||
| int resize_width = target_size[0].as<int>(); | ||
| int resize_height = target_size[1].as<int>(); | ||
| processors_.push_back( | ||
| std::make_shared<Resize>(resize_width, resize_height)); | ||
| } | ||
| } | ||
| processors_.push_back(std::make_shared<HWC2CHW>()); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool Model::Preprocess(Mat* mat, FDTensor* output) { | ||
| for (size_t i = 0; i < processors_.size(); ++i) { | ||
| if (!(*(processors_[i].get()))(mat)) { | ||
| FDERROR << "Failed to process image data in " << processors_[i]->Name() | ||
| << "." << std::endl; | ||
| return false; | ||
| } | ||
| } | ||
| mat->ShareWithTensor(output); | ||
| output->shape.insert(output->shape.begin(), 1); | ||
| output->name = InputInfoOfRuntime(0).name; | ||
| return true; | ||
| } | ||
|
|
||
| bool Model::Postprocess(const FDTensor& infer_result, | ||
| SegmentationResult* result) { | ||
| FDASSERT(infer_result.dtype == FDDataType::INT64, | ||
| "Require the data type of output is int64, but now it's " + | ||
| Str(const_cast<fastdeploy::FDDataType&>(infer_result.dtype)) + | ||
| "."); | ||
| result->Clear(); | ||
| std::vector<int64_t> output_shape = infer_result.shape; | ||
| int out_num = std::accumulate(output_shape.begin(), output_shape.end(), 1, | ||
| std::multiplies<int>()); | ||
| const int64_t* infer_result_buffer = | ||
| reinterpret_cast<const int64_t*>(infer_result.data.data()); | ||
| int64_t height = output_shape[1]; | ||
| int64_t width = output_shape[2]; | ||
| result->Resize(height, width); | ||
| for (int64_t i = 0; i < height; i++) { | ||
| int64_t begin = i * width; | ||
| int64_t end = (i + 1) * width - 1; | ||
| std::copy(infer_result_buffer + begin, infer_result_buffer + end, | ||
| result->masks[i].begin()); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool Model::Predict(cv::Mat* im, SegmentationResult* result) { | ||
| Mat mat(*im); | ||
| std::vector<FDTensor> processed_data(1); | ||
| if (!Preprocess(&mat, &(processed_data[0]))) { | ||
| FDERROR << "Failed to preprocess input data while using model:" | ||
| << ModelName() << "." << std::endl; | ||
| return false; | ||
| } | ||
| std::vector<FDTensor> infer_result(1); | ||
| if (!Infer(processed_data, &infer_result)) { | ||
| FDERROR << "Failed to inference while using model:" << ModelName() << "." | ||
| << std::endl; | ||
| return false; | ||
| } | ||
| if (!Postprocess(infer_result[0], result)) { | ||
| FDERROR << "Failed to postprocess while using model:" << ModelName() << "." | ||
| << std::endl; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace ppseg | ||
| } // namespace vision | ||
| } // namespace fastdeploy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #pragma once | ||
| #include "fastdeploy/fastdeploy_model.h" | ||
| #include "fastdeploy/vision/common/processors/transform.h" | ||
| #include "fastdeploy/vision/common/result.h" | ||
|
|
||
| namespace fastdeploy { | ||
| namespace vision { | ||
| namespace ppseg { | ||
|
|
||
| class FASTDEPLOY_DECL Model : public FastDeployModel { | ||
| public: | ||
| Model(const std::string& model_file, const std::string& params_file, | ||
| const std::string& config_file, | ||
| const RuntimeOption& custom_option = RuntimeOption(), | ||
| const Frontend& model_format = Frontend::PADDLE); | ||
|
|
||
| std::string ModelName() const { return "ppseg"; } | ||
|
|
||
| virtual bool Predict(cv::Mat* im, SegmentationResult* result); | ||
|
|
||
| private: | ||
| bool Initialize(); | ||
|
|
||
| bool BuildPreprocessPipelineFromConfig(); | ||
|
|
||
| bool Preprocess(Mat* mat, FDTensor* outputs); | ||
|
|
||
| bool Postprocess(const FDTensor& infer_result, SegmentationResult* result); | ||
|
|
||
| std::vector<std::shared_ptr<Processor>> processors_; | ||
| std::string config_file_; | ||
| }; | ||
| } // namespace ppseg | ||
| } // namespace vision | ||
| } // namespace fastdeploy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| #include "fastdeploy/pybind/main.h" | ||
|
|
||
| namespace fastdeploy { | ||
| void BindPPSeg(pybind11::module& m) { | ||
| auto ppseg_module = | ||
| m.def_submodule("ppseg", "Module to deploy PaddleSegmentation."); | ||
| pybind11::class_<vision::ppseg::Model, FastDeployModel>(ppseg_module, "Model") | ||
| .def(pybind11::init<std::string, std::string, std::string, RuntimeOption, | ||
| Frontend>()) | ||
| .def("predict", [](vision::ppseg::Model& self, pybind11::array& data) { | ||
| auto mat = PyArrayToCvMat(data); | ||
| vision::SegmentationResult res; | ||
| self.Predict(&mat, &res); | ||
| return res; | ||
| }); | ||
| } | ||
| } // namespace fastdeploy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看这里是设定模型的输入是已经过完argmax,输出的int64的值,那在函数初始加一个判断吧