mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-06 00:57:33 +08:00
[Model] Support YOLOv7-face Model (#651)
* 测试 * delete test * add yolov7-face * fit vision.h * add yolov7-face test * fit: yolov7-face infer.cc * fit * fit Yolov7-face Cmakelist * fit yolov7Face.cc * add yolov7-face pybind * add yolov7-face python infer * feat yolov7-face pybind * feat yolov7-face format error * feat yolov7face_pybind error * feat add yolov7face-pybind to facedet-pybind * same as before * same sa before * feat __init__.py * add yolov7face.py * feat yolov7face.h ignore "," * feat .py * fit yolov7face.py * add yolov7face test teadme file * add test file * fit postprocess * delete remain annotation * fit preview * fit yolov7facepreprocessor * fomat code * fomat code * fomat code * fit format error and confthreshold and nmsthres * fit confthreshold and nmsthres * fit test-yolov7-face * fit test_yolov7face * fit review * fit ci error Co-authored-by: kongbohua <kongbh2022@stu.pku.edu.cn> Co-authored-by: CoolCola <49013063+kongbohua@users.noreply.github.com>
This commit is contained in:
14
examples/vision/facedet/yolov7face/cpp/CMakeLists.txt
Normal file
14
examples/vision/facedet/yolov7face/cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
PROJECT(infer_demo C CXX)
|
||||
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
|
||||
|
||||
# Specifies the path to the fastdeploy library after you have downloaded it
|
||||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
|
||||
|
||||
include(../../../../../FastDeploy.cmake)
|
||||
|
||||
# Add the FastDeploy dependency header
|
||||
include_directories(${FASTDEPLOY_INCS})
|
||||
|
||||
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc)
|
||||
# Add the FastDeploy library dependency
|
||||
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
|
90
examples/vision/facedet/yolov7face/cpp/README.md
Normal file
90
examples/vision/facedet/yolov7face/cpp/README.md
Normal file
@@ -0,0 +1,90 @@
|
||||
# YOLOv7Face C++部署示例
|
||||
|
||||
本目录下提供`infer.cc`快速完成YOLOv7Face在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。
|
||||
|
||||
在部署前,需确认以下两个步骤
|
||||
|
||||
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
|
||||
以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试
|
||||
|
||||
```bash
|
||||
mkdir build
|
||||
cd build
|
||||
# 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
|
||||
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz # x.x.x > 1.0.2
|
||||
tar xvf fastdeploy-linux-x64-x.x.x.tgz # x.x.x > 1.0.2
|
||||
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x # x.x.x > 1.0.2
|
||||
make -j
|
||||
|
||||
#下载官方转换好的YOLOv7Face模型文件和测试图片
|
||||
wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov7-lite-e.onnx
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov7-tiny-face.onnx
|
||||
|
||||
#使用yolov7-tiny-face.onnx模型
|
||||
# CPU推理
|
||||
./infer_demo yolov7-tiny-face.onnx test_lite_face_detector_3.jpg 0
|
||||
# GPU推理
|
||||
./infer_demo yolov7-tiny-face.onnx test_lite_face_detector_3.jpg 1
|
||||
# GPU上TensorRT推理
|
||||
./infer_demo yolov7-tiny-face.onnx test_lite_face_detector_3.jpg 2
|
||||
|
||||
#使用yolov7-lite-e.onnx模型
|
||||
# CPU推理
|
||||
./infer_demo yolov7-lite-e.onnx test_lite_face_detector_3.jpg 0
|
||||
# GPU推理
|
||||
./infer_demo yolov7-lite-e.onnx test_lite_face_detector_3.jpg 1
|
||||
# GPU上TensorRT推理
|
||||
./infer_demo yolov7-lite-e.onnx test_lite_face_detector_3.jpg 2
|
||||
```
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
|
||||
<img width="640" src="https://user-images.githubusercontent.com/49013063/206170111-843febb6-67d6-4c46-a121-d87d003bba21.jpg">
|
||||
|
||||
以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考:
|
||||
- [如何在Windows中使用FastDeploy C++ SDK](../../../../../docs/cn/faq/use_sdk_on_windows.md)
|
||||
|
||||
## YOLOv7Face C++接口
|
||||
|
||||
### YOLOv7Face类
|
||||
|
||||
```c++
|
||||
fastdeploy::vision::facedet::YOLOv7Face(
|
||||
const string& model_file,
|
||||
const string& params_file = "",
|
||||
const RuntimeOption& runtime_option = RuntimeOption(),
|
||||
const ModelFormat& model_format = ModelFormat::ONNX)
|
||||
```
|
||||
|
||||
YOLOv7Face模型加载和初始化,其中model_file为导出的ONNX模型格式。
|
||||
|
||||
**参数**
|
||||
|
||||
> * **model_file**(str): 模型文件路径
|
||||
> * **params_file**(str): 参数文件路径,当模型格式为ONNX时,此参数传入空字符串即可
|
||||
> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置
|
||||
> * **model_format**(ModelFormat): 模型格式,默认为ONNX格式
|
||||
|
||||
#### Predict函数
|
||||
|
||||
> ```c++
|
||||
> YOLOv7Face::Predict(cv::Mat* im, FaceDetectionResult* result,
|
||||
> float conf_threshold = 0.3,
|
||||
> float nms_iou_threshold = 0.5)
|
||||
> ```
|
||||
>
|
||||
> 模型预测接口,输入图像直接输出检测结果。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> > * **im**: 输入图像,注意需为HWC,BGR格式
|
||||
> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceDetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
|
||||
> > * **conf_threshold**: 检测框置信度过滤阈值
|
||||
> > * **nms_iou_threshold**: NMS处理过程中iou阈值
|
||||
|
||||
- [模型介绍](../../)
|
||||
- [Python部署](../python)
|
||||
- [视觉模型预测结果](../../../../../docs/api/vision_results/)
|
105
examples/vision/facedet/yolov7face/cpp/infer.cc
Normal file
105
examples/vision/facedet/yolov7face/cpp/infer.cc
Normal file
@@ -0,0 +1,105 @@
|
||||
// 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"
|
||||
|
||||
void CpuInfer(const std::string& model_file, const std::string& image_file) {
|
||||
auto model = fastdeploy::vision::facedet::YOLOv7Face(model_file);
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
|
||||
fastdeploy::vision::FaceDetectionResult res;
|
||||
if (!model.Predict(im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << res.Str() << std::endl;
|
||||
|
||||
auto vis_im = fastdeploy::vision::VisFaceDetection(im, res);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
|
||||
}
|
||||
|
||||
void GpuInfer(const std::string& model_file, const std::string& image_file) {
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
option.UseGpu();
|
||||
auto model = fastdeploy::vision::facedet::YOLOv7Face(model_file, "", option);
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
|
||||
fastdeploy::vision::FaceDetectionResult res;
|
||||
if (!model.Predict(im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << res.Str() << std::endl;
|
||||
|
||||
auto vis_im = fastdeploy::vision::VisFaceDetection(im, res);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
|
||||
}
|
||||
|
||||
void TrtInfer(const std::string& model_file, const std::string& image_file) {
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
option.UseGpu();
|
||||
option.UseTrtBackend();
|
||||
option.SetTrtInputShape("images", {1, 3, 640, 640});
|
||||
auto model = fastdeploy::vision::facedet::YOLOv7Face(model_file, "", option);
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
|
||||
fastdeploy::vision::FaceDetectionResult res;
|
||||
if (!model.Predict(im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << res.Str() << std::endl;
|
||||
|
||||
auto vis_im = fastdeploy::vision::VisFaceDetection(im, res);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 4) {
|
||||
std::cout << "Usage: infer_demo path/to/model path/to/image run_option, "
|
||||
"e.g ./infer_model yolov5s-face.onnx ./test.jpeg 0"
|
||||
<< std::endl;
|
||||
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run "
|
||||
"with gpu; 2: run with gpu and use tensorrt backend."
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (std::atoi(argv[3]) == 0) {
|
||||
CpuInfer(argv[1], argv[2]);
|
||||
} else if (std::atoi(argv[3]) == 1) {
|
||||
GpuInfer(argv[1], argv[2]);
|
||||
} else if (std::atoi(argv[3]) == 2) {
|
||||
TrtInfer(argv[1], argv[2]);
|
||||
}
|
||||
return 0;
|
||||
}
|
87
examples/vision/facedet/yolov7face/python/README.md
Normal file
87
examples/vision/facedet/yolov7face/python/README.md
Normal file
@@ -0,0 +1,87 @@
|
||||
# YOLOv7Face Python部署示例
|
||||
|
||||
在部署前,需确认以下两个步骤
|
||||
|
||||
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
- 2. FastDeploy Python whl包安装,参考[FastDeploy Python安装](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
|
||||
本目录下提供`infer.py`快速完成YOLOv7Face在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成
|
||||
|
||||
```bash
|
||||
#下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd examples/vision/facedet/yolov7face/python/
|
||||
|
||||
#下载YOLOv7Face模型文件和测试图片
|
||||
wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov7-lite-e.onnx
|
||||
|
||||
#使用yolov7-tiny-face.onnx模型
|
||||
# CPU推理
|
||||
python infer.py --model yolov7-tiny-face.onnx --image test_lite_face_detector_3.jpg --device cpu
|
||||
# GPU推理
|
||||
python infer.py --model yolov7-tiny-face.onnx --image test_lite_face_detector_3.jpg --device gpu
|
||||
# GPU上使用TensorRT推理
|
||||
python infer.py --model yolov7-tiny-face.onnx --image test_lite_face_detector_3.jpg --device gpu --use_trt True
|
||||
|
||||
#使用yolov7-lite-e.onnx模型
|
||||
# CPU推理
|
||||
python infer.py --model yolov7-lite-e.onnx --image test_lite_face_detector_3.jpg --device cpu
|
||||
# GPU推理
|
||||
python infer.py --model yolov7-lite-e.onnx --image test_lite_face_detector_3.jpg --device gpu
|
||||
# GPU上使用TensorRT推理
|
||||
python infer.py --model yolov7-lite-e.onnx --image test_lite_face_detector_3.jpg --device gpu --use_trt True
|
||||
```
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
|
||||
<img width="640" src="https://user-images.githubusercontent.com/67993288/184301839-a29aefae-16c9-4196-bf9d-9c6cf694f02d.jpg">
|
||||
|
||||
## YOLOv7Face Python接口
|
||||
|
||||
```python
|
||||
fastdeploy.vision.facedet.YOLOv7Face(model_file, params_file=None, runtime_option=None, model_format=ModelFormat.ONNX)
|
||||
```
|
||||
|
||||
YOLOv7Face模型加载和初始化,其中model_file为导出的ONNX模型格式
|
||||
|
||||
**参数**
|
||||
|
||||
> * **model_file**(str): 模型文件路径
|
||||
> * **params_file**(str): 参数文件路径,当模型格式为ONNX格式时,此参数无需设定
|
||||
> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置
|
||||
> * **model_format**(ModelFormat): 模型格式,默认为ONNX
|
||||
|
||||
### predict函数
|
||||
|
||||
> ```python
|
||||
> YOLOv7Face.predict(image_data, conf_threshold=0.3, nms_iou_threshold=0.5)
|
||||
> ```
|
||||
>
|
||||
> 模型预测结口,输入图像直接输出检测结果。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> > * **image_data**(np.ndarray): 输入数据,注意需为HWC,BGR格式
|
||||
> > * **conf_threshold**(float): 检测框置信度过滤阈值
|
||||
> > * **nms_iou_threshold**(float): NMS处理过程中iou阈值
|
||||
|
||||
> **返回**
|
||||
>
|
||||
> > 返回`fastdeploy.vision.FaceDetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/)
|
||||
|
||||
### 类成员属性
|
||||
#### 预处理参数
|
||||
用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果
|
||||
|
||||
> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640]
|
||||
> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114]
|
||||
> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False`
|
||||
> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False`
|
||||
> > * **stride**(int): 配合`is_mini_pad`成员变量使用, 默认值为`stride=32`
|
||||
|
||||
## 其它文档
|
||||
|
||||
- [YOLOv7Face 模型介绍](..)
|
||||
- [YOLOv7Face C++部署](../cpp)
|
||||
- [模型预测结果说明](../../../../../docs/api/vision_results/)
|
51
examples/vision/facedet/yolov7face/python/infer.py
Normal file
51
examples/vision/facedet/yolov7face/python/infer.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--model", required=True, help="Path of yolov7face onnx model.")
|
||||
parser.add_argument(
|
||||
"--image", required=True, help="Path of test image file.")
|
||||
parser.add_argument(
|
||||
"--device",
|
||||
type=str,
|
||||
default='cpu',
|
||||
help="Type of inference device, support 'cpu' or 'gpu'.")
|
||||
parser.add_argument(
|
||||
"--use_trt",
|
||||
type=ast.literal_eval,
|
||||
default=False,
|
||||
help="Wether to use tensorrt.")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def build_option(args):
|
||||
option = fd.RuntimeOption()
|
||||
|
||||
if args.device.lower() == "gpu":
|
||||
option.use_gpu()
|
||||
|
||||
if args.use_trt:
|
||||
option.use_trt_backend()
|
||||
option.set_trt_input_shape("images", [1, 3, 640, 640])
|
||||
return option
|
||||
|
||||
|
||||
args = parse_arguments()
|
||||
|
||||
# Configure runtime and load the model
|
||||
runtime_option = build_option(args)
|
||||
model = fd.vision.facedet.YOLOv7Face(args.model, runtime_option=runtime_option)
|
||||
|
||||
# Predict image detection results
|
||||
im = cv2.imread(args.image)
|
||||
result = model.predict(im)
|
||||
print(result)
|
||||
# Visualization of prediction Results
|
||||
vis_im = fd.vision.vis_face_detection(im, result)
|
||||
cv2.imwrite("visualized_result.jpg", vis_im)
|
||||
print("Visualized result save in ./visualized_result.jpg")
|
@@ -37,6 +37,7 @@
|
||||
#include "fastdeploy/vision/facedet/contrib/scrfd.h"
|
||||
#include "fastdeploy/vision/facedet/contrib/ultraface.h"
|
||||
#include "fastdeploy/vision/facedet/contrib/yolov5face.h"
|
||||
#include "fastdeploy/vision/facedet/yolov7-face/yolov7face.h"
|
||||
#include "fastdeploy/vision/faceid/contrib/adaface.h"
|
||||
#include "fastdeploy/vision/faceid/contrib/arcface.h"
|
||||
#include "fastdeploy/vision/faceid/contrib/cosface.h"
|
||||
|
@@ -19,6 +19,7 @@ namespace fastdeploy {
|
||||
void BindRetinaFace(pybind11::module& m);
|
||||
void BindUltraFace(pybind11::module& m);
|
||||
void BindYOLOv5Face(pybind11::module& m);
|
||||
void BindYOLOv7Face(pybind11::module& m);
|
||||
void BindSCRFD(pybind11::module& m);
|
||||
|
||||
void BindFaceDet(pybind11::module& m) {
|
||||
@@ -26,6 +27,7 @@ void BindFaceDet(pybind11::module& m) {
|
||||
BindRetinaFace(facedet_module);
|
||||
BindUltraFace(facedet_module);
|
||||
BindYOLOv5Face(facedet_module);
|
||||
BindYOLOv7Face(facedet_module);
|
||||
BindSCRFD(facedet_module);
|
||||
}
|
||||
} // namespace fastdeploy
|
||||
|
101
fastdeploy/vision/facedet/yolov7-face/postprocessor.cc
Normal file
101
fastdeploy/vision/facedet/yolov7-face/postprocessor.cc
Normal file
@@ -0,0 +1,101 @@
|
||||
// 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/facedet/yolov7-face/postprocessor.h"
|
||||
#include "fastdeploy/vision/utils/utils.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
namespace vision {
|
||||
|
||||
namespace facedet {
|
||||
|
||||
Yolov7FacePostprocessor::Yolov7FacePostprocessor() {
|
||||
conf_threshold_ = 0.5;
|
||||
nms_threshold_ = 0.45;
|
||||
max_wh_ = 7680.0;
|
||||
}
|
||||
|
||||
bool Yolov7FacePostprocessor::Run(const std::vector<FDTensor>& infer_result,
|
||||
std::vector<FaceDetectionResult>* results,
|
||||
const std::vector<std::map<std::string, std::array<float, 2>>>& ims_info) {
|
||||
int batch = infer_result[0].shape[0];
|
||||
|
||||
results->resize(batch);
|
||||
|
||||
for (size_t bs = 0; bs < batch; ++bs) {
|
||||
(*results)[bs].Clear();
|
||||
(*results)[bs].Reserve(infer_result[0].shape[1]);
|
||||
if (infer_result[0].dtype != FDDataType::FP32) {
|
||||
FDERROR << "Only support post process with float32 data." << std::endl;
|
||||
return false;
|
||||
}
|
||||
const float* data = reinterpret_cast<const float*>(infer_result[0].Data()) + bs * infer_result[0].shape[1] * infer_result[0].shape[2];
|
||||
for (size_t i = 0; i < infer_result[0].shape[1]; ++i) {
|
||||
int s = i * infer_result[0].shape[2];
|
||||
float confidence = data[s + 4];
|
||||
const float* reg_cls_ptr = data + s;
|
||||
const float* class_score = data + s + 5;
|
||||
confidence *= (*class_score);
|
||||
// filter boxes by conf_threshold
|
||||
if (confidence <= conf_threshold_) {
|
||||
continue;
|
||||
}
|
||||
float x = reg_cls_ptr[0];
|
||||
float y = reg_cls_ptr[1];
|
||||
float w = reg_cls_ptr[2];
|
||||
float h = reg_cls_ptr[3];
|
||||
|
||||
// convert from [x, y, w, h] to [x1, y1, x2, y2]
|
||||
(*results)[bs].boxes.emplace_back(std::array<float, 4>{
|
||||
(x - w / 2.f), (y - h / 2.f), (x + w / 2.f), (y + h / 2.f)});
|
||||
(*results)[bs].scores.push_back(confidence);
|
||||
}
|
||||
|
||||
if ((*results)[bs].boxes.size() == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
utils::NMS(&((*results)[bs]), nms_threshold_);
|
||||
|
||||
// scale the boxes to the origin image shape
|
||||
auto iter_out = ims_info[bs].find("output_shape");
|
||||
auto iter_ipt = ims_info[bs].find("input_shape");
|
||||
FDASSERT(iter_out != ims_info[bs].end() && iter_ipt != ims_info[bs].end(),
|
||||
"Cannot find input_shape or output_shape from im_info.");
|
||||
float out_h = iter_out->second[0];
|
||||
float out_w = iter_out->second[1];
|
||||
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);
|
||||
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;
|
||||
// clip box
|
||||
(*results)[bs].boxes[i][0] = std::max(((*results)[bs].boxes[i][0] - pad_w) / scale, 0.0f);
|
||||
(*results)[bs].boxes[i][1] = std::max(((*results)[bs].boxes[i][1] - pad_h) / scale, 0.0f);
|
||||
(*results)[bs].boxes[i][2] = std::max(((*results)[bs].boxes[i][2] - pad_w) / scale, 0.0f);
|
||||
(*results)[bs].boxes[i][3] = std::max(((*results)[bs].boxes[i][3] - pad_h) / scale, 0.0f);
|
||||
(*results)[bs].boxes[i][0] = std::min((*results)[bs].boxes[i][0], ipt_w - 1.0f);
|
||||
(*results)[bs].boxes[i][1] = std::min((*results)[bs].boxes[i][1], ipt_h - 1.0f);
|
||||
(*results)[bs].boxes[i][2] = std::min((*results)[bs].boxes[i][2], ipt_w - 1.0f);
|
||||
(*results)[bs].boxes[i][3] = std::min((*results)[bs].boxes[i][3], ipt_h - 1.0f);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace detection
|
||||
} // namespace vision
|
||||
} // namespace fastdeploy
|
68
fastdeploy/vision/facedet/yolov7-face/postprocessor.h
Normal file
68
fastdeploy/vision/facedet/yolov7-face/postprocessor.h
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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/transform.h"
|
||||
#include "fastdeploy/vision/common/result.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
namespace vision {
|
||||
|
||||
namespace facedet {
|
||||
|
||||
class FASTDEPLOY_DECL Yolov7FacePostprocessor{
|
||||
public:
|
||||
/*! @brief Postprocessor object for YOLOv7Face serials model.
|
||||
*/
|
||||
Yolov7FacePostprocessor();
|
||||
|
||||
/** \brief Process the result of runtime and fill to FaceDetectionResult structure
|
||||
*
|
||||
* \param[in] infer_result The inference result from runtime
|
||||
* \param[in] results The output result of detection
|
||||
* \param[in] ims_info The shape info list, record input_shape and output_shape
|
||||
* \return true if the postprocess successed, otherwise false
|
||||
*/
|
||||
bool Run(const std::vector<FDTensor>& infer_result,
|
||||
std::vector<FaceDetectionResult>* results,
|
||||
const std::vector<std::map<std::string,
|
||||
std::array<float, 2>>>& ims_info);
|
||||
|
||||
/// Set conf_threshold, default 0.5
|
||||
void SetConfThreshold(const float& conf_threshold) {
|
||||
conf_threshold_ = conf_threshold;
|
||||
}
|
||||
|
||||
/// Get conf_threshold, default 0.5
|
||||
float GetConfThreshold() const { return conf_threshold_; }
|
||||
|
||||
/// Set nms_threshold, default 0.45
|
||||
void SetNMSThreshold(const float& nms_threshold) {
|
||||
nms_threshold_ = nms_threshold;
|
||||
}
|
||||
|
||||
/// Get nms_threshold, default 0.45
|
||||
float GetNMSThreshold() const { return nms_threshold_; }
|
||||
|
||||
protected:
|
||||
float conf_threshold_;
|
||||
float nms_threshold_;
|
||||
bool multi_label_;
|
||||
float max_wh_;
|
||||
};
|
||||
|
||||
} // namespace facedet
|
||||
} // namespace vision
|
||||
} // namespace fastdeploy
|
120
fastdeploy/vision/facedet/yolov7-face/preprocessor.cc
Normal file
120
fastdeploy/vision/facedet/yolov7-face/preprocessor.cc
Normal file
@@ -0,0 +1,120 @@
|
||||
// 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/facedet/yolov7-face/preprocessor.h"
|
||||
#include "fastdeploy/function/concat.h"
|
||||
#include "fastdeploy/vision/common/processors/mat.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
namespace vision {
|
||||
|
||||
namespace facedet {
|
||||
|
||||
Yolov7FacePreprocessor::Yolov7FacePreprocessor() {
|
||||
size_ = {640, 640};
|
||||
padding_color_value_ = {114.0, 114.0, 114.0};
|
||||
is_mini_pad_ = false;
|
||||
is_no_pad_ = false;
|
||||
is_scale_up_ = false;
|
||||
stride_ = 32;
|
||||
max_wh_ = 7680.0;
|
||||
}
|
||||
|
||||
bool Yolov7FacePreprocessor::Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs,
|
||||
std::vector<std::map<std::string, std::array<float, 2>>>* ims_info) {
|
||||
if (images->size() == 0) {
|
||||
FDERROR << "The size of input images should be greater than 0." << std::endl;
|
||||
return false;
|
||||
}
|
||||
ims_info->resize(images->size());
|
||||
outputs->resize(1);
|
||||
std::vector<FDTensor> tensors(images->size());
|
||||
for (size_t i = 0; i < images->size(); i++) {
|
||||
if (!Preprocess(&(*images)[i], &tensors[i], &(*ims_info)[i])) {
|
||||
FDERROR << "Failed to preprocess input image." << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (tensors.size() == 1) {
|
||||
(*outputs)[0] = std::move(tensors[0]);
|
||||
} else {
|
||||
function::Concat(tensors, &((*outputs)[0]), 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Yolov7FacePreprocessor::Preprocess(FDMat* mat, FDTensor* output,
|
||||
std::map<std::string, std::array<float, 2>>* im_info){
|
||||
// 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())};
|
||||
|
||||
// yolov7-face's preprocess steps
|
||||
// 1. letterbox
|
||||
// 2. convert_and_permute(swap_rb=true)
|
||||
LetterBox(mat);
|
||||
std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
|
||||
std::vector<float> beta = {0.0f, 0.0f, 0.0f};
|
||||
ConvertAndPermute::Run(mat, alpha, beta, true);
|
||||
|
||||
// Record output shape of preprocessed image
|
||||
(*im_info)["output_shape"] = {static_cast<float>(mat->Height()),
|
||||
static_cast<float>(mat->Width())};
|
||||
|
||||
mat->ShareWithTensor(output);
|
||||
output->ExpandDim(0); // reshape to n, h, w, c
|
||||
return true;
|
||||
}
|
||||
|
||||
void Yolov7FacePreprocessor::LetterBox(FDMat* mat) {
|
||||
float scale =
|
||||
std::min(size_[1] * 1.0 / mat->Height(), size_[0] * 1.0 / mat->Width());
|
||||
if (!is_scale_up_) {
|
||||
scale = std::min(scale, 1.0f);
|
||||
}
|
||||
|
||||
int resize_h = int(round(mat->Height() * scale));
|
||||
int resize_w = int(round(mat->Width() * scale));
|
||||
|
||||
int pad_w = size_[0] - resize_w;
|
||||
int pad_h = size_[1] - resize_h;
|
||||
if (is_mini_pad_) {
|
||||
pad_h = pad_h % stride_;
|
||||
pad_w = pad_w % stride_;
|
||||
} else if (is_no_pad_) {
|
||||
pad_h = 0;
|
||||
pad_w = 0;
|
||||
resize_h = size_[1];
|
||||
resize_w = size_[0];
|
||||
}
|
||||
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));
|
||||
int bottom = int(round(half_h + 0.1));
|
||||
float half_w = pad_w * 1.0 / 2;
|
||||
int left = int(round(half_w - 0.1));
|
||||
int right = int(round(half_w + 0.1));
|
||||
Pad::Run(mat, top, bottom, left, right, padding_color_value_);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace facedet
|
||||
|
||||
} // namespace vision
|
||||
|
||||
} // namespacefastdeploy
|
100
fastdeploy/vision/facedet/yolov7-face/preprocessor.h
Normal file
100
fastdeploy/vision/facedet/yolov7-face/preprocessor.h
Normal file
@@ -0,0 +1,100 @@
|
||||
// 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/transform.h"
|
||||
#include "fastdeploy/vision/common/result.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
namespace vision {
|
||||
|
||||
namespace facedet {
|
||||
|
||||
class FASTDEPLOY_DECL Yolov7FacePreprocessor{
|
||||
public:
|
||||
/** \brief Create a preprocessor instance for YOLOv7Face serials model
|
||||
*/
|
||||
Yolov7FacePreprocessor();
|
||||
|
||||
/** \brief Process the input image and prepare input tensors for runtime
|
||||
*
|
||||
* \param[in] images The input image data list, all the elements are returned by cv::imread()
|
||||
* \param[in] outputs The output tensors which will feed in runtime
|
||||
* \param[in] ims_info The shape info list, record input_shape and output_shape
|
||||
* \ret
|
||||
*/
|
||||
bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs,
|
||||
std::vector<std::map<std::string, std::array<float, 2>>>* ims_info);
|
||||
|
||||
/// Set target size, tuple of (width, height), default size = {640, 640}
|
||||
void SetSize(const std::vector<int>& size) { size_ = size; }
|
||||
|
||||
/// Get target size, tuple of (width, height), default size = {640, 640}
|
||||
std::vector<int> GetSize() const { return size_; }
|
||||
|
||||
/// Set padding value, size should be the same as channels
|
||||
void SetPaddingColorValue(const std::vector<float>& padding_color_value) {
|
||||
padding_color_value_ = padding_color_value;
|
||||
}
|
||||
|
||||
/// Get padding value, size should be the same as channels
|
||||
std::vector<float> GetPaddingColorValue() const {
|
||||
return padding_color_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);
|
||||
|
||||
void LetterBox(FDMat* mat);
|
||||
|
||||
// target size, tuple of (width, height), default size = {640, 640}
|
||||
std::vector<int> size_;
|
||||
|
||||
// padding value, size should be the same as channels
|
||||
std::vector<float> padding_color_value_;
|
||||
|
||||
// only pad to the minimum rectange which height and width is times of stride
|
||||
bool is_mini_pad_;
|
||||
|
||||
// while is_mini_pad = false and is_no_pad = true,
|
||||
// will resize the image to the set size
|
||||
bool is_no_pad_;
|
||||
|
||||
// if is_scale_up is false, the input image only can be zoom out,
|
||||
// the maximum resize scale cannot exceed 1.0
|
||||
bool is_scale_up_;
|
||||
|
||||
// padding stride, for is_mini_pad
|
||||
int stride_;
|
||||
|
||||
// for offseting the boxes by classes when using NMS
|
||||
float max_wh_;
|
||||
};
|
||||
|
||||
} // namespace facedet
|
||||
|
||||
} // namespace vision
|
||||
|
||||
} // namespace fastdeploy
|
88
fastdeploy/vision/facedet/yolov7-face/yolov7face.cc
Normal file
88
fastdeploy/vision/facedet/yolov7-face/yolov7face.cc
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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/facedet/yolov7-face/yolov7face.h"
|
||||
#include "fastdeploy/utils/perf.h"
|
||||
#include "fastdeploy/vision/utils/utils.h"
|
||||
|
||||
namespace fastdeploy{
|
||||
|
||||
namespace vision{
|
||||
|
||||
namespace facedet{
|
||||
|
||||
YOLOv7Face::YOLOv7Face(const std::string& model_file,
|
||||
const std::string& params_file,
|
||||
const RuntimeOption& custom_option,
|
||||
const ModelFormat& model_format) {
|
||||
if (model_format == ModelFormat::ONNX) {
|
||||
valid_cpu_backends = {Backend::ORT};
|
||||
valid_gpu_backends = {Backend::ORT, Backend::TRT};
|
||||
} else {
|
||||
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
|
||||
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
|
||||
}
|
||||
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 YOLOv7Face::Initialize(){
|
||||
if (!InitRuntime()){
|
||||
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool YOLOv7Face::Predict(const cv::Mat& im, FaceDetectionResult* result){
|
||||
std::vector<FaceDetectionResult> results;
|
||||
if (!BatchPredict({im}, &results)) {
|
||||
return false;
|
||||
}
|
||||
*result = std::move(results[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool YOLOv7Face::BatchPredict(const std::vector<cv::Mat>& images,
|
||||
std::vector<FaceDetectionResult>* results){
|
||||
std::vector<FDMat> fd_images = WrapMat(images);
|
||||
FDASSERT(images.size() == 1, "Only support batch = 1 now.");
|
||||
std::vector<std::map<std::string, std::array<float, 2>>> ims_info;
|
||||
if (!preprocessor_.Run(&fd_images, &reused_input_tensors_, &ims_info)) {
|
||||
FDERROR << "Failed to preprocess the input image." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
|
||||
if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
|
||||
FDERROR << "Failed to inference by runtime." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!postprocessor_.Run(reused_output_tensors_, results, ims_info)){
|
||||
FDERROR << "Failed to postprocess the inference results by runtime." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace facedet
|
||||
|
||||
} // namespace vision
|
||||
|
||||
} // namespace fastdeploy
|
81
fastdeploy/vision/facedet/yolov7-face/yolov7face.h
Normal file
81
fastdeploy/vision/facedet/yolov7-face/yolov7face.h
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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/fastdeploy_model.h"
|
||||
#include "fastdeploy/vision/common/processors/transform.h"
|
||||
#include "fastdeploy/vision/common/result.h"
|
||||
#include "fastdeploy/vision/facedet/yolov7-face/preprocessor.h"
|
||||
#include "fastdeploy/vision/facedet/yolov7-face/postprocessor.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
namespace vision {
|
||||
|
||||
namespace facedet {
|
||||
/*! @brief YOLOv7Face model object used when to load a YOLOv7Face model exported by YOLOv7Face.
|
||||
*/
|
||||
class FASTDEPLOY_DECL YOLOv7Face: public FastDeployModel{
|
||||
public:
|
||||
/** \brief Set path of model file and the configuration of runtime.
|
||||
*
|
||||
* \param[in] model_file Path of model file, e.g ./yolov7face.onnx
|
||||
* \param[in] params_file Path of parameter file, e.g ppyoloe/model.pdiparams, if the model format is ONNX, this parameter will be ignored
|
||||
* \param[in] custom_option RuntimeOption for inference, the default will use cpu, and choose the backend defined in "valid_cpu_backends"
|
||||
* \param[in] model_format Model format of the loaded model, default is ONNX format
|
||||
*/
|
||||
YOLOv7Face(const std::string& model_file, const std::string& params_file = "",
|
||||
const RuntimeOption& custom_option = RuntimeOption(),
|
||||
const ModelFormat& model_format = ModelFormat::ONNX);
|
||||
|
||||
std::string ModelName() {return "yolov7-face";}
|
||||
|
||||
/** \brief Predict the detection result for an input image
|
||||
*
|
||||
* \param[in] img The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
|
||||
* \param[in] result The output detection result will be writen to this structure
|
||||
* \return true if the prediction successed, otherwise false
|
||||
*/
|
||||
virtual bool Predict(const cv::Mat& im, FaceDetectionResult* result);
|
||||
|
||||
/** \brief Predict the detection results for a batch of input images
|
||||
*
|
||||
* \param[in] imgs, The input image list, each element comes from cv::imread()
|
||||
* \param[in] results The output detection result list
|
||||
* \return true if the prediction successed, otherwise false
|
||||
*/
|
||||
virtual bool BatchPredict(const std::vector<cv::Mat>& images,
|
||||
std::vector<FaceDetectionResult>* results);
|
||||
|
||||
/// Get preprocessor reference of YOLOv7Face
|
||||
virtual Yolov7FacePreprocessor& GetPreprocessor() {
|
||||
return preprocessor_;
|
||||
}
|
||||
|
||||
/// Get postprocessor reference of YOLOv7Face
|
||||
virtual Yolov7FacePostprocessor& GetPostprocessor() {
|
||||
return postprocessor_;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool Initialize();
|
||||
Yolov7FacePreprocessor preprocessor_;
|
||||
Yolov7FacePostprocessor postprocessor_;
|
||||
};
|
||||
|
||||
} // namespace facedet
|
||||
|
||||
} // namespace vision
|
||||
|
||||
} // namespace fastdeploy
|
87
fastdeploy/vision/facedet/yolov7-face/yolov7face_pybind.cc
Normal file
87
fastdeploy/vision/facedet/yolov7-face/yolov7face_pybind.cc
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/pybind/main.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
void BindYOLOv7Face(pybind11::module& m) {
|
||||
pybind11::class_<vision::facedet::Yolov7FacePreprocessor>(
|
||||
m, "Yolov7FacePreprocessor")
|
||||
.def(pybind11::init<>())
|
||||
.def("run", [](vision::facedet::Yolov7FacePreprocessor& self, std::vector<pybind11::array>& im_list) {
|
||||
std::vector<vision::FDMat> images;
|
||||
for (size_t i = 0; i < im_list.size(); ++i) {
|
||||
images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i])));
|
||||
}
|
||||
std::vector<FDTensor> outputs;
|
||||
std::vector<std::map<std::string, std::array<float, 2>>> ims_info;
|
||||
if (!self.Run(&images, &outputs, &ims_info)) {
|
||||
throw std::runtime_error("Failed to preprocess the input data in PaddleClasPreprocessor.");
|
||||
}
|
||||
for (size_t i = 0; i < outputs.size(); ++i) {
|
||||
outputs[i].StopSharing();
|
||||
}
|
||||
return make_pair(outputs, ims_info);
|
||||
})
|
||||
.def_property("size", &vision::facedet::Yolov7FacePreprocessor::GetSize, &vision::facedet::Yolov7FacePreprocessor::SetSize)
|
||||
.def_property("padding_color_value", &vision::facedet::Yolov7FacePreprocessor::GetPaddingColorValue, &vision::facedet::Yolov7FacePreprocessor::SetPaddingColorValue)
|
||||
.def_property("is_scale_up", &vision::facedet::Yolov7FacePreprocessor::GetScaleUp, &vision::facedet::Yolov7FacePreprocessor::SetScaleUp);
|
||||
|
||||
pybind11::class_<vision::facedet::Yolov7FacePostprocessor>(
|
||||
m, "YOLOv7FacePostprocessor")
|
||||
.def(pybind11::init<>())
|
||||
.def("run", [](vision::facedet::Yolov7FacePostprocessor& self, std::vector<FDTensor>& inputs,
|
||||
const std::vector<std::map<std::string, std::array<float, 2>>>& ims_info) {
|
||||
std::vector<vision::FaceDetectionResult> results;
|
||||
if (!self.Run(inputs, &results, ims_info)) {
|
||||
throw std::runtime_error("Failed to postprocess the runtime result in Yolov7Postprocessor.");
|
||||
}
|
||||
return results;
|
||||
})
|
||||
.def("run", [](vision::facedet::Yolov7FacePostprocessor& self, std::vector<pybind11::array>& input_array,
|
||||
const std::vector<std::map<std::string, std::array<float, 2>>>& ims_info) {
|
||||
std::vector<vision::FaceDetectionResult> results;
|
||||
std::vector<FDTensor> inputs;
|
||||
PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
|
||||
if (!self.Run(inputs, &results, ims_info)) {
|
||||
throw std::runtime_error("Failed to postprocess the runtime result in YOLOv7Postprocessor.");
|
||||
}
|
||||
return results;
|
||||
})
|
||||
.def_property("conf_threshold", &vision::facedet::Yolov7FacePostprocessor::GetConfThreshold, &vision::facedet::Yolov7FacePostprocessor::SetConfThreshold)
|
||||
.def_property("nms_threshold", &vision::facedet::Yolov7FacePostprocessor::GetNMSThreshold, &vision::facedet::Yolov7FacePostprocessor::SetNMSThreshold);
|
||||
|
||||
pybind11::class_<vision::facedet::YOLOv7Face, FastDeployModel>(m, "YOLOv7Face")
|
||||
.def(pybind11::init<std::string, std::string, RuntimeOption,
|
||||
ModelFormat>())
|
||||
.def("predict",
|
||||
[](vision::facedet::YOLOv7Face& self, pybind11::array& data) {
|
||||
auto mat = PyArrayToCvMat(data);
|
||||
vision::FaceDetectionResult res;
|
||||
self.Predict(mat, &res);
|
||||
return res;
|
||||
})
|
||||
.def("batch_predict", [](vision::facedet::YOLOv7Face& self, std::vector<pybind11::array>& data) {
|
||||
std::vector<cv::Mat> images;
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
images.push_back(PyArrayToCvMat(data[i]));
|
||||
}
|
||||
std::vector<vision::FaceDetectionResult> results;
|
||||
self.BatchPredict(images, &results);
|
||||
return results;
|
||||
})
|
||||
.def_property_readonly("preprocessor", &vision::facedet::YOLOv7Face::GetPreprocessor)
|
||||
.def_property_readonly("postprocessor", &vision::facedet::YOLOv7Face::GetPostprocessor);
|
||||
}
|
||||
} // namespace fastdeploy
|
@@ -14,6 +14,7 @@
|
||||
|
||||
from __future__ import absolute_import
|
||||
from .contrib.yolov5face import YOLOv5Face
|
||||
from .contrib.yolov7face import *
|
||||
from .contrib.retinaface import RetinaFace
|
||||
from .contrib.scrfd import SCRFD
|
||||
from .contrib.ultraface import UltraFace
|
||||
|
166
python/fastdeploy/vision/facedet/contrib/yolov7face.py
Normal file
166
python/fastdeploy/vision/facedet/contrib/yolov7face.py
Normal file
@@ -0,0 +1,166 @@
|
||||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import absolute_import
|
||||
import logging
|
||||
from .... import FastDeployModel, ModelFormat
|
||||
from .... import c_lib_wrap as C
|
||||
|
||||
|
||||
class Yolov7FacePreprocessor:
|
||||
def __init__(self):
|
||||
"""Create a preprocessor for Yolov7Face
|
||||
"""
|
||||
self._preprocessor = C.vision.facedet.Yolov7Preprocessor()
|
||||
|
||||
def run(self, input_ims):
|
||||
"""Preprocess input images for Yolov7Face
|
||||
|
||||
:param: input_ims: (list of numpy.ndarray)The input image
|
||||
:return: list of FDTensor
|
||||
"""
|
||||
return self._preprocessor.run(input_ims)
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
"""
|
||||
Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default size = [640, 640]
|
||||
"""
|
||||
return self._preprocessor.size
|
||||
|
||||
@property
|
||||
def padding_color_value(self):
|
||||
"""
|
||||
padding value for preprocessing, default [114.0, 114.0, 114.0]
|
||||
"""
|
||||
# padding value, size should be the same as channels
|
||||
return self._preprocessor.padding_color_value
|
||||
|
||||
@property
|
||||
def is_scale_up(self):
|
||||
"""
|
||||
is_scale_up for preprocessing, the input image only can be zoom out, the maximum resize scale cannot exceed 1.0, default true
|
||||
"""
|
||||
return self._preprocessor.is_scale_up
|
||||
|
||||
@size.setter
|
||||
def size(self, wh):
|
||||
assert isinstance(wh, (list, tuple)),\
|
||||
"The value to set `size` must be type of tuple or list."
|
||||
assert len(wh) == 2,\
|
||||
"The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
|
||||
len(wh))
|
||||
self._preprocessor.size = wh
|
||||
|
||||
@padding_color_value.setter
|
||||
def padding_color_value(self, value):
|
||||
assert isinstance(
|
||||
value, list
|
||||
), "The value to set `padding_color_value` must be type of list."
|
||||
self._preprocessor.padding_color_value = value
|
||||
|
||||
@is_scale_up.setter
|
||||
def is_scale_up(self, value):
|
||||
assert isinstance(
|
||||
value,
|
||||
bool), "The value to set `is_scale_up` must be type of bool."
|
||||
self._preprocessor.is_scale_up = value
|
||||
|
||||
|
||||
class Yolov7FacePostprocessor:
|
||||
def __init__(self):
|
||||
"""Create a postprocessor for Yolov7Face
|
||||
"""
|
||||
self._postprocessor = C.vision.facedet.Yolov7FacePostprocessor()
|
||||
|
||||
def run(self, runtime_results, ims_info):
|
||||
"""Postprocess the runtime results for Yolov7Face
|
||||
|
||||
:param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
|
||||
:param: ims_info: (list of dict)Record input_shape and output_shape
|
||||
:return: list of DetectionResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
|
||||
"""
|
||||
return self._postprocessor.run(runtime_results, ims_info)
|
||||
|
||||
@property
|
||||
def conf_threshold(self):
|
||||
"""
|
||||
confidence threshold for postprocessing, default is 0.5
|
||||
"""
|
||||
return self._postprocessor.conf_threshold
|
||||
|
||||
@property
|
||||
def nms_threshold(self):
|
||||
"""
|
||||
nms threshold for postprocessing, default is 0.45
|
||||
"""
|
||||
return self._postprocessor.nms_threshold
|
||||
|
||||
@conf_threshold.setter
|
||||
def conf_threshold(self, conf_threshold):
|
||||
assert isinstance(conf_threshold, float),\
|
||||
"The value to set `conf_threshold` must be type of float."
|
||||
self._postprocessor.conf_threshold = conf_threshold
|
||||
|
||||
@nms_threshold.setter
|
||||
def nms_threshold(self, nms_threshold):
|
||||
assert isinstance(nms_threshold, float),\
|
||||
"The value to set `nms_threshold` must be type of float."
|
||||
self._postprocessor.nms_threshold = nms_threshold
|
||||
|
||||
|
||||
class YOLOv7Face(FastDeployModel):
|
||||
def __init__(self,
|
||||
model_file,
|
||||
params_file="",
|
||||
runtime_option=None,
|
||||
model_format=ModelFormat.ONNX):
|
||||
"""Load a YOLOv7Face model exported by YOLOv7Face.
|
||||
|
||||
:param model_file: (str)Path of model file, e.g ./yolov7face.onnx
|
||||
:param params_file: (str)Path of parameters file, e.g yolox/model.pdiparams, if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string
|
||||
:param runtime_option: (fastdeploy.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
|
||||
:param model_format: (fastdeploy.ModelForamt)Model format of the loaded model
|
||||
"""
|
||||
super(YOLOv7Face, self).__init__(runtime_option)
|
||||
|
||||
self._model = C.vision.facedet.YOLOv7Face(
|
||||
model_file, params_file, self._runtime_option, model_format)
|
||||
|
||||
assert self.initialized, "YOLOv7Face initialize failed."
|
||||
|
||||
def batch_predict(self, images):
|
||||
"""Classify a batch of input image
|
||||
|
||||
:param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
|
||||
:return list of DetectionResult
|
||||
"""
|
||||
|
||||
return self._model.batch_predict(images)
|
||||
|
||||
@property
|
||||
def preprocessor(self):
|
||||
"""Get YOLOv7Preprocessor object of the loaded model
|
||||
|
||||
:return YOLOv7Preprocessor
|
||||
"""
|
||||
return self._model.preprocessor
|
||||
|
||||
@property
|
||||
def postprocessor(self):
|
||||
"""Get YOLOv7Postprocessor object of the loaded model
|
||||
|
||||
:return YOLOv7Postprocessor
|
||||
"""
|
||||
return self._model.postprocessor
|
142
tests/models/test_yolov7face.py
Normal file
142
tests/models/test_yolov7face.py
Normal file
@@ -0,0 +1,142 @@
|
||||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from fastdeploy import ModelFormat
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
import pickle
|
||||
import numpy as np
|
||||
import runtime_config as rc
|
||||
|
||||
|
||||
def test_detection_yolov7face():
|
||||
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/yolov7-lite-e.onnx"
|
||||
input_url1 = "https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg"
|
||||
input_url2 = "https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000570688.jpg"
|
||||
result_url1 = "https://bj.bcebos.com/paddlehub/fastdeploy/yolov7face_result1.pkl"
|
||||
result_url2 = "https://bj.bcebos.com/paddlehub/fastdeploy/yolov7face_result2.pkl"
|
||||
fd.download(model_url, "resources")
|
||||
fd.download(input_url1, "resources")
|
||||
fd.download(input_url2, "resources")
|
||||
fd.download(result_url1, "resources")
|
||||
fd.download(result_url2, "resources")
|
||||
|
||||
model_file = "resources/yolov7-lite-e.onnx"
|
||||
model = fd.vision.facedet.YOLOv7Face(
|
||||
model_file, runtime_option=rc.test_option)
|
||||
model.postprocessor.conf_threshold = 0.3
|
||||
|
||||
with open("resources/yolov7face_result1.pkl", "rb") as f:
|
||||
expect1 = pickle.load(f)
|
||||
|
||||
with open("resources/yolov7face_result2.pkl", "rb") as f:
|
||||
expect2 = pickle.load(f)
|
||||
|
||||
im1 = cv2.imread("./resources/000000014439.jpg")
|
||||
im2 = cv2.imread("./resources/000000570688.jpg")
|
||||
|
||||
for i in range(3):
|
||||
# test single predict
|
||||
result1 = model.predict(im1)
|
||||
result2 = model.predict(im2)
|
||||
|
||||
diff_boxes_1 = np.fabs(
|
||||
np.array(result1.boxes) - np.array(expect1["boxes"]))
|
||||
diff_boxes_2 = np.fabs(
|
||||
np.array(result2.boxes) - np.array(expect2["boxes"]))
|
||||
|
||||
diff_scores_1 = np.fabs(
|
||||
np.array(result1.scores) - np.array(expect1["scores"]))
|
||||
diff_scores_2 = np.fabs(
|
||||
np.array(result2.scores) - np.array(expect2["scores"]))
|
||||
|
||||
assert diff_boxes_1.max(
|
||||
) < 1e-03, "There's difference in detection boxes 1."
|
||||
assert diff_scores_1.max(
|
||||
) < 1e-04, "There's difference in detection score 1."
|
||||
|
||||
assert diff_boxes_2.max(
|
||||
) < 1e-03, "There's difference in detection boxes 2."
|
||||
assert diff_scores_2.max(
|
||||
) < 1e-04, "There's difference in detection score 2."
|
||||
|
||||
# test batch predict
|
||||
results = model.batch_predict([im1, im2])
|
||||
result1 = results[0]
|
||||
result2 = results[1]
|
||||
|
||||
diff_boxes_1 = np.fabs(
|
||||
np.array(result1.boxes) - np.array(expect1["boxes"]))
|
||||
diff_boxes_2 = np.fabs(
|
||||
np.array(result2.boxes) - np.array(expect2["boxes"]))
|
||||
|
||||
diff_scores_1 = np.fabs(
|
||||
np.array(result1.scores) - np.array(expect1["scores"]))
|
||||
diff_scores_2 = np.fabs(
|
||||
np.array(result2.scores) - np.array(expect2["scores"]))
|
||||
assert diff_boxes_1.max(
|
||||
) < 1e-03, "There's difference in detection boxes 1."
|
||||
assert diff_scores_1.max(
|
||||
) < 1e-04, "There's difference in detection score 1."
|
||||
|
||||
assert diff_boxes_2.max(
|
||||
) < 1e-03, "There's difference in detection boxes 2."
|
||||
assert diff_scores_2.max(
|
||||
) < 1e-04, "There's difference in detection score 2."
|
||||
|
||||
|
||||
def test_detection_yolov7face_runtime():
|
||||
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/yolov7-lite-e.onnx"
|
||||
input_url1 = "https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg"
|
||||
result_url1 = "https://bj.bcebos.com/paddlehub/fastdeploy/yolov7_result1.pkl"
|
||||
fd.download(model_url, "resources")
|
||||
fd.download(input_url1, "resources")
|
||||
fd.download(result_url1, "resources")
|
||||
|
||||
model_file = "resources/yolov7-lite-e.onnx"
|
||||
|
||||
preprocessor = fd.vision.detection.Yolov7FacePreprocessor()
|
||||
postprocessor = fd.vision.detection.YOLOv7FacePostprocessor()
|
||||
|
||||
rc.test_option.set_model_path(model_file, model_format=ModelFormat.ONNX)
|
||||
rc.test_option.use_openvino_backend()
|
||||
runtime = fd.Runtime(rc.test_option)
|
||||
|
||||
with open("resources/yolov7_result1.pkl", "rb") as f:
|
||||
expect1 = pickle.load(f)
|
||||
|
||||
im1 = cv2.imread("resources/000000014439.jpg")
|
||||
|
||||
for i in range(3):
|
||||
# test runtime
|
||||
input_tensors, ims_info = preprocessor.run([im1.copy()])
|
||||
output_tensors = runtime.infer({"images": input_tensors[0]})
|
||||
results = postprocessor.run(output_tensors, ims_info)
|
||||
result1 = results[0]
|
||||
|
||||
diff_boxes_1 = np.fabs(
|
||||
np.array(result1.boxes) - np.array(expect1["boxes"]))
|
||||
diff_scores_1 = np.fabs(
|
||||
np.array(result1.scores) - np.array(expect1["scores"]))
|
||||
|
||||
assert diff_boxes_1.max(
|
||||
) < 1e-03, "There's difference in detection boxes 1."
|
||||
assert diff_scores_1.max(
|
||||
) < 1e-04, "There's difference in detection score 1."
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_detection_yolov7face()
|
||||
test_detection_yolov7face_runtime()
|
Reference in New Issue
Block a user