Skip to content

Commit a7bf10b

Browse files
ziqi-jinjiangjiajunrootDefTruthfelixhjh
authored
Develop (#20)
* Modify documents of YOLOv7 (PaddlePaddle#44) * first commit for yolov7 * pybind for yolov7 * CPP README.md * CPP README.md * modified yolov7.cc * README.md * python file modify * delete license in fastdeploy/ * repush the conflict part * README.md modified * README.md modified * file path modified * file path modified * file path modified * file path modified * file path modified * README modified * README modified * move some helpers to private * add examples for yolov7 * api.md modified * api.md modified * api.md modified * YOLOv7 * yolov7 release link * yolov7 release link * yolov7 release link * copyright * change some helpers to private * change variables to const and fix documents. * gitignore * Transfer some funtions to private member of class * Transfer some funtions to private member of class * Merge from develop (#9) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * first commit for yolor * for merge * Develop (#11) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * Yolor (#16) * Develop (#11) (#12) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * Develop (#13) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * documents * documents * documents * documents * documents * documents * documents * documents * documents * documents * documents * documents * Develop (#14) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason <928090362@qq.com> * about onnx file with nms operation in yolov7 * retrigger ci * retrigger ci Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason <928090362@qq.com> * Fix evaluation import problem (PaddlePaddle#46) * Detection evaluation function * Add license * Fix python import problem * Modify requirement.txt * Add requirements.txt Co-authored-by: Jason <jiangjiajun@baidu.com> * Fix bug in ppyoloe (PaddlePaddle#47) * Fix output format for PPYOLOE (PaddlePaddle#50) * Fix bug in ppyoloe * fix ppyoloe output format * Add RetinaFace Model support (PaddlePaddle#48) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * Add RetinaFace Model support * fixed retinaface/api.md typos Co-authored-by: Jason <jiangjiajun@baidu.com> Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason <928090362@qq.com>
1 parent b8db1fe commit a7bf10b

25 files changed

Lines changed: 924 additions & 22 deletions

File tree

csrcs/fastdeploy/vision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "fastdeploy/core/config.h"
1717
#ifdef ENABLE_VISION
18+
#include "fastdeploy/vision/biubug6/retinaface.h"
1819
#include "fastdeploy/vision/deepcam/yolov5face.h"
1920
#include "fastdeploy/vision/linzaer/ultraface.h"
2021
#include "fastdeploy/vision/megvii/yolox.h"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "fastdeploy/pybind/main.h"
16+
17+
namespace fastdeploy {
18+
void BindBiubug6(pybind11::module& m) {
19+
auto biubug6_module = m.def_submodule(
20+
"biubug6", "https://github.com/biubug6/Pytorch_Retinaface");
21+
pybind11::class_<vision::biubug6::RetinaFace, FastDeployModel>(biubug6_module,
22+
"RetinaFace")
23+
.def(pybind11::init<std::string, std::string, RuntimeOption, Frontend>())
24+
.def("predict",
25+
[](vision::biubug6::RetinaFace& self, pybind11::array& data,
26+
float conf_threshold, float nms_iou_threshold) {
27+
auto mat = PyArrayToCvMat(data);
28+
vision::FaceDetectionResult res;
29+
self.Predict(&mat, &res, conf_threshold, nms_iou_threshold);
30+
return res;
31+
})
32+
.def_readwrite("size", &vision::biubug6::RetinaFace::size)
33+
.def_readwrite("variance", &vision::biubug6::RetinaFace::variance)
34+
.def_readwrite("downsample_strides",
35+
&vision::biubug6::RetinaFace::downsample_strides)
36+
.def_readwrite("min_sizes", &vision::biubug6::RetinaFace::min_sizes)
37+
.def_readwrite("landmarks_per_face",
38+
&vision::biubug6::RetinaFace::landmarks_per_face);
39+
}
40+
} // namespace fastdeploy
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "fastdeploy/vision/biubug6/retinaface.h"
16+
#include "fastdeploy/utils/perf.h"
17+
#include "fastdeploy/vision/utils/utils.h"
18+
19+
namespace fastdeploy {
20+
21+
namespace vision {
22+
23+
namespace biubug6 {
24+
25+
struct RetinaAnchor {
26+
float cx;
27+
float cy;
28+
float s_kx;
29+
float s_ky;
30+
};
31+
32+
void GenerateRetinaAnchors(const std::vector<int>& size,
33+
const std::vector<int>& downsample_strides,
34+
const std::vector<std::vector<int>>& min_sizes,
35+
std::vector<RetinaAnchor>* anchors) {
36+
// size: tuple of input (width, height)
37+
// downsample_strides: downsample strides (steps), e.g (8,16,32)
38+
// min_sizes: width and height for each anchor,
39+
// e.g {{16, 32}, {64, 128}, {256, 512}}
40+
int h = size[1];
41+
int w = size[0];
42+
std::vector<std::vector<int>> feature_maps;
43+
for (auto s : downsample_strides) {
44+
feature_maps.push_back(
45+
{static_cast<int>(
46+
std::ceil(static_cast<float>(h) / static_cast<float>(s))),
47+
static_cast<int>(
48+
std::ceil(static_cast<float>(w) / static_cast<float>(s)))});
49+
}
50+
51+
(*anchors).clear();
52+
const size_t num_feature_map = feature_maps.size();
53+
// reference: layers/functions/prior_box.py#L21
54+
for (size_t k = 0; k < num_feature_map; ++k) {
55+
auto f_map = feature_maps.at(k); // e.g [640//8,640//8]
56+
auto tmp_min_sizes = min_sizes.at(k); // e.g [8,16]
57+
int f_h = f_map.at(0);
58+
int f_w = f_map.at(1);
59+
for (size_t i = 0; i < f_h; ++i) {
60+
for (size_t j = 0; j < f_w; ++j) {
61+
for (auto min_size : tmp_min_sizes) {
62+
float s_kx =
63+
static_cast<float>(min_size) / static_cast<float>(w); // e.g 16/w
64+
float s_ky =
65+
static_cast<float>(min_size) / static_cast<float>(h); // e.g 16/h
66+
// (x + 0.5) * step / w normalized loc mapping to input width
67+
// (y + 0.5) * step / h normalized loc mapping to input height
68+
float s = static_cast<float>(downsample_strides.at(k));
69+
float cx = (static_cast<float>(j) + 0.5f) * s / static_cast<float>(w);
70+
float cy = (static_cast<float>(i) + 0.5f) * s / static_cast<float>(h);
71+
(*anchors).emplace_back(
72+
RetinaAnchor{cx, cy, s_kx, s_ky}); // without clip
73+
}
74+
}
75+
}
76+
}
77+
}
78+
79+
RetinaFace::RetinaFace(const std::string& model_file,
80+
const std::string& params_file,
81+
const RuntimeOption& custom_option,
82+
const Frontend& model_format) {
83+
if (model_format == Frontend::ONNX) {
84+
valid_cpu_backends = {Backend::ORT}; // 指定可用的CPU后端
85+
valid_gpu_backends = {Backend::ORT, Backend::TRT}; // 指定可用的GPU后端
86+
} else {
87+
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
88+
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
89+
}
90+
runtime_option = custom_option;
91+
runtime_option.model_format = model_format;
92+
runtime_option.model_file = model_file;
93+
runtime_option.params_file = params_file;
94+
initialized = Initialize();
95+
}
96+
97+
bool RetinaFace::Initialize() {
98+
// parameters for preprocess
99+
size = {640, 640};
100+
variance = {0.1f, 0.2f};
101+
downsample_strides = {8, 16, 32};
102+
min_sizes = {{16, 32}, {64, 128}, {256, 512}};
103+
landmarks_per_face = 5;
104+
105+
if (!InitRuntime()) {
106+
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
107+
return false;
108+
}
109+
// Check if the input shape is dynamic after Runtime already initialized,
110+
is_dynamic_input_ = false;
111+
auto shape = InputInfoOfRuntime(0).shape;
112+
for (int i = 0; i < shape.size(); ++i) {
113+
// if height or width is dynamic
114+
if (i >= 2 && shape[i] <= 0) {
115+
is_dynamic_input_ = true;
116+
break;
117+
}
118+
}
119+
return true;
120+
}
121+
122+
bool RetinaFace::Preprocess(
123+
Mat* mat, FDTensor* output,
124+
std::map<std::string, std::array<float, 2>>* im_info) {
125+
// retinaface's preprocess steps
126+
// 1. Resize
127+
// 2. Convert(opencv style) or Normalize
128+
// 3. HWC->CHW
129+
int resize_w = size[0];
130+
int resize_h = size[1];
131+
if (resize_h != mat->Height() || resize_w != mat->Width()) {
132+
Resize::Run(mat, resize_w, resize_h);
133+
}
134+
135+
// Compute `result = mat * alpha + beta` directly by channel
136+
// Reference: detect.py#L94
137+
std::vector<float> alpha = {1.f, 1.f, 1.f};
138+
std::vector<float> beta = {-104.f, -117.f, -123.f}; // BGR;
139+
Convert::Run(mat, alpha, beta);
140+
141+
// Record output shape of preprocessed image
142+
(*im_info)["output_shape"] = {static_cast<float>(mat->Height()),
143+
static_cast<float>(mat->Width())};
144+
145+
HWC2CHW::Run(mat);
146+
Cast::Run(mat, "float");
147+
mat->ShareWithTensor(output);
148+
output->shape.insert(output->shape.begin(), 1); // reshape to n, h, w, c
149+
return true;
150+
}
151+
152+
bool RetinaFace::Postprocess(
153+
std::vector<FDTensor>& infer_result, FaceDetectionResult* result,
154+
const std::map<std::string, std::array<float, 2>>& im_info,
155+
float conf_threshold, float nms_iou_threshold) {
156+
// retinaface has 3 output tensors, boxes & conf & landmarks
157+
FDASSERT(
158+
(infer_result.size() == 3),
159+
"The default number of output tensor must be 3 according to retinaface.");
160+
FDTensor& boxes_tensor = infer_result.at(0); // (1,n,4)
161+
FDTensor& conf_tensor = infer_result.at(1); // (1,n,2)
162+
FDTensor& landmarks_tensor = infer_result.at(2); // (1,n,10)
163+
FDASSERT((boxes_tensor.shape[0] == 1), "Only support batch =1 now.");
164+
if (boxes_tensor.dtype != FDDataType::FP32) {
165+
FDERROR << "Only support post process with float32 data." << std::endl;
166+
return false;
167+
}
168+
169+
result->Clear();
170+
// must be setup landmarks_per_face before reserve
171+
result->landmarks_per_face = landmarks_per_face;
172+
result->Reserve(boxes_tensor.shape[1]);
173+
174+
float* boxes_ptr = static_cast<float*>(boxes_tensor.Data());
175+
float* conf_ptr = static_cast<float*>(conf_tensor.Data());
176+
float* landmarks_ptr = static_cast<float*>(landmarks_tensor.Data());
177+
const size_t num_bboxes = boxes_tensor.shape[1]; // n
178+
// fetch original image shape
179+
auto iter_ipt = im_info.find("input_shape");
180+
FDASSERT((iter_ipt != im_info.end()),
181+
"Cannot find input_shape from im_info.");
182+
float ipt_h = iter_ipt->second[0];
183+
float ipt_w = iter_ipt->second[1];
184+
185+
// generate anchors with dowmsample strides
186+
std::vector<RetinaAnchor> anchors;
187+
GenerateRetinaAnchors(size, downsample_strides, min_sizes, &anchors);
188+
189+
// decode bounding boxes
190+
for (size_t i = 0; i < num_bboxes; ++i) {
191+
float confidence = conf_ptr[2 * i + 1];
192+
// filter boxes by conf_threshold
193+
if (confidence <= conf_threshold) {
194+
continue;
195+
}
196+
float prior_cx = anchors.at(i).cx;
197+
float prior_cy = anchors.at(i).cy;
198+
float prior_s_kx = anchors.at(i).s_kx;
199+
float prior_s_ky = anchors.at(i).s_ky;
200+
201+
// fetch offsets (dx,dy,dw,dh)
202+
float dx = boxes_ptr[4 * i + 0];
203+
float dy = boxes_ptr[4 * i + 1];
204+
float dw = boxes_ptr[4 * i + 2];
205+
float dh = boxes_ptr[4 * i + 3];
206+
// reference: Pytorch_Retinaface/utils/box_utils.py
207+
float x = prior_cx + dx * variance[0] * prior_s_kx;
208+
float y = prior_cy + dy * variance[0] * prior_s_ky;
209+
float w = prior_s_kx * std::exp(dw * variance[1]);
210+
float h = prior_s_ky * std::exp(dh * variance[1]); // (0.~1.)
211+
// from (x,y,w,h) to (x1,y1,x2,y2)
212+
float x1 = (x - w / 2.f) * ipt_w;
213+
float y1 = (y - h / 2.f) * ipt_h;
214+
float x2 = (x + w / 2.f) * ipt_w;
215+
float y2 = (y + h / 2.f) * ipt_h;
216+
result->boxes.emplace_back(std::array<float, 4>{x1, y1, x2, y2});
217+
result->scores.push_back(confidence);
218+
// decode landmarks (default 5 landmarks)
219+
if (landmarks_per_face > 0) {
220+
// reference: utils/box_utils.py#L241
221+
for (size_t j = 0; j < landmarks_per_face * 2; j += 2) {
222+
float ldx = landmarks_ptr[i * (landmarks_per_face * 2) + (j + 0)];
223+
float ldy = landmarks_ptr[i * (landmarks_per_face * 2) + (j + 1)];
224+
float lx = (prior_cx + ldx * variance[0] * prior_s_kx) * ipt_w;
225+
float ly = (prior_cy + ldy * variance[0] * prior_s_ky) * ipt_h;
226+
result->landmarks.emplace_back(std::array<float, 2>{lx, ly});
227+
}
228+
}
229+
}
230+
231+
if (result->boxes.size() == 0) {
232+
return true;
233+
}
234+
235+
utils::NMS(result, nms_iou_threshold);
236+
237+
// scale and clip box
238+
for (size_t i = 0; i < result->boxes.size(); ++i) {
239+
result->boxes[i][0] = std::max(result->boxes[i][0], 0.0f);
240+
result->boxes[i][1] = std::max(result->boxes[i][1], 0.0f);
241+
result->boxes[i][2] = std::max(result->boxes[i][2], 0.0f);
242+
result->boxes[i][3] = std::max(result->boxes[i][3], 0.0f);
243+
result->boxes[i][0] = std::min(result->boxes[i][0], ipt_w - 1.0f);
244+
result->boxes[i][1] = std::min(result->boxes[i][1], ipt_h - 1.0f);
245+
result->boxes[i][2] = std::min(result->boxes[i][2], ipt_w - 1.0f);
246+
result->boxes[i][3] = std::min(result->boxes[i][3], ipt_h - 1.0f);
247+
}
248+
// scale and clip landmarks
249+
for (size_t i = 0; i < result->landmarks.size(); ++i) {
250+
result->landmarks[i][0] = std::max(result->landmarks[i][0], 0.0f);
251+
result->landmarks[i][1] = std::max(result->landmarks[i][1], 0.0f);
252+
result->landmarks[i][0] = std::min(result->landmarks[i][0], ipt_w - 1.0f);
253+
result->landmarks[i][1] = std::min(result->landmarks[i][1], ipt_h - 1.0f);
254+
}
255+
return true;
256+
}
257+
258+
bool RetinaFace::Predict(cv::Mat* im, FaceDetectionResult* result,
259+
float conf_threshold, float nms_iou_threshold) {
260+
#ifdef FASTDEPLOY_DEBUG
261+
TIMERECORD_START(0)
262+
#endif
263+
264+
Mat mat(*im);
265+
std::vector<FDTensor> input_tensors(1);
266+
267+
std::map<std::string, std::array<float, 2>> im_info;
268+
269+
// Record the shape of image and the shape of preprocessed image
270+
im_info["input_shape"] = {static_cast<float>(mat.Height()),
271+
static_cast<float>(mat.Width())};
272+
im_info["output_shape"] = {static_cast<float>(mat.Height()),
273+
static_cast<float>(mat.Width())};
274+
275+
if (!Preprocess(&mat, &input_tensors[0], &im_info)) {
276+
FDERROR << "Failed to preprocess input image." << std::endl;
277+
return false;
278+
}
279+
280+
#ifdef FASTDEPLOY_DEBUG
281+
TIMERECORD_END(0, "Preprocess")
282+
TIMERECORD_START(1)
283+
#endif
284+
285+
input_tensors[0].name = InputInfoOfRuntime(0).name;
286+
std::vector<FDTensor> output_tensors;
287+
if (!Infer(input_tensors, &output_tensors)) {
288+
FDERROR << "Failed to inference." << std::endl;
289+
return false;
290+
}
291+
#ifdef FASTDEPLOY_DEBUG
292+
TIMERECORD_END(1, "Inference")
293+
TIMERECORD_START(2)
294+
#endif
295+
296+
if (!Postprocess(output_tensors, result, im_info, conf_threshold,
297+
nms_iou_threshold)) {
298+
FDERROR << "Failed to post process." << std::endl;
299+
return false;
300+
}
301+
302+
#ifdef FASTDEPLOY_DEBUG
303+
TIMERECORD_END(2, "Postprocess")
304+
#endif
305+
return true;
306+
}
307+
308+
} // namespace biubug6
309+
} // namespace vision
310+
} // namespace fastdeploy

0 commit comments

Comments
 (0)