diff --git a/csrc/fastdeploy/vision.h b/csrc/fastdeploy/vision.h index fd0824e48..e93ed6199 100644 --- a/csrc/fastdeploy/vision.h +++ b/csrc/fastdeploy/vision.h @@ -15,6 +15,7 @@ #include "fastdeploy/core/config.h" #ifdef ENABLE_VISION +#include "fastdeploy/vision/classification/ppcls/model.h" #include "fastdeploy/vision/detection/contrib/nanodet_plus.h" #include "fastdeploy/vision/detection/contrib/scaledyolov4.h" #include "fastdeploy/vision/detection/contrib/yolor.h" @@ -23,6 +24,7 @@ #include "fastdeploy/vision/detection/contrib/yolov6.h" #include "fastdeploy/vision/detection/contrib/yolov7.h" #include "fastdeploy/vision/detection/contrib/yolox.h" +#include "fastdeploy/vision/detection/ppdet/model.h" #include "fastdeploy/vision/facedet/contrib/retinaface.h" #include "fastdeploy/vision/facedet/contrib/scrfd.h" #include "fastdeploy/vision/facedet/contrib/ultraface.h" @@ -33,9 +35,7 @@ #include "fastdeploy/vision/faceid/contrib/partial_fc.h" #include "fastdeploy/vision/faceid/contrib/vpl.h" #include "fastdeploy/vision/matting/contrib/modnet.h" -#include "fastdeploy/vision/classification/ppcls/model.h" -#include "fastdeploy/vision/detection/ppdet/model.h" -#include "fastdeploy/vision/ppseg/model.h" +#include "fastdeploy/vision/segmentation/ppseg/model.h" #endif #include "fastdeploy/vision/visualize/visualize.h" diff --git a/csrc/fastdeploy/vision/classification/classification_pybind.cc b/csrc/fastdeploy/vision/classification/classification_pybind.cc new file mode 100644 index 000000000..fe64a1996 --- /dev/null +++ b/csrc/fastdeploy/vision/classification/classification_pybind.cc @@ -0,0 +1,26 @@ +// 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 BindPaddleClas(pybind11::module& m); + +void BindClassification(pybind11::module& m) { + auto classification_module = + m.def_submodule("classification", "Image classification models."); + BindPaddleClas(classification_module); +} +} // namespace fastdeploy diff --git a/csrc/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc b/csrc/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc index 190818ac3..64fda235e 100644 --- a/csrc/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc +++ b/csrc/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc @@ -14,16 +14,17 @@ #include "fastdeploy/pybind/main.h" namespace fastdeploy { -void BindPPCls(pybind11::module& m) { - pybind11::class_(m, "PaddleClasModel") +void BindPaddleClas(pybind11::module& m) { + pybind11::class_( + m, "PaddleClasModel") .def(pybind11::init()) - .def("predict", - [](vision::classification::PaddleClasModel& self, pybind11::array& data, int topk = 1) { - auto mat = PyArrayToCvMat(data); - vision::ClassifyResult res; - self.Predict(&mat, &res, topk); - return res; - }); + .def("predict", [](vision::classification::PaddleClasModel& self, + pybind11::array& data, int topk = 1) { + auto mat = PyArrayToCvMat(data); + vision::ClassifyResult res; + self.Predict(&mat, &res, topk); + return res; + }); } } // namespace fastdeploy diff --git a/csrc/fastdeploy/vision/ppseg/model.cc b/csrc/fastdeploy/vision/segmentation/ppseg/model.cc similarity index 89% rename from csrc/fastdeploy/vision/ppseg/model.cc rename to csrc/fastdeploy/vision/segmentation/ppseg/model.cc index 7f692c6a7..2c37a6079 100644 --- a/csrc/fastdeploy/vision/ppseg/model.cc +++ b/csrc/fastdeploy/vision/segmentation/ppseg/model.cc @@ -1,15 +1,17 @@ -#include "fastdeploy/vision/ppseg/model.h" +#include "fastdeploy/vision/segmentation/ppseg/model.h" #include "fastdeploy/vision.h" #include "fastdeploy/vision/utils/utils.h" #include "yaml-cpp/yaml.h" namespace fastdeploy { namespace vision { -namespace ppseg { +namespace segmentation { -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) { +PaddleSegModel::PaddleSegModel(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::PDINFER, Backend::ORT}; valid_gpu_backends = {Backend::PDINFER, Backend::ORT}; @@ -20,7 +22,7 @@ Model::Model(const std::string& model_file, const std::string& params_file, initialized = Initialize(); } -bool Model::Initialize() { +bool PaddleSegModel::Initialize() { if (!BuildPreprocessPipelineFromConfig()) { FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; @@ -33,7 +35,7 @@ bool Model::Initialize() { return true; } -bool Model::BuildPreprocessPipelineFromConfig() { +bool PaddleSegModel::BuildPreprocessPipelineFromConfig() { processors_.clear(); YAML::Node cfg; processors_.push_back(std::make_shared()); @@ -75,8 +77,9 @@ bool Model::BuildPreprocessPipelineFromConfig() { return true; } -bool Model::Preprocess(Mat* mat, FDTensor* output, - std::map>* im_info) { +bool PaddleSegModel::Preprocess( + Mat* mat, FDTensor* output, + std::map>* im_info) { for (size_t i = 0; i < processors_.size(); ++i) { if (processors_[i]->Name().compare("Resize") == 0) { auto processor = dynamic_cast(processors_[i].get()); @@ -107,8 +110,9 @@ bool Model::Preprocess(Mat* mat, FDTensor* output, return true; } -bool Model::Postprocess(FDTensor& infer_result, SegmentationResult* result, - std::map>* im_info) { +bool PaddleSegModel::Postprocess( + FDTensor& infer_result, SegmentationResult* result, + std::map>* im_info) { // PaddleSeg has three types of inference output: // 1. output with argmax and without softmax. 3-D matrix CHW, Channel // always 1, the element in matrix is classified label_id INT64 Type. @@ -196,7 +200,7 @@ bool Model::Postprocess(FDTensor& infer_result, SegmentationResult* result, return true; } -bool Model::Predict(cv::Mat* im, SegmentationResult* result) { +bool PaddleSegModel::Predict(cv::Mat* im, SegmentationResult* result) { Mat mat(*im); std::vector processed_data(1); @@ -227,6 +231,6 @@ bool Model::Predict(cv::Mat* im, SegmentationResult* result) { return true; } -} // namespace ppseg +} // namespace segmentation } // namespace vision } // namespace fastdeploy diff --git a/csrc/fastdeploy/vision/ppseg/model.h b/csrc/fastdeploy/vision/segmentation/ppseg/model.h similarity index 65% rename from csrc/fastdeploy/vision/ppseg/model.h rename to csrc/fastdeploy/vision/segmentation/ppseg/model.h index 72f8dbc64..a180f5c5b 100644 --- a/csrc/fastdeploy/vision/ppseg/model.h +++ b/csrc/fastdeploy/vision/segmentation/ppseg/model.h @@ -5,16 +5,16 @@ namespace fastdeploy { namespace vision { -namespace ppseg { +namespace segmentation { -class FASTDEPLOY_DECL Model : public FastDeployModel { +class FASTDEPLOY_DECL PaddleSegModel : 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); + PaddleSegModel(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"; } + std::string ModelName() const { return "PaddleSeg"; } virtual bool Predict(cv::Mat* im, SegmentationResult* result); @@ -38,6 +38,6 @@ class FASTDEPLOY_DECL Model : public FastDeployModel { std::vector> processors_; std::string config_file_; }; -} // namespace ppseg +} // namespace segmentation } // namespace vision } // namespace fastdeploy diff --git a/csrc/fastdeploy/vision/ppseg/ppseg_pybind.cc b/csrc/fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc similarity index 75% rename from csrc/fastdeploy/vision/ppseg/ppseg_pybind.cc rename to csrc/fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc index 949c27487..f2adc3512 100644 --- a/csrc/fastdeploy/vision/ppseg/ppseg_pybind.cc +++ b/csrc/fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc @@ -15,21 +15,22 @@ namespace fastdeploy { void BindPPSeg(pybind11::module& m) { - auto ppseg_module = - m.def_submodule("ppseg", "Module to deploy PaddleSegmentation."); - pybind11::class_(ppseg_module, "Model") + pybind11::class_( + m, "PaddleSegModel") .def(pybind11::init()) .def("predict", - [](vision::ppseg::Model& self, pybind11::array& data) { + [](vision::segmentation::PaddleSegModel& self, + pybind11::array& data) { auto mat = PyArrayToCvMat(data); vision::SegmentationResult* res = new vision::SegmentationResult(); // self.Predict(&mat, &res); self.Predict(&mat, res); return res; }) - .def_readwrite("with_softmax", &vision::ppseg::Model::with_softmax) + .def_readwrite("with_softmax", + &vision::segmentation::PaddleSegModel::with_softmax) .def_readwrite("is_vertical_screen", - &vision::ppseg::Model::is_vertical_screen); + &vision::segmentation::PaddleSegModel::is_vertical_screen); } } // namespace fastdeploy diff --git a/csrc/fastdeploy/vision/segmentation/segmentation_pybind.cc b/csrc/fastdeploy/vision/segmentation/segmentation_pybind.cc new file mode 100644 index 000000000..b4be04cc5 --- /dev/null +++ b/csrc/fastdeploy/vision/segmentation/segmentation_pybind.cc @@ -0,0 +1,26 @@ +// 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); + +void BindSegmentation(pybind11::module& m) { + auto segmentation_module = + m.def_submodule("segmentation", "Image semantic segmentation models."); + BindPPSeg(segmentation_module); +} +} // namespace fastdeploy diff --git a/csrc/fastdeploy/vision/vision_pybind.cc b/csrc/fastdeploy/vision/vision_pybind.cc index bd92a522c..5d4ba00fc 100644 --- a/csrc/fastdeploy/vision/vision_pybind.cc +++ b/csrc/fastdeploy/vision/vision_pybind.cc @@ -16,10 +16,9 @@ namespace fastdeploy { -void BindPPCls(pybind11::module& m); -void BindPPSeg(pybind11::module& m); - void BindDetection(pybind11::module& m); +void BindClassification(pybind11::module& m); +void BindSegmentation(pybind11::module& m); void BindMatting(pybind11::module& m); void BindFaceDet(pybind11::module& m); void BindFaceId(pybind11::module& m); @@ -77,10 +76,9 @@ void BindVision(pybind11::module& m) { .def("__repr__", &vision::MattingResult::Str) .def("__str__", &vision::MattingResult::Str); - BindPPCls(m); - BindPPSeg(m); - BindDetection(m); + BindClassification(m); + BindSegmentation(m); BindFaceDet(m); BindFaceId(m); BindMatting(m); diff --git a/fastdeploy/runtime.py b/fastdeploy/runtime.py index 43936533d..9613a344b 100644 --- a/fastdeploy/runtime.py +++ b/fastdeploy/runtime.py @@ -19,7 +19,8 @@ from . import c_lib_wrap as C class Runtime: def __init__(self, runtime_option): self._runtime = C.Runtime() - assert self._runtime.init(runtime_option), "Initialize Runtime Failed!" + assert self._runtime.init( + runtime_option._option), "Initialize Runtime Failed!" def infer(self, data): assert isinstance(data, dict), "The input data should be type of dict." diff --git a/fastdeploy/vision/__init__.py b/fastdeploy/vision/__init__.py index 97c81eb50..fb9f9df39 100644 --- a/fastdeploy/vision/__init__.py +++ b/fastdeploy/vision/__init__.py @@ -15,11 +15,11 @@ from __future__ import absolute_import from . import detection from . import classification +from . import segmentation from . import matting from . import facedet from . import faceid -from . import ppseg from . import evaluation from .visualize import * diff --git a/fastdeploy/vision/segmentation/__init__.py b/fastdeploy/vision/segmentation/__init__.py new file mode 100644 index 000000000..05159044b --- /dev/null +++ b/fastdeploy/vision/segmentation/__init__.py @@ -0,0 +1,16 @@ +# 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 + +from .ppseg import PaddleSegModel diff --git a/fastdeploy/vision/ppseg/__init__.py b/fastdeploy/vision/segmentation/ppseg/__init__.py similarity index 85% rename from fastdeploy/vision/ppseg/__init__.py rename to fastdeploy/vision/segmentation/ppseg/__init__.py index 1cb146c74..9d3cfaf4d 100644 --- a/fastdeploy/vision/ppseg/__init__.py +++ b/fastdeploy/vision/segmentation/ppseg/__init__.py @@ -14,11 +14,11 @@ from __future__ import absolute_import import logging -from ... import FastDeployModel, Frontend -from ... import c_lib_wrap as C +from .... import FastDeployModel, Frontend +from .... import c_lib_wrap as C -class Model(FastDeployModel): +class PaddleSegModel(FastDeployModel): def __init__(self, model_file, params_file, @@ -28,9 +28,9 @@ class Model(FastDeployModel): 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) + self._model = C.vision.segmentation.PaddleSegModel( + model_file, params_file, config_file, self._runtime_option, + model_format) assert self.initialized, "PaddleSeg model initialize failed." def predict(self, input_image):