diff --git a/cmake/rknpu2.cmake b/cmake/rknpu2.cmake index e8ed424be..0ef8d9d29 100644 --- a/cmake/rknpu2.cmake +++ b/cmake/rknpu2.cmake @@ -10,10 +10,10 @@ download_and_decompress(${RKNPU2_URL} ${CMAKE_CURRENT_BINARY_DIR}/${RKNPU2_FILE} # set path set(RKNPU_RUNTIME_PATH ${THIRD_PARTY_PATH}/install/rknpu2_runtime) -if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") -else () - message(FATAL_ERROR "[rknpu2.cmake] Only support build rknpu2 in Linux") -endif () +#if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") +#else () +# message(FATAL_ERROR "[rknpu2.cmake] Only support build rknpu2 in Linux") +#endif () if (EXISTS ${RKNPU_RUNTIME_PATH}) diff --git a/examples/vision/faceid/README.md b/examples/vision/faceid/README.md index aa5b6b6fd..b897314cb 100644 --- a/examples/vision/faceid/README.md +++ b/examples/vision/faceid/README.md @@ -1,6 +1,8 @@ # 人脸识别模型 +## 模型支持列表 + FastDeploy目前支持如下人脸识别模型部署 | 模型 | 说明 | 模型格式 | 版本 | @@ -10,3 +12,7 @@ FastDeploy目前支持如下人脸识别模型部署 | [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) | | [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作为推理框架。 diff --git a/examples/vision/faceid/adaface/cpp/CMakeLists.txt b/examples/vision/faceid/adaface/cpp/CMakeLists.txt index 434573219..9df990f64 100644 --- a/examples/vision/faceid/adaface/cpp/CMakeLists.txt +++ b/examples/vision/faceid/adaface/cpp/CMakeLists.txt @@ -1,4 +1,4 @@ -PROJECT(infer_demo C CXX) +PROJECT(infer_adaface_demo C CXX) CMAKE_MINIMUM_REQUIRED (VERSION 3.12) # 指定下载解压后的fastdeploy库路径 @@ -9,5 +9,5 @@ include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) # 添加FastDeploy依赖头文件 include_directories(${FASTDEPLOY_INCS}) -add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc) -target_link_libraries(infer_demo ${FASTDEPLOY_LIBS}) +add_executable(infer_adaface_demo ${PROJECT_SOURCE_DIR}/infer.cc) +target_link_libraries(infer_adaface_demo ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/faceid/adaface/cpp/README.md b/examples/vision/faceid/adaface/cpp/README.md index b4a6a5991..ceebb96ef 100755 --- a/examples/vision/faceid/adaface/cpp/README.md +++ b/examples/vision/faceid/adaface/cpp/README.md @@ -8,56 +8,43 @@ - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) -以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本0.7.0以上(x.x.x>=0.7.0) +以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试 ```bash +# “如果预编译库不包含本模型,请从最新代码编译SDK” mkdir build cd build -# 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用 wget https://bj.bcebos.com/fastdeploy/release/cpp/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 make -j #下载测试图片 -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_0.JPG -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_1.JPG -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip +unzip face_demo.zip # 如果为Paddle模型,运行以下代码 wget https://bj.bcebos.com/paddlehub/fastdeploy/mobilefacenet_adaface.tgz tar zxvf mobilefacenet_adaface.tgz -C ./ # CPU推理 -./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ +./infer_adaface_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ - test_lite_focal_arcface_0.JPG \ - test_lite_focal_arcface_1.JPG \ - test_lite_focal_arcface_2.JPG \ - 0 + face_0.jpg face_1.jpg face_2.jpg 0 # GPU推理 -./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ +./infer_adaface_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ - test_lite_focal_arcface_0.JPG \ - test_lite_focal_arcface_1.JPG \ - test_lite_focal_arcface_2.JPG \ - 1 + face_0.jpg face_1.jpg face_2.jpg 1 # GPU上TensorRT推理 -./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ +./infer_adaface_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ - test_lite_focal_arcface_0.JPG \ - test_lite_focal_arcface_1.JPG \ - test_lite_focal_arcface_2.JPG \ - 2 + face_0.jpg face_1.jpg face_2.jpg 2 # XPU推理 ./infer_demo mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ - test_lite_focal_arcface_0.JPG \ - test_lite_focal_arcface_1.JPG \ - test_lite_focal_arcface_2.JPG \ - 3 + face_0.jpg face_1.jpg face_2.jpg 3 ``` 运行完成可视化结果如下图所示 @@ -101,16 +88,22 @@ AdaFace模型加载和初始化,如果使用PaddleInference推理,model_file > > * **im**: 输入图像,注意需为HWC,BGR格式 > > * **result**: 检测结果,包括检测框,各个框的置信度, FaceRecognitionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) -### 类成员变量 -#### 预处理参数 -用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 +### 修改预处理以及后处理的参数 +预处理和后处理的参数的需要通过修改AdaFacePostprocessor,AdaFacePreprocessor的成员变量来进行修改。 +#### AdaFacePreprocessor成员变量(预处理参数) +> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[112, 112], + 通过AdaFacePreprocessor::SetSize(std::vector& size)来进行修改 +> > * **alpha**(vector<float>): 预处理归一化的alpha值,计算公式为`x'=x*alpha+beta`,alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5], + 通过AdaFacePreprocessor::SetAlpha(std::vector& alpha)来进行修改 +> > * **beta**(vector<float>): 预处理归一化的beta值,计算公式为`x'=x*alpha+beta`,beta默认为[-1.f, -1.f, -1.f], + 通过AdaFacePreprocessor::SetBeta(std::vector& beta)来进行修改 +> > * **permute**(bool): 预处理是否将BGR转换成RGB,默认true, + 通过AdaFacePreprocessor::SetPermute(bool permute)来进行修改 -> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[112, 112] -> > * **alpha**(vector<float>): 预处理归一化的alpha值,计算公式为`x'=x*alpha+beta`,alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5] -> > * **beta**(vector<float>): 预处理归一化的beta值,计算公式为`x'=x*alpha+beta`,beta默认为[-1.f, -1.f, -1.f] -> > * **swap_rb**(bool): 预处理是否将BGR转换成RGB,默认true -> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化,默认false +#### AdaFacePostprocessor成员变量(后处理参数) +> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化,默认false, + AdaFacePostprocessor::SetL2Normalize(bool& l2_normalize)来进行修改 - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/faceid/adaface/cpp/infer.cc b/examples/vision/faceid/adaface/cpp/infer.cc index 8d0ca938c..3915aeb21 100755 --- a/examples/vision/faceid/adaface/cpp/infer.cc +++ b/examples/vision/faceid/adaface/cpp/infer.cc @@ -1,14 +1,17 @@ -/*************************************************************************** - * - * Copyright (c) 2021 Baidu.com, Inc. All Rights Reserved - * - **************************************************************************/ +// 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. -/** - * @author Baidu - * @brief demo_image_inference - * - **/ #include "fastdeploy/vision.h" void CpuInfer(const std::string &model_file, const std::string ¶ms_file, @@ -28,8 +31,8 @@ void CpuInfer(const std::string &model_file, const std::string ¶ms_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -40,9 +43,11 @@ void CpuInfer(const std::string &model_file, const std::string ¶ms_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -65,8 +70,8 @@ void XpuInfer(const std::string &model_file, const std::string ¶ms_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -77,9 +82,11 @@ void XpuInfer(const std::string &model_file, const std::string ¶ms_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -103,8 +110,8 @@ void GpuInfer(const std::string &model_file, const std::string ¶ms_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -115,9 +122,11 @@ void GpuInfer(const std::string &model_file, const std::string ¶ms_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -143,8 +152,8 @@ void TrtInfer(const std::string &model_file, const std::string ¶ms_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -155,9 +164,11 @@ void TrtInfer(const std::string &model_file, const std::string ¶ms_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } diff --git a/examples/vision/faceid/adaface/python/README.md b/examples/vision/faceid/adaface/python/README.md index 699da9001..bd8d66424 100755 --- a/examples/vision/faceid/adaface/python/README.md +++ b/examples/vision/faceid/adaface/python/README.md @@ -15,9 +15,8 @@ cd examples/vision/faceid/adaface/python/ #下载AdaFace模型文件和测试图片 #下载测试图片 -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_0.JPG -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_1.JPG -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip +unzip face_demo.zip # 如果为Paddle模型,运行以下代码 wget https://bj.bcebos.com/paddlehub/fastdeploy/mobilefacenet_adaface.tgz @@ -26,25 +25,25 @@ tar zxvf mobilefacenet_adaface.tgz -C ./ # CPU推理 python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ --params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ - --face test_lite_focal_arcface_0.JPG \ - --face_positive test_lite_focal_arcface_1.JPG \ - --face_negative test_lite_focal_arcface_2.JPG \ + --face face_0.jpg \ + --face_positive face_1.jpg \ + --face_negative face_2.jpg \ --device cpu # GPU推理 python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ --params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ - --face test_lite_focal_arcface_0.JPG \ - --face_positive test_lite_focal_arcface_1.JPG \ - --face_negative test_lite_focal_arcface_2.JPG \ + --face face_0.jpg \ + --face_positive face_1.jpg \ + --face_negative face_2.jpg \ --device gpu # GPU上使用TensorRT推理 python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ --params_file mobilefacenet_adaface/mobilefacenet_adaface.pdiparams \ - --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 + --face face_0.jpg \ + --face_positive face_1.jpg \ + --face_negative face_2.jpg \ + --device gpu \ + --use_trt True # XPU推理 python infer.py --model mobilefacenet_adaface/mobilefacenet_adaface.pdmodel \ @@ -106,11 +105,15 @@ AdaFace模型加载和初始化,其中model_file为导出的ONNX模型格式 #### 预处理参数 用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 - +#### AdaFacePreprocessor的成员变量 +以下变量为AdaFacePreprocessor的成员变量 > > * **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] > > * **beta**(list[float]): 预处理归一化的beta值,计算公式为`x'=x*alpha+beta`,beta默认为[-1.f, -1.f, -1.f] > > * **swap_rb**(bool): 预处理是否将BGR转换成RGB,默认True + +#### AdaFacePostprocessor的成员变量 +以下变量为AdaFacePostprocessor的成员变量 > > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化,默认False diff --git a/examples/vision/faceid/insightface/cpp/README.md b/examples/vision/faceid/insightface/cpp/README.md index 341478b7c..1722d9d5d 100644 --- a/examples/vision/faceid/insightface/cpp/README.md +++ b/examples/vision/faceid/insightface/cpp/README.md @@ -7,12 +7,11 @@ - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) -以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本0.7.0以上(x.x.x>=0.7.0) +以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试 ```bash mkdir build cd build -# 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用 wget https://bj.bcebos.com/fastdeploy/release/cpp/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 @@ -20,17 +19,15 @@ make -j #下载官方转换好的ArcFace模型文件和测试图片 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/test_samples/test_lite_focal_arcface_1.JPG -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG - +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip +unzip face_demo.zip # 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推理 -./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推理 -./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**: 输入图像,注意需为HWC,BGR格式 > > * **result**: 检测结果,包括检测框,各个框的置信度, FaceRecognitionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) -### 类成员变量 -#### 预处理参数 -用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 +### 修改预处理以及后处理的参数 +预处理和后处理的参数的需要通过修改InsightFaceRecognitionPostprocessor,InsightFaceRecognitionPreprocessor的成员变量来进行修改。 +#### InsightFaceRecognitionPreprocessor成员变量(预处理参数) +> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[112, 112], + 通过InsightFaceRecognitionPreprocessor::SetSize(std::vector& size)来进行修改 +> > * **alpha**(vector<float>): 预处理归一化的alpha值,计算公式为`x'=x*alpha+beta`,alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5], + 通过InsightFaceRecognitionPreprocessor::SetAlpha(std::vector& alpha)来进行修改 +> > * **beta**(vector<float>): 预处理归一化的beta值,计算公式为`x'=x*alpha+beta`,beta默认为[-1.f, -1.f, -1.f], + 通过InsightFaceRecognitionPreprocessor::SetBeta(std::vector& beta)来进行修改 +> > * **permute**(bool): 预处理是否将BGR转换成RGB,默认true, + 通过InsightFaceRecognitionPreprocessor::SetPermute(bool permute)来进行修改 -> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[112, 112] -> > * **alpha**(vector<float>): 预处理归一化的alpha值,计算公式为`x'=x*alpha+beta`,alpha默认为[1. / 127.5, 1.f / 127.5, 1. / 127.5] -> > * **beta**(vector<float>): 预处理归一化的beta值,计算公式为`x'=x*alpha+beta`,beta默认为[-1.f, -1.f, -1.f] -> > * **swap_rb**(bool): 预处理是否将BGR转换成RGB,默认true -> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化,默认false +#### InsightFaceRecognitionPostprocessor成员变量(后处理参数) +> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化,默认false, + InsightFaceRecognitionPostprocessor::SetL2Normalize(bool& l2_normalize)来进行修改 - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/faceid/insightface/cpp/infer_arcface.cc b/examples/vision/faceid/insightface/cpp/infer_arcface.cc index ce4c0fcd4..dbd390dc1 100644 --- a/examples/vision/faceid/insightface/cpp/infer_arcface.cc +++ b/examples/vision/faceid/insightface/cpp/infer_arcface.cc @@ -16,11 +16,7 @@ void CpuInfer(const std::string& model_file, const std::vector& image_file) { - auto model = fastdeploy::vision::faceid::ArcFace(model_file); - if (!model.Initialized()) { - std::cerr << "Failed to initialize." << std::endl; - return; - } + auto model = fastdeploy::vision::faceid::ArcFace(model_file, ""); cv::Mat face0 = cv::imread(image_file[0]); 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 res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -129,8 +131,7 @@ int main(int argc, char* argv[]) { if (argc < 6) { std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " "e.g ./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" + "face_0.jpg face_1.jpg face_2.jpg 0" << std::endl; std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " "with gpu; 2: run with gpu and use tensorrt backend." diff --git a/examples/vision/faceid/insightface/cpp/infer_cosface.cc b/examples/vision/faceid/insightface/cpp/infer_cosface.cc index e3b554b04..e8925faa3 100644 --- a/examples/vision/faceid/insightface/cpp/infer_cosface.cc +++ b/examples/vision/faceid/insightface/cpp/infer_cosface.cc @@ -16,11 +16,7 @@ void CpuInfer(const std::string& model_file, const std::vector& image_file) { - auto model = fastdeploy::vision::faceid::CosFace(model_file); - if (!model.Initialized()) { - std::cerr << "Failed to initialize." << std::endl; - return; - } + auto model = fastdeploy::vision::faceid::CosFace(model_file, ""); cv::Mat face0 = cv::imread(image_file[0]); 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 res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -128,9 +130,8 @@ void TrtInfer(const std::string& model_file, int main(int argc, char* argv[]) { if (argc < 6) { std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " - "e.g ./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" + "e.g ./infer_cosface_demo ms1mv3_cosface_r100.onnx " + "face_0.jpg face_1.jpg face_2.jpg 0" << std::endl; std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " "with gpu; 2: run with gpu and use tensorrt backend." diff --git a/examples/vision/faceid/insightface/cpp/infer_partial_fc.cc b/examples/vision/faceid/insightface/cpp/infer_partial_fc.cc index 607e59b9f..879e1013e 100644 --- a/examples/vision/faceid/insightface/cpp/infer_partial_fc.cc +++ b/examples/vision/faceid/insightface/cpp/infer_partial_fc.cc @@ -16,11 +16,7 @@ void CpuInfer(const std::string& model_file, const std::vector& image_file) { - auto model = fastdeploy::vision::faceid::PartialFC(model_file); - if (!model.Initialized()) { - std::cerr << "Failed to initialize." << std::endl; - return; - } + auto model = fastdeploy::vision::faceid::PartialFC(model_file, ""); cv::Mat face0 = cv::imread(image_file[0]); 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 res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -128,9 +130,8 @@ void TrtInfer(const std::string& model_file, int main(int argc, char* argv[]) { if (argc < 6) { std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " - "e.g ./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" + "e.g ./infer_arcface_demo ms1mv3_partial_fc_r100.onnx " + "face_0.jpg face_1.jpg face_2.jpg 0" << std::endl; std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " "with gpu; 2: run with gpu and use tensorrt backend." diff --git a/examples/vision/faceid/insightface/cpp/infer_vpl.cc b/examples/vision/faceid/insightface/cpp/infer_vpl.cc index 1629994a0..10816e8db 100644 --- a/examples/vision/faceid/insightface/cpp/infer_vpl.cc +++ b/examples/vision/faceid/insightface/cpp/infer_vpl.cc @@ -16,11 +16,7 @@ void CpuInfer(const std::string& model_file, const std::vector& image_file) { - auto model = fastdeploy::vision::faceid::VPL(model_file); - if (!model.Initialized()) { - std::cerr << "Failed to initialize." << std::endl; - return; - } + auto model = fastdeploy::vision::faceid::VPL(model_file, ""); cv::Mat face0 = cv::imread(image_file[0]); 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 res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -42,9 +38,11 @@ void CpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -67,8 +65,8 @@ void GpuInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -79,9 +77,11 @@ void GpuInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -106,8 +106,8 @@ void TrtInfer(const std::string& model_file, fastdeploy::vision::FaceRecognitionResult res1; fastdeploy::vision::FaceRecognitionResult res2; - if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || - (!model.Predict(&face2, &res2))) { + if ((!model.Predict(face0, &res0)) || (!model.Predict(face1, &res1)) || + (!model.Predict(face2, &res2))) { std::cerr << "Prediction Failed." << std::endl; } @@ -118,9 +118,11 @@ void TrtInfer(const std::string& model_file, std::cout << "--- [Face 2]:" << res2.Str(); 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( - res0.embedding, res2.embedding, model.l2_normalize); + res0.embedding, res2.embedding, + model.GetPostprocessor().GetL2Normalize()); std::cout << "Detect Done! Cosine 01: " << cosine01 << ", Cosine 02:" << cosine02 << std::endl; } @@ -128,9 +130,8 @@ void TrtInfer(const std::string& model_file, int main(int argc, char* argv[]) { if (argc < 6) { std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " - "e.g ./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" + "e.g ./infer_arcface_demo ms1mv3_vpl_r100.onnx " + "face_0.jpg face_1.jpg face_2.jpg 0" << std::endl; std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " "with gpu; 2: run with gpu and use tensorrt backend." diff --git a/examples/vision/faceid/insightface/python/README.md b/examples/vision/faceid/insightface/python/README.md index 0df0f8fbe..b3b44dc92 100644 --- a/examples/vision/faceid/insightface/python/README.md +++ b/examples/vision/faceid/insightface/python/README.md @@ -15,16 +15,28 @@ cd examples/vision/faceid/insightface/python/ #下载ArcFace模型文件和测试图片 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/test_samples/test_lite_focal_arcface_1.JPG -wget https://bj.bcebos.com/paddlehub/test_samples/test_lite_focal_arcface_2.JPG +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/face_demo.zip +unzip face_demo.zip # 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推理 -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推理 -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] > > * **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] > > * **swap_rb**(bool): 预处理是否将BGR转换成RGB,默认True + +#### AdaFacePostprocessor的成员变量 +以下变量为AdaFacePostprocessor的成员变量 > > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化,默认False diff --git a/examples/vision/faceid/insightface/python/infer_arcface.py b/examples/vision/faceid/insightface/python/infer_arcface.py index a400bd32f..06e8ef001 100644 --- a/examples/vision/faceid/insightface/python/infer_arcface.py +++ b/examples/vision/faceid/insightface/python/infer_arcface.py @@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive) face2 = cv2.imread(args.face_negative) # 0,2 不同的人 # 设置 l2 normalize -model.l2_normalize = True +model.postprocessor.l2_normalize = True # 预测图片检测结果 result0 = model.predict(face0) diff --git a/examples/vision/faceid/insightface/python/infer_cosface.py b/examples/vision/faceid/insightface/python/infer_cosface.py index 267bff37d..2bb1292f4 100644 --- a/examples/vision/faceid/insightface/python/infer_cosface.py +++ b/examples/vision/faceid/insightface/python/infer_cosface.py @@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive) face2 = cv2.imread(args.face_negative) # 0,2 不同的人 # 设置 l2 normalize -model.l2_normalize = True +model.postprocessor.l2_normalize = True # 预测图片检测结果 result0 = model.predict(face0) diff --git a/examples/vision/faceid/insightface/python/infer_partial_fc.py b/examples/vision/faceid/insightface/python/infer_partial_fc.py index 2e503116f..e81531e6e 100644 --- a/examples/vision/faceid/insightface/python/infer_partial_fc.py +++ b/examples/vision/faceid/insightface/python/infer_partial_fc.py @@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive) face2 = cv2.imread(args.face_negative) # 0,2 不同的人 # 设置 l2 normalize -model.l2_normalize = True +model.postprocessor.l2_normalize = True # 预测图片检测结果 result0 = model.predict(face0) diff --git a/examples/vision/faceid/insightface/python/infer_vpl.py b/examples/vision/faceid/insightface/python/infer_vpl.py index dad84d647..6113ad3df 100644 --- a/examples/vision/faceid/insightface/python/infer_vpl.py +++ b/examples/vision/faceid/insightface/python/infer_vpl.py @@ -66,7 +66,7 @@ face1 = cv2.imread(args.face_positive) face2 = cv2.imread(args.face_negative) # 0,2 不同的人 # 设置 l2 normalize -model.l2_normalize = True +model.postprocessor.l2_normalize = True # 预测图片检测结果 result0 = model.predict(face0) diff --git a/fastdeploy/vision.h b/fastdeploy/vision.h index 9c22d45ad..f5e4d0624 100644 --- a/fastdeploy/vision.h +++ b/fastdeploy/vision.h @@ -38,12 +38,8 @@ #include "fastdeploy/vision/facedet/contrib/ultraface.h" #include "fastdeploy/vision/facedet/contrib/yolov5face.h" #include "fastdeploy/vision/facedet/contrib/yolov7face/yolov7face.h" -#include "fastdeploy/vision/faceid/contrib/adaface.h" -#include "fastdeploy/vision/faceid/contrib/arcface.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/faceid/contrib/insightface/model.h" +#include "fastdeploy/vision/faceid/contrib/adaface/adaface.h" #include "fastdeploy/vision/headpose/contrib/fsanet.h" #include "fastdeploy/vision/keypointdet/pptinypose/pptinypose.h" #include "fastdeploy/vision/matting/contrib/modnet.h" diff --git a/fastdeploy/vision/faceid/contrib/adaface.cc b/fastdeploy/vision/faceid/contrib/adaface.cc deleted file mode 100644 index 916a2ef76..000000000 --- a/fastdeploy/vision/faceid/contrib/adaface.cc +++ /dev/null @@ -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& 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 \ No newline at end of file diff --git a/fastdeploy/vision/faceid/contrib/adaface.h b/fastdeploy/vision/faceid/contrib/adaface.h deleted file mode 100644 index a11d61298..000000000 --- a/fastdeploy/vision/faceid/contrib/adaface.h +++ /dev/null @@ -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& infer_result, - FaceRecognitionResult* result) override; -}; - -} // namespace faceid -} // namespace vision -} // namespace fastdeploy diff --git a/fastdeploy/vision/faceid/contrib/adaface/adaface.cc b/fastdeploy/vision/faceid/contrib/adaface/adaface.cc new file mode 100755 index 000000000..99403d464 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/adaface/adaface.cc @@ -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 results; + if (!BatchPredict({im}, &results)) { + return false; + } + if(!results.empty()){ + *result = std::move(results[0]); + } + return true; +} + +bool AdaFace::BatchPredict(const std::vector& images, + std::vector* results){ + std::vector 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 diff --git a/fastdeploy/vision/faceid/contrib/adaface/adaface.h b/fastdeploy/vision/faceid/contrib/adaface/adaface.h new file mode 100755 index 000000000..e6198f320 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/adaface/adaface.h @@ -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& images, + std::vector* 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 diff --git a/fastdeploy/vision/faceid/contrib/adaface/adaface_pybind.cc b/fastdeploy/vision/faceid/contrib/adaface/adaface_pybind.cc new file mode 100644 index 000000000..f04ebdad5 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/adaface/adaface_pybind.cc @@ -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_( + m, "AdaFacePreprocessor") + .def(pybind11::init()) + .def("run", [](vision::faceid::AdaFacePreprocessor& self, + std::vector& im_list) { + std::vector images; + for (size_t i = 0; i < im_list.size(); ++i) { + images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i]))); + } + std::vector 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_( + m, "AdaFacePostprocessor") + .def(pybind11::init()) + .def("run", [](vision::faceid::AdaFacePostprocessor& self, std::vector& inputs) { + std::vector 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& input_array) { + std::vector results; + std::vector 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_( + m, "AdaFace") + .def(pybind11::init()) + .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& data) { + std::vector images; + for (size_t i = 0; i < data.size(); ++i) { + images.push_back(PyArrayToCvMat(data[i])); + } + std::vector 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 diff --git a/fastdeploy/vision/faceid/contrib/adaface/postprocessor.cc b/fastdeploy/vision/faceid/contrib/adaface/postprocessor.cc new file mode 100755 index 000000000..0f2d81ce9 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/adaface/postprocessor.cc @@ -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& infer_result, + std::vector* 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 diff --git a/fastdeploy/vision/faceid/contrib/adaface/postprocessor.h b/fastdeploy/vision/faceid/contrib/adaface/postprocessor.h new file mode 100755 index 000000000..8ad7708dc --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/adaface/postprocessor.h @@ -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& infer_result, + std::vector* 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 diff --git a/fastdeploy/vision/faceid/contrib/adaface/preprocessor.cc b/fastdeploy/vision/faceid/contrib/adaface/preprocessor.cc new file mode 100755 index 000000000..8e8f95950 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/adaface/preprocessor.cc @@ -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* images, + std::vector* 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 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 diff --git a/fastdeploy/vision/faceid/contrib/adaface/preprocessor.h b/fastdeploy/vision/faceid/contrib/adaface/preprocessor.h new file mode 100755 index 000000000..008df3d10 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/adaface/preprocessor.h @@ -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* images, std::vector* outputs); + + /// Get Size + std::vector GetSize() { return size_; } + + /// Set size. + void SetSize(std::vector& size) { size_ = size; } + + /// Get alpha + std::vector GetAlpha() { return alpha_; } + + /// Set alpha. + void SetAlpha(std::vector& alpha) { alpha_ = alpha; } + + /// Get beta + std::vector GetBeta() { return beta_; } + + /// Set beta. + void SetBeta(std::vector& 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 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 alpha_; + // Argument for image preprocessing step, beta values for normalization, + // default beta = {-1.f, -1.f, -1.f} + std::vector 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 diff --git a/fastdeploy/vision/faceid/contrib/adaface_pybind.cc b/fastdeploy/vision/faceid/contrib/adaface_pybind.cc deleted file mode 100644 index 1333bb6e3..000000000 --- a/fastdeploy/vision/faceid/contrib/adaface_pybind.cc +++ /dev/null @@ -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_(m, "AdaFace") - .def(pybind11::init()) - .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 diff --git a/fastdeploy/vision/faceid/contrib/arcface.cc b/fastdeploy/vision/faceid/contrib/arcface.cc deleted file mode 100644 index 5f6cb834d..000000000 --- a/fastdeploy/vision/faceid/contrib/arcface.cc +++ /dev/null @@ -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& 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 \ No newline at end of file diff --git a/fastdeploy/vision/faceid/contrib/arcface.h b/fastdeploy/vision/faceid/contrib/arcface.h deleted file mode 100644 index 05478fc66..000000000 --- a/fastdeploy/vision/faceid/contrib/arcface.h +++ /dev/null @@ -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& infer_result, - FaceRecognitionResult* result) override; -}; - -} // namespace faceid -} // namespace vision -} // namespace fastdeploy diff --git a/fastdeploy/vision/faceid/contrib/arcface_pybind.cc b/fastdeploy/vision/faceid/contrib/arcface_pybind.cc deleted file mode 100644 index 0d508f444..000000000 --- a/fastdeploy/vision/faceid/contrib/arcface_pybind.cc +++ /dev/null @@ -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_(m, "ArcFace") - .def(pybind11::init()) - .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 diff --git a/fastdeploy/vision/faceid/contrib/cosface.cc b/fastdeploy/vision/faceid/contrib/cosface.cc deleted file mode 100644 index 5dffcc02a..000000000 --- a/fastdeploy/vision/faceid/contrib/cosface.cc +++ /dev/null @@ -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& 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 \ No newline at end of file diff --git a/fastdeploy/vision/faceid/contrib/cosface.h b/fastdeploy/vision/faceid/contrib/cosface.h deleted file mode 100644 index dbf2e4ac6..000000000 --- a/fastdeploy/vision/faceid/contrib/cosface.h +++ /dev/null @@ -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& infer_result, - FaceRecognitionResult* result) override; -}; - -} // namespace faceid -} // namespace vision -} // namespace fastdeploy diff --git a/fastdeploy/vision/faceid/contrib/cosface_pybind.cc b/fastdeploy/vision/faceid/contrib/cosface_pybind.cc deleted file mode 100644 index fe27a95b7..000000000 --- a/fastdeploy/vision/faceid/contrib/cosface_pybind.cc +++ /dev/null @@ -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_(m, "CosFace") - .def(pybind11::init()) - .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 diff --git a/fastdeploy/vision/faceid/contrib/insightface/base.cc b/fastdeploy/vision/faceid/contrib/insightface/base.cc new file mode 100755 index 000000000..ba40985e2 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/base.cc @@ -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 results; + if (!BatchPredict({im}, &results)) { + return false; + } + *result = std::move(results[0]); + return true; +} + +bool InsightFaceRecognitionBase::BatchPredict(const std::vector& images, + std::vector* results){ + std::vector 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 diff --git a/fastdeploy/vision/faceid/contrib/insightface/base.h b/fastdeploy/vision/faceid/contrib/insightface/base.h new file mode 100755 index 000000000..571a5bd4c --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/base.h @@ -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& images, + std::vector* 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 diff --git a/fastdeploy/vision/faceid/contrib/insightface/insightface_pybind.cc b/fastdeploy/vision/faceid/contrib/insightface/insightface_pybind.cc new file mode 100644 index 000000000..b193d9fb7 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/insightface_pybind.cc @@ -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_( + m, "InsightFaceRecognitionPreprocessor") + .def(pybind11::init()) + .def("run", [](vision::faceid::InsightFaceRecognitionPreprocessor& self, + std::vector& im_list) { + std::vector images; + for (size_t i = 0; i < im_list.size(); ++i) { + images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i]))); + } + std::vector 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_( + m, "InsightFaceRecognitionPostprocessor") + .def(pybind11::init()) + .def("run", [](vision::faceid::InsightFaceRecognitionPostprocessor& self, std::vector& inputs) { + std::vector 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& input_array) { + std::vector results; + std::vector 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_( + m, "InsightFaceRecognitionBase") + .def(pybind11::init()) + .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& data) { + std::vector images; + for (size_t i = 0; i < data.size(); ++i) { + images.push_back(PyArrayToCvMat(data[i])); + } + std::vector 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_(m, "ArcFace") + .def(pybind11::init()); + + pybind11::class_(m, "CosFace") + .def(pybind11::init()); + + pybind11::class_(m, "PartialFC") + .def(pybind11::init()); + + pybind11::class_(m, "VPL") + .def(pybind11::init()); +} +} // namespace fastdeploy diff --git a/fastdeploy/vision/faceid/contrib/insightface/model.h b/fastdeploy/vision/faceid/contrib/insightface/model.h new file mode 100644 index 000000000..9f4466bb7 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/model.h @@ -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 diff --git a/fastdeploy/vision/faceid/contrib/insightface/postprocessor.cc b/fastdeploy/vision/faceid/contrib/insightface/postprocessor.cc new file mode 100755 index 000000000..ca30719f6 --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/postprocessor.cc @@ -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& infer_result, + std::vector* 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 diff --git a/fastdeploy/vision/faceid/contrib/insightface/postprocessor.h b/fastdeploy/vision/faceid/contrib/insightface/postprocessor.h new file mode 100755 index 000000000..90c90980d --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/postprocessor.h @@ -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& infer_result, + std::vector* 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 diff --git a/fastdeploy/vision/faceid/contrib/insightface/preprocessor.cc b/fastdeploy/vision/faceid/contrib/insightface/preprocessor.cc new file mode 100755 index 000000000..c846522cc --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/preprocessor.cc @@ -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* images, + std::vector* 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 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 diff --git a/fastdeploy/vision/faceid/contrib/insightface/preprocessor.h b/fastdeploy/vision/faceid/contrib/insightface/preprocessor.h new file mode 100755 index 000000000..88d0dce8d --- /dev/null +++ b/fastdeploy/vision/faceid/contrib/insightface/preprocessor.h @@ -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* images, std::vector* outputs); + + /// Get Size + std::vector GetSize() { return size_; } + + /// Set size. + void SetSize(std::vector& size) { size_ = size; } + + /// Get alpha + std::vector GetAlpha() { return alpha_; } + + /// Set alpha. + void SetAlpha(std::vector& alpha) { alpha_ = alpha; } + + /// Get beta + std::vector GetBeta() { return beta_; } + + /// Set beta. + void SetBeta(std::vector& 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 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 alpha_; + // Argument for image preprocessing step, beta values for normalization, + // default beta = {-1.f, -1.f, -1.f} + std::vector 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 diff --git a/fastdeploy/vision/faceid/contrib/insightface_rec.cc b/fastdeploy/vision/faceid/contrib/insightface_rec.cc deleted file mode 100755 index f8810e82a..000000000 --- a/fastdeploy/vision/faceid/contrib/insightface_rec.cc +++ /dev/null @@ -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& 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 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 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 \ No newline at end of file diff --git a/fastdeploy/vision/faceid/contrib/insightface_rec.h b/fastdeploy/vision/faceid/contrib/insightface_rec.h deleted file mode 100644 index 2e66d3d71..000000000 --- a/fastdeploy/vision/faceid/contrib/insightface_rec.h +++ /dev/null @@ -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 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 alpha; - /*! @brief - Argument for image preprocessing step, beta values for normalization, default beta = {-1.f, -1.f, -1.f} - */ - std::vector 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& infer_result, - FaceRecognitionResult* result); -}; - -} // namespace faceid -} // namespace vision -} // namespace fastdeploy diff --git a/fastdeploy/vision/faceid/contrib/insightface_rec_pybind.cc b/fastdeploy/vision/faceid/contrib/insightface_rec_pybind.cc deleted file mode 100644 index bbbe60d3a..000000000 --- a/fastdeploy/vision/faceid/contrib/insightface_rec_pybind.cc +++ /dev/null @@ -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_(m, "InsightFaceRecognitionModel") - .def(pybind11::init()) - .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 diff --git a/fastdeploy/vision/faceid/contrib/partial_fc.cc b/fastdeploy/vision/faceid/contrib/partial_fc.cc deleted file mode 100644 index 1ef1f218b..000000000 --- a/fastdeploy/vision/faceid/contrib/partial_fc.cc +++ /dev/null @@ -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& 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 \ No newline at end of file diff --git a/fastdeploy/vision/faceid/contrib/partial_fc.h b/fastdeploy/vision/faceid/contrib/partial_fc.h deleted file mode 100644 index fac525be7..000000000 --- a/fastdeploy/vision/faceid/contrib/partial_fc.h +++ /dev/null @@ -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& infer_result, - FaceRecognitionResult* result) override; -}; - -} // namespace faceid -} // namespace vision -} // namespace fastdeploy diff --git a/fastdeploy/vision/faceid/contrib/partial_fc_pybind.cc b/fastdeploy/vision/faceid/contrib/partial_fc_pybind.cc deleted file mode 100644 index 4b1761ca1..000000000 --- a/fastdeploy/vision/faceid/contrib/partial_fc_pybind.cc +++ /dev/null @@ -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_(m, "PartialFC") - .def(pybind11::init()) - .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 diff --git a/fastdeploy/vision/faceid/contrib/vpl.cc b/fastdeploy/vision/faceid/contrib/vpl.cc deleted file mode 100644 index 841c889a9..000000000 --- a/fastdeploy/vision/faceid/contrib/vpl.cc +++ /dev/null @@ -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& 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 \ No newline at end of file diff --git a/fastdeploy/vision/faceid/contrib/vpl.h b/fastdeploy/vision/faceid/contrib/vpl.h deleted file mode 100644 index c4a265072..000000000 --- a/fastdeploy/vision/faceid/contrib/vpl.h +++ /dev/null @@ -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& infer_result, - FaceRecognitionResult* result) override; -}; - -} // namespace faceid -} // namespace vision -} // namespace fastdeploy diff --git a/fastdeploy/vision/faceid/contrib/vpl_pybind.cc b/fastdeploy/vision/faceid/contrib/vpl_pybind.cc deleted file mode 100644 index 8d3369b95..000000000 --- a/fastdeploy/vision/faceid/contrib/vpl_pybind.cc +++ /dev/null @@ -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_(m, "VPL") - .def(pybind11::init()) - .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 diff --git a/fastdeploy/vision/faceid/faceid_pybind.cc b/fastdeploy/vision/faceid/faceid_pybind.cc index 22e9d5140..1b3b2b6cf 100644 --- a/fastdeploy/vision/faceid/faceid_pybind.cc +++ b/fastdeploy/vision/faceid/faceid_pybind.cc @@ -15,20 +15,11 @@ #include "fastdeploy/pybind/main.h" namespace fastdeploy { +void BindInsightFace(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) { auto faceid_module = m.def_submodule("faceid", "Face recognition models."); - BindInsightFaceRecognitionModel(faceid_module); + BindInsightFace(faceid_module); BindAdaFace(faceid_module); - BindArcFace(faceid_module); - BindCosFace(faceid_module); - BindPartialFC(faceid_module); - BindVPL(faceid_module); } } // namespace fastdeploy diff --git a/python/__init__.py b/python/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/python/fastdeploy/vision/faceid/__init__.py b/python/fastdeploy/vision/faceid/__init__.py index cc95414f4..90958f0c8 100644 --- a/python/fastdeploy/vision/faceid/__init__.py +++ b/python/fastdeploy/vision/faceid/__init__.py @@ -13,9 +13,4 @@ # limitations under the License. from __future__ import absolute_import -from .contrib.adaface import AdaFace -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 +from .contrib import * diff --git a/python/fastdeploy/vision/faceid/contrib/__init__.py b/python/fastdeploy/vision/faceid/contrib/__init__.py index 8034e10bf..a23ab41f7 100644 --- a/python/fastdeploy/vision/faceid/contrib/__init__.py +++ b/python/fastdeploy/vision/faceid/contrib/__init__.py @@ -13,3 +13,5 @@ # limitations under the License. from __future__ import absolute_import +from .insightface import * +from .adaface import * \ No newline at end of file diff --git a/python/fastdeploy/vision/faceid/contrib/adaface.py b/python/fastdeploy/vision/faceid/contrib/adaface.py deleted file mode 100644 index 140cdb504..000000000 --- a/python/fastdeploy/vision/faceid/contrib/adaface.py +++ /dev/null @@ -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 diff --git a/python/fastdeploy/vision/faceid/contrib/adaface/__init__.py b/python/fastdeploy/vision/faceid/contrib/adaface/__init__.py new file mode 100644 index 000000000..0c4a5f973 --- /dev/null +++ b/python/fastdeploy/vision/faceid/contrib/adaface/__init__.py @@ -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 diff --git a/python/fastdeploy/vision/faceid/contrib/arcface.py b/python/fastdeploy/vision/faceid/contrib/arcface.py deleted file mode 100644 index f4341b170..000000000 --- a/python/fastdeploy/vision/faceid/contrib/arcface.py +++ /dev/null @@ -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 diff --git a/python/fastdeploy/vision/faceid/contrib/cosface.py b/python/fastdeploy/vision/faceid/contrib/cosface.py deleted file mode 100644 index 61d1f2cb9..000000000 --- a/python/fastdeploy/vision/faceid/contrib/cosface.py +++ /dev/null @@ -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 diff --git a/python/fastdeploy/vision/faceid/contrib/insightface/__init__.py b/python/fastdeploy/vision/faceid/contrib/insightface/__init__.py new file mode 100644 index 000000000..3353c8e44 --- /dev/null +++ b/python/fastdeploy/vision/faceid/contrib/insightface/__init__.py @@ -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." diff --git a/python/fastdeploy/vision/faceid/contrib/insightface_rec.py b/python/fastdeploy/vision/faceid/contrib/insightface_rec.py deleted file mode 100644 index ea4aed81a..000000000 --- a/python/fastdeploy/vision/faceid/contrib/insightface_rec.py +++ /dev/null @@ -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 diff --git a/python/fastdeploy/vision/faceid/contrib/partial_fc.py b/python/fastdeploy/vision/faceid/contrib/partial_fc.py deleted file mode 100644 index 0798af56e..000000000 --- a/python/fastdeploy/vision/faceid/contrib/partial_fc.py +++ /dev/null @@ -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 diff --git a/python/fastdeploy/vision/faceid/contrib/vpl.py b/python/fastdeploy/vision/faceid/contrib/vpl.py deleted file mode 100644 index 5db5b4e67..000000000 --- a/python/fastdeploy/vision/faceid/contrib/vpl.py +++ /dev/null @@ -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