Skip to content

Commit

Permalink
[Model] Refactor YOLOv5 module (PaddlePaddle#562)
Browse files Browse the repository at this point in the history
* add paddle_trt in benchmark

* update benchmark in device

* update benchmark

* update result doc

* fixed for CI

* update python api_docs

* update index.rst

* add runtime cpp examples

* deal with comments

* Update infer_paddle_tensorrt.py

* Add runtime quick start

* deal with comments

* fixed reused_input_tensors&&reused_output_tensors

* fixed docs

* fixed headpose typo

* fixed typo

* refactor yolov5

* update model infer

* refactor pybind for yolov5

* rm origin yolov5

* fixed bugs

* rm cuda preprocess

* fixed bugs

* fixed bugs

* fixed bug

* fixed bug

* fix pybind

* rm useless code

* add convert_and_permute

* fixed bugs

* fixed im_info for bs_predict

* fixed bug

* add bs_predict for yolov5

* Add runtime test and batch eval

* deal with comments

* fixed bug

* update testcase

* fixed batch eval bug

* fixed preprocess bug

Co-authored-by: Jason <[email protected]>
Co-authored-by: Jason <[email protected]>
  • Loading branch information
3 people authored Nov 15, 2022
1 parent e8b2228 commit aa21272
Show file tree
Hide file tree
Showing 19 changed files with 1,141 additions and 722 deletions.
8 changes: 8 additions & 0 deletions fastdeploy/core/fd_tensor.cc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const void* FDTensor::Data() const {
return buffer_;
}

void FDTensor::StopSharing() {
if (IsShared()) {
ReallocFn(Nbytes());
CopyBuffer(buffer_, external_data_ptr, Nbytes());
external_data_ptr = nullptr;
}
}

const void* FDTensor::CpuData() const {
if (device == Device::GPU) {
#ifdef WITH_GPU
Expand Down
8 changes: 1 addition & 7 deletions fastdeploy/core/fd_tensor.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ struct FASTDEPLOY_DECL FDTensor {
return external_data_ptr != nullptr;
}

void StopSharing() {
if (IsShared()) {
ReallocFn(Nbytes());
CopyBuffer(buffer_, external_data_ptr, Nbytes());
external_data_ptr = nullptr;
}
}
void StopSharing();

const void* Data() const;

Expand Down
2 changes: 1 addition & 1 deletion fastdeploy/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "fastdeploy/vision/detection/contrib/nanodet_plus.h"
#include "fastdeploy/vision/detection/contrib/scaledyolov4.h"
#include "fastdeploy/vision/detection/contrib/yolor.h"
#include "fastdeploy/vision/detection/contrib/yolov5.h"
#include "fastdeploy/vision/detection/contrib/yolov5/yolov5.h"
#include "fastdeploy/vision/detection/contrib/yolov5lite.h"
#include "fastdeploy/vision/detection/contrib/yolov6.h"
#include "fastdeploy/vision/detection/contrib/yolov7.h"
Expand Down
94 changes: 94 additions & 0 deletions fastdeploy/vision/common/processors/convert_and_permute.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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/convert_and_permute.h"

namespace fastdeploy {
namespace vision {

ConvertAndPermute::ConvertAndPermute(const std::vector<float>& alpha,
const std::vector<float>& beta,
bool swap_rb) {
FDASSERT(alpha.size() == beta.size(),
"ConvertAndPermute: requires the size of alpha equal to the size of beta.");
FDASSERT(alpha.size() > 0 && beta.size() > 0,
"ConvertAndPermute: requires the size of alpha and beta > 0.");
alpha_.assign(alpha.begin(), alpha.end());
beta_.assign(beta.begin(), beta.end());
swap_rb_ = swap_rb;
}

bool ConvertAndPermute::ImplByOpenCV(FDMat* mat) {
cv::Mat* im = mat->GetOpenCVMat();
int origin_w = im->cols;
int origin_h = im->rows;
std::vector<cv::Mat> split_im;
cv::split(*im, split_im);
if (swap_rb_) std::swap(split_im[0], split_im[2]);
for (int c = 0; c < im->channels(); c++) {
split_im[c].convertTo(split_im[c], CV_32FC1, alpha_[c], beta_[c]);
}
cv::Mat res(origin_h, origin_w, CV_32FC(im->channels()));
for (int i = 0; i < im->channels(); ++i) {
cv::extractChannel(split_im[i],
cv::Mat(origin_h, origin_w, CV_32FC1,
res.ptr() + i * origin_h * origin_w * 4),
0);
}

mat->SetMat(res);
mat->layout = Layout::CHW;
return true;
}

#ifdef ENABLE_FLYCV
bool ConvertAndPermute::ImplByFlyCV(FDMat* mat) {
if (mat->layout != Layout::HWC) {
FDERROR << "Only supports input with HWC layout." << std::endl;
return false;
}
fcv::Mat* im = mat->GetFlyCVMat();
if (im->channels() != 3) {
FDERROR << "Only supports 3-channels image in FlyCV, but now it's "
<< im->channels() << "." << std::endl;
return false;
}
std::vector<float> mean(3, 0);
std::vector<float> std(3, 0);
for (size_t i = 0; i < 3; ++i) {
std[i] = 1.0 / alpha_[i];
mean[i] = -1 * beta_[i] * std[i];
}

std::vector<uint32_t> channel_reorder_index = {0, 1, 2};
if (swap_rb_) std::swap(channel_reorder_index[0], channel_reorder_index[2]);

fcv::Mat new_im;
fcv::normalize_to_submean_to_reorder(*im, mean, std, channel_reorder_index,
new_im, false);
mat->SetMat(new_im);
mat->layout = Layout::CHW;
return true;
}
#endif

bool ConvertAndPermute::Run(FDMat* mat, const std::vector<float>& alpha,
const std::vector<float>& beta, bool swap_rb,
ProcLib lib) {
auto n = ConvertAndPermute(alpha, beta, swap_rb);
return n(mat, lib);
}

} // namespace vision
} // namespace fastdeploy
66 changes: 66 additions & 0 deletions fastdeploy/vision/common/processors/convert_and_permute.h
Original file line number Diff line number Diff line change
@@ -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.

#pragma once

#include "fastdeploy/vision/common/processors/base.h"

namespace fastdeploy {
namespace vision {
class FASTDEPLOY_DECL ConvertAndPermute : public Processor {
public:
ConvertAndPermute(const std::vector<float>& alpha = std::vector<float>(),
const std::vector<float>& beta = std::vector<float>(),
bool swap_rb = false);
bool ImplByOpenCV(FDMat* mat);
#ifdef ENABLE_FLYCV
bool ImplByFlyCV(FDMat* mat);
#endif
std::string Name() { return "ConvertAndPermute"; }

static bool Run(FDMat* mat, const std::vector<float>& alpha,
const std::vector<float>& beta, bool swap_rb = false,
ProcLib lib = ProcLib::DEFAULT);

std::vector<float> GetAlpha() const { return alpha_; }

void SetAlpha(const std::vector<float>& alpha) {
alpha_.clear();
std::vector<float>().swap(alpha_);
alpha_.assign(alpha.begin(), alpha.end());
}

std::vector<float> GetBeta() const { return beta_; }

void SetBeta(const std::vector<float>& beta) {
beta_.clear();
std::vector<float>().swap(beta_);
beta_.assign(beta.begin(), beta.end());
}

bool GetSwapRB() {
return swap_rb_;
}

void SetSwapRB(bool swap_rb) {
swap_rb_ = swap_rb;
}

private:
std::vector<float> alpha_;
std::vector<float> beta_;
bool swap_rb_;
};
} // namespace vision
} // namespace fastdeploy
1 change: 1 addition & 0 deletions fastdeploy/vision/common/processors/transform.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "fastdeploy/vision/common/processors/center_crop.h"
#include "fastdeploy/vision/common/processors/color_space_convert.h"
#include "fastdeploy/vision/common/processors/convert.h"
#include "fastdeploy/vision/common/processors/convert_and_permute.h"
#include "fastdeploy/vision/common/processors/crop.h"
#include "fastdeploy/vision/common/processors/hwc2chw.h"
#include "fastdeploy/vision/common/processors/limit_by_stride.h"
Expand Down
Loading

0 comments on commit aa21272

Please sign in to comment.