Add lots of paddle detection models (#67)

* Fix runtime with python

* Add CenterNet/PicoDet/PPYOLO/PPYOLOv2/YOLOv3

* add more ppdet models

* add model

* fix some usage bugs for detection models
This commit is contained in:
Jason
2022-08-05 09:34:12 +08:00
committed by GitHub
parent 2f362e1645
commit 821dc756e6
37 changed files with 1238 additions and 317 deletions

View File

@@ -348,6 +348,6 @@ endif(BUILD_FASTDEPLOY_PYTHON)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0") if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0")
string(STRIP "${CMAKE_CXX_COMPILER_VERSION}" CMAKE_CXX_COMPILER_VERSION) string(STRIP "${CMAKE_CXX_COMPILER_VERSION}" CMAKE_CXX_COMPILER_VERSION)
message(WARNING "[WARNING] FastDeploy require g++ version >= 5.4.0, but now your g++ version is ${CMAKE_CXX_COMPILER_VERSION}, this may cause failure! Use -DCMAKE_CXX_COMPILER to define path of your compiler.") message(FATAL_ERROR "[ERROR] FastDeploy require g++ version >= 5.4.0, but now your g++ version is ${CMAKE_CXX_COMPILER_VERSION}, this may cause failure! Use -DCMAKE_CXX_COMPILER to define path of your compiler.")
endif() endif()
endif() endif()

View File

@@ -113,6 +113,6 @@ message(STATUS " ENABLE_VISION : ${ENABLE_VISION}")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0") if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0")
string(STRIP "${CMAKE_CXX_COMPILER_VERSION}" CMAKE_CXX_COMPILER_VERSION) string(STRIP "${CMAKE_CXX_COMPILER_VERSION}" CMAKE_CXX_COMPILER_VERSION)
message(WARNING "[WARNING] FastDeploy require g++ version >= 5.4.0, but now your g++ version is ${CMAKE_CXX_COMPILER_VERSION}, this may cause failure! Use -DCMAKE_CXX_COMPILER to define path of your compiler.") message(FATAL_ERROR "[ERROR] FastDeploy require g++ version >= 5.4.0, but now your g++ version is ${CMAKE_CXX_COMPILER_VERSION}, this may cause failure! Use -DCMAKE_CXX_COMPILER to define path of your compiler.")
endif() endif()
endif() endif()

View File

@@ -54,8 +54,8 @@ std::vector<int> toVec(const nvinfer1::Dims& dim) {
bool CheckDynamicShapeConfig(const paddle2onnx::OnnxReader& reader, bool CheckDynamicShapeConfig(const paddle2onnx::OnnxReader& reader,
const TrtBackendOption& option) { const TrtBackendOption& option) {
//paddle2onnx::ModelTensorInfo inputs[reader.NumInputs()]; // paddle2onnx::ModelTensorInfo inputs[reader.NumInputs()];
//std::string input_shapes[reader.NumInputs()]; // std::string input_shapes[reader.NumInputs()];
std::vector<paddle2onnx::ModelTensorInfo> inputs(reader.NumInputs()); std::vector<paddle2onnx::ModelTensorInfo> inputs(reader.NumInputs());
std::vector<std::string> input_shapes(reader.NumInputs()); std::vector<std::string> input_shapes(reader.NumInputs());
for (int i = 0; i < reader.NumInputs(); ++i) { for (int i = 0; i < reader.NumInputs(); ++i) {
@@ -374,27 +374,27 @@ bool TrtBackend::CreateTrtEngine(const std::string& onnx_model,
1U << static_cast<uint32_t>( 1U << static_cast<uint32_t>(
nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH); nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);
auto builder = SampleUniquePtr<nvinfer1::IBuilder>( builder_ = SampleUniquePtr<nvinfer1::IBuilder>(
nvinfer1::createInferBuilder(sample::gLogger.getTRTLogger())); nvinfer1::createInferBuilder(sample::gLogger.getTRTLogger()));
if (!builder) { if (!builder_) {
FDERROR << "Failed to call createInferBuilder()." << std::endl; FDERROR << "Failed to call createInferBuilder()." << std::endl;
return false; return false;
} }
auto network = SampleUniquePtr<nvinfer1::INetworkDefinition>( network_ = SampleUniquePtr<nvinfer1::INetworkDefinition>(
builder->createNetworkV2(explicitBatch)); builder_->createNetworkV2(explicitBatch));
if (!network) { if (!network_) {
FDERROR << "Failed to call createNetworkV2()." << std::endl; FDERROR << "Failed to call createNetworkV2()." << std::endl;
return false; return false;
} }
auto config = auto config = SampleUniquePtr<nvinfer1::IBuilderConfig>(
SampleUniquePtr<nvinfer1::IBuilderConfig>(builder->createBuilderConfig()); builder_->createBuilderConfig());
if (!config) { if (!config) {
FDERROR << "Failed to call createBuilderConfig()." << std::endl; FDERROR << "Failed to call createBuilderConfig()." << std::endl;
return false; return false;
} }
if (option.enable_fp16) { if (option.enable_fp16) {
if (!builder->platformHasFastFp16()) { if (!builder_->platformHasFastFp16()) {
FDWARNING << "Detected FP16 is not supported in the current GPU, " FDWARNING << "Detected FP16 is not supported in the current GPU, "
"will use FP32 instead." "will use FP32 instead."
<< std::endl; << std::endl;
@@ -403,25 +403,25 @@ bool TrtBackend::CreateTrtEngine(const std::string& onnx_model,
} }
} }
auto parser = SampleUniquePtr<nvonnxparser::IParser>( parser_ = SampleUniquePtr<nvonnxparser::IParser>(
nvonnxparser::createParser(*network, sample::gLogger.getTRTLogger())); nvonnxparser::createParser(*network_, sample::gLogger.getTRTLogger()));
if (!parser) { if (!parser_) {
FDERROR << "Failed to call createParser()." << std::endl; FDERROR << "Failed to call createParser()." << std::endl;
return false; return false;
} }
if (!parser->parse(onnx_model.data(), onnx_model.size())) { if (!parser_->parse(onnx_model.data(), onnx_model.size())) {
FDERROR << "Failed to parse ONNX model by TensorRT." << std::endl; FDERROR << "Failed to parse ONNX model by TensorRT." << std::endl;
return false; return false;
} }
FDINFO << "Start to building TensorRT Engine..." << std::endl; FDINFO << "Start to building TensorRT Engine..." << std::endl;
bool fp16 = builder->platformHasFastFp16(); bool fp16 = builder_->platformHasFastFp16();
builder->setMaxBatchSize(option.max_batch_size); builder_->setMaxBatchSize(option.max_batch_size);
config->setMaxWorkspaceSize(option.max_workspace_size); config->setMaxWorkspaceSize(option.max_workspace_size);
if (option.max_shape.size() > 0) { if (option.max_shape.size() > 0) {
auto profile = builder->createOptimizationProfile(); auto profile = builder_->createOptimizationProfile();
FDASSERT(option.max_shape.size() == option.min_shape.size() && FDASSERT(option.max_shape.size() == option.min_shape.size() &&
option.min_shape.size() == option.opt_shape.size(), option.min_shape.size() == option.opt_shape.size(),
"[TrtBackend] Size of max_shape/opt_shape/min_shape in " "[TrtBackend] Size of max_shape/opt_shape/min_shape in "
@@ -459,7 +459,7 @@ bool TrtBackend::CreateTrtEngine(const std::string& onnx_model,
} }
SampleUniquePtr<IHostMemory> plan{ SampleUniquePtr<IHostMemory> plan{
builder->buildSerializedNetwork(*network, *config)}; builder_->buildSerializedNetwork(*network_, *config)};
if (!plan) { if (!plan) {
FDERROR << "Failed to call buildSerializedNetwork()." << std::endl; FDERROR << "Failed to call buildSerializedNetwork()." << std::endl;
return false; return false;

View File

@@ -85,6 +85,9 @@ class TrtBackend : public BaseBackend {
private: private:
std::shared_ptr<nvinfer1::ICudaEngine> engine_; std::shared_ptr<nvinfer1::ICudaEngine> engine_;
std::shared_ptr<nvinfer1::IExecutionContext> context_; std::shared_ptr<nvinfer1::IExecutionContext> context_;
SampleUniquePtr<nvonnxparser::IParser> parser_;
SampleUniquePtr<nvinfer1::IBuilder> builder_;
SampleUniquePtr<nvinfer1::INetworkDefinition> network_;
cudaStream_t stream_{}; cudaStream_t stream_{};
std::vector<void*> bindings_; std::vector<void*> bindings_;
std::vector<TrtValueInfo> inputs_desc_; std::vector<TrtValueInfo> inputs_desc_;

View File

@@ -53,7 +53,7 @@ bool FastDeployModel::InitRuntime() {
<< std::endl; << std::endl;
return false; return false;
} }
runtime_ = new Runtime(); runtime_ = std::unique_ptr<Runtime>(new Runtime());
if (!runtime_->Init(runtime_option)) { if (!runtime_->Init(runtime_option)) {
return false; return false;
} }
@@ -88,7 +88,7 @@ bool FastDeployModel::CreateCpuBackend() {
continue; continue;
} }
runtime_option.backend = valid_cpu_backends[i]; runtime_option.backend = valid_cpu_backends[i];
runtime_ = new Runtime(); runtime_ = std::unique_ptr<Runtime>(new Runtime());
if (!runtime_->Init(runtime_option)) { if (!runtime_->Init(runtime_option)) {
return false; return false;
} }
@@ -111,7 +111,7 @@ bool FastDeployModel::CreateGpuBackend() {
continue; continue;
} }
runtime_option.backend = valid_gpu_backends[i]; runtime_option.backend = valid_gpu_backends[i];
runtime_ = new Runtime(); runtime_ = std::unique_ptr<Runtime>(new Runtime());
if (!runtime_->Init(runtime_option)) { if (!runtime_->Init(runtime_option)) {
return false; return false;
} }

View File

@@ -18,7 +18,7 @@ namespace fastdeploy {
class FASTDEPLOY_DECL FastDeployModel { class FASTDEPLOY_DECL FastDeployModel {
public: public:
virtual std::string ModelName() const { return "NameUndefined"; }; virtual std::string ModelName() const { return "NameUndefined"; }
virtual bool InitRuntime(); virtual bool InitRuntime();
virtual bool CreateCpuBackend(); virtual bool CreateCpuBackend();
@@ -47,21 +47,21 @@ class FASTDEPLOY_DECL FastDeployModel {
virtual bool DebugEnabled(); virtual bool DebugEnabled();
private: private:
Runtime* runtime_ = nullptr; std::unique_ptr<Runtime> runtime_;
bool runtime_initialized_ = false; bool runtime_initialized_ = false;
bool debug_ = false; bool debug_ = false;
}; };
#define TIMERECORD_START(id) \ #define TIMERECORD_START(id) \
TimeCounter tc_##id; \ TimeCounter tc_##id; \
tc_##id.Start(); tc_##id.Start();
#define TIMERECORD_END(id, prefix) \ #define TIMERECORD_END(id, prefix) \
if (DebugEnabled()) { \ if (DebugEnabled()) { \
tc_##id.End(); \ tc_##id.End(); \
FDLogger() << __FILE__ << "(" << __LINE__ << "):" << __FUNCTION__ << " " \ FDLogger() << __FILE__ << "(" << __LINE__ << "):" << __FUNCTION__ << " " \
<< prefix << " duration = " << tc_##id.Duration() << "s." \ << prefix << " duration = " << tc_##id.Duration() << "s." \
<< std::endl; \ << std::endl; \
} }
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -27,7 +27,7 @@
#include "fastdeploy/vision/megvii/yolox.h" #include "fastdeploy/vision/megvii/yolox.h"
#include "fastdeploy/vision/meituan/yolov6.h" #include "fastdeploy/vision/meituan/yolov6.h"
#include "fastdeploy/vision/ppcls/model.h" #include "fastdeploy/vision/ppcls/model.h"
#include "fastdeploy/vision/ppdet/ppyoloe.h" #include "fastdeploy/vision/ppdet/model.h"
#include "fastdeploy/vision/ppogg/yolov5lite.h" #include "fastdeploy/vision/ppogg/yolov5lite.h"
#include "fastdeploy/vision/ppseg/model.h" #include "fastdeploy/vision/ppseg/model.h"
#include "fastdeploy/vision/rangilyu/nanodet_plus.h" #include "fastdeploy/vision/rangilyu/nanodet_plus.h"

View File

@@ -0,0 +1,141 @@
// 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/common/processors/pad_to_size.h"
namespace fastdeploy {
namespace vision {
bool PadToSize::CpuRun(Mat* mat) {
if (mat->layout != Layout::HWC) {
FDERROR << "PadToSize: The input data must be Layout::HWC format!"
<< std::endl;
return false;
}
if (mat->Channels() > 4) {
FDERROR << "PadToSize: Only support channels <= 4." << std::endl;
return false;
}
if (mat->Channels() != value_.size()) {
FDERROR
<< "PadToSize: Require input channels equals to size of padding value, "
"but now channels = "
<< mat->Channels() << ", the size of padding values = " << value_.size()
<< "." << std::endl;
return false;
}
int origin_w = mat->Width();
int origin_h = mat->Height();
if (origin_w > width_) {
FDERROR << "PadToSize: the input width:" << origin_w
<< " is greater than the target width: " << width_ << "."
<< std::endl;
return false;
}
if (origin_h > height_) {
FDERROR << "PadToSize: the input height:" << origin_h
<< " is greater than the target height: " << height_ << "."
<< std::endl;
return false;
}
if (origin_w == width_ && origin_h == height_) {
return true;
}
cv::Mat* im = mat->GetCpuMat();
cv::Scalar value;
if (value_.size() == 1) {
value = cv::Scalar(value_[0]);
} else if (value_.size() == 2) {
value = cv::Scalar(value_[0], value_[1]);
} else if (value_.size() == 3) {
value = cv::Scalar(value_[0], value_[1], value_[2]);
} else {
value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]);
}
// top, bottom, left, right
cv::copyMakeBorder(*im, *im, 0, height_ - origin_h, 0, width_ - origin_w,
cv::BORDER_CONSTANT, value);
mat->SetHeight(height_);
mat->SetWidth(width_);
return true;
}
#ifdef ENABLE_OPENCV_CUDA
bool PadToSize::GpuRun(Mat* mat) {
if (mat->layout != Layout::HWC) {
FDERROR << "PadToSize: The input data must be Layout::HWC format!"
<< std::endl;
return false;
}
if (mat->Channels() > 4) {
FDERROR << "PadToSize: Only support channels <= 4." << std::endl;
return false;
}
if (mat->Channels() != value_.size()) {
FDERROR
<< "PadToSize: Require input channels equals to size of padding value, "
"but now channels = "
<< mat->Channels() << ", the size of padding values = " << value_.size()
<< "." << std::endl;
return false;
}
int origin_w = mat->Width();
int origin_h = mat->Height();
if (origin_w > width_) {
FDERROR << "PadToSize: the input width:" << origin_w
<< " is greater than the target width: " << width_ << "."
<< std::endl;
return false;
}
if (origin_h > height_) {
FDERROR << "PadToSize: the input height:" << origin_h
<< " is greater than the target height: " << height_ << "."
<< std::endl;
return false;
}
if (origin_w == width_ && origin_h == height_) {
return true;
}
cv::cuda::GpuMat* im = mat->GetGpuMat();
cv::Scalar value;
if (value_.size() == 1) {
value = cv::Scalar(value_[0]);
} else if (value_.size() == 2) {
value = cv::Scalar(value_[0], value_[1]);
} else if (value_.size() == 3) {
value = cv::Scalar(value_[0], value_[1], value_[2]);
} else {
value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]);
}
// top, bottom, left, right
cv::cuda::copyMakeBorder(*im, *im, 0, height_ - origin_h, 0,
width_ - origin_w, cv::BORDER_CONSTANT, value);
mat->SetHeight(height_);
mat->SetWidth(width_);
return true;
}
#endif
bool PadToSize::Run(Mat* mat, int width, int height,
const std::vector<float>& value, ProcLib lib) {
auto p = PadToSize(width, height, value);
return p(mat, lib);
}
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,46 @@
// 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.
#pragma once
#include "fastdeploy/vision/common/processors/base.h"
namespace fastdeploy {
namespace vision {
class PadToSize : public Processor {
public:
// only support pad with left-top padding mode
PadToSize(int width, int height, const std::vector<float>& value) {
width_ = width;
height_ = height;
value_ = value;
}
bool CpuRun(Mat* mat);
#ifdef ENABLE_OPENCV_CUDA
bool GpuRun(Mat* mat);
#endif
std::string Name() { return "PadToSize"; }
static bool Run(Mat* mat, int width, int height,
const std::vector<float>& value,
ProcLib lib = ProcLib::OPENCV_CPU);
private:
int width_;
int height_;
std::vector<float> value_;
};
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,124 @@
// 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/common/processors/stride_pad.h"
namespace fastdeploy {
namespace vision {
bool StridePad::CpuRun(Mat* mat) {
if (mat->layout != Layout::HWC) {
FDERROR << "StridePad: The input data must be Layout::HWC format!"
<< std::endl;
return false;
}
if (mat->Channels() > 4) {
FDERROR << "StridePad: Only support channels <= 4." << std::endl;
return false;
}
if (mat->Channels() != value_.size()) {
FDERROR
<< "StridePad: Require input channels equals to size of padding value, "
"but now channels = "
<< mat->Channels() << ", the size of padding values = " << value_.size()
<< "." << std::endl;
return false;
}
int origin_w = mat->Width();
int origin_h = mat->Height();
int pad_h = (mat->Height() / stride_) * stride_ +
(mat->Height() % stride_ != 0) * stride_ - mat->Height();
int pad_w = (mat->Width() / stride_) * stride_ +
(mat->Width() % stride_ != 0) * stride_ - mat->Width();
if (pad_h == 0 && pad_w == 0) {
return true;
}
cv::Mat* im = mat->GetCpuMat();
cv::Scalar value;
if (value_.size() == 1) {
value = cv::Scalar(value_[0]);
} else if (value_.size() == 2) {
value = cv::Scalar(value_[0], value_[1]);
} else if (value_.size() == 3) {
value = cv::Scalar(value_[0], value_[1], value_[2]);
} else {
value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]);
}
// top, bottom, left, right
cv::copyMakeBorder(*im, *im, 0, pad_h, 0, pad_w, cv::BORDER_CONSTANT, value);
mat->SetHeight(origin_h + pad_h);
mat->SetWidth(origin_w + pad_w);
return true;
}
#ifdef ENABLE_OPENCV_CUDA
bool StridePad::GpuRun(Mat* mat) {
if (mat->layout != Layout::HWC) {
FDERROR << "StridePad: The input data must be Layout::HWC format!"
<< std::endl;
return false;
}
if (mat->Channels() > 4) {
FDERROR << "StridePad: Only support channels <= 4." << std::endl;
return false;
}
if (mat->Channels() != value_.size()) {
FDERROR
<< "StridePad: Require input channels equals to size of padding value, "
"but now channels = "
<< mat->Channels() << ", the size of padding values = " << value_.size()
<< "." << std::endl;
return false;
}
int origin_w = mat->Width();
int origin_h = mat->Height();
int pad_h = (mat->Height() / stride_) * stride_ +
(mat->Height() % stride_ != 0) * stride_;
int pad_w = (mat->Width() / stride_) * stride_ +
(mat->Width() % stride_ != 0) * stride_;
if (pad_h == 0 && pad_w == 0) {
return true;
}
cv::cuda::GpuMat* im = mat->GetGpuMat();
cv::Scalar value;
if (value_.size() == 1) {
value = cv::Scalar(value_[0]);
} else if (value_.size() == 2) {
value = cv::Scalar(value_[0], value_[1]);
} else if (value_.size() == 3) {
value = cv::Scalar(value_[0], value_[1], value_[2]);
} else {
value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]);
}
// top, bottom, left, right
cv::cuda::copyMakeBorder(*im, *im, 0, pad_h, 0, pad_w, cv::BORDER_CONSTANT,
value);
mat->SetHeight(origin_h + pad_h);
mat->SetWidth(origin_w + pad_w);
return true;
}
#endif
bool StridePad::Run(Mat* mat, int stride, const std::vector<float>& value,
ProcLib lib) {
auto p = StridePad(stride, value);
return p(mat, lib);
}
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,44 @@
// 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.
#pragma once
#include "fastdeploy/vision/common/processors/base.h"
namespace fastdeploy {
namespace vision {
class StridePad : public Processor {
public:
// only support pad with left-top padding mode
StridePad(int stride, const std::vector<float>& value) {
stride_ = stride;
value_ = value;
}
bool CpuRun(Mat* mat);
#ifdef ENABLE_OPENCV_CUDA
bool GpuRun(Mat* mat);
#endif
std::string Name() { return "StridePad"; }
static bool Run(Mat* mat, int stride,
const std::vector<float>& value = std::vector<float>(),
ProcLib lib = ProcLib::OPENCV_CPU);
private:
int stride_ = 32;
std::vector<float> value_;
};
} // namespace vision
} // namespace fastdeploy

View File

@@ -21,5 +21,7 @@
#include "fastdeploy/vision/common/processors/hwc2chw.h" #include "fastdeploy/vision/common/processors/hwc2chw.h"
#include "fastdeploy/vision/common/processors/normalize.h" #include "fastdeploy/vision/common/processors/normalize.h"
#include "fastdeploy/vision/common/processors/pad.h" #include "fastdeploy/vision/common/processors/pad.h"
#include "fastdeploy/vision/common/processors/pad_to_size.h"
#include "fastdeploy/vision/common/processors/resize.h" #include "fastdeploy/vision/common/processors/resize.h"
#include "fastdeploy/vision/common/processors/resize_by_short.h" #include "fastdeploy/vision/common/processors/resize_by_short.h"
#include "fastdeploy/vision/common/processors/stride_pad.h"

View File

@@ -0,0 +1,86 @@
// 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/common/processors/transform.h"
#include "fastdeploy/vision/ppdet/ppyoloe.h"
#include "yaml-cpp/yaml.h"
namespace fastdeploy {
namespace vision {
bool BuildPreprocessPipelineFromConfig(
std::vector<std::shared_ptr<Processor>>* processors,
const std::string& config_file) {
processors->clear();
YAML::Node cfg;
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;
}
processors->push_back(std::make_shared<BGR2RGB>());
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 = op["is_scale"].as<bool>();
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>>();
int interp = op["interp"].as<int>();
FDASSERT(target_size.size(),
"Require size of target_size be 2, but now it's " +
std::to_string(target_size.size()) + ".");
if (!keep_ratio) {
int width = target_size[1];
int height = target_size[0];
processors->push_back(
std::make_shared<Resize>(width, height, -1.0, -1.0, interp, false));
} else {
int min_target_size = std::min(target_size[0], target_size[1]);
int max_target_size = std::max(target_size[0], target_size[1]);
processors->push_back(std::make_shared<ResizeByShort>(
min_target_size, interp, true, max_target_size));
}
} else if (op_name == "Permute") {
// Do nothing, do permute as the last operation
continue;
} else if (op_name == "Pad") {
auto size = op["size"].as<std::vector<int>>();
auto value = op["fill_value"].as<std::vector<float>>();
processors->push_back(std::make_shared<Cast>("float"));
processors->push_back(
std::make_shared<PadToSize>(size[1], size[0], value));
} else if (op_name == "PadStride") {
auto stride = op["stride"].as<int>();
processors->push_back(
std::make_shared<StridePad>(stride, std::vector<float>(3, 0)));
} else {
FDERROR << "Unexcepted preprocess operator: " << op_name << "."
<< std::endl;
return false;
}
}
processors->push_back(std::make_shared<HWC2CHW>());
return true;
}
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,21 @@
// 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.
#pragma once
#include "fastdeploy/vision/ppdet/picodet.h"
#include "fastdeploy/vision/ppdet/ppyolo.h"
#include "fastdeploy/vision/ppdet/ppyoloe.h"
#include "fastdeploy/vision/ppdet/rcnn.h"
#include "fastdeploy/vision/ppdet/yolov3.h"
#include "fastdeploy/vision/ppdet/yolox.h"

View File

@@ -0,0 +1,66 @@
// 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/ppdet/picodet.h"
#include "yaml-cpp/yaml.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
PicoDet::PicoDet(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};
runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
runtime_option.params_file = params_file;
background_label = -1;
keep_top_k = 100;
nms_eta = 1;
nms_threshold = 0.6;
nms_top_k = 1000;
normalized = true;
score_threshold = 0.025;
CheckIfContainDecodeAndNMS();
initialized = Initialize();
}
bool PicoDet::CheckIfContainDecodeAndNMS() {
YAML::Node cfg;
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["arch"].as<std::string>() == "PicoDet") {
FDERROR << "The arch in config file is PicoDet, which means this model "
"doesn contain box decode and nms, please export model with "
"decode and nms."
<< std::endl;
return false;
}
return true;
}
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,36 @@
// 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.
#pragma once
#include "fastdeploy/vision/ppdet/ppyoloe.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
class FASTDEPLOY_DECL PicoDet : public PPYOLOE {
public:
PicoDet(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);
// Only support picodet contains decode and nms
bool CheckIfContainDecodeAndNMS();
virtual std::string ModelName() const { return "PaddleDetection/PicoDet"; }
};
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -27,5 +27,60 @@ void BindPPDet(pybind11::module& m) {
self.Predict(&mat, &res); self.Predict(&mat, &res);
return res; return res;
}); });
pybind11::class_<vision::ppdet::PPYOLO, FastDeployModel>(ppdet_module,
"PPYOLO")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
Frontend>())
.def("predict", [](vision::ppdet::PPYOLO& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::DetectionResult res;
self.Predict(&mat, &res);
return res;
});
pybind11::class_<vision::ppdet::PicoDet, FastDeployModel>(ppdet_module,
"PicoDet")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
Frontend>())
.def("predict", [](vision::ppdet::PicoDet& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::DetectionResult res;
self.Predict(&mat, &res);
return res;
});
pybind11::class_<vision::ppdet::YOLOX, FastDeployModel>(ppdet_module, "YOLOX")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
Frontend>())
.def("predict", [](vision::ppdet::YOLOX& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::DetectionResult res;
self.Predict(&mat, &res);
return res;
});
pybind11::class_<vision::ppdet::FasterRCNN, FastDeployModel>(ppdet_module,
"FasterRCNN")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
Frontend>())
.def("predict",
[](vision::ppdet::FasterRCNN& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::DetectionResult res;
self.Predict(&mat, &res);
return res;
});
pybind11::class_<vision::ppdet::YOLOv3, FastDeployModel>(ppdet_module,
"YOLOv3")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
Frontend>())
.def("predict", [](vision::ppdet::YOLOv3& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::DetectionResult res;
self.Predict(&mat, &res);
return res;
});
} }
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -0,0 +1,78 @@
// 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/ppdet/ppyolo.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
PPYOLO::PPYOLO(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};
valid_gpu_backends = {Backend::PDINFER};
has_nms_ = true;
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 PPYOLO::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 PPYOLO::Preprocess(Mat* mat, std::vector<FDTensor>* outputs) {
int origin_w = mat->Width();
int origin_h = mat->Height();
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;
}
}
outputs->resize(3);
(*outputs)[0].Allocate({1, 2}, FDDataType::FP32, "im_shape");
(*outputs)[2].Allocate({1, 2}, FDDataType::FP32, "scale_factor");
float* ptr0 = static_cast<float*>((*outputs)[0].MutableData());
ptr0[0] = mat->Height();
ptr0[1] = mat->Width();
float* ptr2 = static_cast<float*>((*outputs)[2].MutableData());
ptr2[0] = mat->Height() * 1.0 / origin_h;
ptr2[1] = mat->Width() * 1.0 / origin_w;
(*outputs)[1].name = "image";
mat->ShareWithTensor(&((*outputs)[1]));
// reshape to [1, c, h, w]
(*outputs)[1].shape.insert((*outputs)[1].shape.begin(), 1);
return true;
}
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,25 @@
#pragma once
#include "fastdeploy/vision/ppdet/ppyoloe.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
class FASTDEPLOY_DECL PPYOLO : public PPYOLOE {
public:
PPYOLO(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);
virtual std::string ModelName() const { return "PaddleDetection/PPYOLO"; }
virtual bool Preprocess(Mat* mat, std::vector<FDTensor>* outputs);
virtual bool Initialize();
protected:
PPYOLO() {}
};
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -85,12 +85,6 @@ bool PPYOLOE::BuildPreprocessPipelineFromConfig() {
return false; return false;
} }
if (cfg["arch"].as<std::string>() != "YOLO") {
FDERROR << "Require the arch of model is YOLO, but arch defined in "
"config file is "
<< cfg["arch"].as<std::string>() << "." << std::endl;
return false;
}
processors_.push_back(std::make_shared<BGR2RGB>()); processors_.push_back(std::make_shared<BGR2RGB>());
for (const auto& op : cfg["Preprocess"]) { for (const auto& op : cfg["Preprocess"]) {
@@ -107,21 +101,38 @@ bool PPYOLOE::BuildPreprocessPipelineFromConfig() {
FDASSERT(target_size.size(), FDASSERT(target_size.size(),
"Require size of target_size be 2, but now it's " + "Require size of target_size be 2, but now it's " +
std::to_string(target_size.size()) + "."); std::to_string(target_size.size()) + ".");
FDASSERT(!keep_ratio, if (!keep_ratio) {
"Only support keep_ratio is false while deploy " int width = target_size[1];
"PaddleDetection model."); int height = target_size[0];
int width = target_size[1]; processors_.push_back(
int height = target_size[0]; std::make_shared<Resize>(width, height, -1.0, -1.0, interp, false));
processors_.push_back( } else {
std::make_shared<Resize>(width, height, -1.0, -1.0, interp, false)); int min_target_size = std::min(target_size[0], target_size[1]);
int max_target_size = std::max(target_size[0], target_size[1]);
processors_.push_back(std::make_shared<ResizeByShort>(
min_target_size, interp, true, max_target_size));
}
} else if (op_name == "Permute") { } else if (op_name == "Permute") {
processors_.push_back(std::make_shared<HWC2CHW>()); // Do nothing, do permute as the last operation
continue;
// processors_.push_back(std::make_shared<HWC2CHW>());
} else if (op_name == "Pad") {
auto size = op["size"].as<std::vector<int>>();
auto value = op["fill_value"].as<std::vector<float>>();
processors_.push_back(std::make_shared<Cast>("float"));
processors_.push_back(
std::make_shared<PadToSize>(size[1], size[0], value));
} else if (op_name == "PadStride") {
auto stride = op["stride"].as<int>();
processors_.push_back(
std::make_shared<StridePad>(stride, std::vector<float>(3, 0)));
} else { } else {
FDERROR << "Unexcepted preprocess operator: " << op_name << "." FDERROR << "Unexcepted preprocess operator: " << op_name << "."
<< std::endl; << std::endl;
return false; return false;
} }
} }
processors_.push_back(std::make_shared<HWC2CHW>());
return true; return true;
} }
@@ -217,8 +228,7 @@ bool PPYOLOE::Postprocess(std::vector<FDTensor>& infer_result,
return true; return true;
} }
bool PPYOLOE::Predict(cv::Mat* im, DetectionResult* result, bool PPYOLOE::Predict(cv::Mat* im, DetectionResult* result) {
float conf_threshold, float iou_threshold) {
Mat mat(*im); Mat mat(*im);
std::vector<FDTensor> processed_data; std::vector<FDTensor> processed_data;
if (!Preprocess(&mat, &processed_data)) { if (!Preprocess(&mat, &processed_data)) {
@@ -227,6 +237,7 @@ bool PPYOLOE::Predict(cv::Mat* im, DetectionResult* result,
return false; return false;
} }
float* tmp = static_cast<float*>(processed_data[1].Data());
std::vector<FDTensor> infer_result; std::vector<FDTensor> infer_result;
if (!Infer(processed_data, &infer_result)) { if (!Infer(processed_data, &infer_result)) {
FDERROR << "Failed to inference while using model:" << ModelName() << "." FDERROR << "Failed to inference while using model:" << ModelName() << "."

View File

@@ -1,3 +1,17 @@
// 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.
#pragma once #pragma once
#include "fastdeploy/fastdeploy_model.h" #include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h" #include "fastdeploy/vision/common/processors/transform.h"
@@ -16,7 +30,7 @@ class FASTDEPLOY_DECL PPYOLOE : public FastDeployModel {
const RuntimeOption& custom_option = RuntimeOption(), const RuntimeOption& custom_option = RuntimeOption(),
const Frontend& model_format = Frontend::PADDLE); const Frontend& model_format = Frontend::PADDLE);
std::string ModelName() const { return "PaddleDetection/PPYOLOE"; } virtual std::string ModelName() const { return "PaddleDetection/PPYOLOE"; }
virtual bool Initialize(); virtual bool Initialize();
@@ -27,10 +41,11 @@ class FASTDEPLOY_DECL PPYOLOE : public FastDeployModel {
virtual bool Postprocess(std::vector<FDTensor>& infer_result, virtual bool Postprocess(std::vector<FDTensor>& infer_result,
DetectionResult* result); DetectionResult* result);
virtual bool Predict(cv::Mat* im, DetectionResult* result, virtual bool Predict(cv::Mat* im, DetectionResult* result);
float conf_threshold = 0.5, float nms_threshold = 0.7);
protected:
PPYOLOE() {}
private:
std::vector<std::shared_ptr<Processor>> processors_; std::vector<std::shared_ptr<Processor>> processors_;
std::string config_file_; std::string config_file_;
// configuration for nms // configuration for nms
@@ -47,6 +62,11 @@ class FASTDEPLOY_DECL PPYOLOE : public FastDeployModel {
// and get parameters from the operator // and get parameters from the operator
void GetNmsInfo(); void GetNmsInfo();
}; };
// Read configuration and build pipeline to process input image
bool BuildPreprocessPipelineFromConfig(
std::vector<std::shared_ptr<Processor>>* processors,
const std::string& config_file);
} // namespace ppdet } // namespace ppdet
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -0,0 +1,84 @@
// 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/ppdet/rcnn.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
FasterRCNN::FasterRCNN(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};
valid_gpu_backends = {Backend::PDINFER};
has_nms_ = true;
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 FasterRCNN::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 FasterRCNN::Preprocess(Mat* mat, std::vector<FDTensor>* outputs) {
int origin_w = mat->Width();
int origin_h = mat->Height();
float scale[2] = {1.0, 1.0};
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;
}
if (processors_[i]->Name().find("Resize") != std::string::npos) {
scale[0] = mat->Height() * 1.0 / origin_h;
scale[1] = mat->Width() * 1.0 / origin_w;
}
}
outputs->resize(3);
(*outputs)[0].Allocate({1, 2}, FDDataType::FP32, "im_shape");
(*outputs)[2].Allocate({1, 2}, FDDataType::FP32, "scale_factor");
float* ptr0 = static_cast<float*>((*outputs)[0].MutableData());
ptr0[0] = mat->Height();
ptr0[1] = mat->Width();
float* ptr2 = static_cast<float*>((*outputs)[2].MutableData());
ptr2[0] = scale[0];
ptr2[1] = scale[1];
(*outputs)[1].name = "image";
mat->ShareWithTensor(&((*outputs)[1]));
// reshape to [1, c, h, w]
(*outputs)[1].shape.insert((*outputs)[1].shape.begin(), 1);
return true;
}
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,39 @@
// 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.
#pragma once
#include "fastdeploy/vision/ppdet/ppyoloe.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
class FASTDEPLOY_DECL FasterRCNN : public PPYOLOE {
public:
FasterRCNN(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);
virtual std::string ModelName() const { return "PaddleDetection/FasterRCNN"; }
virtual bool Preprocess(Mat* mat, std::vector<FDTensor>* outputs);
virtual bool Initialize();
protected:
FasterRCNN() {}
};
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,64 @@
// 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/ppdet/yolov3.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
YOLOv3::YOLOv3(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};
valid_gpu_backends = {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 YOLOv3::Preprocess(Mat* mat, std::vector<FDTensor>* outputs) {
int origin_w = mat->Width();
int origin_h = mat->Height();
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;
}
}
outputs->resize(3);
(*outputs)[0].Allocate({1, 2}, FDDataType::FP32, "im_shape");
(*outputs)[2].Allocate({1, 2}, FDDataType::FP32, "scale_factor");
float* ptr0 = static_cast<float*>((*outputs)[0].MutableData());
ptr0[0] = mat->Height();
ptr0[1] = mat->Width();
float* ptr2 = static_cast<float*>((*outputs)[2].MutableData());
ptr2[0] = mat->Height() * 1.0 / origin_h;
ptr2[1] = mat->Width() * 1.0 / origin_w;
(*outputs)[1].name = "image";
mat->ShareWithTensor(&((*outputs)[1]));
// reshape to [1, c, h, w]
(*outputs)[1].shape.insert((*outputs)[1].shape.begin(), 1);
return true;
}
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,35 @@
// 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.
#pragma once
#include "fastdeploy/vision/ppdet/ppyoloe.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
class FASTDEPLOY_DECL YOLOv3 : public PPYOLOE {
public:
YOLOv3(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);
virtual std::string ModelName() const { return "PaddleDetection/YOLOv3"; }
virtual bool Preprocess(Mat* mat, std::vector<FDTensor>* outputs);
};
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,72 @@
// 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/ppdet/yolox.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
YOLOX::YOLOX(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};
runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
runtime_option.params_file = params_file;
background_label = -1;
keep_top_k = 1000;
nms_eta = 1;
nms_threshold = 0.65;
nms_top_k = 10000;
normalized = true;
score_threshold = 0.001;
initialized = Initialize();
}
bool YOLOX::Preprocess(Mat* mat, std::vector<FDTensor>* outputs) {
int origin_w = mat->Width();
int origin_h = mat->Height();
float scale[2] = {1.0, 1.0};
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;
}
if (processors_[i]->Name().find("Resize") != std::string::npos) {
scale[0] = mat->Height() * 1.0 / origin_h;
scale[1] = mat->Width() * 1.0 / origin_w;
}
}
outputs->resize(2);
(*outputs)[0].name = InputInfoOfRuntime(0).name;
mat->ShareWithTensor(&((*outputs)[0]));
// reshape to [1, c, h, w]
(*outputs)[0].shape.insert((*outputs)[0].shape.begin(), 1);
(*outputs)[1].Allocate({1, 2}, FDDataType::FP32, InputInfoOfRuntime(1).name);
float* ptr = static_cast<float*>((*outputs)[1].MutableData());
ptr[0] = scale[0];
ptr[1] = scale[1];
return true;
}
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,35 @@
// 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.
#pragma once
#include "fastdeploy/vision/ppdet/ppyoloe.h"
namespace fastdeploy {
namespace vision {
namespace ppdet {
class FASTDEPLOY_DECL YOLOX : public PPYOLOE {
public:
YOLOX(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);
virtual bool Preprocess(Mat* mat, std::vector<FDTensor>* outputs);
virtual std::string ModelName() const { return "PaddleDetection/YOLOX"; }
};
} // namespace ppdet
} // namespace vision
} // namespace fastdeploy

View File

@@ -343,7 +343,6 @@ bool YOLOv5Lite::Predict(cv::Mat* im, DetectionResult* result,
#ifdef FASTDEPLOY_DEBUG #ifdef FASTDEPLOY_DEBUG
TIMERECORD_START(0) TIMERECORD_START(0)
#endif #endif
std::cout << nms_iou_threshold << nms_iou_threshold << std::endl;
Mat mat(*im); Mat mat(*im);
std::vector<FDTensor> input_tensors(1); std::vector<FDTensor> input_tensors(1);

View File

@@ -16,6 +16,11 @@ import logging
import os import os
import sys import sys
try:
import paddle
except:
pass
def add_dll_search_dir(dir_path): def add_dll_search_dir(dir_path):
os.environ["path"] = dir_path + ";" + os.environ["path"] os.environ["path"] = dir_path + ";" + os.environ["path"]

View File

@@ -27,7 +27,7 @@ class PPYOLOE(FastDeployModel):
model_format=Frontend.PADDLE): model_format=Frontend.PADDLE):
super(PPYOLOE, self).__init__(runtime_option) super(PPYOLOE, self).__init__(runtime_option)
assert model_format == Frontend.PADDLE, "PPYOLOE only support model format of Frontend.Paddle now." assert model_format == Frontend.PADDLE, "PPYOLOE model only support model format of Frontend.Paddle now."
self._model = C.vision.ppdet.PPYOLOE(model_file, params_file, self._model = C.vision.ppdet.PPYOLOE(model_file, params_file,
config_file, self._runtime_option, config_file, self._runtime_option,
model_format) model_format)
@@ -36,3 +36,83 @@ class PPYOLOE(FastDeployModel):
def predict(self, input_image): def predict(self, input_image):
assert input_image is not None, "The input image data is None." assert input_image is not None, "The input image data is None."
return self._model.predict(input_image) return self._model.predict(input_image)
class PPYOLO(PPYOLOE):
def __init__(self,
model_file,
params_file,
config_file,
runtime_option=None,
model_format=Frontend.PADDLE):
super(PPYOLOE, self).__init__(runtime_option)
assert model_format == Frontend.PADDLE, "PPYOLO model only support model format of Frontend.Paddle now."
self._model = C.vision.ppdet.PPYOLO(model_file, params_file,
config_file, self._runtime_option,
model_format)
assert self.initialized, "PPYOLO model initialize failed."
class YOLOX(PPYOLOE):
def __init__(self,
model_file,
params_file,
config_file,
runtime_option=None,
model_format=Frontend.PADDLE):
super(PPYOLOE, self).__init__(runtime_option)
assert model_format == Frontend.PADDLE, "YOLOX model only support model format of Frontend.Paddle now."
self._model = C.vision.ppdet.YOLOX(model_file, params_file,
config_file, self._runtime_option,
model_format)
assert self.initialized, "YOLOX model initialize failed."
class PicoDet(PPYOLOE):
def __init__(self,
model_file,
params_file,
config_file,
runtime_option=None,
model_format=Frontend.PADDLE):
super(PPYOLOE, self).__init__(runtime_option)
assert model_format == Frontend.PADDLE, "PicoDet model only support model format of Frontend.Paddle now."
self._model = C.vision.ppdet.PicoDet(model_file, params_file,
config_file, self._runtime_option,
model_format)
assert self.initialized, "PicoDet model initialize failed."
class FasterRCNN(PPYOLOE):
def __init__(self,
model_file,
params_file,
config_file,
runtime_option=None,
model_format=Frontend.PADDLE):
super(PPYOLOE, self).__init__(runtime_option)
assert model_format == Frontend.PADDLE, "FasterRCNN model only support model format of Frontend.Paddle now."
self._model = C.vision.ppdet.FasterRCNN(
model_file, params_file, config_file, self._runtime_option,
model_format)
assert self.initialized, "FasterRCNN model initialize failed."
class YOLOv3(PPYOLOE):
def __init__(self,
model_file,
params_file,
config_file,
runtime_option=None,
model_format=Frontend.PADDLE):
super(PPYOLOE, self).__init__(runtime_option)
assert model_format == Frontend.PADDLE, "YOLOv3 model only support model format of Frontend.Paddle now."
self._model = C.vision.ppdet.YOLOv3(model_file, params_file,
config_file, self._runtime_option,
model_format)
assert self.initialized, "YOLOv3 model initialize failed."

View File

@@ -1,52 +0,0 @@
# PaddleDetection/PPYOLOE部署示例
- 当前支持PaddleDetection版本为[release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4)
本文档说明如何进行[PPYOLOE](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe)的快速部署推理。本目录结构如下
```
.
├── cpp # C++ 代码目录
│   ├── CMakeLists.txt # C++ 代码编译CMakeLists文件
│   ├── README.md # C++ 代码编译部署文档
│   └── ppyoloe.cc # C++ 示例代码
├── README.md # PPYOLOE 部署文档
└── ppyoloe.py # Python示例代码
```
## 安装FastDeploy
使用如下命令安装FastDeploy注意到此处安装的是`vision-cpu`,也可根据需求安装`vision-gpu`
```
# 安装fastdeploy-python工具
pip install fastdeploy-python
```
## Python部署
执行如下代码即会自动下载PPYOLOE模型和测试图片
```
python ppyoloe.py
```
执行完成后会将可视化结果保存在本地`vis_result.jpg`,同时输出检测结果如下
```
DetectionResult: [xmin, ymin, xmax, ymax, score, label_id]
162.380249,132.057449, 463.178345, 413.167114, 0.962918, 33
414.914642,141.148666, 91.275269, 308.688293, 0.951003, 0
163.449234,129.669067, 35.253891, 135.111786, 0.900734, 0
267.232239,142.290436, 31.578918, 126.329773, 0.848709, 0
581.790833,179.027115, 30.893127, 135.484940, 0.837986, 0
104.407021,72.602615, 22.900627, 75.469055, 0.796468, 0
348.795380,70.122147, 18.806061, 85.829330, 0.785557, 0
364.118683,92.457428, 17.437622, 89.212891, 0.774282, 0
75.180283,192.470490, 41.898407, 55.552414, 0.712569, 56
328.133759,61.894299, 19.100616, 65.633575, 0.710519, 0
504.797760,181.732574, 107.740814, 248.115082, 0.708902, 0
379.063080,64.762360, 15.956146, 68.312546, 0.680725, 0
25.858747,186.564178, 34.958130, 56.007080, 0.580415, 0
```
## 其它文档
- [C++部署](./cpp/README.md)
- [PPYOLOE API文档](./api.md)

View File

@@ -1,74 +0,0 @@
# PPYOLOE API说明
## Python API
### PPYOLOE类
```
fastdeploy.vision.ultralytics.PPYOLOE(model_file, params_file, config_file, runtime_option=None, model_format=fd.Frontend.PADDLE)
```
PPYOLOE模型加载和初始化需同时提供model_file和params_file, 当前仅支持model_format为Paddle格式
**参数**
> * **model_file**(str): 模型文件路径
> * **params_file**(str): 参数文件路径
> * **config_file**(str): 模型推理配置文件
> * **runtime_option**(RuntimeOption): 后端推理配置默认为None即采用默认配置
> * **model_format**(Frontend): 模型格式
#### predict函数
> ```
> PPYOLOE.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5)
> ```
> 模型预测结口,输入图像直接输出检测结果。
>
> **参数**
>
> > * **image_data**(np.ndarray): 输入数据注意需为HWCBGR格式
> > * **conf_threshold**(float): 检测框置信度过滤阈值
> > * **nms_iou_threshold**(float): NMS处理过程中iou阈值当模型中包含nms处理时此参数自动无效
示例代码参考[ppyoloe.py](./ppyoloe.py)
## C++ API
### PPYOLOE类
```
fastdeploy::vision::ultralytics::PPYOLOE(
const string& model_file,
const string& params_file,
const string& config_file,
const RuntimeOption& runtime_option = RuntimeOption(),
const Frontend& model_format = Frontend::ONNX)
```
PPYOLOE模型加载和初始化需同时提供model_file和params_file, 当前仅支持model_format为Paddle格式
**参数**
> * **model_file**(str): 模型文件路径
> * **params_file**(str): 参数文件路径
> * **config_file**(str): 模型推理配置文件
> * **runtime_option**(RuntimeOption): 后端推理配置默认为None即采用默认配置
> * **model_format**(Frontend): 模型格式
#### Predict函数
> ```
> YOLOv5::Predict(cv::Mat* im, DetectionResult* result,
> float conf_threshold = 0.25,
> float nms_iou_threshold = 0.5)
> ```
> 模型预测接口,输入图像直接输出检测结果。
>
> **参数**
>
> > * **im**: 输入图像注意需为HWCBGR格式
> > * **result**: 检测结果,包括检测框,各个框的置信度
> > * **conf_threshold**: 检测框置信度过滤阈值
> > * **nms_iou_threshold**: NMS处理过程中iou阈值(当模型中包含nms处理时此参数自动无效
示例代码参考[cpp/yolov5.cc](cpp/yolov5.cc)
## 其它API使用
- [模型部署RuntimeOption配置](../../../docs/api/runtime_option.md)

View File

@@ -1,17 +0,0 @@
PROJECT(ppyoloe_demo C CXX)
CMAKE_MINIMUM_REQUIRED (VERSION 3.16)
# 在低版本ABI环境中通过如下代码进行兼容性编译
# add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
# 指定下载解压后的fastdeploy库路径
set(FASTDEPLOY_INSTALL_DIR /fastdeploy/CustomOp/FastDeploy/build1/fastdeploy-linux-x64-gpu-0.3.0)
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
# 添加FastDeploy依赖头文件
include_directories(${FASTDEPLOY_INCS})
add_executable(ppyoloe_demo ${PROJECT_SOURCE_DIR}/ppyoloe.cc)
# 添加FastDeploy库依赖
target_link_libraries(ppyoloe_demo ${FASTDEPLOY_LIBS})

View File

@@ -1,39 +0,0 @@
# 编译PPYOLOE示例
```
# 下载和解压预测库
wget https://bj.bcebos.com/paddle2onnx/fastdeploy/fastdeploy-linux-x64-0.0.3.tgz
tar xvf fastdeploy-linux-x64-0.0.3.tgz
# 编译示例代码
mkdir build & cd build
cmake ..
make -j
# 下载模型和图片
wget https://bj.bcebos.com/paddle2onnx/fastdeploy/models/ppdet/ppyoloe_crn_l_300e_coco.tgz
tar xvf ppyoloe_crn_l_300e_coco.tgz
wget https://raw.githubusercontent.com/PaddlePaddle/PaddleDetection/release/2.4/demo/000000014439_640x640.jpg
# 执行
./ppyoloe_demo
```
执行完后可视化的结果保存在本地`vis_result.jpg`,同时会将检测框输出在终端,如下所示
```
DetectionResult: [xmin, ymin, xmax, ymax, score, label_id]
162.380249,132.057449, 463.178345, 413.167114, 0.962918, 33
414.914642,141.148666, 91.275269, 308.688293, 0.951003, 0
163.449234,129.669067, 35.253891, 135.111786, 0.900734, 0
267.232239,142.290436, 31.578918, 126.329773, 0.848709, 0
581.790833,179.027115, 30.893127, 135.484940, 0.837986, 0
104.407021,72.602615, 22.900627, 75.469055, 0.796468, 0
348.795380,70.122147, 18.806061, 85.829330, 0.785557, 0
364.118683,92.457428, 17.437622, 89.212891, 0.774282, 0
75.180283,192.470490, 41.898407, 55.552414, 0.712569, 56
328.133759,61.894299, 19.100616, 65.633575, 0.710519, 0
504.797760,181.732574, 107.740814, 248.115082, 0.708902, 0
379.063080,64.762360, 15.956146, 68.312546, 0.680725, 0
25.858747,186.564178, 34.958130, 56.007080, 0.580415, 0
```

View File

@@ -1,51 +0,0 @@
// 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"
int main() {
namespace vis = fastdeploy::vision;
std::string model_file = "ppyoloe_crn_l_300e_coco/model.pdmodel";
std::string params_file = "ppyoloe_crn_l_300e_coco/model.pdiparams";
std::string config_file = "ppyoloe_crn_l_300e_coco/infer_cfg.yml";
std::string img_path = "000000014439_640x640.jpg";
std::string vis_path = "vis.jpeg";
auto model = vis::ppdet::PPYOLOE(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 = im.clone();
vis::DetectionResult 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;
// 可视化预测结果
vis::Visualize::VisDetection(&vis_im, res);
cv::imwrite(vis_path, vis_im);
std::cout << "Detect Done! Saved: " << vis_path << std::endl;
return 0;
}

View File

@@ -1,24 +0,0 @@
import fastdeploy as fd
import cv2
# 下载模型和测试图片
model_url = "https://bj.bcebos.com/paddle2onnx/fastdeploy/models/ppdet/ppyoloe_crn_l_300e_coco.tgz"
test_jpg_url = "https://raw.githubusercontent.com/PaddlePaddle/PaddleDetection/release/2.4/demo/000000014439_640x640.jpg"
fd.download_and_decompress(model_url, ".")
fd.download(test_jpg_url, ".", show_progress=True)
# 加载模型
model = fd.vision.ppdet.PPYOLOE("ppyoloe_crn_l_300e_coco/model.pdmodel",
"ppyoloe_crn_l_300e_coco/model.pdiparams",
"ppyoloe_crn_l_300e_coco/infer_cfg.yml")
# 预测图片
im = cv2.imread("000000014439_640x640.jpg")
result = model.predict(im)
# 可视化结果
fd.vision.visualize.vis_detection(im, result)
cv2.imwrite("vis_result.jpg", im)
# 输出预测结果
print(result)

View File

@@ -371,9 +371,13 @@ if sys.argv[1] == "install" or sys.argv[1] == "bdist_wheel":
for f1 in os.listdir(lib_dir_name): for f1 in os.listdir(lib_dir_name):
release_dir = os.path.join(lib_dir_name, f1) release_dir = os.path.join(lib_dir_name, f1)
if f1 == "Release" and not os.path.isfile(release_dir): if f1 == "Release" and not os.path.isfile(release_dir):
if os.path.exists(os.path.join("fastdeploy/libs/third_libs", f)): if os.path.exists(
shutil.rmtree(os.path.join("fastdeploy/libs/third_libs", f)) os.path.join("fastdeploy/libs/third_libs", f)):
shutil.copytree(release_dir, os.path.join("fastdeploy/libs/third_libs", f, "lib")) shutil.rmtree(
os.path.join("fastdeploy/libs/third_libs", f))
shutil.copytree(release_dir,
os.path.join("fastdeploy/libs/third_libs",
f, "lib"))
if platform.system().lower() == "windows": if platform.system().lower() == "windows":
release_dir = os.path.join(".setuptools-cmake-build", "Release") release_dir = os.path.join(".setuptools-cmake-build", "Release")
@@ -398,6 +402,9 @@ if sys.argv[1] == "install" or sys.argv[1] == "bdist_wheel":
path)) path))
rpaths = ":".join(rpaths) rpaths = ":".join(rpaths)
command = "patchelf --set-rpath '{}' ".format(rpaths) + pybind_so_file command = "patchelf --set-rpath '{}' ".format(rpaths) + pybind_so_file
print(
"=========================Set rpath for library===================")
print(command)
# The sw_64 not suppot patchelf, so we just disable that. # The sw_64 not suppot patchelf, so we just disable that.
if platform.machine() != 'sw_64' and platform.machine() != 'mips64': if platform.machine() != 'sw_64' and platform.machine() != 'mips64':
assert os.system( assert os.system(