Skip to content

Commit

Permalink
[Model] Refactor YOLOv7 module (PaddlePaddle#611)
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

* refactor yolov7

* add yolov7 testcase

* rm resize_after_load and add is_scale_up

* fixed bug

* set multi_label true

Co-authored-by: Jason <928090362@qq.com>
  • Loading branch information
2 people authored and felixhjh committed Nov 25, 2022
1 parent 8529590 commit a3790f7
Show file tree
Hide file tree
Showing 20 changed files with 971 additions and 601 deletions.
2 changes: 1 addition & 1 deletion fastdeploy/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#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"
#include "fastdeploy/vision/detection/contrib/yolov7/yolov7.h"
#include "fastdeploy/vision/detection/contrib/yolov7end2end_ort.h"
#include "fastdeploy/vision/detection/contrib/yolov7end2end_trt.h"
#include "fastdeploy/vision/detection/contrib/yolox.h"
Expand Down
4 changes: 2 additions & 2 deletions fastdeploy/vision/detection/contrib/yolov5/postprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ bool YOLOv5Postprocessor::Run(const std::vector<FDTensor>& tensors, std::vector<
float ipt_h = iter_ipt->second[0];
float ipt_w = iter_ipt->second[1];
float scale = std::min(out_h / ipt_h, out_w / ipt_w);
float pad_h = (out_h - ipt_h * scale) / 2;
float pad_w = (out_w - ipt_w * scale) / 2;
for (size_t i = 0; i < (*results)[bs].boxes.size(); ++i) {
float pad_h = (out_h - ipt_h * scale) / 2;
float pad_w = (out_w - ipt_w * scale) / 2;
int32_t label_id = ((*results)[bs].label_ids)[i];
// clip box
(*results)[bs].boxes[i][0] = (*results)[bs].boxes[i][0] - max_wh_ * label_id;
Expand Down
2 changes: 1 addition & 1 deletion fastdeploy/vision/detection/contrib/yolov5/postprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FASTDEPLOY_DECL YOLOv5Postprocessor {
/// Get nms_threshold, default 0.5
float GetNMSThreshold() const { return nms_threshold_; }

/// Set multi_label, default true
/// Set multi_label, set true for eval, default true
void SetMultiLabel(bool multi_label) {
multi_label_ = multi_label;
}
Expand Down
19 changes: 4 additions & 15 deletions fastdeploy/vision/detection/contrib/yolov5/preprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ YOLOv5Preprocessor::YOLOv5Preprocessor() {
padding_value_ = {114.0, 114.0, 114.0};
is_mini_pad_ = false;
is_no_pad_ = false;
is_scale_up_ = false;
is_scale_up_ = true;
stride_ = 32;
max_wh_ = 7680.0;
}
Expand All @@ -50,7 +50,9 @@ void YOLOv5Preprocessor::LetterBox(FDMat* mat) {
resize_h = size_[1];
resize_w = size_[0];
}
Resize::Run(mat, resize_w, resize_h);
if (std::fabs(scale - 1.0f) > 1e-06) {
Resize::Run(mat, resize_w, resize_h);
}
if (pad_h > 0 || pad_w > 0) {
float half_h = pad_h * 1.0 / 2;
int top = int(round(half_h - 0.1));
Expand All @@ -67,19 +69,6 @@ bool YOLOv5Preprocessor::Preprocess(FDMat* mat, FDTensor* output,
// Record the shape of image and the shape of preprocessed image
(*im_info)["input_shape"] = {static_cast<float>(mat->Height()),
static_cast<float>(mat->Width())};

// process after image load
double ratio = (size_[0] * 1.0) / std::max(static_cast<float>(mat->Height()),
static_cast<float>(mat->Width()));
if (std::fabs(ratio - 1.0f) > 1e-06) {
int interp = cv::INTER_AREA;
if (ratio > 1.0) {
interp = cv::INTER_LINEAR;
}
int resize_h = int(mat->Height() * ratio);
int resize_w = int(mat->Width() * ratio);
Resize::Run(mat, resize_w, resize_h, -1, -1, interp);
}
// yolov5's preprocess steps
// 1. letterbox
// 2. convert_and_permute(swap_rb=true)
Expand Down
9 changes: 9 additions & 0 deletions fastdeploy/vision/detection/contrib/yolov5/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ class FASTDEPLOY_DECL YOLOv5Preprocessor {
/// Get padding value, size should be the same as channels
std::vector<float> GetPaddingValue() const { return padding_value_; }

/// Set is_scale_up, if is_scale_up is false, the input image only
/// can be zoom out, the maximum resize scale cannot exceed 1.0, default true
void SetScaleUp(bool is_scale_up) {
is_scale_up_ = is_scale_up;
}

/// Get is_scale_up, default true
bool GetScaleUp() const { return is_scale_up_; }

protected:
bool Preprocess(FDMat* mat, FDTensor* output,
std::map<std::string, std::array<float, 2>>* im_info);
Expand Down
3 changes: 2 additions & 1 deletion fastdeploy/vision/detection/contrib/yolov5/yolov5_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void BindYOLOv5(pybind11::module& m) {
return make_pair(outputs, ims_info);
})
.def_property("size", &vision::detection::YOLOv5Preprocessor::GetSize, &vision::detection::YOLOv5Preprocessor::SetSize)
.def_property("padding_value", &vision::detection::YOLOv5Preprocessor::GetPaddingValue, &vision::detection::YOLOv5Preprocessor::SetPaddingValue);
.def_property("padding_value", &vision::detection::YOLOv5Preprocessor::GetPaddingValue, &vision::detection::YOLOv5Preprocessor::SetPaddingValue)
.def_property("is_scale_up", &vision::detection::YOLOv5Preprocessor::GetScaleUp, &vision::detection::YOLOv5Preprocessor::SetScaleUp);

pybind11::class_<vision::detection::YOLOv5Postprocessor>(
m, "YOLOv5Postprocessor")
Expand Down
Loading

0 comments on commit a3790f7

Please sign in to comment.