Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions csrc/fastdeploy/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
26 changes: 26 additions & 0 deletions csrc/fastdeploy/vision/classification/classification_pybind.cc
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions csrc/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
#include "fastdeploy/pybind/main.h"

namespace fastdeploy {
void BindPPCls(pybind11::module& m) {
pybind11::class_<vision::classification::PaddleClasModel, FastDeployModel>(m, "PaddleClasModel")
void BindPaddleClas(pybind11::module& m) {
pybind11::class_<vision::classification::PaddleClasModel, FastDeployModel>(
m, "PaddleClasModel")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
Frontend>())
.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
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand All @@ -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<BGR2RGB>());
Expand Down Expand Up @@ -75,8 +77,9 @@ bool Model::BuildPreprocessPipelineFromConfig() {
return true;
}

bool Model::Preprocess(Mat* mat, FDTensor* output,
std::map<std::string, std::array<int, 2>>* im_info) {
bool PaddleSegModel::Preprocess(
Mat* mat, FDTensor* output,
std::map<std::string, std::array<int, 2>>* im_info) {
for (size_t i = 0; i < processors_.size(); ++i) {
if (processors_[i]->Name().compare("Resize") == 0) {
auto processor = dynamic_cast<Resize*>(processors_[i].get());
Expand Down Expand Up @@ -107,8 +110,9 @@ bool Model::Preprocess(Mat* mat, FDTensor* output,
return true;
}

bool Model::Postprocess(FDTensor& infer_result, SegmentationResult* result,
std::map<std::string, std::array<int, 2>>* im_info) {
bool PaddleSegModel::Postprocess(
FDTensor& infer_result, SegmentationResult* result,
std::map<std::string, std::array<int, 2>>* 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.
Expand Down Expand Up @@ -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<FDTensor> processed_data(1);

Expand Down Expand Up @@ -227,6 +231,6 @@ bool Model::Predict(cv::Mat* im, SegmentationResult* result) {
return true;
}

} // namespace ppseg
} // namespace segmentation
} // namespace vision
} // namespace fastdeploy
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -38,6 +38,6 @@ class FASTDEPLOY_DECL Model : public FastDeployModel {
std::vector<std::shared_ptr<Processor>> processors_;
std::string config_file_;
};
} // namespace ppseg
} // namespace segmentation
} // namespace vision
} // namespace fastdeploy
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@

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")
pybind11::class_<vision::segmentation::PaddleSegModel, FastDeployModel>(
m, "PaddleSegModel")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
Frontend>())
.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
26 changes: 26 additions & 0 deletions csrc/fastdeploy/vision/segmentation/segmentation_pybind.cc
Original file line number Diff line number Diff line change
@@ -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
10 changes: 4 additions & 6 deletions csrc/fastdeploy/vision/vision_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion fastdeploy/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
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."
Expand Down
2 changes: 1 addition & 1 deletion fastdeploy/vision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

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 *
16 changes: 16 additions & 0 deletions fastdeploy/vision/segmentation/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,9 +28,9 @@ def __init__(self,
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):
Expand Down