[Model] Refactor insightface models (#919)

* 重构insightface代码

* 重写insightface example代码

* 重写insightface example代码

* 删除多余代码

* 修改预处理代码

* 修改文档

* 修改文档

* 恢复误删除的文件

* 修改cpp example

* 修改cpp example

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 跑通python代码

* 修复重复初始化的bug

* 更新adaface的python代码

* 修复c++重复初始化的问题

* 修复c++重复初始化的问题

* 修复Python重复初始化的问题

* 新增preprocess的几个参数的获取方式

* 修复注释的错误

* 按照要求修改

* 修改文档中的图片为图片压缩包

* 修改编译完成后程序的提示

* 更新错误include

* 删除无用文件

* 更新文档
This commit is contained in:
Zheng_Bicheng
2022-12-26 21:01:58 +08:00
committed by GitHub
parent df940b750f
commit ec67f8ee6d
62 changed files with 1750 additions and 2100 deletions

View File

@@ -10,10 +10,10 @@ download_and_decompress(${RKNPU2_URL} ${CMAKE_CURRENT_BINARY_DIR}/${RKNPU2_FILE}
# set path # set path
set(RKNPU_RUNTIME_PATH ${THIRD_PARTY_PATH}/install/rknpu2_runtime) set(RKNPU_RUNTIME_PATH ${THIRD_PARTY_PATH}/install/rknpu2_runtime)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") #if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
else () #else ()
message(FATAL_ERROR "[rknpu2.cmake] Only support build rknpu2 in Linux") # message(FATAL_ERROR "[rknpu2.cmake] Only support build rknpu2 in Linux")
endif () #endif ()
if (EXISTS ${RKNPU_RUNTIME_PATH}) if (EXISTS ${RKNPU_RUNTIME_PATH})

View File

@@ -1,6 +1,8 @@
# 人脸识别模型 # 人脸识别模型
## 模型支持列表
FastDeploy目前支持如下人脸识别模型部署 FastDeploy目前支持如下人脸识别模型部署
| 模型 | 说明 | 模型格式 | 版本 | | 模型 | 说明 | 模型格式 | 版本 |
@@ -10,3 +12,7 @@ FastDeploy目前支持如下人脸识别模型部署
| [deepinsight/PartialFC](./insightface) | PartialFC 系列模型 | ONNX | [CommitID:babb9a5](https://github.com/deepinsight/insightface/commit/babb9a5) | | [deepinsight/PartialFC](./insightface) | PartialFC 系列模型 | ONNX | [CommitID:babb9a5](https://github.com/deepinsight/insightface/commit/babb9a5) |
| [deepinsight/VPL](./insightface) | VPL 系列模型 | ONNX | [CommitID:babb9a5](https://github.com/deepinsight/insightface/commit/babb9a5) | | [deepinsight/VPL](./insightface) | VPL 系列模型 | ONNX | [CommitID:babb9a5](https://github.com/deepinsight/insightface/commit/babb9a5) |
| [paddleclas/AdaFace](./adaface) | AdaFace 系列模型 | PADDLE | [CommitID:babb9a5](https://github.com/PaddlePaddle/PaddleClas/tree/v2.4.0) | | [paddleclas/AdaFace](./adaface) | AdaFace 系列模型 | PADDLE | [CommitID:babb9a5](https://github.com/PaddlePaddle/PaddleClas/tree/v2.4.0) |
## 模型demo简介
ArcFace,CosFace,PartialFC,VPL同属于deepinsight系列因此demo使用ONNX作为推理框架。AdaFace则采用PaddleInference作为推理框架。

View File

@@ -1,4 +1,4 @@
PROJECT(infer_demo C CXX) PROJECT(infer_adaface_demo C CXX)
CMAKE_MINIMUM_REQUIRED (VERSION 3.12) CMAKE_MINIMUM_REQUIRED (VERSION 3.12)
# 指定下载解压后的fastdeploy库路径 # 指定下载解压后的fastdeploy库路径
@@ -9,5 +9,5 @@ include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
# 添加FastDeploy依赖头文件 # 添加FastDeploy依赖头文件
include_directories(${FASTDEPLOY_INCS}) include_directories(${FASTDEPLOY_INCS})
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc) add_executable(infer_adaface_demo ${PROJECT_SOURCE_DIR}/infer.cc)
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS}) target_link_libraries(infer_adaface_demo ${FASTDEPLOY_LIBS})

View File

@@ -8,56 +8,43 @@
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. 根据开发环境下载预编译部署库和samples代码参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 2. 根据开发环境下载预编译部署库和samples代码参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
以Linux上CPU推理为例在本目录执行如下命令即可完成编译测试支持此模型需保证FastDeploy版本0.7.0以上(x.x.x>=0.7.0) 以Linux上CPU推理为例在本目录执行如下命令即可完成编译测试
```bash ```bash
# “如果预编译库不包含本模型请从最新代码编译SDK”
mkdir build mkdir build
cd build cd build
# 下载FastDeploy预编译库用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
tar xvf fastdeploy-linux-x64-x.x.x.tgz tar xvf fastdeploy-linux-x64-x.x.x.tgz
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
make -j make -j
#下载测试图片 #下载测试图片
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_0.JPG wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_1.JPG unzip face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG
# 如果为Paddle模型运行以下代码 # 如果为Paddle模型运行以下代码
wget https://bj.bcebos.com/paddlehub/fastdeploy/mobilefacenet_adaface.tgz wget https://bj.bcebos.com/paddlehub/fastdeploy/mobilefacenet_adaface.tgz
tar zxvf mobilefacenet_adaface.tgz -C ./ tar zxvf mobilefacenet_adaface.tgz -C ./
# CPU推理 # CPU推理
./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ ./infer_adaface_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \
mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \
test_lite_focal_arcface_0.JPG \ face_0.jpg face_1.jpg face_2.jpg 0
test_lite_focal_arcface_1.JPG \
test_lite_focal_arcface_2.JPG \
0
# GPU推理 # GPU推理
./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ ./infer_adaface_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \
mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \
test_lite_focal_arcface_0.JPG \ face_0.jpg face_1.jpg face_2.jpg 1
test_lite_focal_arcface_1.JPG \
test_lite_focal_arcface_2.JPG \
1
# GPU上TensorRT推理 # GPU上TensorRT推理
./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ ./infer_adaface_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \
mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \
test_lite_focal_arcface_0.JPG \ face_0.jpg face_1.jpg face_2.jpg 2
test_lite_focal_arcface_1.JPG \
test_lite_focal_arcface_2.JPG \
2
# XPU推理 # XPU推理
./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ ./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \
mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \
test_lite_focal_arcface_0.JPG \ face_0.jpg face_1.jpg face_2.jpg 3
test_lite_focal_arcface_1.JPG \
test_lite_focal_arcface_2.JPG \
3
``` ```
运行完成可视化结果如下图所示 运行完成可视化结果如下图所示
@@ -101,16 +88,22 @@ AdaFace模型加载和初始化如果使用PaddleInference推理model_file
> > * **im**: 输入图像注意需为HWCBGR格式 > > * **im**: 输入图像注意需为HWCBGR格式
> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceRecognitionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) > > * **result**: 检测结果,包括检测框,各个框的置信度, FaceRecognitionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
### 类成员变量 ### 修改预处理以及后处理的参数
#### 预处理参数 预处理和后处理的参数的需要通过修改AdaFacePostprocessorAdaFacePreprocessor的成员变量来进行修改。
用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果
#### AdaFacePreprocessor成员变量(预处理参数)
> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112],
通过AdaFacePreprocessor::SetSize(std::vector<int>& size)来进行修改
> > * **alpha**(vector&lt;float&gt;): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5],
通过AdaFacePreprocessor::SetAlpha(std::vector<float>& alpha)来进行修改
> > * **beta**(vector&lt;float&gt;): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f],
通过AdaFacePreprocessor::SetBeta(std::vector<float>& beta)来进行修改
> > * **permute**(bool): 预处理是否将BGR转换成RGB默认true,
通过AdaFacePreprocessor::SetPermute(bool permute)来进行修改
> > * **size**(vector&lt;int&gt;): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112] #### AdaFacePostprocessor成员变量(后处理参数)
> > * **alpha**(vector&lt;float&gt;): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5] > > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认false,
> > * **beta**(vector&lt;float&gt;): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f] AdaFacePostprocessor::SetL2Normalize(bool& l2_normalize)来进行修改
> > * **swap_rb**(bool): 预处理是否将BGR转换成RGB默认true
> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认false
- [模型介绍](../../) - [模型介绍](../../)
- [Python部署](../python) - [Python部署](../python)

View File

@@ -1,14 +1,17 @@
/*************************************************************************** // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
* //
* Copyright (c) 2021 Baidu.com, Inc. 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.
/**
* @author Baidu
* @brief demo_image_inference
*
**/
#include "fastdeploy/vision.h" #include "fastdeploy/vision.h"
void CpuInfer(const std::string &model_file, const std::string &params_file, void CpuInfer(const std::string &model_file, const std::string &params_file,
@@ -28,8 +31,8 @@ void CpuInfer(const std::string &model_file, const std::string &params_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -40,9 +43,11 @@ void CpuInfer(const std::string &model_file, const std::string &params_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -65,8 +70,8 @@ void XpuInfer(const std::string &model_file, const std::string &params_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -77,9 +82,11 @@ void XpuInfer(const std::string &model_file, const std::string &params_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -103,8 +110,8 @@ void GpuInfer(const std::string &model_file, const std::string &params_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -115,9 +122,11 @@ void GpuInfer(const std::string &model_file, const std::string &params_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -143,8 +152,8 @@ void TrtInfer(const std::string &model_file, const std::string &params_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -155,9 +164,11 @@ void TrtInfer(const std::string &model_file, const std::string &params_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }

View File

@@ -15,9 +15,8 @@ cd examples/vision/faceid/adaface/python/
#下载AdaFace模型文件和测试图片 #下载AdaFace模型文件和测试图片
#下载测试图片 #下载测试图片
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_0.JPG wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_1.JPG unzip face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG
# 如果为Paddle模型运行以下代码 # 如果为Paddle模型运行以下代码
wget https://bj.bcebos.com/paddlehub/fastdeploy/mobilefacenet_adaface.tgz wget https://bj.bcebos.com/paddlehub/fastdeploy/mobilefacenet_adaface.tgz
@@ -26,23 +25,23 @@ tar zxvf mobilefacenet_adaface.tgz -C ./
# CPU推理 # CPU推理
python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \
--params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ --params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \
--face test_lite_focal_arcface_0.JPG \ --face face_0.jpg \
--face_positive test_lite_focal_arcface_1.JPG \ --face_positive face_1.jpg \
--face_negative test_lite_focal_arcface_2.JPG \ --face_negative face_2.jpg \
--device cpu --device cpu
# GPU推理 # GPU推理
python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \
--params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ --params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \
--face test_lite_focal_arcface_0.JPG \ --face face_0.jpg \
--face_positive test_lite_focal_arcface_1.JPG \ --face_positive face_1.jpg \
--face_negative test_lite_focal_arcface_2.JPG \ --face_negative face_2.jpg \
--device gpu --device gpu
# GPU上使用TensorRT推理 # GPU上使用TensorRT推理
python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \
--params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ --params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \
--face test_lite_focal_arcface_0.JPG \ --face face_0.jpg \
--face_positive test_lite_focal_arcface_1.JPG \ --face_positive face_1.jpg \
--face_negative test_lite_focal_arcface_2.JPG \ --face_negative face_2.jpg \
--device gpu \ --device gpu \
--use_trt True --use_trt True
@@ -106,11 +105,15 @@ AdaFace模型加载和初始化其中model_file为导出的ONNX模型格式
#### 预处理参数 #### 预处理参数
用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果
#### AdaFacePreprocessor的成员变量
以下变量为AdaFacePreprocessor的成员变量
> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112] > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112]
> > * **alpha**(list[float]): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5] > > * **alpha**(list[float]): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5]
> > * **beta**(list[float]): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f] > > * **beta**(list[float]): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f]
> > * **swap_rb**(bool): 预处理是否将BGR转换成RGB默认True > > * **swap_rb**(bool): 预处理是否将BGR转换成RGB默认True
#### AdaFacePostprocessor的成员变量
以下变量为AdaFacePostprocessor的成员变量
> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认False > > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认False

View File

@@ -7,12 +7,11 @@
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. 根据开发环境下载预编译部署库和samples代码参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 2. 根据开发环境下载预编译部署库和samples代码参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
以Linux上CPU推理为例在本目录执行如下命令即可完成编译测试支持此模型需保证FastDeploy版本0.7.0以上(x.x.x>=0.7.0) 以Linux上CPU推理为例在本目录执行如下命令即可完成编译测试
```bash ```bash
mkdir build mkdir build
cd build cd build
# 下载FastDeploy预编译库用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
tar xvf fastdeploy-linux-x64-x.x.x.tgz tar xvf fastdeploy-linux-x64-x.x.x.tgz
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
@@ -20,17 +19,15 @@ make -j
#下载官方转换好的ArcFace模型文件和测试图片 #下载官方转换好的ArcFace模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r100.onnx wget https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r100.onnx
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_0.JPG wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_1.JPG unzip face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG
# CPU推理 # CPU推理
./infer_arcface_demo ms1mv3_arcface_r100.onnx test_lite_focal_arcface_0.JPG test_lite_focal_arcface_1.JPG test_lite_focal_arcface_2.JPG 0 ./infer_arcface_demo ms1mv3_arcface_r100.onnx face_0.jpg face_1.jpg face_2.jpg 0
# GPU推理 # GPU推理
./infer_arcface_demo ms1mv3_arcface_r100.onnx test_lite_focal_arcface_0.JPG test_lite_focal_arcface_1.JPG test_lite_focal_arcface_2.JPG 1 ./infer_arcface_demo ms1mv3_arcface_r100.onnx face_0.jpg face_1.jpg face_2.jpg 1
# GPU上TensorRT推理 # GPU上TensorRT推理
./infer_arcface_demo ms1mv3_arcface_r100.onnx test_lite_focal_arcface_0.JPG test_lite_focal_arcface_1.JPG test_lite_focal_arcface_2.JPG 2 ./infer_arcface_demo ms1mv3_arcface_r100.onnx face_0.jpg face_1.jpg face_2.jpg 2
``` ```
运行完成可视化结果如下图所示 运行完成可视化结果如下图所示
@@ -113,16 +110,22 @@ VPL模型加载和初始化其中model_file为导出的ONNX模型格式。
> > * **im**: 输入图像注意需为HWCBGR格式 > > * **im**: 输入图像注意需为HWCBGR格式
> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceRecognitionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) > > * **result**: 检测结果,包括检测框,各个框的置信度, FaceRecognitionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
### 类成员变量 ### 修改预处理以及后处理的参数
#### 预处理参数 预处理和后处理的参数的需要通过修改InsightFaceRecognitionPostprocessorInsightFaceRecognitionPreprocessor的成员变量来进行修改。
用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果
#### InsightFaceRecognitionPreprocessor成员变量(预处理参数)
> > * **size**(vector&lt;int&gt;): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112],
通过InsightFaceRecognitionPreprocessor::SetSize(std::vector<int>& size)来进行修改
> > * **alpha**(vector&lt;float&gt;): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5],
通过InsightFaceRecognitionPreprocessor::SetAlpha(std::vector<float>& alpha)来进行修改
> > * **beta**(vector&lt;float&gt;): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f],
通过InsightFaceRecognitionPreprocessor::SetBeta(std::vector<float>& beta)来进行修改
> > * **permute**(bool): 预处理是否将BGR转换成RGB默认true,
通过InsightFaceRecognitionPreprocessor::SetPermute(bool permute)来进行修改
> > * **size**(vector&lt;int&gt;): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112] #### InsightFaceRecognitionPostprocessor成员变量(后处理参数)
> > * **alpha**(vector&lt;float&gt;): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5] > > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认false,
> > * **beta**(vector&lt;float&gt;): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f] InsightFaceRecognitionPostprocessor::SetL2Normalize(bool& l2_normalize)来进行修改
> > * **swap_rb**(bool): 预处理是否将BGR转换成RGB默认true
> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认false
- [模型介绍](../../) - [模型介绍](../../)
- [Python部署](../python) - [Python部署](../python)

View File

@@ -16,11 +16,7 @@
void CpuInfer(const std::string& model_file, void CpuInfer(const std::string& model_file,
const std::vector<std::string>& image_file) { const std::vector<std::string>& image_file) {
auto model = fastdeploy::vision::faceid::ArcFace(model_file); auto model = fastdeploy::vision::faceid::ArcFace(model_file, "");
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
cv::Mat face0 = cv::imread(image_file[0]); cv::Mat face0 = cv::imread(image_file[0]);
cv::Mat face1 = cv::imread(image_file[1]); cv::Mat face1 = cv::imread(image_file[1]);
@@ -30,8 +26,8 @@ void CpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -129,8 +131,7 @@ int main(int argc, char* argv[]) {
if (argc < 6) { if (argc < 6) {
std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " std::cout << "Usage: infer_demo path/to/model path/to/image run_option, "
"e.g ./infer_arcface_demo ms1mv3_arcface_r100.onnx " "e.g ./infer_arcface_demo ms1mv3_arcface_r100.onnx "
"test_lite_focal_arcface_0.JPG test_lite_focal_arcface_1.JPG " "face_0.jpg face_1.jpg face_2.jpg 0"
"test_lite_focal_arcface_2.JPG 0"
<< std::endl; << std::endl;
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " 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." "with gpu; 2: run with gpu and use tensorrt backend."

View File

@@ -16,11 +16,7 @@
void CpuInfer(const std::string& model_file, void CpuInfer(const std::string& model_file,
const std::vector<std::string>& image_file) { const std::vector<std::string>& image_file) {
auto model = fastdeploy::vision::faceid::CosFace(model_file); auto model = fastdeploy::vision::faceid::CosFace(model_file, "");
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
cv::Mat face0 = cv::imread(image_file[0]); cv::Mat face0 = cv::imread(image_file[0]);
cv::Mat face1 = cv::imread(image_file[1]); cv::Mat face1 = cv::imread(image_file[1]);
@@ -30,8 +26,8 @@ void CpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -128,9 +130,8 @@ void TrtInfer(const std::string& model_file,
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc < 6) { if (argc < 6) {
std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " std::cout << "Usage: infer_demo path/to/model path/to/image run_option, "
"e.g ./infer_arcface_demo ms1mv3_arcface_r100.onnx " "e.g ./infer_cosface_demo ms1mv3_cosface_r100.onnx "
"test_lite_focal_arcface_0.JPG test_lite_focal_arcface_1.JPG " "face_0.jpg face_1.jpg face_2.jpg 0"
"test_lite_focal_arcface_2.JPG 0"
<< std::endl; << std::endl;
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " 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." "with gpu; 2: run with gpu and use tensorrt backend."

View File

@@ -16,11 +16,7 @@
void CpuInfer(const std::string& model_file, void CpuInfer(const std::string& model_file,
const std::vector<std::string>& image_file) { const std::vector<std::string>& image_file) {
auto model = fastdeploy::vision::faceid::PartialFC(model_file); auto model = fastdeploy::vision::faceid::PartialFC(model_file, "");
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
cv::Mat face0 = cv::imread(image_file[0]); cv::Mat face0 = cv::imread(image_file[0]);
cv::Mat face1 = cv::imread(image_file[1]); cv::Mat face1 = cv::imread(image_file[1]);
@@ -30,8 +26,8 @@ void CpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -128,9 +130,8 @@ void TrtInfer(const std::string& model_file,
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc < 6) { if (argc < 6) {
std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " std::cout << "Usage: infer_demo path/to/model path/to/image run_option, "
"e.g ./infer_arcface_demo ms1mv3_arcface_r100.onnx " "e.g ./infer_arcface_demo ms1mv3_partial_fc_r100.onnx "
"test_lite_focal_arcface_0.JPG test_lite_focal_arcface_1.JPG " "face_0.jpg face_1.jpg face_2.jpg 0"
"test_lite_focal_arcface_2.JPG 0"
<< std::endl; << std::endl;
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " 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." "with gpu; 2: run with gpu and use tensorrt backend."

View File

@@ -16,11 +16,7 @@
void CpuInfer(const std::string& model_file, void CpuInfer(const std::string& model_file,
const std::vector<std::string>& image_file) { const std::vector<std::string>& image_file) {
auto model = fastdeploy::vision::faceid::VPL(model_file); auto model = fastdeploy::vision::faceid::VPL(model_file, "");
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
cv::Mat face0 = cv::imread(image_file[0]); cv::Mat face0 = cv::imread(image_file[0]);
cv::Mat face1 = cv::imread(image_file[1]); cv::Mat face1 = cv::imread(image_file[1]);
@@ -30,8 +26,8 @@ void CpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file,
fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res1;
fastdeploy::vision::FaceRecognitionResult res2; fastdeploy::vision::FaceRecognitionResult res2;
if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) ||
(!model.Predict(&face2, &res2))) { (!model.Predict(face2, &res2))) {
std::cerr << "Prediction Failed." << std::endl; std::cerr << "Prediction Failed." << std::endl;
} }
@@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file,
std::cout << "--- [Face 2]:" << res2.Str(); std::cout << "--- [Face 2]:" << res2.Str();
float cosine01 = fastdeploy::vision::utils::CosineSimilarity( float cosine01 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res1.embedding, model.l2_normalize); res0.embedding, res1.embedding,
model.GetPostprocessor().GetL2Normalize());
float cosine02 = fastdeploy::vision::utils::CosineSimilarity( float cosine02 = fastdeploy::vision::utils::CosineSimilarity(
res0.embedding, res2.embedding, model.l2_normalize); res0.embedding, res2.embedding,
model.GetPostprocessor().GetL2Normalize());
std::cout << "Detect Done! Cosine 01: " << cosine01 std::cout << "Detect Done! Cosine 01: " << cosine01
<< ", Cosine 02:" << cosine02 << std::endl; << ", Cosine 02:" << cosine02 << std::endl;
} }
@@ -128,9 +130,8 @@ void TrtInfer(const std::string& model_file,
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc < 6) { if (argc < 6) {
std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " std::cout << "Usage: infer_demo path/to/model path/to/image run_option, "
"e.g ./infer_arcface_demo ms1mv3_arcface_r100.onnx " "e.g ./infer_arcface_demo ms1mv3_vpl_r100.onnx "
"test_lite_focal_arcface_0.JPG test_lite_focal_arcface_1.JPG " "face_0.jpg face_1.jpg face_2.jpg 0"
"test_lite_focal_arcface_2.JPG 0"
<< std::endl; << std::endl;
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " 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." "with gpu; 2: run with gpu and use tensorrt backend."

View File

@@ -15,16 +15,28 @@ cd examples/vision/faceid/insightface/python/
#下载ArcFace模型文件和测试图片 #下载ArcFace模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r100.onnx wget https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r100.onnx
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_0.JPG wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_1.JPG unzip face_demo.zip
wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG
# CPU推理 # CPU推理
python infer_arcface.py --model ms1mv3_arcface_r100.onnx --face test_lite_focal_arcface_0.JPG --face_positive test_lite_focal_arcface_1.JPG --face_negative test_lite_focal_arcface_2.JPG --device cpu python infer_arcface.py --model ms1mv3_arcface_r100.onnx \
--face face_0.jpg \
--face_positive face_1.jpg \
--face_negative face_2.jpg \
--device cpu
# GPU推理 # GPU推理
python infer_arcface.py --model ms1mv3_arcface_r100.onnx --face test_lite_focal_arcface_0.JPG --face_positive test_lite_focal_arcface_1.JPG --face_negative test_lite_focal_arcface_2.JPG --device gpu python infer_arcface.py --model ms1mv3_arcface_r100.onnx \
--face face_0.jpg \
--face_positive face_1.jpg \
--face_negative face_2.jpg \
--device gpu
# GPU上使用TensorRT推理 # GPU上使用TensorRT推理
python infer_arcface.py --model ms1mv3_arcface_r100.onnx --face test_lite_focal_arcface_0.JPG --face_positive test_lite_focal_arcface_1.JPG --face_negative test_lite_focal_arcface_2.JPG --device gpu --use_trt True python infer_arcface.py --model ms1mv3_arcface_r100.onnx \
--face face_0.jpg \
--face_positive face_1.jpg \
--face_negative face_2.jpg \
--device gpu \
--use_trt True
``` ```
运行完成可视化结果如下图所示 运行完成可视化结果如下图所示
@@ -82,11 +94,15 @@ ArcFace模型加载和初始化其中model_file为导出的ONNX模型格式
#### 预处理参数 #### 预处理参数
用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果
#### AdaFacePreprocessor的成员变量
以下变量为AdaFacePreprocessor的成员变量
> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112] > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小包含两个整型元素表示[width, height], 默认值为[112, 112]
> > * **alpha**(list[float]): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5] > > * **alpha**(list[float]): 预处理归一化的alpha值计算公式为`x'=x*alpha+beta`alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5]
> > * **beta**(list[float]): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f] > > * **beta**(list[float]): 预处理归一化的beta值计算公式为`x'=x*alpha+beta`beta默认为[-1.f, -1.f, -1.f]
> > * **swap_rb**(bool): 预处理是否将BGR转换成RGB默认True > > * **swap_rb**(bool): 预处理是否将BGR转换成RGB默认True
#### AdaFacePostprocessor的成员变量
以下变量为AdaFacePostprocessor的成员变量
> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认False > > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化默认False

View File

@@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive)
face2 = cv2.imread(args.face_negative) # 0,2 不同的人 face2 = cv2.imread(args.face_negative) # 0,2 不同的人
# 设置 l2 normalize # 设置 l2 normalize
model.l2_normalize = True model.postprocessor.l2_normalize = True
# 预测图片检测结果 # 预测图片检测结果
result0 = model.predict(face0) result0 = model.predict(face0)

View File

@@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive)
face2 = cv2.imread(args.face_negative) # 0,2 不同的人 face2 = cv2.imread(args.face_negative) # 0,2 不同的人
# 设置 l2 normalize # 设置 l2 normalize
model.l2_normalize = True model.postprocessor.l2_normalize = True
# 预测图片检测结果 # 预测图片检测结果
result0 = model.predict(face0) result0 = model.predict(face0)

View File

@@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive)
face2 = cv2.imread(args.face_negative) # 0,2 不同的人 face2 = cv2.imread(args.face_negative) # 0,2 不同的人
# 设置 l2 normalize # 设置 l2 normalize
model.l2_normalize = True model.postprocessor.l2_normalize = True
# 预测图片检测结果 # 预测图片检测结果
result0 = model.predict(face0) result0 = model.predict(face0)

View File

@@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive)
face2 = cv2.imread(args.face_negative) # 0,2 不同的人 face2 = cv2.imread(args.face_negative) # 0,2 不同的人
# 设置 l2 normalize # 设置 l2 normalize
model.l2_normalize = True model.postprocessor.l2_normalize = True
# 预测图片检测结果 # 预测图片检测结果
result0 = model.predict(face0) result0 = model.predict(face0)

View File

@@ -38,12 +38,8 @@
#include "fastdeploy/vision/facedet/contrib/ultraface.h" #include "fastdeploy/vision/facedet/contrib/ultraface.h"
#include "fastdeploy/vision/facedet/contrib/yolov5face.h" #include "fastdeploy/vision/facedet/contrib/yolov5face.h"
#include "fastdeploy/vision/facedet/contrib/yolov7face/yolov7face.h" #include "fastdeploy/vision/facedet/contrib/yolov7face/yolov7face.h"
#include "fastdeploy/vision/faceid/contrib/adaface.h" #include "fastdeploy/vision/faceid/contrib/insightface/model.h"
#include "fastdeploy/vision/faceid/contrib/arcface.h" #include "fastdeploy/vision/faceid/contrib/adaface/adaface.h"
#include "fastdeploy/vision/faceid/contrib/cosface.h"
#include "fastdeploy/vision/faceid/contrib/insightface_rec.h"
#include "fastdeploy/vision/faceid/contrib/partial_fc.h"
#include "fastdeploy/vision/faceid/contrib/vpl.h"
#include "fastdeploy/vision/headpose/contrib/fsanet.h" #include "fastdeploy/vision/headpose/contrib/fsanet.h"
#include "fastdeploy/vision/keypointdet/pptinypose/pptinypose.h" #include "fastdeploy/vision/keypointdet/pptinypose/pptinypose.h"
#include "fastdeploy/vision/matting/contrib/modnet.h" #include "fastdeploy/vision/matting/contrib/modnet.h"

View File

@@ -1,74 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/adaface.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
AdaFace::AdaFace(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option,
const ModelFormat& model_format)
: InsightFaceRecognitionModel(model_file, params_file, custom_option,
model_format) {
initialized = Initialize();
}
bool AdaFace::Initialize() {
// (1) if parent class initialed backend
if (initialized) {
// (1.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
// (2) if parent class not initialed backend
if (!InsightFaceRecognitionModel::Initialize()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
// (2.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
bool AdaFace::Preprocess(Mat* mat, FDTensor* output) {
return InsightFaceRecognitionModel::Preprocess(mat, output);
}
bool AdaFace::Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Postprocess(infer_result, result);
}
bool AdaFace::Predict(cv::Mat* im, FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Predict(im, result);
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,65 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
#include "fastdeploy/vision/faceid/contrib/insightface_rec.h"
namespace fastdeploy {
namespace vision {
/** \brief All object face recognition model APIs are defined inside this namespace
*
*/
namespace faceid {
/*! @brief AdaFace model object used when to load a AdaFace model exported by AdaFacePaddleCLas.
*/
class FASTDEPLOY_DECL AdaFace : public InsightFaceRecognitionModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./adaface.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 PADDLE format
*/
AdaFace(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::PADDLE);
std::string ModelName() const override {
return "Zheng-Bicheng/AdaFacePaddleCLas";
}
/** \brief Predict the face recognition result for an input image
*
* \param[in] im The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result The output face recognition result will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
bool Predict(cv::Mat* im, FaceRecognitionResult* result) override;
private:
bool Initialize() override;
bool Preprocess(Mat* mat, FDTensor* output) override;
bool Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) override;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,84 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/adaface/adaface.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
AdaFace::AdaFace(
const std::string& model_file, const std::string& params_file,
const fastdeploy::RuntimeOption& custom_option,
const fastdeploy::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, Backend::LITE};
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 AdaFace::Initialize() {
if (!InitRuntime()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
return true;
}
bool AdaFace::Predict(const cv::Mat& im,
FaceRecognitionResult* result) {
std::vector<FaceRecognitionResult> results;
if (!BatchPredict({im}, &results)) {
return false;
}
if(!results.empty()){
*result = std::move(results[0]);
}
return true;
}
bool AdaFace::BatchPredict(const std::vector<cv::Mat>& images,
std::vector<FaceRecognitionResult>* results){
std::vector<FDMat> fd_images = WrapMat(images);
FDASSERT(images.size() == 1, "Only support batch = 1 now.");
if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
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)){
FDERROR << "Failed to postprocess the inference results by runtime." << std::endl;
return false;
}
return true;
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,77 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. //NOLINT
//
// 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/faceid/contrib/adaface/postprocessor.h"
#include "fastdeploy/vision/faceid/contrib/adaface/preprocessor.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
/*! @brief AdaFace model object used when to load a AdaFace model exported by AdaFace.
*/
class FASTDEPLOY_DECL AdaFace : public FastDeployModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./adaface.onnx
* \param[in] params_file Path of parameter file, e.g adaface/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
*/
AdaFace(
const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX);
std::string ModelName() const { return "insightface_rec"; }
/** \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 FaceRecognitionResult will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
virtual bool Predict(const cv::Mat& im, FaceRecognitionResult* 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 FaceRecognitionResult list
* \return true if the prediction successed, otherwise false
*/
virtual bool BatchPredict(const std::vector<cv::Mat>& images,
std::vector<FaceRecognitionResult>* results);
/// Get preprocessor reference of AdaFace
virtual AdaFacePreprocessor& GetPreprocessor() {
return preprocessor_;
}
/// Get postprocessor reference of AdaFace
virtual AdaFacePostprocessor& GetPostprocessor() {
return postprocessor_;
}
protected:
bool Initialize();
AdaFacePreprocessor preprocessor_;
AdaFacePostprocessor postprocessor_;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,89 @@
// 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 BindAdaFace(pybind11::module& m) {
pybind11::class_<vision::faceid::AdaFacePreprocessor>(
m, "AdaFacePreprocessor")
.def(pybind11::init())
.def("run", [](vision::faceid::AdaFacePreprocessor& 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;
if (!self.Run(&images, &outputs)) {
throw std::runtime_error("Failed to preprocess the input data in AdaFacePreprocessor.");
}
for (size_t i = 0; i < outputs.size(); ++i) {
outputs[i].StopSharing();
}
return outputs;
})
.def_property("permute", &vision::faceid::AdaFacePreprocessor::GetPermute,
&vision::faceid::AdaFacePreprocessor::SetPermute)
.def_property("alpha", &vision::faceid::AdaFacePreprocessor::GetAlpha,
&vision::faceid::AdaFacePreprocessor::SetAlpha)
.def_property("beta", &vision::faceid::AdaFacePreprocessor::GetBeta,
&vision::faceid::AdaFacePreprocessor::SetBeta)
.def_property("size", &vision::faceid::AdaFacePreprocessor::GetSize,
&vision::faceid::AdaFacePreprocessor::SetSize);
pybind11::class_<vision::faceid::AdaFacePostprocessor>(
m, "AdaFacePostprocessor")
.def(pybind11::init())
.def("run", [](vision::faceid::AdaFacePostprocessor& self, std::vector<FDTensor>& inputs) {
std::vector<vision::FaceRecognitionResult> results;
if (!self.Run(inputs, &results)) {
throw std::runtime_error("Failed to postprocess the runtime result in AdaFacePostprocessor.");
}
return results;
})
.def("run", [](vision::faceid::AdaFacePostprocessor& self, std::vector<pybind11::array>& input_array) {
std::vector<vision::FaceRecognitionResult> results;
std::vector<FDTensor> inputs;
PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
if (!self.Run(inputs, &results)) {
throw std::runtime_error("Failed to postprocess the runtime result in AdaFacePostprocessor.");
}
return results;
})
.def_property("l2_normalize", &vision::faceid::AdaFacePostprocessor::GetL2Normalize,
&vision::faceid::AdaFacePostprocessor::SetL2Normalize);
pybind11::class_<vision::faceid::AdaFace, FastDeployModel>(
m, "AdaFace")
.def(pybind11::init<std::string, std::string, RuntimeOption, ModelFormat>())
.def("predict", [](vision::faceid::AdaFace& self, pybind11::array& data) {
cv::Mat im = PyArrayToCvMat(data);
vision::FaceRecognitionResult result;
self.Predict(im, &result);
return result;
})
.def("batch_predict", [](vision::faceid::AdaFace& 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::FaceRecognitionResult> results;
self.BatchPredict(images, &results);
return results;
})
.def_property_readonly("preprocessor", &vision::faceid::AdaFace::GetPreprocessor)
.def_property_readonly("postprocessor", &vision::faceid::AdaFace::GetPostprocessor);
}
} // namespace fastdeploy

View File

@@ -0,0 +1,67 @@
// 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/faceid/contrib/adaface/postprocessor.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
AdaFacePostprocessor::AdaFacePostprocessor() {
l2_normalize_ = false;
}
bool AdaFacePostprocessor::Run(std::vector<FDTensor>& infer_result,
std::vector<FaceRecognitionResult>* results) {
if (infer_result[0].dtype != FDDataType::FP32) {
FDERROR << "Only support post process with float32 data." << std::endl;
return false;
}
if(infer_result.size() != 1){
FDERROR << "The default number of output tensor "
"must be 1 according to insightface." << std::endl;
}
int batch = infer_result[0].shape[0];
results->resize(batch);
for (size_t bs = 0; bs < batch; ++bs) {
FDTensor& embedding_tensor = infer_result.at(bs);
FDASSERT((embedding_tensor.shape[0] == 1), "Only support batch = 1 now.");
if (embedding_tensor.dtype != FDDataType::FP32) {
FDERROR << "Only support post process with float32 data." << std::endl;
return false;
}
(*results)[bs].Clear();
(*results)[bs].Resize(embedding_tensor.Numel());
// Copy the raw embedding vector directly without L2 normalize
// post process. Let the user decide whether to normalize or not.
// Will call utils::L2Normlize() method to perform L2
// normalize if l2_normalize was set as 'true'.
std::memcpy((*results)[bs].embedding.data(),
embedding_tensor.Data(),
embedding_tensor.Nbytes());
if (l2_normalize_) {
auto norm_embedding = utils::L2Normalize((*results)[bs].embedding);
std::memcpy((*results)[bs].embedding.data(),
norm_embedding.data(),
embedding_tensor.Nbytes());
}
}
return true;
}
} // namespace detection
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,50 @@
// 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 faceid {
/*! @brief Postprocessor object for AdaFace serials model.
*/
class FASTDEPLOY_DECL AdaFacePostprocessor {
public:
/** \brief Create a postprocessor instance for AdaFace serials model
*/
AdaFacePostprocessor();
/** \brief Process the result of runtime and fill to FaceRecognitionResult structure
*
* \param[in] tensors The inference result from runtime
* \param[in] result The output result of FaceRecognitionResult
* \return true if the postprocess successed, otherwise false
*/
bool Run(std::vector<FDTensor>& infer_result,
std::vector<FaceRecognitionResult>* results);
void SetL2Normalize(bool& l2_normalize) { l2_normalize_ = l2_normalize; }
bool GetL2Normalize() { return l2_normalize_; }
private:
bool l2_normalize_;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,76 @@
// 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/faceid/contrib/adaface/preprocessor.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
AdaFacePreprocessor::AdaFacePreprocessor() {
// parameters for preprocess
size_ = {112, 112};
alpha_ = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta_ = {-1.f, -1.f, -1.f}; // RGB
permute_ = true;
}
bool AdaFacePreprocessor::Preprocess(FDMat * mat, FDTensor* output) {
// face recognition model's preprocess steps in insightface
// reference: insightface/recognition/arcface_torch/inference.py
// 1. Resize
// 2. BGR2RGB
// 3. Convert(opencv style) or Normalize
// 4. HWC2CHW
int resize_w = size_[0];
int resize_h = size_[1];
if (resize_h != mat->Height() || resize_w != mat->Width()) {
Resize::Run(mat, resize_w, resize_h);
}
if (permute_) {
BGR2RGB::Run(mat);
}
Convert::Run(mat, alpha_, beta_);
HWC2CHW::Run(mat);
Cast::Run(mat, "float");
mat->ShareWithTensor(output);
output->ExpandDim(0); // reshape to n, h, w, c
return true;
}
bool AdaFacePreprocessor::Run(std::vector<FDMat>* images,
std::vector<FDTensor>* outputs) {
if (images->empty()) {
FDERROR << "The size of input images should be greater than 0." << std::endl;
return false;
}
FDASSERT(images->size() == 1, "Only support batch = 1 now.");
outputs->resize(1);
// Concat all the preprocessed data to a batch tensor
std::vector<FDTensor> tensors(images->size());
for (size_t i = 0; i < images->size(); ++i) {
if (!Preprocess(&(*images)[i], &tensors[i])) {
FDERROR << "Failed to preprocess input image." << std::endl;
return false;
}
}
(*outputs)[0] = std::move(tensors[0]);
return true;
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,80 @@
// 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 faceid {
/*! @brief Preprocessor object for AdaFace serials model.
*/
class FASTDEPLOY_DECL AdaFacePreprocessor {
public:
/** \brief Create a preprocessor instance for AdaFace serials model
*/
AdaFacePreprocessor();
/** \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
* \return true if the preprocess successed, otherwise false
*/
bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs);
/// Get Size
std::vector<int> GetSize() { return size_; }
/// Set size.
void SetSize(std::vector<int>& size) { size_ = size; }
/// Get alpha
std::vector<float> GetAlpha() { return alpha_; }
/// Set alpha.
void SetAlpha(std::vector<float>& alpha) { alpha_ = alpha; }
/// Get beta
std::vector<float> GetBeta() { return beta_; }
/// Set beta.
void SetBeta(std::vector<float>& beta) { beta_ = beta; }
bool GetPermute() { return permute_; }
/// Set permute.
void SetPermute(bool permute) { permute_ = permute; }
protected:
bool Preprocess(FDMat* mat, FDTensor* output);
// Argument for image preprocessing step, tuple of (width, height),
// decide the target size after resize, default (112, 112)
std::vector<int> size_;
// Argument for image preprocessing step, alpha values for normalization,
// default alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
std::vector<float> alpha_;
// Argument for image preprocessing step, beta values for normalization,
// default beta = {-1.f, -1.f, -1.f}
std::vector<float> beta_;
// Argument for image preprocessing step, whether to swap the B and R channel,
// such as BGR->RGB, default true.
bool permute_;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,38 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/pybind/main.h"
namespace fastdeploy {
void BindAdaFace(pybind11::module& m) {
// Bind AdaFace
pybind11::class_<vision::faceid::AdaFace,
vision::faceid::InsightFaceRecognitionModel>(m, "AdaFace")
.def(pybind11::init<std::string, std::string, RuntimeOption,
ModelFormat>())
.def("predict",
[](vision::faceid::AdaFace& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::FaceRecognitionResult res;
self.Predict(&mat, &res);
return res;
})
.def_readwrite("size", &vision::faceid::AdaFace::size)
.def_readwrite("alpha", &vision::faceid::AdaFace::alpha)
.def_readwrite("beta", &vision::faceid::AdaFace::beta)
.def_readwrite("swap_rb", &vision::faceid::AdaFace::swap_rb)
.def_readwrite("l2_normalize", &vision::faceid::AdaFace::l2_normalize);
}
} // namespace fastdeploy

View File

@@ -1,74 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/arcface.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
ArcFace::ArcFace(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option,
const ModelFormat& model_format)
: InsightFaceRecognitionModel(model_file, params_file, custom_option,
model_format) {
initialized = Initialize();
}
bool ArcFace::Initialize() {
// (1) if parent class initialed backend
if (initialized) {
// (1.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
// (2) if parent class not initialed backend
if (!InsightFaceRecognitionModel::Initialize()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
// (2.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
bool ArcFace::Preprocess(Mat* mat, FDTensor* output) {
return InsightFaceRecognitionModel::Preprocess(mat, output);
}
bool ArcFace::Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Postprocess(infer_result, result);
}
bool ArcFace::Predict(cv::Mat* im, FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Predict(im, result);
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,63 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
#include "fastdeploy/vision/faceid/contrib/insightface_rec.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
/*! @brief ArcFace model object used when to load a ArcFace model exported by IngsightFace.
*/
class FASTDEPLOY_DECL ArcFace : public InsightFaceRecognitionModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./arcface.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
*/
ArcFace(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX);
std::string ModelName() const override {
return "deepinsight/insightface/recognition/arcface_pytorch";
}
/** \brief Predict the face recognition result for an input image
*
* \param[in] im The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result The output face recognition result will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
bool Predict(cv::Mat* im, FaceRecognitionResult* result) override;
private:
bool Initialize() override;
bool Preprocess(Mat* mat, FDTensor* output) override;
bool Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) override;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,38 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/pybind/main.h"
namespace fastdeploy {
void BindArcFace(pybind11::module& m) {
// Bind ArcFace
pybind11::class_<vision::faceid::ArcFace,
vision::faceid::InsightFaceRecognitionModel>(m, "ArcFace")
.def(pybind11::init<std::string, std::string, RuntimeOption,
ModelFormat>())
.def("predict",
[](vision::faceid::ArcFace& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::FaceRecognitionResult res;
self.Predict(&mat, &res);
return res;
})
.def_readwrite("size", &vision::faceid::ArcFace::size)
.def_readwrite("alpha", &vision::faceid::ArcFace::alpha)
.def_readwrite("beta", &vision::faceid::ArcFace::beta)
.def_readwrite("swap_rb", &vision::faceid::ArcFace::swap_rb)
.def_readwrite("l2_normalize", &vision::faceid::ArcFace::l2_normalize);
}
} // namespace fastdeploy

View File

@@ -1,72 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/cosface.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
CosFace::CosFace(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option,
const ModelFormat& model_format)
: InsightFaceRecognitionModel(model_file, params_file, custom_option,
model_format) {
initialized = Initialize();
}
bool CosFace::Initialize() {
if (initialized) {
// (1.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
if (!InsightFaceRecognitionModel::Initialize()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
// (2.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
bool CosFace::Preprocess(Mat* mat, FDTensor* output) {
return InsightFaceRecognitionModel::Preprocess(mat, output);
}
bool CosFace::Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Postprocess(infer_result, result);
}
bool CosFace::Predict(cv::Mat* im, FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Predict(im, result);
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,63 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
#include "fastdeploy/vision/faceid/contrib/insightface_rec.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
/*! @brief CosFace model object used when to load a CosFace model exported by IngsightFace.
*/
class FASTDEPLOY_DECL CosFace : public InsightFaceRecognitionModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./cosface.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
*/
CosFace(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX);
std::string ModelName() const override {
return "deepinsight/insightface/recognition/arcface_pytorch";
}
/** \brief Predict the face recognition result for an input image
*
* \param[in] im The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result The output face recognition result will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
bool Predict(cv::Mat* im, FaceRecognitionResult* result) override;
private:
bool Initialize() override;
bool Preprocess(Mat* mat, FDTensor* output) override;
bool Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) override;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,38 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/pybind/main.h"
namespace fastdeploy {
void BindCosFace(pybind11::module& m) {
// Bind CosFace
pybind11::class_<vision::faceid::CosFace,
vision::faceid::InsightFaceRecognitionModel>(m, "CosFace")
.def(pybind11::init<std::string, std::string, RuntimeOption,
ModelFormat>())
.def("predict",
[](vision::faceid::CosFace& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::FaceRecognitionResult res;
self.Predict(&mat, &res);
return res;
})
.def_readwrite("size", &vision::faceid::CosFace::size)
.def_readwrite("alpha", &vision::faceid::CosFace::alpha)
.def_readwrite("beta", &vision::faceid::CosFace::beta)
.def_readwrite("swap_rb", &vision::faceid::CosFace::swap_rb)
.def_readwrite("l2_normalize", &vision::faceid::CosFace::l2_normalize);
}
} // namespace fastdeploy

View File

@@ -0,0 +1,82 @@
// 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/faceid/contrib/insightface/base.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
InsightFaceRecognitionBase::InsightFaceRecognitionBase(
const std::string& model_file, const std::string& params_file,
const fastdeploy::RuntimeOption& custom_option,
const fastdeploy::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, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_xpu_backends = {Backend::LITE};
}
runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
runtime_option.params_file = params_file;
}
bool InsightFaceRecognitionBase::Initialize() {
if (!InitRuntime()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
return true;
}
bool InsightFaceRecognitionBase::Predict(const cv::Mat& im,
FaceRecognitionResult* result) {
std::vector<FaceRecognitionResult> results;
if (!BatchPredict({im}, &results)) {
return false;
}
*result = std::move(results[0]);
return true;
}
bool InsightFaceRecognitionBase::BatchPredict(const std::vector<cv::Mat>& images,
std::vector<FaceRecognitionResult>* results){
std::vector<FDMat> fd_images = WrapMat(images);
FDASSERT(images.size() == 1, "Only support batch = 1 now.");
if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
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)){
FDERROR << "Failed to postprocess the inference results by runtime." << std::endl;
return false;
}
return true;
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,77 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. //NOLINT
//
// 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/faceid/contrib/insightface/postprocessor.h"
#include "fastdeploy/vision/faceid/contrib/insightface/preprocessor.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
/*! @brief InsightFaceRecognition model object used when to load a InsightFaceRecognition model exported by InsightFaceRecognition.
*/
class FASTDEPLOY_DECL InsightFaceRecognitionBase : public FastDeployModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./arcface.onnx
* \param[in] params_file Path of parameter file, e.g arcface/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
*/
InsightFaceRecognitionBase(
const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX);
std::string ModelName() const { return "insightface_rec"; }
/** \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 FaceRecognitionResult will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
virtual bool Predict(const cv::Mat& im, FaceRecognitionResult* 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 FaceRecognitionResult list
* \return true if the prediction successed, otherwise false
*/
virtual bool BatchPredict(const std::vector<cv::Mat>& images,
std::vector<FaceRecognitionResult>* results);
/// Get preprocessor reference of InsightFaceRecognition
virtual InsightFaceRecognitionPreprocessor& GetPreprocessor() {
return preprocessor_;
}
/// Get postprocessor reference of InsightFaceRecognition
virtual InsightFaceRecognitionPostprocessor& GetPostprocessor() {
return postprocessor_;
}
protected:
bool Initialize();
InsightFaceRecognitionPreprocessor preprocessor_;
InsightFaceRecognitionPostprocessor postprocessor_;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View 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/pybind/main.h"
namespace fastdeploy {
void BindInsightFace(pybind11::module& m) {
pybind11::class_<vision::faceid::InsightFaceRecognitionPreprocessor>(
m, "InsightFaceRecognitionPreprocessor")
.def(pybind11::init())
.def("run", [](vision::faceid::InsightFaceRecognitionPreprocessor& 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;
if (!self.Run(&images, &outputs)) {
throw std::runtime_error("Failed to preprocess the input data in InsightFaceRecognitionPreprocessor.");
}
for (size_t i = 0; i < outputs.size(); ++i) {
outputs[i].StopSharing();
}
return outputs;
})
.def_property("permute", &vision::faceid::InsightFaceRecognitionPreprocessor::GetPermute,
&vision::faceid::InsightFaceRecognitionPreprocessor::SetPermute)
.def_property("alpha", &vision::faceid::InsightFaceRecognitionPreprocessor::GetAlpha,
&vision::faceid::InsightFaceRecognitionPreprocessor::SetAlpha)
.def_property("beta", &vision::faceid::InsightFaceRecognitionPreprocessor::GetBeta,
&vision::faceid::InsightFaceRecognitionPreprocessor::SetBeta)
.def_property("size", &vision::faceid::InsightFaceRecognitionPreprocessor::GetSize,
&vision::faceid::InsightFaceRecognitionPreprocessor::SetSize);
pybind11::class_<vision::faceid::InsightFaceRecognitionPostprocessor>(
m, "InsightFaceRecognitionPostprocessor")
.def(pybind11::init())
.def("run", [](vision::faceid::InsightFaceRecognitionPostprocessor& self, std::vector<FDTensor>& inputs) {
std::vector<vision::FaceRecognitionResult> results;
if (!self.Run(inputs, &results)) {
throw std::runtime_error("Failed to postprocess the runtime result in InsightFaceRecognitionPostprocessor.");
}
return results;
})
.def("run", [](vision::faceid::InsightFaceRecognitionPostprocessor& self, std::vector<pybind11::array>& input_array) {
std::vector<vision::FaceRecognitionResult> results;
std::vector<FDTensor> inputs;
PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
if (!self.Run(inputs, &results)) {
throw std::runtime_error("Failed to postprocess the runtime result in InsightFaceRecognitionPostprocessor.");
}
return results;
})
.def_property("l2_normalize", &vision::faceid::InsightFaceRecognitionPostprocessor::GetL2Normalize,
&vision::faceid::InsightFaceRecognitionPostprocessor::SetL2Normalize);
pybind11::class_<vision::faceid::InsightFaceRecognitionBase, FastDeployModel>(
m, "InsightFaceRecognitionBase")
.def(pybind11::init<std::string, std::string, RuntimeOption, ModelFormat>())
.def("predict", [](vision::faceid::InsightFaceRecognitionBase& self, pybind11::array& data) {
cv::Mat im = PyArrayToCvMat(data);
vision::FaceRecognitionResult result;
self.Predict(im, &result);
return result;
})
.def("batch_predict", [](vision::faceid::InsightFaceRecognitionBase& 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::FaceRecognitionResult> results;
self.BatchPredict(images, &results);
return results;
})
.def_property_readonly("preprocessor", &vision::faceid::InsightFaceRecognitionBase::GetPreprocessor)
.def_property_readonly("postprocessor", &vision::faceid::InsightFaceRecognitionBase::GetPostprocessor);
pybind11::class_<vision::faceid::ArcFace, vision::faceid::InsightFaceRecognitionBase>(m, "ArcFace")
.def(pybind11::init<std::string, std::string, RuntimeOption,ModelFormat>());
pybind11::class_<vision::faceid::CosFace, vision::faceid::InsightFaceRecognitionBase>(m, "CosFace")
.def(pybind11::init<std::string, std::string, RuntimeOption,ModelFormat>());
pybind11::class_<vision::faceid::PartialFC, vision::faceid::InsightFaceRecognitionBase>(m, "PartialFC")
.def(pybind11::init<std::string, std::string, RuntimeOption,ModelFormat>());
pybind11::class_<vision::faceid::VPL, vision::faceid::InsightFaceRecognitionBase>(m, "VPL")
.def(pybind11::init<std::string, std::string, RuntimeOption,ModelFormat>());
}
} // namespace fastdeploy

View File

@@ -0,0 +1,133 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. //NOLINT
//
// 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/faceid/contrib/insightface/base.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
class FASTDEPLOY_DECL ArcFace : public InsightFaceRecognitionBase {
public:
/** \brief Set path of model file and configuration file, and the configuration of runtime
*
* \param[in] model_file Path of model file, e.g ArcFace/model.pdmodel
* \param[in] params_file Path of parameter file, e.g ArcFace/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 Paddle format
*/
ArcFace(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX)
: InsightFaceRecognitionBase(model_file, params_file, custom_option,
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, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_xpu_backends = {Backend::LITE};
}
initialized = Initialize();
}
virtual std::string ModelName() const { return "ArcFace"; }
};
class FASTDEPLOY_DECL CosFace : public InsightFaceRecognitionBase {
public:
/** \brief Set path of model file and configuration file, and the configuration of runtime
*
* \param[in] model_file Path of model file, e.g CosFace/model.pdmodel
* \param[in] params_file Path of parameter file, e.g CosFace/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 Paddle format
*/
CosFace(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX)
: InsightFaceRecognitionBase(model_file, params_file, custom_option,
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, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_xpu_backends = {Backend::LITE};
}
initialized = Initialize();
}
virtual std::string ModelName() const { return "CosFace"; }
};
class FASTDEPLOY_DECL PartialFC : public InsightFaceRecognitionBase {
public:
/** \brief Set path of model file and configuration file, and the configuration of runtime
*
* \param[in] model_file Path of model file, e.g PartialFC/model.pdmodel
* \param[in] params_file Path of parameter file, e.g PartialFC/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 Paddle format
*/
PartialFC(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX)
: InsightFaceRecognitionBase(model_file, params_file, custom_option,
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, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_xpu_backends = {Backend::LITE};
}
initialized = Initialize();
}
virtual std::string ModelName() const { return "PartialFC"; }
};
class FASTDEPLOY_DECL VPL : public InsightFaceRecognitionBase {
public:
/** \brief Set path of model file and configuration file, and the configuration of runtime
*
* \param[in] model_file Path of model file, e.g VPL/model.pdmodel
* \param[in] params_file Path of parameter file, e.g VPL/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 Paddle format
*/
VPL(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX)
: InsightFaceRecognitionBase(model_file, params_file, custom_option,
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, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_xpu_backends = {Backend::LITE};
}
initialized = Initialize();
}
virtual std::string ModelName() const { return "VPL"; }
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,67 @@
// 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/faceid/contrib/insightface/postprocessor.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
InsightFaceRecognitionPostprocessor::InsightFaceRecognitionPostprocessor() {
l2_normalize_ = false;
}
bool InsightFaceRecognitionPostprocessor::Run(std::vector<FDTensor>& infer_result,
std::vector<FaceRecognitionResult>* results) {
if (infer_result[0].dtype != FDDataType::FP32) {
FDERROR << "Only support post process with float32 data." << std::endl;
return false;
}
if(infer_result.size() != 1){
FDERROR << "The default number of output tensor "
"must be 1 according to insightface." << std::endl;
}
int batch = infer_result[0].shape[0];
results->resize(batch);
for (size_t bs = 0; bs < batch; ++bs) {
FDTensor& embedding_tensor = infer_result.at(bs);
FDASSERT((embedding_tensor.shape[0] == 1), "Only support batch = 1 now.");
if (embedding_tensor.dtype != FDDataType::FP32) {
FDERROR << "Only support post process with float32 data." << std::endl;
return false;
}
(*results)[bs].Clear();
(*results)[bs].Resize(embedding_tensor.Numel());
// Copy the raw embedding vector directly without L2 normalize
// post process. Let the user decide whether to normalize or not.
// Will call utils::L2Normlize() method to perform L2
// normalize if l2_normalize was set as 'true'.
std::memcpy((*results)[bs].embedding.data(),
embedding_tensor.Data(),
embedding_tensor.Nbytes());
if (l2_normalize_) {
auto norm_embedding = utils::L2Normalize((*results)[bs].embedding);
std::memcpy((*results)[bs].embedding.data(),
norm_embedding.data(),
embedding_tensor.Nbytes());
}
}
return true;
}
} // namespace detection
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,50 @@
// 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 faceid {
/*! @brief Postprocessor object for InsightFaceRecognition serials model.
*/
class FASTDEPLOY_DECL InsightFaceRecognitionPostprocessor {
public:
/** \brief Create a postprocessor instance for InsightFaceRecognition serials model
*/
InsightFaceRecognitionPostprocessor();
/** \brief Process the result of runtime and fill to FaceRecognitionResult structure
*
* \param[in] tensors The inference result from runtime
* \param[in] result The output result of FaceRecognitionResult
* \return true if the postprocess successed, otherwise false
*/
bool Run(std::vector<FDTensor>& infer_result,
std::vector<FaceRecognitionResult>* results);
void SetL2Normalize(bool& l2_normalize) { l2_normalize_ = l2_normalize; }
bool GetL2Normalize() { return l2_normalize_; }
private:
bool l2_normalize_;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,76 @@
// 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/faceid/contrib/insightface/preprocessor.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
InsightFaceRecognitionPreprocessor::InsightFaceRecognitionPreprocessor() {
// parameters for preprocess
size_ = {112, 112};
alpha_ = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta_ = {-1.f, -1.f, -1.f}; // RGB
permute_ = true;
}
bool InsightFaceRecognitionPreprocessor::Preprocess(FDMat * mat, FDTensor* output) {
// face recognition model's preprocess steps in insightface
// reference: insightface/recognition/arcface_torch/inference.py
// 1. Resize
// 2. BGR2RGB
// 3. Convert(opencv style) or Normalize
// 4. HWC2CHW
int resize_w = size_[0];
int resize_h = size_[1];
if (resize_h != mat->Height() || resize_w != mat->Width()) {
Resize::Run(mat, resize_w, resize_h);
}
if (permute_) {
BGR2RGB::Run(mat);
}
Convert::Run(mat, alpha_, beta_);
HWC2CHW::Run(mat);
Cast::Run(mat, "float");
mat->ShareWithTensor(output);
output->ExpandDim(0); // reshape to n, h, w, c
return true;
}
bool InsightFaceRecognitionPreprocessor::Run(std::vector<FDMat>* images,
std::vector<FDTensor>* outputs) {
if (images->empty()) {
FDERROR << "The size of input images should be greater than 0." << std::endl;
return false;
}
FDASSERT(images->size() == 1, "Only support batch = 1 now.");
outputs->resize(1);
// Concat all the preprocessed data to a batch tensor
std::vector<FDTensor> tensors(images->size());
for (size_t i = 0; i < images->size(); ++i) {
if (!Preprocess(&(*images)[i], &tensors[i])) {
FDERROR << "Failed to preprocess input image." << std::endl;
return false;
}
}
(*outputs)[0] = std::move(tensors[0]);
return true;
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,80 @@
// 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 faceid {
/*! @brief Preprocessor object for InsightFaceRecognition serials model.
*/
class FASTDEPLOY_DECL InsightFaceRecognitionPreprocessor {
public:
/** \brief Create a preprocessor instance for InsightFaceRecognition serials model
*/
InsightFaceRecognitionPreprocessor();
/** \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
* \return true if the preprocess successed, otherwise false
*/
bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs);
/// Get Size
std::vector<int> GetSize() { return size_; }
/// Set size.
void SetSize(std::vector<int>& size) { size_ = size; }
/// Get alpha
std::vector<float> GetAlpha() { return alpha_; }
/// Set alpha.
void SetAlpha(std::vector<float>& alpha) { alpha_ = alpha; }
/// Get beta
std::vector<float> GetBeta() { return beta_; }
/// Set beta.
void SetBeta(std::vector<float>& beta) { beta_ = beta; }
bool GetPermute() { return permute_; }
/// Set permute.
void SetPermute(bool permute) { permute_ = permute; }
protected:
bool Preprocess(FDMat* mat, FDTensor* output);
// Argument for image preprocessing step, tuple of (width, height),
// decide the target size after resize, default (112, 112)
std::vector<int> size_;
// Argument for image preprocessing step, alpha values for normalization,
// default alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
std::vector<float> alpha_;
// Argument for image preprocessing step, beta values for normalization,
// default beta = {-1.f, -1.f, -1.f}
std::vector<float> beta_;
// Argument for image preprocessing step, whether to swap the B and R channel,
// such as BGR->RGB, default true.
bool permute_;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,138 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/insightface_rec.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
InsightFaceRecognitionModel::InsightFaceRecognitionModel(
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, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_xpu_backends = {Backend::LITE};
}
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 InsightFaceRecognitionModel::Initialize() {
// parameters for preprocess
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
if (!InitRuntime()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
return true;
}
bool InsightFaceRecognitionModel::Preprocess(Mat* mat, FDTensor* output) {
// face recognition model's preprocess steps in insightface
// reference: insightface/recognition/arcface_torch/inference.py
// 1. Resize
// 2. BGR2RGB
// 3. Convert(opencv style) or Normalize
// 4. HWC2CHW
int resize_w = size[0];
int resize_h = size[1];
if (resize_h != mat->Height() || resize_w != mat->Width()) {
Resize::Run(mat, resize_w, resize_h);
}
if (swap_rb) {
BGR2RGB::Run(mat);
}
Convert::Run(mat, alpha, beta);
HWC2CHW::Run(mat);
Cast::Run(mat, "float");
mat->ShareWithTensor(output);
output->shape.insert(output->shape.begin(), 1); // reshape to n, h, w, c
return true;
}
bool InsightFaceRecognitionModel::Postprocess(
std::vector<FDTensor>& infer_result, FaceRecognitionResult* result) {
FDASSERT((infer_result.size() == 1),
"The default number of output tensor must be 1 according to "
"insightface.");
FDTensor& embedding_tensor = infer_result.at(0);
FDASSERT((embedding_tensor.shape[0] == 1), "Only support batch =1 now.");
if (embedding_tensor.dtype != FDDataType::FP32) {
FDERROR << "Only support post process with float32 data." << std::endl;
return false;
}
result->Clear();
result->Resize(embedding_tensor.Numel());
// Copy the raw embedding vector directly without L2 normalize
// post process. Let the user decide whether to normalize or not.
// Will call utils::L2Normlize() method to perform L2
// normalize if l2_normalize was set as 'true'.
std::memcpy(result->embedding.data(), embedding_tensor.Data(),
embedding_tensor.Nbytes());
if (l2_normalize) {
auto norm_embedding = utils::L2Normalize(result->embedding);
std::memcpy(result->embedding.data(), norm_embedding.data(),
embedding_tensor.Nbytes());
}
return true;
}
bool InsightFaceRecognitionModel::Predict(cv::Mat* im,
FaceRecognitionResult* result) {
Mat mat(*im);
std::vector<FDTensor> input_tensors(1);
if (!Preprocess(&mat, &input_tensors[0])) {
FDERROR << "Failed to preprocess input image." << std::endl;
return false;
}
input_tensors[0].name = InputInfoOfRuntime(0).name;
std::vector<FDTensor> output_tensors;
if (!Infer(input_tensors, &output_tensors)) {
FDERROR << "Failed to inference." << std::endl;
return false;
}
if (!Postprocess(output_tensors, result)) {
FDERROR << "Failed to post process." << std::endl;
return false;
}
return true;
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,81 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
/*! @brief CosFace model object used when to load a CosFace model exported by IngsightFace.
*/
class FASTDEPLOY_DECL InsightFaceRecognitionModel : public FastDeployModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./arcface.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
*/
InsightFaceRecognitionModel(
const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX);
virtual std::string ModelName() const { return "deepinsight/insightface"; }
/*! @brief
Argument for image preprocessing step, tuple of (width, height), decide the target size after resize, default (112, 112)
*/
std::vector<int> size;
/*! @brief
Argument for image preprocessing step, alpha values for normalization, default alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
*/
std::vector<float> alpha;
/*! @brief
Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f}
*/
std::vector<float> beta;
/*! @brief
Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default true.
*/
bool swap_rb;
/*! @brief
Argument for image postprocessing step, whether to apply l2 normalize to embedding values, default false;
*/
bool l2_normalize;
/** \brief Predict the face recognition result for an input image
*
* \param[in] im The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result The output face recognition result will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
virtual bool Predict(cv::Mat* im, FaceRecognitionResult* result);
virtual bool Initialize();
virtual bool Preprocess(Mat* mat, FDTensor* output);
virtual bool Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result);
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,43 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/pybind/main.h"
namespace fastdeploy {
void BindInsightFaceRecognitionModel(pybind11::module& m) {
// Bind InsightFaceRecognitionModel
pybind11::class_<vision::faceid::InsightFaceRecognitionModel,
FastDeployModel>(m, "InsightFaceRecognitionModel")
.def(pybind11::init<std::string, std::string, RuntimeOption,
ModelFormat>())
.def("predict",
[](vision::faceid::InsightFaceRecognitionModel& self,
pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::FaceRecognitionResult res;
self.Predict(&mat, &res);
return res;
})
.def_readwrite("size", &vision::faceid::InsightFaceRecognitionModel::size)
.def_readwrite("alpha",
&vision::faceid::InsightFaceRecognitionModel::alpha)
.def_readwrite("beta", &vision::faceid::InsightFaceRecognitionModel::beta)
.def_readwrite("swap_rb",
&vision::faceid::InsightFaceRecognitionModel::swap_rb)
.def_readwrite(
"l2_normalize",
&vision::faceid::InsightFaceRecognitionModel::l2_normalize);
}
} // namespace fastdeploy

View File

@@ -1,73 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/partial_fc.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
PartialFC::PartialFC(const std::string& model_file,
const std::string& params_file,
const RuntimeOption& custom_option,
const ModelFormat& model_format)
: InsightFaceRecognitionModel(model_file, params_file, custom_option,
model_format) {
initialized = Initialize();
}
bool PartialFC::Initialize() {
if (initialized) {
// (1.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
if (!InsightFaceRecognitionModel::Initialize()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
// (2.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
bool PartialFC::Preprocess(Mat* mat, FDTensor* output) {
return InsightFaceRecognitionModel::Preprocess(mat, output);
}
bool PartialFC::Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Postprocess(infer_result, result);
}
bool PartialFC::Predict(cv::Mat* im, FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Predict(im, result);
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,63 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
#include "fastdeploy/vision/faceid/contrib/insightface_rec.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
/*! @brief PartialFC model object used when to load a PartialFC model exported by IngsightFace.
*/
class FASTDEPLOY_DECL PartialFC : public InsightFaceRecognitionModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./partial_fc.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
*/
PartialFC(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX);
std::string ModelName() const override {
return "deepinsight/insightface/recognition/partial_fc";
}
/** \brief Predict the face recognition result for an input image
*
* \param[in] im The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result The output face recognition result will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
bool Predict(cv::Mat* im, FaceRecognitionResult* result) override;
private:
bool Initialize() override;
bool Preprocess(Mat* mat, FDTensor* output) override;
bool Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) override;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,38 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/pybind/main.h"
namespace fastdeploy {
void BindPartialFC(pybind11::module& m) {
// Bind Partial FC
pybind11::class_<vision::faceid::PartialFC,
vision::faceid::InsightFaceRecognitionModel>(m, "PartialFC")
.def(pybind11::init<std::string, std::string, RuntimeOption,
ModelFormat>())
.def("predict",
[](vision::faceid::PartialFC& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::FaceRecognitionResult res;
self.Predict(&mat, &res);
return res;
})
.def_readwrite("size", &vision::faceid::PartialFC::size)
.def_readwrite("alpha", &vision::faceid::PartialFC::alpha)
.def_readwrite("beta", &vision::faceid::PartialFC::beta)
.def_readwrite("swap_rb", &vision::faceid::PartialFC::swap_rb)
.def_readwrite("l2_normalize", &vision::faceid::PartialFC::l2_normalize);
}
} // namespace fastdeploy

View File

@@ -1,71 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/vpl.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
VPL::VPL(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option, const ModelFormat& model_format)
: InsightFaceRecognitionModel(model_file, params_file, custom_option,
model_format) {
initialized = Initialize();
}
bool VPL::Initialize() {
if (initialized) {
// (1.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
if (!InsightFaceRecognitionModel::Initialize()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
// (2.1) re-init parameters for specific sub-classes
size = {112, 112};
alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta = {-1.f, -1.f, -1.f}; // RGB
swap_rb = true;
l2_normalize = false;
return true;
}
bool VPL::Preprocess(Mat* mat, FDTensor* output) {
return InsightFaceRecognitionModel::Preprocess(mat, output);
}
bool VPL::Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Postprocess(infer_result, result);
}
bool VPL::Predict(cv::Mat* im, FaceRecognitionResult* result) {
return InsightFaceRecognitionModel::Predict(im, result);
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,63 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
#include "fastdeploy/vision/faceid/contrib/insightface_rec.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
/*! @brief VPL model object used when to load a VPL model exported by IngsightFace.
*/
class FASTDEPLOY_DECL VPL : public InsightFaceRecognitionModel {
public:
/** \brief Set path of model file and the configuration of runtime.
*
* \param[in] model_file Path of model file, e.g ./vpl.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
*/
VPL(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::ONNX);
std::string ModelName() const override {
return "deepinsight/insightface/recognition/vpl";
}
/** \brief Predict the face recognition result for an input image
*
* \param[in] im The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result The output face recognition result will be writen to this structure
* \return true if the prediction successed, otherwise false
*/
bool Predict(cv::Mat* im, FaceRecognitionResult* result) override;
private:
bool Initialize() override;
bool Preprocess(Mat* mat, FDTensor* output) override;
bool Postprocess(std::vector<FDTensor>& infer_result,
FaceRecognitionResult* result) override;
};
} // namespace faceid
} // namespace vision
} // namespace fastdeploy

View File

@@ -1,38 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/pybind/main.h"
namespace fastdeploy {
void BindVPL(pybind11::module& m) {
// Bind VPL
pybind11::class_<vision::faceid::VPL,
vision::faceid::InsightFaceRecognitionModel>(m, "VPL")
.def(pybind11::init<std::string, std::string, RuntimeOption,
ModelFormat>())
.def("predict",
[](vision::faceid::VPL& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data);
vision::FaceRecognitionResult res;
self.Predict(&mat, &res);
return res;
})
.def_readwrite("size", &vision::faceid::VPL::size)
.def_readwrite("alpha", &vision::faceid::VPL::alpha)
.def_readwrite("beta", &vision::faceid::VPL::beta)
.def_readwrite("swap_rb", &vision::faceid::VPL::swap_rb)
.def_readwrite("l2_normalize", &vision::faceid::VPL::l2_normalize);
}
} // namespace fastdeploy

View File

@@ -15,20 +15,11 @@
#include "fastdeploy/pybind/main.h" #include "fastdeploy/pybind/main.h"
namespace fastdeploy { namespace fastdeploy {
void BindInsightFace(pybind11::module& m);
void BindAdaFace(pybind11::module& m); void BindAdaFace(pybind11::module& m);
void BindArcFace(pybind11::module& m);
void BindInsightFaceRecognitionModel(pybind11::module& m);
void BindCosFace(pybind11::module& m);
void BindPartialFC(pybind11::module& m);
void BindVPL(pybind11::module& m);
void BindFaceId(pybind11::module& m) { void BindFaceId(pybind11::module& m) {
auto faceid_module = m.def_submodule("faceid", "Face recognition models."); auto faceid_module = m.def_submodule("faceid", "Face recognition models.");
BindInsightFaceRecognitionModel(faceid_module); BindInsightFace(faceid_module);
BindAdaFace(faceid_module); BindAdaFace(faceid_module);
BindArcFace(faceid_module);
BindCosFace(faceid_module);
BindPartialFC(faceid_module);
BindVPL(faceid_module);
} }
} // namespace fastdeploy } // namespace fastdeploy

0
python/__init__.py Normal file
View File

View File

@@ -13,9 +13,4 @@
# limitations under the License. # limitations under the License.
from __future__ import absolute_import from __future__ import absolute_import
from .contrib.adaface import AdaFace from .contrib import *
from .contrib.arcface import ArcFace
from .contrib.cosface import CosFace
from .contrib.insightface_rec import InsightFaceRecognitionModel
from .contrib.partial_fc import PartialFC
from .contrib.vpl import VPL

View File

@@ -13,3 +13,5 @@
# limitations under the License. # limitations under the License.
from __future__ import absolute_import from __future__ import absolute_import
from .insightface import *
from .adaface import *

View File

@@ -1,126 +0,0 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from .... import FastDeployModel, ModelFormat
from .... import c_lib_wrap as C
class AdaFace(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.PADDLE):
"""Load a AdaFace model exported by InsigtFace.
:param model_file: (str)Path of model file, e.g ./adaface.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
"""
# 调用基函数进行backend_option的初始化
# 初始化后的option保存在self._runtime_option
super(AdaFace, self).__init__(runtime_option)
self._model = C.vision.faceid.AdaFace(
model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "AdaFace initialize failed."
def predict(self, input_image):
""" Predict the face recognition result for an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: FaceRecognitionResult
"""
return self._model.predict(input_image)
# 一些跟模型有关的属性封装
# 多数是预处理相关可通过修改如model.size = [112, 112]改变预处理时resize的大小前提是模型支持
@property
def size(self):
"""
Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default (112, 112)
"""
return self._model.size
@property
def alpha(self):
"""
Argument for image preprocessing step, alpha value for normalization, default alpha = [1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f]
"""
return self._model.alpha
@property
def beta(self):
"""
Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f}
"""
return self._model.beta
@property
def swap_rb(self):
"""
Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default True.
"""
return self._model.swap_rb
@property
def l2_normalize(self):
"""
Argument for image preprocessing step, whether to apply l2 normalize to embedding values, default False;
"""
return self._model.l2_normalize
@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._model.size = wh
@alpha.setter
def alpha(self, value):
assert isinstance(value, (list, tuple)), \
"The value to set `alpha` must be type of tuple or list."
assert len(value) == 3, \
"The value to set `alpha` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.alpha = value
@beta.setter
def beta(self, value):
assert isinstance(value, (list, tuple)), \
"The value to set `beta` must be type of tuple or list."
assert len(value) == 3, \
"The value to set `beta` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.beta = value
@swap_rb.setter
def swap_rb(self, value):
assert isinstance(
value, bool), "The value to set `swap_rb` must be type of bool."
self._model.swap_rb = value
@l2_normalize.setter
def l2_normalize(self, value):
assert isinstance(
value,
bool), "The value to set `l2_normalize` must be type of bool."
self._model.l2_normalize = value

View File

@@ -0,0 +1,109 @@
# 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
from ..... import FastDeployModel, ModelFormat
from ..... import c_lib_wrap as C
class AdaFacePreprocessor:
def __init__(self):
"""Create a preprocessor for AdaFace Model
"""
self._preprocessor = C.vision.faceid.AdaFacePreprocessor()
def run(self, input_ims):
"""Preprocess input images for AdaFace Model
:param: input_ims: (list of numpy.ndarray)The input image
:return: list of FDTensor, include image, scale_factor, im_shape
"""
return self._preprocessor.run(input_ims)
class AdaFacePostprocessor:
def __init__(self):
"""Create a postprocessor for AdaFace Model
"""
self._postprocessor = C.vision.faceid.AdaFacePostprocessor()
def run(self, runtime_results):
"""Postprocess the runtime results for PaddleClas Model
:param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
:return: list of FaceRecognitionResult(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)
@property
def l2_normalize(self):
"""
confidence threshold for postprocessing, default is 0.5
"""
return self._postprocessor.l2_normalize
class AdaFace(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a AdaFace model exported by PaddleClas.
:param model_file: (str)Path of model file, e.g adaface/model.pdmodel
:param params_file: (str)Path of parameters file, e.g adaface/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(AdaFace, self).__init__(runtime_option)
self._model = C.vision.faceid.AdaFace(
model_file, params_file, self._runtime_option, model_format)
assert self.initialized, "AdaFace model initialize failed."
def predict(self, im):
"""Detect an input image
:param im: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: DetectionResult
"""
assert im is not None, "The input image data is None."
return self._model.predict(im)
def batch_predict(self, images):
"""Detect a batch of input image list
: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 AdaFacePreprocessor object of the loaded model
:return AdaFacePreprocessor
"""
return self._model.preprocessor
@property
def postprocessor(self):
"""Get AdaFacePostprocessor object of the loaded model
:return AdaFacePostprocessor
"""
return self._model.postprocessor

View File

@@ -1,127 +0,0 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import logging
from .... import FastDeployModel, ModelFormat
from .... import c_lib_wrap as C
from ..contrib.insightface_rec import InsightFaceRecognitionModel
class ArcFace(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a ArcFace model exported by InsigtFace.
:param model_file: (str)Path of model file, e.g ./arcface.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
"""
# 调用基函数进行backend_option的初始化
# 初始化后的option保存在self._runtime_option
super(ArcFace, self).__init__(runtime_option)
self._model = C.vision.faceid.ArcFace(
model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "ArcFace initialize failed."
def predict(self, input_image):
""" Predict the face recognition result for an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: FaceRecognitionResult
"""
return self._model.predict(input_image)
# 一些跟模型有关的属性封装
# 多数是预处理相关可通过修改如model.size = [112, 112]改变预处理时resize的大小前提是模型支持
@property
def size(self):
"""
Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default (112, 112)
"""
return self._model.size
@property
def alpha(self):
"""
Argument for image preprocessing step, alpha value for normalization, default alpha = [1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f]
"""
return self._model.alpha
@property
def beta(self):
"""
Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f}
"""
return self._model.beta
@property
def swap_rb(self):
"""
Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default True.
"""
return self._model.swap_rb
@property
def l2_normalize(self):
"""
Argument for image preprocessing step, whether to apply l2 normalize to embedding values, default False;
"""
return self._model.l2_normalize
@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._model.size = wh
@alpha.setter
def alpha(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `alpha` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `alpha` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.alpha = value
@beta.setter
def beta(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `beta` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `beta` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.beta = value
@swap_rb.setter
def swap_rb(self, value):
assert isinstance(
value, bool), "The value to set `swap_rb` must be type of bool."
self._model.swap_rb = value
@l2_normalize.setter
def l2_normalize(self, value):
assert isinstance(
value,
bool), "The value to set `l2_normalize` must be type of bool."
self._model.l2_normalize = value

View File

@@ -1,126 +0,0 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import logging
from .... import FastDeployModel, ModelFormat
from .... import c_lib_wrap as C
class CosFace(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a CosFace model exported by InsigtFace.
:param model_file: (str)Path of model file, e.g ./cosface.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
"""
# 调用基函数进行backend_option的初始化
# 初始化后的option保存在self._runtime_option
super(CosFace, self).__init__(runtime_option)
self._model = C.vision.faceid.CosFace(
model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "CosFace initialize failed."
def predict(self, input_image):
""" Predict the face recognition result for an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: FaceRecognitionResult
"""
return self._model.predict(input_image)
# 一些跟模型有关的属性封装
# 多数是预处理相关可通过修改如model.size = [112, 112]改变预处理时resize的大小前提是模型支持
@property
def size(self):
"""
Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default (112, 112)
"""
return self._model.size
@property
def alpha(self):
"""
Argument for image preprocessing step, alpha value for normalization, default alpha = [1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f]
"""
return self._model.alpha
@property
def beta(self):
"""
Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f}
"""
return self._model.beta
@property
def swap_rb(self):
"""
Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default True.
"""
return self._model.swap_rb
@property
def l2_normalize(self):
"""
Argument for image preprocessing step, whether to apply l2 normalize to embedding values, default False;
"""
return self._model.l2_normalize
@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._model.size = wh
@alpha.setter
def alpha(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `alpha` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `alpha` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.alpha = value
@beta.setter
def beta(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `beta` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `beta` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.beta = value
@swap_rb.setter
def swap_rb(self, value):
assert isinstance(
value, bool), "The value to set `swap_rb` must be type of bool."
self._model.swap_rb = value
@l2_normalize.setter
def l2_normalize(self, value):
assert isinstance(
value,
bool), "The value to set `l2_normalize` must be type of bool."
self._model.l2_normalize = value

View File

@@ -0,0 +1,222 @@
# 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
from ..... import FastDeployModel, ModelFormat
from ..... import c_lib_wrap as C
class InsightFaceRecognitionPreprocessor:
def __init__(self):
"""Create a preprocessor for InsightFaceRecognition Model
"""
self._preprocessor = C.vision.faceid.InsightFaceRecognitionPreprocessor(
)
def run(self, input_ims):
"""Preprocess input images for InsightFaceRecognition Model
:param: input_ims: (list of numpy.ndarray)The input image
:return: list of FDTensor, include image, scale_factor, im_shape
"""
return self._preprocessor.run(input_ims)
@property
def size(self):
"""
Argument for image preprocessing step, tuple of (width, height),
decide the target size after resize, default (112, 112)
"""
return self._preprocessor.size
@property
def alpha(self):
"""
Argument for image preprocessing step, alpha values for normalization,
default alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
"""
return self._preprocessor.alpha
@property
def beta(self):
"""
Argument for image preprocessing step, beta values for normalization,
default beta = {-1.f, -1.f, -1.f}
"""
return self._preprocessor.beta
@property
def permute(self):
"""
Argument for image preprocessing step, whether to swap the B and R channel,
such as BGR->RGB, default true.
"""
return self._preprocessor.permute
class InsightFaceRecognitionPostprocessor:
def __init__(self):
"""Create a postprocessor for InsightFaceRecognition Model
"""
self._postprocessor = C.vision.faceid.InsightFaceRecognitionPostprocessor(
)
def run(self, runtime_results):
"""Postprocess the runtime results for PaddleClas Model
:param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
:return: list of FaceRecognitionResult(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)
@property
def l2_normalize(self):
"""
confidence threshold for postprocessing, default is 0.5
"""
return self._postprocessor.l2_normalize
class InsightFaceRecognitionBase(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a InsightFaceRecognitionBase model exported by PaddleClas.
:param model_file: (str)Path of model file, e.g InsightFaceRecognitionBase/model.pdmodel
:param params_file: (str)Path of parameters file, e.g InsightFaceRecognitionBase/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(InsightFaceRecognitionBase, self).__init__(runtime_option)
self._model = C.vision.faceid.InsightFaceRecognitionBase(
model_file, params_file, self._runtime_option, model_format)
assert self.initialized, "InsightFaceRecognitionBase model initialize failed."
def predict(self, im):
"""Detect an input image
:param im: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: DetectionResult
"""
assert im is not None, "The input image data is None."
return self._model.predict(im)
def batch_predict(self, images):
"""Detect a batch of input image list
: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 InsightFaceRecognitionPreprocessor object of the loaded model
:return InsightFaceRecognitionPreprocessor
"""
return self._model.preprocessor
@property
def postprocessor(self):
"""Get InsightFaceRecognitionPostprocessor object of the loaded model
:return InsightFaceRecognitionPostprocessor
"""
return self._model.postprocessor
class ArcFace(InsightFaceRecognitionBase):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a ArcFace model exported by PaddleClas.
:param model_file: (str)Path of model file, e.g ArcFace/model.pdmodel
:param params_file: (str)Path of parameters file, e.g ArcFace/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(InsightFaceRecognitionBase, self).__init__(runtime_option)
self._model = C.vision.faceid.ArcFace(
model_file, params_file, self._runtime_option, model_format)
assert self.initialized, "ArcFace model initialize failed."
class CosFace(InsightFaceRecognitionBase):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a CosFace model exported by PaddleClas.
:param model_file: (str)Path of model file, e.g CosFace/model.pdmodel
:param params_file: (str)Path of parameters file, e.g CosFace/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(InsightFaceRecognitionBase, self).__init__(runtime_option)
self._model = C.vision.faceid.CosFace(
model_file, params_file, self._runtime_option, model_format)
assert self.initialized, "CosFace model initialize failed."
class PartialFC(InsightFaceRecognitionBase):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a PartialFC model exported by PaddleClas.
:param model_file: (str)Path of model file, e.g PartialFC/model.pdmodel
:param params_file: (str)Path of parameters file, e.g PartialFC/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(InsightFaceRecognitionBase, self).__init__(runtime_option)
self._model = C.vision.faceid.PartialFC(
model_file, params_file, self._runtime_option, model_format)
assert self.initialized, "PartialFC model initialize failed."
class VPL(InsightFaceRecognitionBase):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a VPL model exported by PaddleClas.
:param model_file: (str)Path of model file, e.g VPL/model.pdmodel
:param params_file: (str)Path of parameters file, e.g VPL/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(InsightFaceRecognitionBase, self).__init__(runtime_option)
self._model = C.vision.faceid.VPL(model_file, params_file,
self._runtime_option, model_format)
assert self.initialized, "VPL model initialize failed."

View File

@@ -1,126 +0,0 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import logging
from .... import FastDeployModel, ModelFormat
from .... import c_lib_wrap as C
class InsightFaceRecognitionModel(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a InsightFace model exported by InsigtFace.
:param model_file: (str)Path of model file, e.g ./arcface.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
"""
# 调用基函数进行backend_option的初始化
# 初始化后的option保存在self._runtime_option
super(InsightFaceRecognitionModel, self).__init__(runtime_option)
self._model = C.vision.faceid.InsightFaceRecognitionModel(
model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "InsightFaceRecognitionModel initialize failed."
def predict(self, input_image):
""" Predict the face recognition result for an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: FaceRecognitionResult
"""
return self._model.predict(input_image)
# 一些跟InsightFaceRecognitionModel模型有关的属性封装
# 多数是预处理相关可通过修改如model.size = [112, 112]改变预处理时resize的大小前提是模型支持
@property
def size(self):
"""
Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default (112, 112)
"""
return self._model.size
@property
def alpha(self):
"""
Argument for image preprocessing step, alpha value for normalization, default alpha = [1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f]
"""
return self._model.alpha
@property
def beta(self):
"""
Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f}
"""
return self._model.beta
@property
def swap_rb(self):
"""
Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default True.
"""
return self._model.swap_rb
@property
def l2_normalize(self):
"""
Argument for image preprocessing step, whether to apply l2 normalize to embedding values, default False;
"""
return self._model.l2_normalize
@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._model.size = wh
@alpha.setter
def alpha(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `alpha` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `alpha` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.alpha = value
@beta.setter
def beta(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `beta` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `beta` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.beta = value
@swap_rb.setter
def swap_rb(self, value):
assert isinstance(
value, bool), "The value to set `swap_rb` must be type of bool."
self._model.swap_rb = value
@l2_normalize.setter
def l2_normalize(self, value):
assert isinstance(
value,
bool), "The value to set `l2_normalize` must be type of bool."
self._model.l2_normalize = value

View File

@@ -1,126 +0,0 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import logging
from .... import FastDeployModel, ModelFormat
from .... import c_lib_wrap as C
class PartialFC(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a PartialFC model exported by InsigtFace.
:param model_file: (str)Path of model file, e.g ./partial_fc.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
"""
# 调用基函数进行backend_option的初始化
# 初始化后的option保存在self._runtime_option
super(PartialFC, self).__init__(runtime_option)
self._model = C.vision.faceid.PartialFC(
model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "PartialFC initialize failed."
def predict(self, input_image):
""" Predict the face recognition result for an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: FaceRecognitionResult
"""
return self._model.predict(input_image)
# 一些跟模型有关的属性封装
# 多数是预处理相关可通过修改如model.size = [112, 112]改变预处理时resize的大小前提是模型支持
@property
def size(self):
"""
Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default (112, 112)
"""
return self._model.size
@property
def alpha(self):
"""
Argument for image preprocessing step, alpha value for normalization, default alpha = [1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f]
"""
return self._model.alpha
@property
def beta(self):
"""
Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f}
"""
return self._model.beta
@property
def swap_rb(self):
"""
Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default True.
"""
return self._model.swap_rb
@property
def l2_normalize(self):
"""
Argument for image preprocessing step, whether to apply l2 normalize to embedding values, default False;
"""
return self._model.l2_normalize
@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._model.size = wh
@alpha.setter
def alpha(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `alpha` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `alpha` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.alpha = value
@beta.setter
def beta(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `beta` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `beta` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.beta = value
@swap_rb.setter
def swap_rb(self, value):
assert isinstance(
value, bool), "The value to set `swap_rb` must be type of bool."
self._model.swap_rb = value
@l2_normalize.setter
def l2_normalize(self, value):
assert isinstance(
value,
bool), "The value to set `l2_normalize` must be type of bool."
self._model.l2_normalize = value

View File

@@ -1,126 +0,0 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import logging
from .... import FastDeployModel, ModelFormat
from .... import c_lib_wrap as C
class VPL(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=ModelFormat.ONNX):
"""Load a VPL model exported by InsigtFace.
:param model_file: (str)Path of model file, e.g ./vpl.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
"""
# 调用基函数进行backend_option的初始化
# 初始化后的option保存在self._runtime_option
super(VPL, self).__init__(runtime_option)
self._model = C.vision.faceid.VPL(model_file, params_file,
self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "VPL initialize failed."
def predict(self, input_image):
""" Predict the face recognition result for an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: FaceRecognitionResult
"""
return self._model.predict(input_image)
# 一些跟模型有关的属性封装
# 多数是预处理相关可通过修改如model.size = [112, 112]改变预处理时resize的大小前提是模型支持
@property
def size(self):
"""
Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default (112, 112)
"""
return self._model.size
@property
def alpha(self):
"""
Argument for image preprocessing step, alpha value for normalization, default alpha = [1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f]
"""
return self._model.alpha
@property
def beta(self):
"""
Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f}
"""
return self._model.beta
@property
def swap_rb(self):
"""
Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default True.
"""
return self._model.swap_rb
@property
def l2_normalize(self):
"""
Argument for image preprocessing step, whether to apply l2 normalize to embedding values, default False;
"""
return self._model.l2_normalize
@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._model.size = wh
@alpha.setter
def alpha(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `alpha` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `alpha` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.alpha = value
@beta.setter
def beta(self, value):
assert isinstance(value, (list, tuple)),\
"The value to set `beta` must be type of tuple or list."
assert len(value) == 3,\
"The value to set `beta` must contatins 3 elements for each channels, but now it contains {} elements.".format(
len(value))
self._model.beta = value
@swap_rb.setter
def swap_rb(self, value):
assert isinstance(
value, bool), "The value to set `swap_rb` must be type of bool."
self._model.swap_rb = value
@l2_normalize.setter
def l2_normalize(self, value):
assert isinstance(
value,
bool), "The value to set `l2_normalize` must be type of bool."
self._model.l2_normalize = value