From 61c4a469d6941fa895f257cf51450063bd3632d0 Mon Sep 17 00:00:00 2001 From: ziqi-jin <67993288+ziqi-jin@users.noreply.github.com> Date: Fri, 12 Aug 2022 20:13:50 +0800 Subject: [PATCH] Add external model's example code and Docs. (#102) * first commit for yolov7 * pybind for yolov7 * CPP README.md * CPP README.md * modified yolov7.cc * README.md * python file modify * delete license in fastdeploy/ * repush the conflict part * README.md modified * README.md modified * file path modified * file path modified * file path modified * file path modified * file path modified * README modified * README modified * move some helpers to private * add examples for yolov7 * api.md modified * api.md modified * api.md modified * YOLOv7 * yolov7 release link * yolov7 release link * yolov7 release link * copyright * change some helpers to private * change variables to const and fix documents. * gitignore * Transfer some funtions to private member of class * Transfer some funtions to private member of class * Merge from develop (#9) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * first commit for yolor * for merge * Develop (#11) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * Yolor (#16) * Develop (#11) (#12) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * Develop (#13) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: Jason Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> * documents * documents * documents * documents * documents * documents * documents * documents * documents * documents * documents * documents * Develop (#14) * Fix compile problem in different python version (#26) * fix some usage problem in linux * Fix compile problem Co-authored-by: root * Add PaddleDetetion/PPYOLOE model support (#22) * add ppdet/ppyoloe * Add demo code and documents * add convert processor to vision (#27) * update .gitignore * Added checking for cmake include dir * fixed missing trt_backend option bug when init from trt * remove un-need data layout and add pre-check for dtype * changed RGB2BRG to BGR2RGB in ppcls model * add model_zoo yolov6 c++/python demo * fixed CMakeLists.txt typos * update yolov6 cpp/README.md * add yolox c++/pybind and model_zoo demo * move some helpers to private * fixed CMakeLists.txt typos * add normalize with alpha and beta * add version notes for yolov5/yolov6/yolox * add copyright to yolov5.cc * revert normalize * fixed some bugs in yolox * fixed examples/CMakeLists.txt to avoid conflicts * add convert processor to vision * format examples/CMakeLists summary * Fix bug while the inference result is empty with YOLOv5 (#29) * Add multi-label function for yolov5 * Update README.md Update doc * Update fastdeploy_runtime.cc fix variable option.trt_max_shape wrong name * Update runtime_option.md Update resnet model dynamic shape setting name from images to x * Fix bug when inference result boxes are empty * Delete detection.py Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason <928090362@qq.com> * add is_dynamic for YOLO series (#22) * first commit test photo * yolov7 doc * yolov7 doc * yolov7 doc * yolov7 doc * add yolov5 docs * modify yolov5 doc * first commit for retinaface * first commit for retinaface * firt commit for ultraface * firt commit for ultraface * firt commit for yolov5face * firt commit for modnet and arcface * firt commit for modnet and arcface * first commit for partial_fc * first commit for partial_fc * first commit for yolox * first commit for yolov6 * first commit for nano_det * first commit for scrfd * first commit for scrfd * first commit for retinaface * first commit for ultraface * first commit for yolov5face * first commit for yolox yolov6 nano * rm jpg * first commit for modnet and modify nano * yolor scaledyolov4 v5lite * first commit for insightface * first commit for insightface * first commit for insightface * docs * docs * docs * docs * docs * add print for detect and modify docs * docs * docs * docs * docs test for insightface * docs test for insightface again * docs test for insightface * modify all wrong expressions in docs * modify all wrong expressions in docs Co-authored-by: Jason Co-authored-by: root Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: Jason <928090362@qq.com> --- docs/api/vision_results/README.md | 4 +- .../vision_results/face_detection_result.md | 34 ++++ docs/api/vision_results/matting_result.md | 35 ++++ examples/vision/README.md | 9 +- .../classification/paddleclas/python/infer.py | 3 +- examples/vision/detection/README.md | 17 +- .../vision/detection/nanodet_plus/README.md | 11 +- .../detection/nanodet_plus/cpp/README.md | 17 +- .../detection/nanodet_plus/cpp/infer.cc | 109 ++++++++++++ .../detection/nanodet_plus/python/README.md | 16 +- .../detection/nanodet_plus/python/infer.py | 53 ++++++ .../vision/detection/scaledyolov4/README.md | 45 +++++ .../scaledyolov4}/cpp/CMakeLists.txt | 0 .../detection/scaledyolov4/cpp/README.md | 87 ++++++++++ .../detection/scaledyolov4/cpp/infer.cc | 110 ++++++++++++ .../detection/scaledyolov4/python/README.md | 84 ++++++++++ .../detection/scaledyolov4/python/infer.py | 53 ++++++ examples/vision/detection/yolor/README.md | 48 ++++++ .../yolor}/cpp/CMakeLists.txt | 0 .../arcface => detection/yolor}/cpp/README.md | 32 ++-- examples/vision/detection/yolor/cpp/infer.cc | 109 ++++++++++++ .../yolor}/python/README.md | 34 ++-- .../vision/detection/yolor/python/infer.py | 52 ++++++ examples/vision/detection/yolov5/README.md | 9 +- .../vision/detection/yolov5/cpp/README.md | 6 +- examples/vision/detection/yolov5/cpp/infer.cc | 3 + .../vision/detection/yolov5/python/README.md | 4 +- .../vision/detection/yolov5/python/infer.py | 3 +- .../vision/detection/yolov5lite/README.md | 72 ++++++++ .../detection/yolov5lite/cpp/CMakeLists.txt | 14 ++ .../yolov5lite}/cpp/README.md | 32 ++-- .../vision/detection/yolov5lite/cpp/infer.cc | 110 ++++++++++++ .../yolov5lite}/python/README.md | 34 ++-- .../detection/yolov5lite/python/infer.py | 53 ++++++ examples/vision/detection/yolov6/README.md | 13 +- .../vision/detection/yolov6/cpp/README.md | 8 +- examples/vision/detection/yolov6/cpp/infer.cc | 108 ++++++++++++ .../vision/detection/yolov6/python/README.md | 7 +- .../vision/detection/yolov6/python/infer.py | 52 ++++++ examples/vision/detection/yolov7/README.md | 11 +- .../vision/detection/yolov7/cpp/README.md | 4 +- examples/vision/detection/yolov7/cpp/infer.cc | 3 + .../vision/detection/yolov7/python/README.md | 2 + .../vision/detection/yolov7/python/infer.py | 2 +- examples/vision/detection/yolox/README.md | 14 +- examples/vision/detection/yolox/cpp/README.md | 9 +- examples/vision/detection/yolox/cpp/infer.cc | 108 ++++++++++++ .../vision/detection/yolox/python/README.md | 12 +- .../vision/detection/yolox/python/infer.py | 51 ++++++ examples/vision/facedet/README.md | 10 +- examples/vision/facedet/retinaface/README.md | 8 +- .../vision/facedet/retinaface/cpp/README.md | 26 +-- .../vision/facedet/retinaface/cpp/infer.cc | 109 ++++++++++++ .../facedet/retinaface/python/README.md | 25 +-- .../vision/facedet/retinaface/python/infer.py | 51 ++++++ examples/vision/facedet/scrfd/README.md | 71 ++++++++ .../vision/facedet/scrfd/cpp/CMakeLists.txt | 14 ++ examples/vision/facedet/scrfd/cpp/README.md | 91 ++++++++++ examples/vision/facedet/scrfd/cpp/infer.cc | 109 ++++++++++++ .../vision/facedet/scrfd/python/README.md | 84 ++++++++++ examples/vision/facedet/scrfd/python/infer.py | 51 ++++++ examples/vision/facedet/ultraface/README.md | 7 +- .../vision/facedet/ultraface/cpp/README.md | 24 ++- .../vision/facedet/ultraface/cpp/infer.cc | 108 ++++++++++++ .../vision/facedet/ultraface/python/README.md | 25 ++- .../vision/facedet/ultraface/python/infer.py | 51 ++++++ examples/vision/facedet/yolov5face/README.md | 13 +- .../vision/facedet/yolov5face/cpp/README.md | 21 ++- .../vision/facedet/yolov5face/cpp/infer.cc | 108 ++++++++++++ .../facedet/yolov5face/python/README.md | 21 +-- .../vision/facedet/yolov5face/python/infer.py | 51 ++++++ examples/vision/faceid/README.md | 10 ++ examples/vision/faceid/arcface/README.md | 40 ----- examples/vision/faceid/insightface/README.md | 62 +++++++ .../faceid/insightface/cpp/CMakeLists.txt | 22 +++ .../vision/faceid/insightface/cpp/README.md | 129 +++++++++++++++ .../faceid/insightface/cpp/infer_arcface.cc | 156 ++++++++++++++++++ .../faceid/insightface/cpp/infer_cosface.cc | 156 ++++++++++++++++++ .../insightface/cpp/infer_partial_fc.cc | 156 ++++++++++++++++++ .../faceid/insightface/cpp/infer_vpl.cc | 156 ++++++++++++++++++ .../faceid/insightface/python/README.md | 100 +++++++++++ .../insightface/python/infer_arcface.py | 90 ++++++++++ .../insightface/python/infer_cosface.py | 90 ++++++++++ .../insightface/python/infer_partial_fc.py | 90 ++++++++++ .../faceid/insightface/python/infer_vpl.py | 90 ++++++++++ examples/vision/faceid/partial_fc/README.md | 37 ----- examples/vision/matting/README.md | 7 + examples/vision/matting/modnet/README.md | 7 +- examples/vision/matting/modnet/cpp/README.md | 32 ++-- examples/vision/matting/modnet/cpp/infer.cc | 109 ++++++++++++ .../vision/matting/modnet/python/README.md | 31 ++-- .../vision/matting/modnet/python/infer.py | 53 ++++++ fastdeploy/vision/evaluation/detection.py | 2 +- 93 files changed, 4100 insertions(+), 309 deletions(-) create mode 100644 docs/api/vision_results/face_detection_result.md create mode 100644 docs/api/vision_results/matting_result.md create mode 100644 examples/vision/detection/nanodet_plus/cpp/infer.cc create mode 100644 examples/vision/detection/nanodet_plus/python/infer.py create mode 100644 examples/vision/detection/scaledyolov4/README.md rename examples/vision/{faceid/arcface => detection/scaledyolov4}/cpp/CMakeLists.txt (100%) create mode 100644 examples/vision/detection/scaledyolov4/cpp/README.md create mode 100644 examples/vision/detection/scaledyolov4/cpp/infer.cc create mode 100644 examples/vision/detection/scaledyolov4/python/README.md create mode 100644 examples/vision/detection/scaledyolov4/python/infer.py create mode 100644 examples/vision/detection/yolor/README.md rename examples/vision/{faceid/partial_fc => detection/yolor}/cpp/CMakeLists.txt (100%) rename examples/vision/{faceid/arcface => detection/yolor}/cpp/README.md (72%) create mode 100644 examples/vision/detection/yolor/cpp/infer.cc rename examples/vision/{faceid/partial_fc => detection/yolor}/python/README.md (64%) create mode 100644 examples/vision/detection/yolor/python/infer.py create mode 100644 examples/vision/detection/yolov5lite/README.md create mode 100644 examples/vision/detection/yolov5lite/cpp/CMakeLists.txt rename examples/vision/{faceid/partial_fc => detection/yolov5lite}/cpp/README.md (72%) create mode 100644 examples/vision/detection/yolov5lite/cpp/infer.cc rename examples/vision/{faceid/arcface => detection/yolov5lite}/python/README.md (64%) create mode 100644 examples/vision/detection/yolov5lite/python/infer.py create mode 100644 examples/vision/detection/yolov6/cpp/infer.cc create mode 100644 examples/vision/detection/yolov6/python/infer.py create mode 100644 examples/vision/detection/yolox/cpp/infer.cc create mode 100644 examples/vision/detection/yolox/python/infer.py create mode 100644 examples/vision/facedet/retinaface/cpp/infer.cc create mode 100644 examples/vision/facedet/retinaface/python/infer.py create mode 100644 examples/vision/facedet/scrfd/README.md create mode 100644 examples/vision/facedet/scrfd/cpp/CMakeLists.txt create mode 100644 examples/vision/facedet/scrfd/cpp/README.md create mode 100644 examples/vision/facedet/scrfd/cpp/infer.cc create mode 100644 examples/vision/facedet/scrfd/python/README.md create mode 100644 examples/vision/facedet/scrfd/python/infer.py create mode 100644 examples/vision/facedet/ultraface/cpp/infer.cc create mode 100644 examples/vision/facedet/ultraface/python/infer.py create mode 100644 examples/vision/facedet/yolov5face/cpp/infer.cc create mode 100644 examples/vision/facedet/yolov5face/python/infer.py create mode 100644 examples/vision/faceid/README.md delete mode 100644 examples/vision/faceid/arcface/README.md create mode 100644 examples/vision/faceid/insightface/README.md create mode 100644 examples/vision/faceid/insightface/cpp/CMakeLists.txt create mode 100644 examples/vision/faceid/insightface/cpp/README.md create mode 100644 examples/vision/faceid/insightface/cpp/infer_arcface.cc create mode 100644 examples/vision/faceid/insightface/cpp/infer_cosface.cc create mode 100644 examples/vision/faceid/insightface/cpp/infer_partial_fc.cc create mode 100644 examples/vision/faceid/insightface/cpp/infer_vpl.cc create mode 100644 examples/vision/faceid/insightface/python/README.md create mode 100644 examples/vision/faceid/insightface/python/infer_arcface.py create mode 100644 examples/vision/faceid/insightface/python/infer_cosface.py create mode 100644 examples/vision/faceid/insightface/python/infer_partial_fc.py create mode 100644 examples/vision/faceid/insightface/python/infer_vpl.py delete mode 100644 examples/vision/faceid/partial_fc/README.md create mode 100644 examples/vision/matting/README.md create mode 100644 examples/vision/matting/modnet/cpp/infer.cc create mode 100644 examples/vision/matting/modnet/python/infer.py diff --git a/docs/api/vision_results/README.md b/docs/api/vision_results/README.md index 0a05aeaf8c..844388cca8 100644 --- a/docs/api/vision_results/README.md +++ b/docs/api/vision_results/README.md @@ -4,5 +4,7 @@ FastDeploy根据视觉模型的任务类型,定义了不同的结构体(`csrcs | 结构体 | 文档 | 说明 | 相应模型 | | :----- | :--- | :---- | :------- | -| ClassificationResult | [C++/Python文档](./classificiation_result.md) | 图像分类返回结果 | ResNet50、MobileNetV3等 | +| ClassificationResult | [C++/Python文档](./classification_result.md) | 图像分类返回结果 | ResNet50、MobileNetV3等 | | DetectionResult | [C++/Python文档](./detection_result.md) | 目标检测返回结果 | PPYOLOE、YOLOv7系列模型等 | +| FaceDetectionResult | [C++/Python文档](./face_detection_result.md) | 目标检测返回结果 | PPYOLOE、YOLOv7系列模型等 | +| MattingResult | [C++/Python文档](./matting_result.md) | 目标检测返回结果 | PPYOLOE、YOLOv7系列模型等 | diff --git a/docs/api/vision_results/face_detection_result.md b/docs/api/vision_results/face_detection_result.md new file mode 100644 index 0000000000..6c9c09f007 --- /dev/null +++ b/docs/api/vision_results/face_detection_result.md @@ -0,0 +1,34 @@ +# FaceDetectionResult 人脸检测结果 + +FaceDetectionResult 代码定义在`csrcs/fastdeploy/vision/common/result.h`中,用于表明图像检测出来的目标框、目标类别和目标置信度。 + +## C++ 结构体 + +`fastdeploy::vision::FaceDetectionResult` + +``` +struct FaceDetectionResult { + std::vector> boxes; + std::vector> landmarks; + std::vector scores; + ResultType type = ResultType::FACE_DETECTION; + int landmarks_per_face; + void Clear(); + std::string Str(); +}; +``` + +- **boxes**: 成员变量,表示单张图片检测出来的所有目标框坐标,`boxes.size()`表示框的个数,每个框以4个float数值依次表示xmin, ymin, xmax, ymax, 即左上角和右下角坐标 +- **scores**: 成员变量,表示单张图片检测出来的所有目标置信度,其元素个数与`boxes.size()`一致 +- **landmarks**: 成员变量,表示单张图片检测出来的所有人脸的关键点,其元素个数与`boxes.size()`一致 +- **landmarks_per_face**: 成员变量,表示每个人脸框中的关键点的数量。 +- **Clear()**: 成员函数,用于清除结构体中存储的结果 +- **Str()**: 成员函数,将结构体中的信息以字符串形式输出(用于Debug) + +## Python结构体 + +`fastdeploy.vision.FaceDetectionResult` + +- **boxes**(list of list(float)): 成员变量,表示单张图片检测出来的所有目标框坐标。boxes是一个list,其每个元素为一个长度为4的list, 表示为一个框,每个框以4个float数值依次表示xmin, ymin, xmax, ymax, 即左上角和右下角坐标 +- **scores**(list of float): 成员变量,表示单张图片检测出来的所有目标置信度 +- **landmarks**: 成员变量,表示单张图片检测出来的所有人脸的关键点 diff --git a/docs/api/vision_results/matting_result.md b/docs/api/vision_results/matting_result.md new file mode 100644 index 0000000000..3418400eca --- /dev/null +++ b/docs/api/vision_results/matting_result.md @@ -0,0 +1,35 @@ +# MattingResult 抠图结果 + +MattingResult 代码定义在`csrcs/fastdeploy/vision/common/result.h`中,用于表明图像检测出来的目标框、目标类别和目标置信度。 + +## C++ 结构体 + +`fastdeploy::vision::MattingResult` + +``` +struct MattingResult { + std::vector alpha; // h x w + std::vector foreground; // h x w x c (c=3 default) + std::vector shape; + bool contain_foreground = false; + void Clear(); + std::string Str(); +}; +``` + +- **alpha**: 是一维向量,为预测的alpha透明度的值,值域为[0.,1.],长度为hxw,h,w为输入图像的高和宽 +- **foreground**: 是一维向量,为预测的前景,值域为[0.,255.],长度为hxwxc,h,w为输入图像的高和宽,c一般为3,foreground不是一定有的,只有模型本身预测了前景,这个属性才会有效 +- **contain_foreground**: 表示预测的结果是否包含前景 +- **shape**: 表示输出结果的shape,当contain_foreground为false,shape只包含(h,w),当contain_foreground为true,shape包含(h,w,c), c一般为3 +- **Clear()**: 成员函数,用于清除结构体中存储的结果 +- **Str()**: 成员函数,将结构体中的信息以字符串形式输出(用于Debug) + + +## Python结构体 + +`fastdeploy.vision.MattingResult` + +- **alpha**: 是一维向量,为预测的alpha透明度的值,值域为[0.,1.],长度为hxw,h,w为输入图像的高和宽 +- **foreground**: 是一维向量,为预测的前景,值域为[0.,255.],长度为hxwxc,h,w为输入图像的高和宽,c一般为3,foreground不是一定有的,只有模型本身预测了前景,这个属性才会有效 +- **contain_foreground**: 表示预测的结果是否包含前景 +- **shape**: 表示输出结果的shape,当contain_foreground为false,shape只包含(h,w),当contain_foreground为true,shape包含(h,w,c), c一般为3 diff --git a/examples/vision/README.md b/examples/vision/README.md index 31f1dd09d7..9f05d2d7f6 100644 --- a/examples/vision/README.md +++ b/examples/vision/README.md @@ -4,10 +4,11 @@ | 任务类型 | 说明 | 预测结果结构体 | |:-------------- |:----------------------------------- |:-------------------------------------------------------------------------------- | -| Detection | 目标检测,输入图像,检测图像中物体位置,并返回检测框坐标及类别和置信度 | [DetectionResult](../../../../docs/api/vision_results/detection_result.md) | -| Segmentation | 语义分割,输入图像,给出图像中每个像素的分类及置信度 | [SegmentationResult](../../../../docs/api/vision_results/segmentation_result.md) | -| Classification | 图像分类,输入图像,给出图像的分类结果和置信度 | [ClassifyResult](../../../../docs/api/vision_results/classification_result.md) | - +| Detection | 目标检测,输入图像,检测图像中物体位置,并返回检测框坐标及类别和置信度 | [DetectionResult](../../docs/api/vision_results/detection_result.md) | +| Segmentation | 语义分割,输入图像,给出图像中每个像素的分类及置信度 | [SegmentationResult](../../docs/api/vision_results/segmentation_result.md) | +| Classification | 图像分类,输入图像,给出图像的分类结果和置信度 | [ClassifyResult](../../docs/api/vision_results/classification_result.md) | +| FaceDetection | 人脸检测,输入图像,检测图像中人脸位置,并返回检测框坐标及人脸关键点 | [FaceDetectionResult](../../docs/api/vision_results/face_detection_result.md) | +| Matting | 抠图,输入图像,返回图片的前景每个像素点的Alpha值 | [MattingResult](../../docs/api/vision_results/matting_result.md) | ## FastDeploy API设计 视觉模型具有较有统一任务范式,在设计API时(包括C++/Python),FastDeploy将视觉模型的部署拆分为四个步骤 diff --git a/examples/vision/classification/paddleclas/python/infer.py b/examples/vision/classification/paddleclas/python/infer.py index 4db8342f02..b588b9f6d0 100644 --- a/examples/vision/classification/paddleclas/python/infer.py +++ b/examples/vision/classification/paddleclas/python/infer.py @@ -43,6 +43,7 @@ def build_option(args): # 配置runtime,加载模型 runtime_option = build_option(args) + model_file = os.path.join(args.model, "inference.pdmodel") params_file = os.path.join(args.model, "inference.pdiparams") config_file = os.path.join(args.model, "inference_cls.yaml") @@ -51,5 +52,5 @@ def build_option(args): # 预测图片分类结果 im = cv2.imread(args.image) -result = model.predict(im, args.topk) +result = model.predict(im.copy(), args.topk) print(result) diff --git a/examples/vision/detection/README.md b/examples/vision/detection/README.md index f6e73e6591..f87d72d916 100644 --- a/examples/vision/detection/README.md +++ b/examples/vision/detection/README.md @@ -1,13 +1,14 @@ -# 目标检测模型 +人脸检测模型 FastDeploy目前支持如下目标检测模型部署 | 模型 | 说明 | 模型格式 | 版本 | | :--- | :--- | :------- | :--- | -| [PaddleDetection/PPYOLOE](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe) | PPYOLOE系列模型 | Paddle | [Release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4) | -| [PaddleDetection/PicoDet](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe) | PicoDet系列模型 | Paddle | [Release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4) | -| [PaddleDetection/YOLOX](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe) | Paddle版本的YOLOX系列模型 | Paddle | [Release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4) | -| [PaddleDetection/YOLOv3](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe) | YOLOv3系列模型 | Paddle | [Release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4) | -| [PaddleDetection/PPYOLO](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe) | PPYOLO系列模型 | Paddle | [Release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4) | -| [PaddleDetection/FasterRCNN](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe) | FasterRCNN系列模型 | Paddle | [Release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4) | -| [WongKinYiu/YOLOv7](https://github.com/WongKinYiu/yolov7) | YOLOv7、YOLOv7-X等系列模型 | ONNX | [v0.1](https://github.com/WongKinYiu/yolov7/tree/v0.1) | +| [nanodet_plus](./nanodet_plus) | NanoDetPlus系列模型 | ONNX | Release/v1.0.0-alpha-1 | +| [yolov5](./yolov5) | YOLOv5系列模型 | ONNX | Release/v6.0 | +| [yolov5lite](./yolov5lite) | YOLOv5-Lite系列模型 | ONNX | Release/v1.4 | +| [yolov6](./yolov6) | YOLOv6系列模型 | ONNX | Release/0.1.0 | +| [yolov7](./yolov7) | YOLOv7系列模型 | ONNX | Release/0.1 | +| [yolor](./yolor) | YOLOR系列模型 | ONNX | Release/weights | +| [yolox](./yolox) | YOLOX系列模型 | ONNX | Release/v0.1.1 | +| [scaledyolov4](./scaledyolov4) | ScaledYOLOv4系列模型 | ONNX | CommitID:6768003 | diff --git a/examples/vision/detection/nanodet_plus/README.md b/examples/vision/detection/nanodet_plus/README.md index 4d23765e81..a295e122fc 100644 --- a/examples/vision/detection/nanodet_plus/README.md +++ b/examples/vision/detection/nanodet_plus/README.md @@ -2,11 +2,11 @@ ## 模型版本说明 -- NanoDetPlus部署实现来自[NanoDetPlus v1.0.0-alpha-1](https://github.com/RangiLyu/nanodet/tree/v1.0.0-alpha-1) 分支代码,基于coco的[预训练模型](https://github.com/RangiLyu/nanodet/releases/tag/v1.0.0-alpha-1)。 +- NanoDetPlus部署实现来自[NanoDetPlus](https://github.com/RangiLyu/nanodet/tree/v1.0.0-alpha-1) 的代码,基于coco的[预训练模型](https://github.com/RangiLyu/nanodet/releases/tag/v1.0.0-alpha-1)。 - (1)[预训练模型](https://github.com/RangiLyu/nanodet/releases/tag/v1.0.0-alpha-1)的*.onnx可直接进行部署; - - (2)自己训练的模型,导出ONNX模型后,参考[详细部署教程](#详细部署文档)完成部署。 - + - (2)自己训练的模型,导出ONNX模型后,参考[详细部署文档](#详细部署文档)完成部署。 + ## 下载预训练ONNX模型 为了方便开发者的测试,下面提供了NanoDetPlus导出的各系列模型,开发者可直接下载使用。 @@ -21,3 +21,8 @@ - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[NanoDetPlus v1.0.0-alpha-1](https://github.com/RangiLyu/nanodet/tree/v1.0.0-alpha-1) 编写 diff --git a/examples/vision/detection/nanodet_plus/cpp/README.md b/examples/vision/detection/nanodet_plus/cpp/README.md index 2dbee5e31d..82b310113e 100644 --- a/examples/vision/detection/nanodet_plus/cpp/README.md +++ b/examples/vision/detection/nanodet_plus/cpp/README.md @@ -12,7 +12,7 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j @@ -32,7 +32,7 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000 运行完成可视化结果如下图所示 - + ## NanoDetPlus C++接口 @@ -74,11 +74,14 @@ NanoDetPlus模型加载和初始化,其中model_file为导出的ONNX模型格 ### 类成员变量 -> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` -> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + +> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[320, 320] +> > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[0, 0, 0] +> > * **keep_ratio**(bool): 通过此参数指定resize时是否保持宽高比例不变,默认是fasle. +> > * **reg_max**(int): GFL回归中的reg_max参数,默认是7. +> > * **downsample_strides**(vector<int>): 通过此参数可以修改生成anchor的特征图的下采样倍数, 包含三个整型元素, 分别表示默认的生成anchor的下采样倍数, 默认值为[8, 16, 32, 64] - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/detection/nanodet_plus/cpp/infer.cc b/examples/vision/detection/nanodet_plus/cpp/infer.cc new file mode 100644 index 0000000000..8443639cc5 --- /dev/null +++ b/examples/vision/detection/nanodet_plus/cpp/infer.cc @@ -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. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::detection::NanoDetPlus(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = + fastdeploy::vision::detection::NanoDetPlus(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 320, 320}); + auto model = + fastdeploy::vision::detection::NanoDetPlus(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model ./nanodet-plus-m_320.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/detection/nanodet_plus/python/README.md b/examples/vision/detection/nanodet_plus/python/README.md index 7aff8059f9..5501823758 100644 --- a/examples/vision/detection/nanodet_plus/python/README.md +++ b/examples/vision/detection/nanodet_plus/python/README.md @@ -26,7 +26,7 @@ python infer.py --model nanodet-plus-m_320.onnx --image 000000014439.jpg --devic 运行完成可视化结果如下图所示 - + ## NanoDetPlus Python接口 @@ -62,12 +62,14 @@ NanoDetPlus模型加载和初始化,其中model_file为导出的ONNX模型格 > > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 - -> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` -> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + +> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[320, 320] +> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[0, 0, 0] +> > * **keep_ratio**(bool): 通过此参数指定resize时是否保持宽高比例不变,默认是fasle. +> > * **reg_max**(int): GFL回归中的reg_max参数,默认是7. +> > * **downsample_strides**(list[int]): 通过此参数可以修改生成anchor的特征图的下采样倍数, 包含三个整型元素, 分别表示默认的生成anchor的下采样倍数, 默认值为[8, 16, 32, 64] diff --git a/examples/vision/detection/nanodet_plus/python/infer.py b/examples/vision/detection/nanodet_plus/python/infer.py new file mode 100644 index 0000000000..a0667db005 --- /dev/null +++ b/examples/vision/detection/nanodet_plus/python/infer.py @@ -0,0 +1,53 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of nanodet_plus onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 320, 320]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.detection.NanoDetPlus( + args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) + +# 预测结果可视化 +vis_im = fd.vision.vis_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/detection/scaledyolov4/README.md b/examples/vision/detection/scaledyolov4/README.md new file mode 100644 index 0000000000..5a0ba000f1 --- /dev/null +++ b/examples/vision/detection/scaledyolov4/README.md @@ -0,0 +1,45 @@ +# ScaledYOLOv4准备部署模型 + +- ScaledYOLOv4部署实现来自[ScaledYOLOv4](https://github.com/WongKinYiu/ScaledYOLOv4)的代码,和[基于COCO的预训练模型](https://github.com/WongKinYiu/ScaledYOLOv4)。 + + - (1)[预训练模型](https://github.com/WongKinYiu/ScaledYOLOv4)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署;*.onnx、*.trt和*.pose模型不支持部署; + - (2)自己数据训练的ScaledYOLOv4模型,按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)操作后,参考[详细部署文档](#详细部署文档)完成部署。 + + +## 导出ONNX模型 + + + 访问[ScaledYOLOv4](https://github.com/WongKinYiu/ScaledYOLOv4)官方github库,按照指引下载安装,下载`scaledyolov4.pt` 模型,利用 `models/export.py` 得到`onnx`格式文件。如果您导出的`onnx`模型出现问题,可以参考[ScaledYOLOv4#401](https://github.com/WongKinYiu/ScaledYOLOv4/issues/401)的解决办法 + + ``` + #下载ScaledYOLOv4模型文件 + Download from the goole drive https://drive.google.com/file/d/1aXZZE999sHMP1gev60XhNChtHPRMH3Fz/view?usp=sharing + + # 导出onnx格式文件 + python models/export.py --weights PATH/TO/scaledyolov4-xx.pt --img-size 640 + ``` + + +## 下载预训练ONNX模型 + +为了方便开发者的测试,下面提供了ScaledYOLOv4导出的各系列模型,开发者可直接下载使用。 + +| 模型 | 大小 | 精度 | +|:---------------------------------------------------------------- |:----- |:----- | +| [ScaledYOLOv4-P5](https://bj.bcebos.com/paddlehub/fastdeploy/yolov4-p5.onnx) | 271MB | 51.2% | +| [ScaledYOLOv4-P5+BoF](https://bj.bcebos.com/paddlehub/fastdeploy/yolov4-p5_.onnx) | 271MB | 51.7% | +| [ScaledYOLOv4-P6](https://bj.bcebos.com/paddlehub/fastdeploy/yolov4-p6.onnx) | 487MB | 53.9% | +| [ScaledYOLOv4-P6+BoF](https://bj.bcebos.com/paddlehub/fastdeploy/yolov4-p6_.onnx) | 487MB | 54.4% | +| [ScaledYOLOv4-P7](https://bj.bcebos.com/paddlehub/fastdeploy/yolov4-p7.onnx) | 1.1GB | 55.0% | + + + +## 详细部署文档 + +- [Python部署](python) +- [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[ScaledYOLOv4 CommitID: 6768003](https://github.com/WongKinYiu/ScaledYOLOv4/commit/676800364a3446900b9e8407bc880ea2127b3415) 编写 diff --git a/examples/vision/faceid/arcface/cpp/CMakeLists.txt b/examples/vision/detection/scaledyolov4/cpp/CMakeLists.txt similarity index 100% rename from examples/vision/faceid/arcface/cpp/CMakeLists.txt rename to examples/vision/detection/scaledyolov4/cpp/CMakeLists.txt diff --git a/examples/vision/detection/scaledyolov4/cpp/README.md b/examples/vision/detection/scaledyolov4/cpp/README.md new file mode 100644 index 0000000000..0d372877f2 --- /dev/null +++ b/examples/vision/detection/scaledyolov4/cpp/README.md @@ -0,0 +1,87 @@ +# ScaledYOLOv4 C++部署示例 + +本目录下提供`infer.cc`快速完成ScaledYOLOv4在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) +- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/compile/prebuild_libraries.md) + +以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试 + +``` +mkdir build +cd build +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz +tar xvf fastdeploy-linux-x64-0.2.0.tgz +cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 +make -j + +#下载官方转换好的ScaledYOLOv4模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/scaled_yolov4-p5.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg + + +# CPU推理 +./infer_demo scaled_yolov4-p5.onnx 000000014439.jpg 0 +# GPU推理 +./infer_demo scaled_yolov4-p5.onnx 000000014439.jpg 1 +# GPU上TensorRT推理 +./infer_demo scaled_yolov4-p5.onnx 000000014439.jpg 2 +``` + +运行完成可视化结果如下图所示 + + + +## ScaledYOLOv4 C++接口 + +### ScaledYOLOv4类 + +``` +fastdeploy::vision::detection::ScaledYOLOv4( + const string& model_file, + const string& params_file = "", + const RuntimeOption& runtime_option = RuntimeOption(), + const Frontend& model_format = Frontend::ONNX) +``` + +ScaledYOLOv4模型加载和初始化,其中model_file为导出的ONNX模型格式。 + +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX时,此参数传入空字符串即可 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX格式 + +#### Predict函数 + +> ``` +> ScaledYOLOv4::Predict(cv::Mat* im, DetectionResult* result, +> float conf_threshold = 0.25, +> float nms_iou_threshold = 0.5) +> ``` +> +> 模型预测接口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **im**: 输入图像,注意需为HWC,BGR格式 +> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **conf_threshold**: 检测框置信度过滤阈值 +> > * **nms_iou_threshold**: NMS处理过程中iou阈值 + +### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + +> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] +> > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] +> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` +> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` +> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` + +- [模型介绍](../../) +- [Python部署](../python) +- [视觉模型预测结果](../../../../../docs/api/vision_results/) diff --git a/examples/vision/detection/scaledyolov4/cpp/infer.cc b/examples/vision/detection/scaledyolov4/cpp/infer.cc new file mode 100644 index 0000000000..7d912b2230 --- /dev/null +++ b/examples/vision/detection/scaledyolov4/cpp/infer.cc @@ -0,0 +1,110 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::detection::ScaledYOLOv4(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = + fastdeploy::vision::detection::ScaledYOLOv4(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = + fastdeploy::vision::detection::ScaledYOLOv4(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model scaled_yolov4-p5.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/detection/scaledyolov4/python/README.md b/examples/vision/detection/scaledyolov4/python/README.md new file mode 100644 index 0000000000..f9fdaabb89 --- /dev/null +++ b/examples/vision/detection/scaledyolov4/python/README.md @@ -0,0 +1,84 @@ +# ScaledYOLOv4 Python部署示例 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) +- 2. FastDeploy Python whl包安装,参考[FastDeploy Python安装](../../../../../docs/quick_start/install.md) + +本目录下提供`infer.py`快速完成ScaledYOLOv4在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 + +``` +#下载scaledyolov4模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/scaled_yolov4-p5.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg + + +#下载部署示例代码 +git clone https://github.com/PaddlePaddle/FastDeploy.git +cd examples/vison/detection/scaledyolov4/python/ + +# CPU推理 +python infer.py --model scaled_yolov4-p5.onnx --image 000000014439.jpg --device cpu +# GPU推理 +python infer.py --model scaled_yolov4-p5.onnx --image 000000014439.jpg --device gpu +# GPU上使用TensorRT推理 +python infer.py --model scaled_yolov4-p5.onnx --image 000000014439.jpg --device gpu --use_trt True +``` + +运行完成可视化结果如下图所示 + + + +## ScaledYOLOv4 Python接口 + +``` +fastdeploy.vision.detection.ScaledYOLOv4(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +``` + +ScaledYOLOv4模型加载和初始化,其中model_file为导出的ONNX模型格式 + +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX格式时,此参数无需设定 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX + +### predict函数 + +> ``` +> ScaledYOLOv4.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) +> ``` +> +> 模型预测结口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **image_data**(np.ndarray): 输入数据,注意需为HWC,BGR格式 +> > * **conf_threshold**(float): 检测框置信度过滤阈值 +> > * **nms_iou_threshold**(float): NMS处理过程中iou阈值 + +> **返回** +> +> > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) + +### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + +> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] +> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] +> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` +> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` +> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` + + + +## 其它文档 + +- [ScaledYOLOv4 模型介绍](..) +- [ScaledYOLOv4 C++部署](../cpp) +- [模型预测结果说明](../../../../../docs/api/vision_results/) diff --git a/examples/vision/detection/scaledyolov4/python/infer.py b/examples/vision/detection/scaledyolov4/python/infer.py new file mode 100644 index 0000000000..d3052381c4 --- /dev/null +++ b/examples/vision/detection/scaledyolov4/python/infer.py @@ -0,0 +1,53 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of scaledyolov4 onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.detection.ScaledYOLOv4( + args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) + +# 预测结果可视化 +vis_im = fd.vision.vis_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/detection/yolor/README.md b/examples/vision/detection/yolor/README.md new file mode 100644 index 0000000000..7889eac9f6 --- /dev/null +++ b/examples/vision/detection/yolor/README.md @@ -0,0 +1,48 @@ +# YOLOR准备部署模型 + +- YOLOR部署实现来自[YOLOR](https://github.com/WongKinYiu/yolor/releases/tag/weights)的代码,和[基于COCO的预训练模型](https://github.com/WongKinYiu/yolor/releases/tag/weights)。 + + - (1)[预训练模型](https://github.com/WongKinYiu/yolor/releases/tag/weights)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署;*.onnx、*.trt和*.pose模型不支持部署; + - (2)自己数据训练的YOLOR模型,按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)操作后,参考[详细部署文档](#详细部署文档)完成部署。 + + +## 导出ONNX模型 + + + 访问[YOLOR](https://github.com/WongKinYiu/yolor)官方github库,按照指引下载安装,下载`yolor.pt` 模型,利用 `models/export.py` 得到`onnx`格式文件。如果您导出的`onnx`模型出现精度不达标或者是数据维度的问题,可以参考[yolor#32](https://github.com/WongKinYiu/yolor/issues/32)的解决办法 + + ``` + #下载yolor模型文件 + wget https://github.com/WongKinYiu/yolor/releases/download/weights/yolor-d6-paper-570.pt + + # 导出onnx格式文件 + python models/export.py --weights PATH/TO/yolor-xx-xx-xx.pt --img-size 640 + ``` + +## 下载预训练ONNX模型 + +为了方便开发者的测试,下面提供了YOLOR导出的各系列模型,开发者可直接下载使用。 + +| 模型 | 大小 | 精度 | +|:---------------------------------------------------------------- |:----- |:----- | +| [YOLOR-P6-1280](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-p6-paper-541-1280-1280.onnx) | 143MB | 54.1% | +| [YOLOR-W6-1280](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-w6-paper-555-1280-1280.onnx) | 305MB | 55.5% | +| [YOLOR-E6-1280](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-e6-paper-564-1280-1280.onnx ) | 443MB | 56.4% | +| [YOLOR-D6-1280](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-d6-paper-570-1280-1280.onnx) | 580MB | 57.0% | +| [YOLOR-D6-1280](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-d6-paper-573-1280-1280.onnx) | 580MB | 57.3% | +| [YOLOR-P6](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-p6-paper-541-640-640.onnx) | 143MB | - | +| [YOLOR-W6](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-w6-paper-555-640-640.onnx) | 305MB | - | +| [YOLOR-E6](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-e6-paper-564-640-640.onnx ) | 443MB | - | +| [YOLOR-D6](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-d6-paper-570-640-640.onnx) | 580MB | - | +| [YOLOR-D6](https://bj.bcebos.com/paddlehub/fastdeploy/yolor-d6-paper-573-640-640.onnx) | 580MB | - | + + + +## 详细部署文档 + +- [Python部署](python) +- [C++部署](cpp) + +## 版本说明 + +- 本版本文档和代码基于[YOLOR weights](https://github.com/WongKinYiu/yolor/releases/tag/weights) 编写 diff --git a/examples/vision/faceid/partial_fc/cpp/CMakeLists.txt b/examples/vision/detection/yolor/cpp/CMakeLists.txt similarity index 100% rename from examples/vision/faceid/partial_fc/cpp/CMakeLists.txt rename to examples/vision/detection/yolor/cpp/CMakeLists.txt diff --git a/examples/vision/faceid/arcface/cpp/README.md b/examples/vision/detection/yolor/cpp/README.md similarity index 72% rename from examples/vision/faceid/arcface/cpp/README.md rename to examples/vision/detection/yolor/cpp/README.md index 505d144bbb..7798c7a436 100644 --- a/examples/vision/faceid/arcface/cpp/README.md +++ b/examples/vision/detection/yolor/cpp/README.md @@ -1,6 +1,6 @@ -# ArcFace C++部署示例 +# YOLOR C++部署示例 -本目录下提供`infer.cc`快速完成ArcFace在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 +本目录下提供`infer.cc`快速完成YOLOR在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 在部署前,需确认以下两个步骤 @@ -12,41 +12,41 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j -#下载官方转换好的ArcFace模型文件和测试图片 -wget https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r34.onnx -wget todo +#下载官方转换好的YOLOR模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/yolor-p6-paper-541-640-640.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg # CPU推理 -./infer_demo ms1mv3_arcface_r34.onnx todo 0 +./infer_demo yolor-p6-paper-541-640-640.onnx 000000014439.jpg 0 # GPU推理 -./infer_demo ms1mv3_arcface_r34.onnx todo 1 +./infer_demo yolor-p6-paper-541-640-640.onnx 000000014439.jpg 1 # GPU上TensorRT推理 -./infer_demo ms1mv3_arcface_r34.onnx todo 2 +./infer_demo yolor-p6-paper-541-640-640.onnx 000000014439.jpg 2 ``` 运行完成可视化结果如下图所示 - + -## ArcFace C++接口 +## YOLOR C++接口 -### ArcFace类 +### YOLOR类 ``` -fastdeploy::vision::faceid::ArcFace( +fastdeploy::vision::detection::YOLOR( const string& model_file, const string& params_file = "", const RuntimeOption& runtime_option = RuntimeOption(), const Frontend& model_format = Frontend::ONNX) ``` -ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式。 +YOLOR模型加载和初始化,其中model_file为导出的ONNX模型格式。 **参数** @@ -58,7 +58,7 @@ ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式 #### Predict函数 > ``` -> ArcFace::Predict(cv::Mat* im, DetectionResult* result, +> YOLOR::Predict(cv::Mat* im, DetectionResult* result, > float conf_threshold = 0.25, > float nms_iou_threshold = 0.5) > ``` @@ -73,6 +73,8 @@ ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] diff --git a/examples/vision/detection/yolor/cpp/infer.cc b/examples/vision/detection/yolor/cpp/infer.cc new file mode 100644 index 0000000000..0fe8913d47 --- /dev/null +++ b/examples/vision/detection/yolor/cpp/infer.cc @@ -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. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::detection::YOLOR(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::detection::YOLOR(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = fastdeploy::vision::detection::YOLOR(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout + << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model ./yolor-p6-paper-541-640-640.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/faceid/partial_fc/python/README.md b/examples/vision/detection/yolor/python/README.md similarity index 64% rename from examples/vision/faceid/partial_fc/python/README.md rename to examples/vision/detection/yolor/python/README.md index 6189e99c47..9fb737a01e 100644 --- a/examples/vision/faceid/partial_fc/python/README.md +++ b/examples/vision/detection/yolor/python/README.md @@ -1,41 +1,41 @@ -# PartialFC Python部署示例 +# YOLOR Python部署示例 在部署前,需确认以下两个步骤 - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) - 2. FastDeploy Python whl包安装,参考[FastDeploy Python安装](../../../../../docs/quick_start/install.md) -本目录下提供`infer.py`快速完成PartialFC在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 +本目录下提供`infer.py`快速完成YOLOR在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 ``` -#下载partial_fc模型文件和测试图片 -wget https://bj.bcebos.com/paddlehub/fastdeploy/partial_fc_glint360k_r50.onnx -wget todo +#下载YOLOR模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/yolor-p6-paper-541-640-640.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg #下载部署示例代码 git clone https://github.com/PaddlePaddle/FastDeploy.git -cd examples/vison/detection/partial_fc/python/ +cd examples/vison/detection/yolor/python/ # CPU推理 -python infer.py --model partial_fc_glint360k_r50.onnx --image todo --device cpu +python infer.py --model yolor-p6-paper-541-640-640.onnx --image 000000014439.jpg --device cpu # GPU推理 -python infer.py --model partial_fc_glint360k_r50.onnx --image todo --device gpu +python infer.py --model yolor-p6-paper-541-640-640.onnx --image 000000014439.jpg --device gpu # GPU上使用TensorRT推理 -python infer.py --model partial_fc_glint360k_r50.onnx --image todo --device gpu --use_trt True +python infer.py --model yolor-p6-paper-541-640-640.onnx --image 000000014439.jpg --device gpu --use_trt True ``` 运行完成可视化结果如下图所示 - + -## PartialFC Python接口 +## YOLOR Python接口 ``` -fastdeploy.vision.faceid.PartialFC(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +fastdeploy.vision.detection.YOLOR(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) ``` -PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式 +YOLOR模型加载和初始化,其中model_file为导出的ONNX模型格式 **参数** @@ -47,7 +47,7 @@ PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式 ### predict函数 > ``` -> PartialFC.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) +> YOLOR.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) > ``` > > 模型预测结口,输入图像直接输出检测结果。 @@ -63,6 +63,8 @@ PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式 > > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] @@ -74,6 +76,6 @@ PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式 ## 其它文档 -- [PartialFC 模型介绍](..) -- [PartialFC C++部署](../cpp) +- [YOLOR 模型介绍](..) +- [YOLOR C++部署](../cpp) - [模型预测结果说明](../../../../../docs/api/vision_results/) diff --git a/examples/vision/detection/yolor/python/infer.py b/examples/vision/detection/yolor/python/infer.py new file mode 100644 index 0000000000..ca0c9f453c --- /dev/null +++ b/examples/vision/detection/yolor/python/infer.py @@ -0,0 +1,52 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of yolor onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.detection.YOLOR(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) + +# 预测结果可视化 +vis_im = fd.vision.vis_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/detection/yolov5/README.md b/examples/vision/detection/yolov5/README.md index 0937a34ea5..e83dcdd504 100644 --- a/examples/vision/detection/yolov5/README.md +++ b/examples/vision/detection/yolov5/README.md @@ -2,14 +2,14 @@ ## 模型版本说明 -- YOLOv5 v6.0部署模型实现来自[YOLOv5 v6.0分支](https://github.com/ultralytics/yolov5/tree/v6.0),和[基于COCO的预训练模型](https://github.com/ultralytics/yolov5/releases/tag/v6.0) +- YOLOv5 v6.0部署模型实现来自[YOLOv5](https://github.com/ultralytics/yolov5/tree/v6.0),和[基于COCO的预训练模型](https://github.com/ultralytics/yolov5/releases/tag/v6.0) - (1)[预训练模型](https://github.com/ultralytics/yolov5/releases/tag/v6.0)的*.onnx可直接进行部署; - (2)开发者基于自己数据训练的YOLOv5 v6.0模型,可使用[YOLOv5](https://github.com/ultralytics/yolov5)中的`export.py`导出ONNX文件后后,完成部署。 ## 下载预训练ONNX模型 -为了方便开发者的测试,下面提供了YOLOv7导出的各系列模型,开发者可直接下载使用。 +为了方便开发者的测试,下面提供了YOLOv5导出的各系列模型,开发者可直接下载使用。 | 模型 | 大小 | 精度 | |:---------------------------------------------------------------- |:----- |:----- | @@ -26,3 +26,8 @@ - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[YOLOv5 v6.0](https://github.com/ultralytics/yolov5/tree/v6.0) 编写 diff --git a/examples/vision/detection/yolov5/cpp/README.md b/examples/vision/detection/yolov5/cpp/README.md index feb44d13df..998046e096 100644 --- a/examples/vision/detection/yolov5/cpp/README.md +++ b/examples/vision/detection/yolov5/cpp/README.md @@ -12,7 +12,7 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j @@ -32,7 +32,7 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000 运行完成可视化结果如下图所示 - + ## YOLOv5 C++接口 @@ -73,6 +73,8 @@ YOLOv5模型加载和初始化,其中model_file为导出的ONNX模型格式。 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] diff --git a/examples/vision/detection/yolov5/cpp/infer.cc b/examples/vision/detection/yolov5/cpp/infer.cc index ef3e47ea1f..a7ac1fe813 100644 --- a/examples/vision/detection/yolov5/cpp/infer.cc +++ b/examples/vision/detection/yolov5/cpp/infer.cc @@ -29,6 +29,7 @@ void CpuInfer(const std::string& model_file, const std::string& image_file) { std::cerr << "Failed to predict." << std::endl; return; } + std::cout << res.Str() << std::endl; auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); cv::imwrite("vis_result.jpg", vis_im); @@ -52,6 +53,7 @@ void GpuInfer(const std::string& model_file, const std::string& image_file) { std::cerr << "Failed to predict." << std::endl; return; } + std::cout << res.Str() << std::endl; auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); cv::imwrite("vis_result.jpg", vis_im); @@ -77,6 +79,7 @@ void TrtInfer(const std::string& model_file, const std::string& image_file) { std::cerr << "Failed to predict." << std::endl; return; } + std::cout << res.Str() << std::endl; auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); cv::imwrite("vis_result.jpg", vis_im); diff --git a/examples/vision/detection/yolov5/python/README.md b/examples/vision/detection/yolov5/python/README.md index 57cdba44cb..8048d7b7ca 100644 --- a/examples/vision/detection/yolov5/python/README.md +++ b/examples/vision/detection/yolov5/python/README.md @@ -27,7 +27,7 @@ python infer.py --model yolov5s.onnx --image 000000014439.jpg --device gpu --use 运行完成可视化结果如下图所示 - + ## YOLOv5 Python接口 @@ -63,6 +63,8 @@ YOLOv5模型加载和初始化,其中model_file为导出的ONNX模型格式 > > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] diff --git a/examples/vision/detection/yolov5/python/infer.py b/examples/vision/detection/yolov5/python/infer.py index 3f7a91f99d..2cf3a03849 100644 --- a/examples/vision/detection/yolov5/python/infer.py +++ b/examples/vision/detection/yolov5/python/infer.py @@ -43,7 +43,8 @@ def build_option(args): # 预测图片检测结果 im = cv2.imread(args.image) -result = model.predict(im) +result = model.predict(im.copy()) +print(result) # 预测结果可视化 vis_im = fd.vision.vis_detection(im, result) diff --git a/examples/vision/detection/yolov5lite/README.md b/examples/vision/detection/yolov5lite/README.md new file mode 100644 index 0000000000..4b95967d1d --- /dev/null +++ b/examples/vision/detection/yolov5lite/README.md @@ -0,0 +1,72 @@ +# YOLOv5Lite准备部署模型 + +- YOLOv5Lite部署实现来自[YOLOv5-Lite](https://github.com/ppogg/YOLOv5-Lite/releases/tag/v1.4) +代码,和[基于COCO的预训练模型](https://github.com/ppogg/YOLOv5-Lite/releases/tag/v1.4)。 + + - (1)[预训练模型](https://github.com/ppogg/YOLOv5-Lite/releases/tag/v1.4)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署;*.onnx、*.trt和*.pose模型不支持部署; + - (2)自己数据训练的YOLOv5Lite模型,按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)操作后,参考[详细部署文档](#详细部署文档)完成部署。 + + +## 导出ONNX模型 + +- 自动获取 + 访问[YOLOv5Lite](https://github.com/ppogg/YOLOv5-Lite) +官方github库,按照指引下载安装,下载`yolov5-lite-xx.onnx` 模型(Tips:官方提供的ONNX文件目前是没有decode模块的) + ``` + #下载yolov5-lite模型文件(.onnx) + Download from https://drive.google.com/file/d/1bJByk9eoS6pv8Z3N4bcLRCV3i7uk24aU/view + 官方Repo也支持百度云下载 + ``` + +- 手动获取 + + 访问[YOLOv5Lite](https://github.com/ppogg/YOLOv5-Lite) +官方github库,按照指引下载安装,下载`yolov5-lite-xx.pt` 模型,利用 `export.py` 得到`onnx`格式文件。 + + - 导出含有decode模块的ONNX文件 + + 首先需要参考[YOLOv5-Lite#189](https://github.com/ppogg/YOLOv5-Lite/pull/189)的解决办法,修改代码。 + + ``` + #下载yolov5-lite模型文件(.pt) + Download from https://drive.google.com/file/d/1oftzqOREGqDCerf7DtD5BZp9YWELlkMe/view + 官方Repo也支持百度云下载 + + # 导出onnx格式文件 + python export.py --grid --dynamic --concat --weights PATH/TO/yolov5-lite-xx.pt + + + ``` + - 导出无decode模块的ONNX文件(不需要修改代码) + + ``` + #下载yolov5-lite模型文件 + Download from https://drive.google.com/file/d/1oftzqOREGqDCerf7DtD5BZp9YWELlkMe/view + 官方Repo也支持百度云下载 + + # 导出onnx格式文件 + python export.py --grid --dynamic --weights PATH/TO/yolov5-lite-xx.pt + + ``` + +## 下载预训练ONNX模型 + +为了方便开发者的测试,下面提供了YOLOv5Lite导出的各系列模型,开发者可直接下载使用。 + +| 模型 | 大小 | 精度 | +|:---------------------------------------------------------------- |:----- |:----- | +| [YOLOv5Lite-e](https://bj.bcebos.com/paddlehub/fastdeploy/v5Lite-e-sim-320.onnx) | 3.1MB | 35.1% | +| [YOLOv5Lite-s](https://bj.bcebos.com/paddlehub/fastdeploy/v5Lite-s-sim-416.onnx) | 6.3MB | 42.0% | +| [YOLOv5Lite-c](https://bj.bcebos.com/paddlehub/fastdeploy/v5Lite-c-sim-512.onnxx) | 18MB | 50.9% | +| [YOLOv5Lite-g](https://bj.bcebos.com/paddlehub/fastdeploy/v5Lite-g-sim-640.onnx) | 21MB | 57.6% | + + +## 详细部署文档 + +- [Python部署](python) +- [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[YOLOv5-Lite v1.4](https://github.com/ppogg/YOLOv5-Lite/releases/tag/v1.4) 编写 diff --git a/examples/vision/detection/yolov5lite/cpp/CMakeLists.txt b/examples/vision/detection/yolov5lite/cpp/CMakeLists.txt new file mode 100644 index 0000000000..fea1a2888b --- /dev/null +++ b/examples/vision/detection/yolov5lite/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +PROJECT(infer_demo C CXX) +CMAKE_MINIMUM_REQUIRED (VERSION 3.12) + +# 指定下载解压后的fastdeploy库路径 +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) + +# 添加FastDeploy依赖头文件 +include_directories(${FASTDEPLOY_INCS}) + +add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc) +# 添加FastDeploy库依赖 +target_link_libraries(infer_demo ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/faceid/partial_fc/cpp/README.md b/examples/vision/detection/yolov5lite/cpp/README.md similarity index 72% rename from examples/vision/faceid/partial_fc/cpp/README.md rename to examples/vision/detection/yolov5lite/cpp/README.md index 20a2f0eb6e..88d6accf4c 100644 --- a/examples/vision/faceid/partial_fc/cpp/README.md +++ b/examples/vision/detection/yolov5lite/cpp/README.md @@ -1,6 +1,6 @@ -# PartialFC C++部署示例 +# YOLOv5Lite C++部署示例 -本目录下提供`infer.cc`快速完成PartialFC在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 +本目录下提供`infer.cc`快速完成YOLOv5Lite在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 在部署前,需确认以下两个步骤 @@ -12,41 +12,41 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j -#下载官方转换好的PartialFC模型文件和测试图片 -wget https://bj.bcebos.com/paddlehub/fastdeploy/partial_fc_glint360k_r50.onnx -wget todo +#下载官方转换好的YOLOv5Lite模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/v5Lite-g-sim-640.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg # CPU推理 -./infer_demo partial_fc_glint360k_r50.onnx todo 0 +./infer_demo v5Lite-g-sim-640.onnx 000000014439.jpg 0 # GPU推理 -./infer_demo partial_fc_glint360k_r50.onnx todo 1 +./infer_demo v5Lite-g-sim-640.onnx 000000014439.jpg 1 # GPU上TensorRT推理 -./infer_demo partial_fc_glint360k_r50.onnx todo 2 +./infer_demo v5Lite-g-sim-640.onnx 000000014439.jpg 2 ``` 运行完成可视化结果如下图所示 - + -## PartialFC C++接口 +## YOLOv5Lite C++接口 -### PartialFC类 +### YOLOv5Lite类 ``` -fastdeploy::vision::faceid::PartialFC( +fastdeploy::vision::detection::YOLOv5Lite( const string& model_file, const string& params_file = "", const RuntimeOption& runtime_option = RuntimeOption(), const Frontend& model_format = Frontend::ONNX) ``` -PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式。 +YOLOv5Lite模型加载和初始化,其中model_file为导出的ONNX模型格式。 **参数** @@ -58,7 +58,7 @@ PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式 #### Predict函数 > ``` -> PartialFC::Predict(cv::Mat* im, DetectionResult* result, +> YOLOv5Lite::Predict(cv::Mat* im, DetectionResult* result, > float conf_threshold = 0.25, > float nms_iou_threshold = 0.5) > ``` @@ -73,6 +73,8 @@ PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] diff --git a/examples/vision/detection/yolov5lite/cpp/infer.cc b/examples/vision/detection/yolov5lite/cpp/infer.cc new file mode 100644 index 0000000000..ac32bca933 --- /dev/null +++ b/examples/vision/detection/yolov5lite/cpp/infer.cc @@ -0,0 +1,110 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::detection::YOLOv5Lite(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = + fastdeploy::vision::detection::YOLOv5Lite(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = + fastdeploy::vision::detection::YOLOv5Lite(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model ./v5Lite-g-sim-640.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/faceid/arcface/python/README.md b/examples/vision/detection/yolov5lite/python/README.md similarity index 64% rename from examples/vision/faceid/arcface/python/README.md rename to examples/vision/detection/yolov5lite/python/README.md index 034b93049e..01e1a90dab 100644 --- a/examples/vision/faceid/arcface/python/README.md +++ b/examples/vision/detection/yolov5lite/python/README.md @@ -1,41 +1,41 @@ -# ArcFace Python部署示例 +# YOLOv5Lite Python部署示例 在部署前,需确认以下两个步骤 - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) - 2. FastDeploy Python whl包安装,参考[FastDeploy Python安装](../../../../../docs/quick_start/install.md) -本目录下提供`infer.py`快速完成ArcFace在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 +本目录下提供`infer.py`快速完成YOLOv5Lite在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 ``` -#下载arcface模型文件和测试图片 -wget https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r34.onnx -wget todo +#下载YOLOv5Lite模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/v5Lite-g-sim-640.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg #下载部署示例代码 git clone https://github.com/PaddlePaddle/FastDeploy.git -cd examples/vison/detection/arcface/python/ +cd examples/vison/detection/yolov5lite/python/ # CPU推理 -python infer.py --model ms1mv3_arcface_r34.onnx --image todo --device cpu +python infer.py --model v5Lite-g-sim-640.onnx --image 000000014439.jpg --device cpu # GPU推理 -python infer.py --model ms1mv3_arcface_r34.onnx --image todo --device gpu +python infer.py --model v5Lite-g-sim-640.onnx --image 000000014439.jpg --device gpu # GPU上使用TensorRT推理 -python infer.py --model ms1mv3_arcface_r34.onnx --image todo --device gpu --use_trt True +python infer.py --model v5Lite-g-sim-640.onnx --image 000000014439.jpg --device gpu --use_trt True ``` 运行完成可视化结果如下图所示 - + -## ArcFace Python接口 +## YOLOv5Lite Python接口 ``` -fastdeploy.vision.faceid.ArcFace(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +fastdeploy.vision.detection.YOLOv5Lite(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) ``` -ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式 +YOLOv5Lite模型加载和初始化,其中model_file为导出的ONNX模型格式 **参数** @@ -47,7 +47,7 @@ ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式 ### predict函数 > ``` -> ArcFace.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) +> YOLOv5Lite.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) > ``` > > 模型预测结口,输入图像直接输出检测结果。 @@ -63,6 +63,8 @@ ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式 > > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] @@ -74,6 +76,6 @@ ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式 ## 其它文档 -- [ArcFace 模型介绍](..) -- [ArcFace C++部署](../cpp) +- [YOLOv5Lite 模型介绍](..) +- [YOLOv5Lite C++部署](../cpp) - [模型预测结果说明](../../../../../docs/api/vision_results/) diff --git a/examples/vision/detection/yolov5lite/python/infer.py b/examples/vision/detection/yolov5lite/python/infer.py new file mode 100644 index 0000000000..0a4312c451 --- /dev/null +++ b/examples/vision/detection/yolov5lite/python/infer.py @@ -0,0 +1,53 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of yolov5lite onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.detection.YOLOv5Lite( + args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) + +# 预测结果可视化 +vis_im = fd.vision.vis_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/detection/yolov6/README.md b/examples/vision/detection/yolov6/README.md index f19f697b2f..497778fe0f 100644 --- a/examples/vision/detection/yolov6/README.md +++ b/examples/vision/detection/yolov6/README.md @@ -2,9 +2,11 @@ ## 模型版本说明 -- YOLOv6 部署实现来自[YOLOv6 0.1分支](https://github.com/meituan/YOLOv6/releases/download/0.1.0),和[基于coco的预训练模型](https://github.com/meituan/YOLOv6/releases/tag/0.1.0)。 +- YOLOv6 部署实现来自[YOLOv6](https://github.com/meituan/YOLOv6/releases/tag/0.1.0),和[基于coco的预训练模型](https://github.com/meituan/YOLOv6/releases/tag/0.1.0)。 + + - (1)[基于coco的预训练模型](https://github.com/meituan/YOLOv6/releases/tag/0.1.0)的*.onnx可直接进行部署; + - (2)自己训练的模型,导出ONNX模型后,参考[详细部署文档](#详细部署文档)完成部署。 - - (1)[基于coco的预训练模型](https://github.com/meituan/YOLOv6/releases/download/0.1.0)的*.onnx可直接进行部署; ## 下载预训练ONNX模型 @@ -15,6 +17,8 @@ |:---------------------------------------------------------------- |:----- |:----- | | [YOLOv6s](https://bj.bcebos.com/paddlehub/fastdeploy/yolov6s.onnx) | 66MB | 43.1% | | [YOLOv6s_640](https://bj.bcebos.com/paddlehub/fastdeploy/yolov6s-640x640.onnx) | 66MB | 43.1% | +| [YOLOv6t](https://bj.bcebos.com/paddlehub/fastdeploy/yolov6t.onnx) | 58MB | 41.3% | +| [YOLOv6n](https://bj.bcebos.com/paddlehub/fastdeploy/yolov6n.onnx) | 17MB | 35.0% | @@ -22,3 +26,8 @@ - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[YOLOv6 0.1.0版本](https://github.com/meituan/YOLOv6/releases/download/0.1.0) 编写 diff --git a/examples/vision/detection/yolov6/cpp/README.md b/examples/vision/detection/yolov6/cpp/README.md index 5a73f8b55e..0d66f97646 100644 --- a/examples/vision/detection/yolov6/cpp/README.md +++ b/examples/vision/detection/yolov6/cpp/README.md @@ -12,7 +12,7 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j @@ -32,7 +32,7 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000 运行完成可视化结果如下图所示 - + ## YOLOv6 C++接口 @@ -73,12 +73,14 @@ YOLOv6模型加载和初始化,其中model_file为导出的ONNX模型格式。 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] > > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` > > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` -> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **stride**(int): 配合`is_mini_pad`成员变量使用, 默认值为`stride=32` - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/detection/yolov6/cpp/infer.cc b/examples/vision/detection/yolov6/cpp/infer.cc new file mode 100644 index 0000000000..72b2e7bede --- /dev/null +++ b/examples/vision/detection/yolov6/cpp/infer.cc @@ -0,0 +1,108 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::detection::YOLOv6(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::detection::YOLOv6(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = fastdeploy::vision::detection::YOLOv6(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model ./yolov6s.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/detection/yolov6/python/README.md b/examples/vision/detection/yolov6/python/README.md index 35c35b2084..2dfd11a4b2 100644 --- a/examples/vision/detection/yolov6/python/README.md +++ b/examples/vision/detection/yolov6/python/README.md @@ -27,7 +27,7 @@ python infer.py --model yolov6s.onnx --image 000000014439.jpg --device gpu --use 运行完成可视化结果如下图所示 - + ## YOLOv6 Python接口 @@ -63,12 +63,15 @@ YOLOv6模型加载和初始化,其中model_file为导出的ONNX模型格式 > > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] > > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` > > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` -> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` +> > * **stride**(int): 配合`is_mini_padide`成员变量使用, 默认值为`stride=32` diff --git a/examples/vision/detection/yolov6/python/infer.py b/examples/vision/detection/yolov6/python/infer.py new file mode 100644 index 0000000000..47ea4cd3a4 --- /dev/null +++ b/examples/vision/detection/yolov6/python/infer.py @@ -0,0 +1,52 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of yolov6 onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.detection.YOLOv6(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) + +# 预测结果可视化 +vis_im = fd.vision.vis_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/detection/yolov7/README.md b/examples/vision/detection/yolov7/README.md index e089051e65..266aeace2d 100644 --- a/examples/vision/detection/yolov7/README.md +++ b/examples/vision/detection/yolov7/README.md @@ -1,10 +1,11 @@ # YOLOv7准备部署模型 -- YOLOv7部署实现来自[YOLOv7 0.1](https://github.com/WongKinYiu/yolov7/tree/v0.1)分支代码,和[基于COCO的预训练模型](https://github.com/WongKinYiu/yolov7/releases/tag/v0.1)。 +- YOLOv7部署实现来自[YOLOv7](https://github.com/WongKinYiu/yolov7/tree/v0.1)分支代码,和[基于COCO的预训练模型](https://github.com/WongKinYiu/yolov7/releases/tag/v0.1)。 - (1)[预训练模型](https://github.com/WongKinYiu/yolov7/releases/tag/v0.1)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署;*.onnx、*.trt和*.pose模型不支持部署; - (2)自己数据训练的YOLOv7 0.1模型,按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)操作后,参考[详细部署文档](#详细部署文档)完成部署。 + ## 导出ONNX模型 ``` @@ -17,8 +18,7 @@ python models/export.py --grid --dynamic --weights PATH/TO/yolov7.pt # 如果您的代码版本中有支持NMS的ONNX文件导出,请使用如下命令导出ONNX文件(请暂时不要使用 "--end2end",我们后续将支持带有NMS的ONNX模型的部署) python models/export.py --grid --dynamic --weights PATH/TO/yolov7.pt -# 移动onnx文件到demo目录 -cp PATH/TO/yolov7.onnx PATH/TO/model_zoo/vision/yolov7/ + ``` ## 下载预训练ONNX模型 @@ -41,3 +41,8 @@ cp PATH/TO/yolov7.onnx PATH/TO/model_zoo/vision/yolov7/ - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[YOLOv7 0.1](https://github.com/WongKinYiu/yolov7/tree/v0.1) 编写 diff --git a/examples/vision/detection/yolov7/cpp/README.md b/examples/vision/detection/yolov7/cpp/README.md index c67689570d..27bfa86ae7 100644 --- a/examples/vision/detection/yolov7/cpp/README.md +++ b/examples/vision/detection/yolov7/cpp/README.md @@ -12,7 +12,7 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j @@ -73,6 +73,8 @@ YOLOv7模型加载和初始化,其中model_file为导出的ONNX模型格式。 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] diff --git a/examples/vision/detection/yolov7/cpp/infer.cc b/examples/vision/detection/yolov7/cpp/infer.cc index 1ddca8f1c8..cf79a16ad7 100644 --- a/examples/vision/detection/yolov7/cpp/infer.cc +++ b/examples/vision/detection/yolov7/cpp/infer.cc @@ -29,6 +29,7 @@ void CpuInfer(const std::string& model_file, const std::string& image_file) { std::cerr << "Failed to predict." << std::endl; return; } + std::cout << res.Str() << std::endl; auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); cv::imwrite("vis_result.jpg", vis_im); @@ -52,6 +53,7 @@ void GpuInfer(const std::string& model_file, const std::string& image_file) { std::cerr << "Failed to predict." << std::endl; return; } + std::cout << res.Str() << std::endl; auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); cv::imwrite("vis_result.jpg", vis_im); @@ -77,6 +79,7 @@ void TrtInfer(const std::string& model_file, const std::string& image_file) { std::cerr << "Failed to predict." << std::endl; return; } + std::cout << res.Str() << std::endl; auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); cv::imwrite("vis_result.jpg", vis_im); diff --git a/examples/vision/detection/yolov7/python/README.md b/examples/vision/detection/yolov7/python/README.md index b3a4f12a1b..f28bef0afd 100644 --- a/examples/vision/detection/yolov7/python/README.md +++ b/examples/vision/detection/yolov7/python/README.md @@ -63,6 +63,8 @@ YOLOv7模型加载和初始化,其中model_file为导出的ONNX模型格式 > > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] diff --git a/examples/vision/detection/yolov7/python/infer.py b/examples/vision/detection/yolov7/python/infer.py index 574755c3a3..aff8cbe304 100644 --- a/examples/vision/detection/yolov7/python/infer.py +++ b/examples/vision/detection/yolov7/python/infer.py @@ -43,7 +43,7 @@ def build_option(args): # 预测图片检测结果 im = cv2.imread(args.image) -result = model.predict(im) +result = model.predict(im.copy()) # 预测结果可视化 vis_im = fd.vision.vis_detection(im, result) diff --git a/examples/vision/detection/yolox/README.md b/examples/vision/detection/yolox/README.md index 69e4f8666a..193089904b 100644 --- a/examples/vision/detection/yolox/README.md +++ b/examples/vision/detection/yolox/README.md @@ -1,18 +1,23 @@ # YOLOX准备部署模型 ## 模型版本说明 -- YOLOX部署实现来自[YOLOX v0.1.1分支](https://github.com/Megvii-BaseDetection/YOLOX/tree/0.1.1rc0),基于[coco的预训练模型](https://github.com/Megvii-BaseDetection/YOLOX/releases/tag/0.1.1rc0)。 + +- YOLOX部署实现来自[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/tree/0.1.1rc0),基于[coco的预训练模型](https://github.com/Megvii-BaseDetection/YOLOX/releases/tag/0.1.1rc0)。 - (1)[预训练模型](https://github.com/Megvii-BaseDetection/YOLOX/releases/tag/0.1.1rc0)中的*.pth通过导出ONNX模型操作后,可进行部署;*.onnx、*.trt和*.pose模型不支持部署; - (2)开发者基于自己数据训练的YOLOX v0.1.1模型,可按照导出ONNX模型后,完成部署。 + ## 下载预训练ONNX模型 为了方便开发者的测试,下面提供了YOLOX导出的各系列模型,开发者可直接下载使用。 | 模型 | 大小 | 精度 | |:---------------------------------------------------------------- |:----- |:----- | -| [YOLOX-s](https://bj.bcebos.com/paddlehub/fastdeploy/yolox_s.onnx) | 35MB | 40.5% | +| [YOLOX-s](https://bj.bcebos.com/paddlehub/fastdeploy/yolox_s.onnx) | 35MB | 39.6% | +| [YOLOX-m](https://bj.bcebos.com/paddlehub/fastdeploy/yolox_m.onnx) | 97MB | 46.4.5% | +| [YOLOX-l](https://bj.bcebos.com/paddlehub/fastdeploy/yolox_tiny.onnx) | 207MB | 50.0% | +| [YOLOX-x](https://bj.bcebos.com/paddlehub/fastdeploy/yolox_x.onnx) | 378MB | 51.2% | @@ -21,3 +26,8 @@ - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[YOLOX v0.1.1版本](https://github.com/Megvii-BaseDetection/YOLOX/tree/0.1.1rc0) 编写 diff --git a/examples/vision/detection/yolox/cpp/README.md b/examples/vision/detection/yolox/cpp/README.md index abe7611266..2094eddd0e 100644 --- a/examples/vision/detection/yolox/cpp/README.md +++ b/examples/vision/detection/yolox/cpp/README.md @@ -12,7 +12,7 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j @@ -32,7 +32,7 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000 运行完成可视化结果如下图所示 - + ## YOLOX C++接口 @@ -73,12 +73,13 @@ YOLOX模型加载和初始化,其中model_file为导出的ONNX模型格式。 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] > > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` -> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **is_decode_exported**(bool): 表示导出的YOLOX的onnx模型文件是否带坐标反算的decode部分, 默认值为`is_decode_exported=false`,官方默认的导出不带decode部分,如果您导出的模型带了decode,请将此参数设置为true - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/detection/yolox/cpp/infer.cc b/examples/vision/detection/yolox/cpp/infer.cc new file mode 100644 index 0000000000..2eeaccbf89 --- /dev/null +++ b/examples/vision/detection/yolox/cpp/infer.cc @@ -0,0 +1,108 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::detection::YOLOX(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::detection::YOLOX(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = fastdeploy::vision::detection::YOLOX(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model ./yolox_s.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/detection/yolox/python/README.md b/examples/vision/detection/yolox/python/README.md index 8c4fff5f3f..dfa3058a19 100644 --- a/examples/vision/detection/yolox/python/README.md +++ b/examples/vision/detection/yolox/python/README.md @@ -26,7 +26,7 @@ python infer.py --model yolox_s.onnx --image 000000014439.jpg --device gpu --use 运行完成可视化结果如下图所示 - + ## YOLOX Python接口 @@ -62,12 +62,12 @@ YOLOX模型加载和初始化,其中model_file为导出的ONNX模型格式 > > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 -> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` -> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` +> >* **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] +> >* **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] +> >* **is_decode_exported**(bool): 表示导出的YOLOX的onnx模型文件是否带坐标反算的decode部分, 默认值为`is_decode_exported=False`,官方默认的导出不带decode部分,如果您导出的模型带了decode,请将此参数设置为True diff --git a/examples/vision/detection/yolox/python/infer.py b/examples/vision/detection/yolox/python/infer.py new file mode 100644 index 0000000000..8af3d9c4c3 --- /dev/null +++ b/examples/vision/detection/yolox/python/infer.py @@ -0,0 +1,51 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of yolox onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.detection.YOLOX(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) +# 预测结果可视化 +vis_im = fd.vision.vis_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/facedet/README.md b/examples/vision/facedet/README.md index fb3b3dfdfe..cd062e26b1 100644 --- a/examples/vision/facedet/README.md +++ b/examples/vision/facedet/README.md @@ -1,10 +1,10 @@ 人脸检测模型 -FastDeploy目前支持如下人脸模型部署 +FastDeploy目前支持如下人脸检测模型部署 | 模型 | 说明 | 模型格式 | 版本 | | :--- | :--- | :------- | :--- | -| [retinaface]() | PPYOLOE系列模型 | ONNX | | -| [ultraface]() | PicoDet系列模型 | ONNX || -| [yolov5face]() | Paddle版本的YOLOX系列模型 | ONNX | | - +| [retinaface](./retinaface) | RetinaFace系列模型 | ONNX | CommitID:b984b4b | +| [ultraface](./ultraface) | UltraFace系列模型 | ONNX |CommitID:dffdddd | +| [yolov5face](./yolov5face) | YOLOv5Face系列模型 | ONNX | CommitID:4fd1ead | +| [scrfd](./scrfd) | SCRFD系列模型 | ONNX | CommitID:17cdeab | diff --git a/examples/vision/facedet/retinaface/README.md b/examples/vision/facedet/retinaface/README.md index 2d1616cda4..525b6cb2f2 100644 --- a/examples/vision/facedet/retinaface/README.md +++ b/examples/vision/facedet/retinaface/README.md @@ -2,7 +2,7 @@ ## 模型版本说明 -- [RetinaFace CommitID:b984b4b](https://github.com/biubug6/Pytorch_Retinaface/commit/b984b4b) +- [RetinaFace](https://github.com/biubug6/Pytorch_Retinaface/commit/b984b4b) - (1)[链接中](https://github.com/biubug6/Pytorch_Retinaface/commit/b984b4b)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署; - (2)自己数据训练的RetinaFace CommitID:b984b4b模型,可按照[导出ONNX模型](#导出ONNX模型)后,完成部署。 @@ -10,6 +10,7 @@ [下载预训练ONNX模型](#下载预训练ONNX模型)已事先转换成ONNX;如果从RetinaFace官方repo下载的模型,需要按如下教程导出ONNX。 + * 下载官方仓库并 ```bash git clone https://github.com/biubug6/Pytorch_Retinaface.git @@ -52,3 +53,8 @@ onnxsim FaceDetector.onnx Pytorch_RetinaFace_resnet50-640-640.onnx # resnet50 - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[RetinaFace CommitID:b984b4b](https://github.com/biubug6/Pytorch_Retinaface/commit/b984b4b) 编写 diff --git a/examples/vision/facedet/retinaface/cpp/README.md b/examples/vision/facedet/retinaface/cpp/README.md index dc36657076..0b072c04d0 100644 --- a/examples/vision/facedet/retinaface/cpp/README.md +++ b/examples/vision/facedet/retinaface/cpp/README.md @@ -12,27 +12,27 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j #下载官方转换好的RetinaFace模型文件和测试图片 wget https://bj.bcebos.com/paddlehub/fastdeploy/Pytorch_RetinaFace_mobile0.25-640-640.onnx -wget todo +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg # CPU推理 -./infer_demo Pytorch_RetinaFace_mobile0.25-640-640.onnx todo 0 +./infer_demo Pytorch_RetinaFace_mobile0.25-640-640.onnx test_lite_face_detector_3.jpg 0 # GPU推理 -./infer_demo Pytorch_RetinaFace_mobile0.25-640-640.onnx todo 1 +./infer_demo Pytorch_RetinaFace_mobile0.25-640-640.onnx test_lite_face_detector_3.jpg 1 # GPU上TensorRT推理 -./infer_demo Pytorch_RetinaFace_mobile0.25-640-640.onnx todo 2 +./infer_demo Pytorch_RetinaFace_mobile0.25-640-640.onnx test_lite_face_detector_3.jpg 2 ``` 运行完成可视化结果如下图所示 - + ## RetinaFace C++接口 @@ -58,7 +58,7 @@ RetinaFace模型加载和初始化,其中model_file为导出的ONNX模型格 #### Predict函数 > ``` -> RetinaFace::Predict(cv::Mat* im, DetectionResult* result, +> RetinaFace::Predict(cv::Mat* im, FaceDetectionResult* result, > float conf_threshold = 0.25, > float nms_iou_threshold = 0.5) > ``` @@ -68,17 +68,19 @@ RetinaFace模型加载和初始化,其中model_file为导出的ONNX模型格 > **参数** > > > * **im**: 输入图像,注意需为HWC,BGR格式 -> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceDetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) > > * **conf_threshold**: 检测框置信度过滤阈值 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` -> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **variance**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[0, 0, 0] +> > * **min_sizes**(vector<vector<int>>): retinaface中的anchor的宽高设置,默认是 {{16, 32}, {64, 128}, {256, 512}},分别和步长8、16和32对应 +> > * **downsample_strides**(vector<int>): 通过此参数可以修改生成anchor的特征图的下采样倍数, 包含三个整型元素, 分别表示默认的生成anchor的下采样倍数, 默认值为[8, 16, 32] +> > * **landmarks_per_face**(int): 指定当前模型检测的人脸所带的关键点个数,默认为5. - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/facedet/retinaface/cpp/infer.cc b/examples/vision/facedet/retinaface/cpp/infer.cc new file mode 100644 index 0000000000..a1fd27b6e8 --- /dev/null +++ b/examples/vision/facedet/retinaface/cpp/infer.cc @@ -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. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::facedet::RetinaFace(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::facedet::RetinaFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = fastdeploy::vision::facedet::RetinaFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model Pytorch_RetinaFace_mobile0.25-640-640.onnx " + "./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/facedet/retinaface/python/README.md b/examples/vision/facedet/retinaface/python/README.md index b8c3251359..815ddf234e 100644 --- a/examples/vision/facedet/retinaface/python/README.md +++ b/examples/vision/facedet/retinaface/python/README.md @@ -10,24 +10,25 @@ ``` #下载retinaface模型文件和测试图片 wget https://bj.bcebos.com/paddlehub/fastdeploy/Pytorch_RetinaFace_mobile0.25-640-640.onnx -wget todo +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg #下载部署示例代码 git clone https://github.com/PaddlePaddle/FastDeploy.git -cd examples/vison/detection/retinaface/python/ +cd examples/vison//retinaface/python/ # CPU推理 -python infer.py --model Pytorch_RetinaFace_mobile0.25-640-640.onnx --image todo --device cpu + +python infer.py --model Pytorch_RetinaFace_mobile0.25-640-640.onnx --image test_lite_face_detector_3.jpg --device cpu # GPU推理 -python infer.py --model Pytorch_RetinaFace_mobile0.25-640-640.onnx --image todo --device gpu +python infer.py --model Pytorch_RetinaFace_mobile0.25-640-640.onnx --image test_lite_face_detector_3.jpg --device gpu # GPU上使用TensorRT推理 -python infer.py --model Pytorch_RetinaFace_mobile0.25-640-640.onnx --image todo --device gpu --use_trt True +python infer.py --model Pytorch_RetinaFace_mobile0.25-640-640.onnx --image test_lite_face_detector_3.jpg --device gpu --use_trt True ``` 运行完成可视化结果如下图所示 - + ## RetinaFace Python接口 @@ -60,15 +61,17 @@ RetinaFace模型加载和初始化,其中model_file为导出的ONNX模型格 > **返回** > -> > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > 返回`fastdeploy.vision.FaceDetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` -> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` +> > * **variance**(list[float]): 通过此参数可以指定retinaface中的方差variance值,默认是[0.1,0.2], 一般不用修改. +> > * **min_sizes**(list[list[int]]): retinaface中的anchor的宽高设置,默认是 {{16, 32}, {64, 128}, {256, 512}},分别和步长8、16和32对应 +> > * **downsample_strides**(list[int]): 通过此参数可以修改生成anchor的特征图的下采样倍数, 包含三个整型元素, 分别表示默认的生成anchor的下采样倍数, 默认值为[8, 16, 32] +> > * **landmarks_per_face**(int): 指定当前模型检测的人脸所带的关键点个数,默认为5. diff --git a/examples/vision/facedet/retinaface/python/infer.py b/examples/vision/facedet/retinaface/python/infer.py new file mode 100644 index 0000000000..3b0152b1c9 --- /dev/null +++ b/examples/vision/facedet/retinaface/python/infer.py @@ -0,0 +1,51 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of retinaface onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.facedet.RetinaFace(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) +# 预测结果可视化 +vis_im = fd.vision.vis_face_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/facedet/scrfd/README.md b/examples/vision/facedet/scrfd/README.md new file mode 100644 index 0000000000..d1694d2c1c --- /dev/null +++ b/examples/vision/facedet/scrfd/README.md @@ -0,0 +1,71 @@ +# SCRFD准备部署模型 + +## 模型版本说明 + +- [SCRFD](https://github.com/deepinsight/insightface/tree/17cdeab12a35efcebc2660453a8cbeae96e20950) + - (1)[链接中](https://github.com/deepinsight/insightface/tree/17cdeab12a35efcebc2660453a8cbeae96e20950)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署; + - (2)开发者基于自己数据训练的SCRFD CID:17cdeab模型,可按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)后,完成部署。 + +## 导出ONNX模型 + + ``` + #下载scrfd模型文件 + e.g. download from https://onedrive.live.com/?authkey=%21ABbFJx2JMhNjhNA&id=4A83B6B633B029CC%215542&cid=4A83B6B633B029CC + + # 安装官方库配置环境,此版本导出环境为: + - 手动配置环境 + torch==1.8.0 + mmcv==1.3.5 + mmdet==2.7.0 + + - 通过docker配置 + docker pull qyjdefdocker/onnx-scrfd-converter:v0.3 + + # 导出onnx格式文件 + - 手动生成 + python tools/scrfd2onnx.py configs/scrfd/scrfd_500m.py weights/scrfd_500m.pth --shape 640 --input-img face-xxx.jpg + + - docker + docker的onnx目录中已有生成好的onnx文件 + + ``` + +## 下载预训练ONNX模型 + +为了方便开发者的测试,下面提供了SCRFD导出的各系列模型,开发者可直接下载使用。 + +| 模型 | 大小 | 精度 | +|:---------------------------------------------------------------- |:----- |:----- | +| [SCRFD-500M-kps-160](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_bnkps_shape160x160.onnx) | 2.5MB | - | +| [SCRFD-500M-160](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_shape160x160.onnx) | 2.2MB | - | +| [SCRFD-500M-kps-320](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_bnkps_shape320x320.onnx) | 2.5MB | - | +| [SCRFD-500M-320](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_shape320x320.onnx) | 2.2MB | - | +| [SCRFD-500M-kps-640](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_bnkps_shape640x640.onnx) | 2.5MB | 90.97% | +| [SCRFD-500M-640](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_shape640x640.onnx) | 2.2MB | 90.57% | +| [SCRFD-1G-160](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_1g_shape160x160.onnx ) | 2.5MB | - | +| [SCRFD-1G-320](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_1g_shape320x320.onnx) | 2.5MB | - | +| [SCRFD-1G-640](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_1g_shape640x640.onnx) | 2.5MB | 92.38% | +| [SCRFD-2.5G-kps-160](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_2.5g_bnkps_shape160x160.onnx) | 3.2MB | - | +| [SCRFD-2.5G-160](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_2.5g_shape160x160.onnx) | 2.6MB | - | +| [SCRFD-2.5G-kps-320](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_2.5g_bnkps_shape320x320.onnx) | 3.2MB | - | +| [SCRFD-2.5G-320](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_2.5g_shape320x320.onnx) | 2.6MB | - | +| [SCRFD-2.5G-kps-640](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_2.5g_bnkps_shape640x640.onnx) | 3.2MB | 93.8% | +| [SCRFD-2.5G-640](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_2.5g_shape640x640.onnx) | 2.6MB | 93.78% | +| [SCRFD-10G-kps-160](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_10g_bnkps_shape160x160.onnx) | 17MB | - | +| [SCRFD-10G-160](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_10g_shape160x160.onnx) | 15MB | - | +| [SCRFD-10G-kps-320](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_10g_bnkps_shape320x320.onnx) | 17MB | - | +| [SCRFD-10G-320](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_10g_shape320x320.onnx) | 15MB | - | +| [SCRFD-10G-kps-640](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_10g_bnkps_shape640x640.onnx) | 17MB | 95.4% | +| [SCRFD-10G-640](https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_10g_shape640x640.onnx) | 15MB | 95.16% | + + + +## 详细部署文档 + +- [Python部署](python) +- [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[SCRFD CommitID:17cdeab](https://github.com/deepinsight/insightface/tree/17cdeab12a35efcebc2660453a8cbeae96e20950) 编写 diff --git a/examples/vision/facedet/scrfd/cpp/CMakeLists.txt b/examples/vision/facedet/scrfd/cpp/CMakeLists.txt new file mode 100644 index 0000000000..fea1a2888b --- /dev/null +++ b/examples/vision/facedet/scrfd/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +PROJECT(infer_demo C CXX) +CMAKE_MINIMUM_REQUIRED (VERSION 3.12) + +# 指定下载解压后的fastdeploy库路径 +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) + +# 添加FastDeploy依赖头文件 +include_directories(${FASTDEPLOY_INCS}) + +add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc) +# 添加FastDeploy库依赖 +target_link_libraries(infer_demo ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/facedet/scrfd/cpp/README.md b/examples/vision/facedet/scrfd/cpp/README.md new file mode 100644 index 0000000000..5309542a77 --- /dev/null +++ b/examples/vision/facedet/scrfd/cpp/README.md @@ -0,0 +1,91 @@ +# SCRFD C++部署示例 + +本目录下提供`infer.cc`快速完成SCRFD在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) +- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/compile/prebuild_libraries.md) + +以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试 + +``` +mkdir build +cd build +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz +tar xvf fastdeploy-linux-x64-0.2.0.tgz +cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 +make -j + +#下载官方转换好的SCRFD模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_bnkps_shape640x640.onnx +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg + + +# CPU推理 +./infer_demo scrfd_500m_bnkps_shape640x640.onnx test_lite_face_detector_3.jpg 0 +# GPU推理 +./infer_demo scrfd_500m_bnkps_shape640x640.onnx test_lite_face_detector_3.jpg 1 +# GPU上TensorRT推理 +./infer_demo scrfd_500m_bnkps_shape640x640.onnx test_lite_face_detector_3.jpg 2 +``` + +运行完成可视化结果如下图所示 + + + +## SCRFD C++接口 + +### SCRFD类 + +``` +fastdeploy::vision::facedet::SCRFD( + const string& model_file, + const string& params_file = "", + const RuntimeOption& runtime_option = RuntimeOption(), + const Frontend& model_format = Frontend::ONNX) +``` + +SCRFD模型加载和初始化,其中model_file为导出的ONNX模型格式。 + +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX时,此参数传入空字符串即可 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX格式 + +#### Predict函数 + +> ``` +> SCRFD::Predict(cv::Mat* im, FaceDetectionResult* result, +> float conf_threshold = 0.25, +> float nms_iou_threshold = 0.5) +> ``` +> +> 模型预测接口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **im**: 输入图像,注意需为HWC,BGR格式 +> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceDetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **conf_threshold**: 检测框置信度过滤阈值 +> > * **nms_iou_threshold**: NMS处理过程中iou阈值 + +### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + +> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] +> > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] +> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` +> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` +> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **downsample_strides**(vector<int>): 通过此参数可以修改生成anchor的特征图的下采样倍数, 包含三个整型元素, 分别表示默认的生成anchor的下采样倍数, 默认值为[8, 16, 32] +> > * **landmarks_per_face**(int): 如果使用具有人脸关键点的输出, 可以修改人脸关键点数量, 默认值为`landmarks_per_face=5` +> > * **use_kps**(bool): 通过此参数可以设置模型是否使用关键点,如果ONNX文件没有关键点输出则需要将`use_kps=false`, 并将`landmarks_per_face=0`, 默认值为`use_kps=true` +> > * **num_anchors**(int): 通过此参数可以设置每个锚点预测的anchor数量, 需要跟进训练模型的参数设定, 默认值为`num_anchors=2` + +- [模型介绍](../../) +- [Python部署](../python) +- [视觉模型预测结果](../../../../../docs/api/vision_results/) diff --git a/examples/vision/facedet/scrfd/cpp/infer.cc b/examples/vision/facedet/scrfd/cpp/infer.cc new file mode 100644 index 0000000000..c804218ee3 --- /dev/null +++ b/examples/vision/facedet/scrfd/cpp/infer.cc @@ -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. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::facedet::SCRFD(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::facedet::SCRFD(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = fastdeploy::vision::facedet::SCRFD(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout + << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model scrfd_500m_bnkps_shape640x640.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/facedet/scrfd/python/README.md b/examples/vision/facedet/scrfd/python/README.md new file mode 100644 index 0000000000..efb1c863cb --- /dev/null +++ b/examples/vision/facedet/scrfd/python/README.md @@ -0,0 +1,84 @@ +# SCRFD Python部署示例 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) +- 2. FastDeploy Python whl包安装,参考[FastDeploy Python安装](../../../../../docs/quick_start/install.md) + +本目录下提供`infer.py`快速完成SCRFD在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 + +``` +#下载SCRFD模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/scrfd_500m_bnkps_shape640x640.onnx +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg + + +#下载部署示例代码 +git clone https://github.com/PaddlePaddle/FastDeploy.git +cd examples/vison/facedet/scrfd/python/ + +# CPU推理 +python infer.py --model scrfd_500m_bnkps_shape640x640.onnx --image test_lite_face_detector_3.jpg --device cpu +# GPU推理 +python infer.py --model scrfd_500m_bnkps_shape640x640.onnx --image test_lite_face_detector_3.jpg --device gpu +# GPU上使用TensorRT推理 +python infer.py --model scrfd_500m_bnkps_shape640x640.onnx --image test_lite_face_detector_3.jpg --device gpu --use_trt True +``` + +运行完成可视化结果如下图所示 + + + +## SCRFD Python接口 + +``` +fastdeploy.vision.facedet.SCRFD(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +``` + +SCRFD模型加载和初始化,其中model_file为导出的ONNX模型格式 + +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX格式时,此参数无需设定 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX + +### predict函数 + +> ``` +> SCRFD.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) +> ``` +> +> 模型预测结口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **image_data**(np.ndarray): 输入数据,注意需为HWC,BGR格式 +> > * **conf_threshold**(float): 检测框置信度过滤阈值 +> > * **nms_iou_threshold**(float): NMS处理过程中iou阈值 + +> **返回** +> +> > 返回`fastdeploy.vision.FaceDetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) + +### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + +> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] +> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] +> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` +> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` +> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` +> > * **downsample_strides**(list[int]): 通过此参数可以修改生成anchor的特征图的下采样倍数, 包含三个整型元素, 分别表示默认的生成anchor的下采样倍数, 默认值为[8, 16, 32] +> > * **landmarks_per_face**(int): 如果使用具有人脸关键点的输出, 可以修改人脸关键点数量, 默认值为`landmarks_per_face=5` +> > * **use_kps**(bool): 通过此参数可以设置模型是否使用关键点,如果ONNX文件没有关键点输出则需要将`use_kps=False`, 并将`landmarks_per_face=0`, 默认值为`use_kps=True` +> > * **num_anchors**(int): 通过此参数可以设置每个锚点预测的anchor数量, 需要跟进训练模型的参数设定, 默认值为`num_anchors=2` + + +## 其它文档 + +- [SCRFD 模型介绍](..) +- [SCRFD C++部署](../cpp) +- [模型预测结果说明](../../../../../docs/api/vision_results/) diff --git a/examples/vision/facedet/scrfd/python/infer.py b/examples/vision/facedet/scrfd/python/infer.py new file mode 100644 index 0000000000..a99e66385f --- /dev/null +++ b/examples/vision/facedet/scrfd/python/infer.py @@ -0,0 +1,51 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of scrfd onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.facedet.SCRFD(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) +# 预测结果可视化 +vis_im = fd.vision.vis_face_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/facedet/ultraface/README.md b/examples/vision/facedet/ultraface/README.md index f1dcca0b98..678fb771f7 100644 --- a/examples/vision/facedet/ultraface/README.md +++ b/examples/vision/facedet/ultraface/README.md @@ -2,7 +2,7 @@ ## 模型版本说明 -- [UltraFace CommitID:dffdddd](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/commit/dffdddd) +- [UltraFace](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/commit/dffdddd) - (1)[链接中](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/commit/dffdddd)的*.onnx可下载, 也可以通过下面模型链接下载并进行部署 @@ -21,3 +21,8 @@ - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[UltraFace CommitID:dffdddd](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/commit/dffdddd) 编写 diff --git a/examples/vision/facedet/ultraface/cpp/README.md b/examples/vision/facedet/ultraface/cpp/README.md index 1eae69c0fb..1610685c98 100644 --- a/examples/vision/facedet/ultraface/cpp/README.md +++ b/examples/vision/facedet/ultraface/cpp/README.md @@ -12,27 +12,27 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j #下载官方转换好的UltraFace模型文件和测试图片 wget https://bj.bcebos.com/paddlehub/fastdeploy/version-RFB-320.onnx -wget todo +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg # CPU推理 -./infer_demo version-RFB-320.onnx todo 0 +./infer_demo version-RFB-320.onnx test_lite_face_detector_3.jpg 0 # GPU推理 -./infer_demo version-RFB-320.onnx todo 1 +./infer_demo version-RFB-320.onnx test_lite_face_detector_3.jpg 1 # GPU上TensorRT推理 -./infer_demo version-RFB-320.onnx todo 2 +./infer_demo version-RFB-320.onnx test_lite_face_detector_3.jpg 2 ``` 运行完成可视化结果如下图所示 - + ## UltraFace C++接口 @@ -58,7 +58,7 @@ UltraFace模型加载和初始化,其中model_file为导出的ONNX模型格式 #### Predict函数 > ``` -> UltraFace::Predict(cv::Mat* im, DetectionResult* result, +> UltraFace::Predict(cv::Mat* im, FaceDetectionResult* result, > float conf_threshold = 0.25, > float nms_iou_threshold = 0.5) > ``` @@ -68,17 +68,15 @@ UltraFace模型加载和初始化,其中model_file为导出的ONNX模型格式 > **参数** > > > * **im**: 输入图像,注意需为HWC,BGR格式 -> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceDetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) > > * **conf_threshold**: 检测框置信度过滤阈值 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 -> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` -> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[320, 240] - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/facedet/ultraface/cpp/infer.cc b/examples/vision/facedet/ultraface/cpp/infer.cc new file mode 100644 index 0000000000..b45bb3b0b3 --- /dev/null +++ b/examples/vision/facedet/ultraface/cpp/infer.cc @@ -0,0 +1,108 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::facedet::UltraFace(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::facedet::UltraFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = fastdeploy::vision::facedet::UltraFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model version-RFB-320.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/facedet/ultraface/python/README.md b/examples/vision/facedet/ultraface/python/README.md index 88026ecff3..d0406d65ac 100644 --- a/examples/vision/facedet/ultraface/python/README.md +++ b/examples/vision/facedet/ultraface/python/README.md @@ -10,24 +10,25 @@ ``` #下载ultraface模型文件和测试图片 wget https://bj.bcebos.com/paddlehub/fastdeploy/version-RFB-320.onnx -wget todo +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg #下载部署示例代码 git clone https://github.com/PaddlePaddle/FastDeploy.git -cd examples/vison/detection/ultraface/python/ +cd examples/vison/facedet/ultraface/python/ # CPU推理 -python infer.py --model version-RFB-320.onnx --image todo --device cpu + +python infer.py --model version-RFB-320.onnx --image test_lite_face_detector_3.jpg --device cpu # GPU推理 -python infer.py --model version-RFB-320.onnx --image todo --device gpu +python infer.py --model version-RFB-320.onnx --image test_lite_face_detector_3.jpg --device gpu # GPU上使用TensorRT推理 -python infer.py --model version-RFB-320.onnx --image todo --device gpu --use_trt True +python infer.py --model version-RFB-320.onnx --image test_lite_face_detector_3.jpg --device gpu --use_trt True ``` 运行完成可视化结果如下图所示 - + ## UltraFace Python接口 @@ -60,17 +61,13 @@ UltraFace模型加载和初始化,其中model_file为导出的ONNX模型格式 > **返回** > -> > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > 返回`fastdeploy.vision.FaceDetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 -> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` -> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` - - +> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[320, 240] ## 其它文档 diff --git a/examples/vision/facedet/ultraface/python/infer.py b/examples/vision/facedet/ultraface/python/infer.py new file mode 100644 index 0000000000..5399110b9a --- /dev/null +++ b/examples/vision/facedet/ultraface/python/infer.py @@ -0,0 +1,51 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of ultraface onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.facedet.UltraFace(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) +# 预测结果可视化 +vis_im = fd.vision.vis_face_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/facedet/yolov5face/README.md b/examples/vision/facedet/yolov5face/README.md index 34828b1938..424a76bbed 100644 --- a/examples/vision/facedet/yolov5face/README.md +++ b/examples/vision/facedet/yolov5face/README.md @@ -2,9 +2,9 @@ ## 模型版本说明 -- [YOLOv5Face CommitID:4fd1ead](https://github.com/deepcam-cn/yolov5-face/commit/4fd1ead) +- [YOLOv5Face](https://github.com/deepcam-cn/yolov5-face/commit/4fd1ead) - (1)[链接中](https://github.com/deepcam-cn/yolov5-face/commit/4fd1ead)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署; - - (2)开发者基于自己数据训练的YOLOv5Face CommitID:b984b4b模型,可按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)后,完成部署。 + - (2)开发者基于自己数据训练的YOLOv5Face模型,可按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)后,完成部署。 ## 导出ONNX模型 @@ -31,12 +31,17 @@ | 模型 | 大小 | 精度 | |:---------------------------------------------------------------- |:----- |:----- | -| [YOLOv5s-Face](https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s-face.onnx) | 30MB | - | +| [YOLOv5s-Face](https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s-face.onnx) | 30MB | 94.3 | | [YOLOv5s-Face-bak](https://bj.bcebos.com/paddlehub/fastdeploy/yolov5face-s-640x640.bak.onnx) | 30MB | -| -| [YOLOv5l-Face](https://bj.bcebos.com/paddlehub/fastdeploy/yolov5face-l-640x640.onnx ) | 181MB | - | +| [YOLOv5l-Face](https://bj.bcebos.com/paddlehub/fastdeploy/yolov5face-l-640x640.onnx ) | 181MB | 95.8 | ## 详细部署文档 - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[YOLOv5Face CommitID:4fd1ead](https://github.com/deepcam-cn/yolov5-face/commit/4fd1ead) 编写 diff --git a/examples/vision/facedet/yolov5face/cpp/README.md b/examples/vision/facedet/yolov5face/cpp/README.md index ec0b48ad0a..4c7368d594 100644 --- a/examples/vision/facedet/yolov5face/cpp/README.md +++ b/examples/vision/facedet/yolov5face/cpp/README.md @@ -12,27 +12,27 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j #下载官方转换好的YOLOv5Face模型文件和测试图片 wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s-face.onnx -wget todo +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg # CPU推理 -./infer_demo yolov5s-face.onnx todo 0 +./infer_demo yolov5s-face.onnx test_lite_face_detector_3.jpg 0 # GPU推理 -./infer_demo yolov5s-face.onnx todo 1 +./infer_demo yolov5s-face.onnx test_lite_face_detector_3.jpg 1 # GPU上TensorRT推理 -./infer_demo yolov5s-face.onnx todo 2 +./infer_demo yolov5s-face.onnx test_lite_face_detector_3.jpg 2 ``` 运行完成可视化结果如下图所示 - + ## YOLOv5Face C++接口 @@ -58,7 +58,7 @@ YOLOv5Face模型加载和初始化,其中model_file为导出的ONNX模型格 #### Predict函数 > ``` -> YOLOv5Face::Predict(cv::Mat* im, DetectionResult* result, +> YOLOv5Face::Predict(cv::Mat* im, FaceDetectionResult* result, > float conf_threshold = 0.25, > float nms_iou_threshold = 0.5) > ``` @@ -68,17 +68,20 @@ YOLOv5Face模型加载和初始化,其中model_file为导出的ONNX模型格 > **参数** > > > * **im**: 输入图像,注意需为HWC,BGR格式 -> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceDetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) > > * **conf_threshold**: 检测框置信度过滤阈值 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] > > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` > > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` -> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **stride**(int): 配合`is_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **landmarks_per_face**(int): 指定当前模型检测的人脸所带的关键点个数,默认为5. - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/facedet/yolov5face/cpp/infer.cc b/examples/vision/facedet/yolov5face/cpp/infer.cc new file mode 100644 index 0000000000..418834e1e4 --- /dev/null +++ b/examples/vision/facedet/yolov5face/cpp/infer.cc @@ -0,0 +1,108 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::facedet::YOLOv5Face(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::facedet::YOLOv5Face(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("images", {1, 3, 640, 640}); + auto model = fastdeploy::vision::facedet::YOLOv5Face(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::FaceDetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + std::cout << res.Str() << std::endl; + + auto vis_im = fastdeploy::vision::Visualize::VisFaceDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model yolov5s-face.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/facedet/yolov5face/python/README.md b/examples/vision/facedet/yolov5face/python/README.md index 2fc847f008..31a1362b26 100644 --- a/examples/vision/facedet/yolov5face/python/README.md +++ b/examples/vision/facedet/yolov5face/python/README.md @@ -10,24 +10,24 @@ ``` #下载YOLOv5Face模型文件和测试图片 wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s-face.onnx -wget todo +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_face_detector_3.jpg #下载部署示例代码 git clone https://github.com/PaddlePaddle/FastDeploy.git -cd examples/vison/detection/yolov5face/python/ +cd examples/vison/facedet/yolov5face/python/ # CPU推理 -python infer.py --model yolov5s-face.onnx --image todo --device cpu +python infer.py --model yolov5s-face.onnx --image test_lite_face_detector_3.jpg --device cpu # GPU推理 -python infer.py --model yolov5s-face.onnx --image todo --device gpu +python infer.py --model yolov5s-face.onnx --image test_lite_face_detector_3.jpg --device gpu # GPU上使用TensorRT推理 -python infer.py --model yolov5s-face.onnx --image todo --device gpu --use_trt True +python infer.py --model yolov5s-face.onnx --image test_lite_face_detector_3.jpg --device gpu --use_trt True ``` 运行完成可视化结果如下图所示 - + ## YOLOv5Face Python接口 @@ -60,17 +60,18 @@ YOLOv5Face模型加载和初始化,其中model_file为导出的ONNX模型格 > **返回** > -> > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > 返回`fastdeploy.vision.FaceDetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 > > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] > > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] > > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` > > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` -> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` - - +> > * **stride**(int): 配合`is_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **landmarks_per_face**(int): 指定当前模型检测的人脸所带的关键点个数,默认为5. ## 其它文档 diff --git a/examples/vision/facedet/yolov5face/python/infer.py b/examples/vision/facedet/yolov5face/python/infer.py new file mode 100644 index 0000000000..a9f0446826 --- /dev/null +++ b/examples/vision/facedet/yolov5face/python/infer.py @@ -0,0 +1,51 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of yolov5face onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("images", [1, 3, 640, 640]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.facedet.YOLOv5Face(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) +print(result) +# 预测结果可视化 +vis_im = fd.vision.vis_face_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/faceid/README.md b/examples/vision/faceid/README.md new file mode 100644 index 0000000000..4950463a1a --- /dev/null +++ b/examples/vision/faceid/README.md @@ -0,0 +1,10 @@ +人脸检测模型 + +FastDeploy目前支持如下人脸识别模型部署 + +| 模型 | 说明 | 模型格式 | 版本 | +| :--- | :--- | :------- | :--- | +| [arcface](./insightface) | ArcFace系列模型 | ONNX | CommitID:babb9a5 | +| [cosface](./insightface) | CosFace系列模型 | ONNX | CommitID:babb9a5 | +| [partial_fc](./insightface) | PartialFC系列模型 | ONNX | CommitID:babb9a5 | +| [vpl](./insightface) | VPL系列模型 | ONNX | CommitID:babb9a5 | diff --git a/examples/vision/faceid/arcface/README.md b/examples/vision/faceid/arcface/README.md deleted file mode 100644 index cb93054029..0000000000 --- a/examples/vision/faceid/arcface/README.md +++ /dev/null @@ -1,40 +0,0 @@ -# ArcFace准备部署模型 - -## 模型版本说明 - -- [ArcFace CommitID:babb9a5](https://github.com/deepinsight/insightface/commit/babb9a5) - - (1)[链接中](https://github.com/deepinsight/insightface/commit/babb9a5)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署; - - (2)开发者基于自己数据训练的ArcFace CommitID:babb9a5模型,可按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)后,完成部署。 - -## 导出ONNX模型 - -访问[ArcFace](https://github.com/deepinsight/insightface/tree/master/recognition/arcface_torch)官方github库,按照指引下载安装,下载pt模型文件,利用 `torch2onnx.py` 得到`onnx`格式文件。 - -* 下载ArcFace模型文件 - ``` - Link: https://pan.baidu.com/share/init?surl=CL-l4zWqsI1oDuEEYVhj-g code: e8pw - ``` - -* 导出onnx格式文件 - ```bash - PYTHONPATH=. python ./torch2onnx.py ms1mv3_arcface_r100_fp16/backbone.pth --output ms1mv3_arcface_r100.onnx --network r100 --simplify 1 - ``` - -## 下载预训练ONNX模型 - - - -todo - - -## 详细部署文档 - -- [Python部署](python) -- [C++部署](cpp) diff --git a/examples/vision/faceid/insightface/README.md b/examples/vision/faceid/insightface/README.md new file mode 100644 index 0000000000..cf3371247a --- /dev/null +++ b/examples/vision/faceid/insightface/README.md @@ -0,0 +1,62 @@ +# InsightFace准备部署模型 + +## 模型版本说明 + +- [InsightFace](https://github.com/deepinsight/insightface/commit/babb9a5) + - (1)[链接中](https://github.com/deepinsight/insightface/commit/babb9a5)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署; + - (2)开发者基于自己数据训练的InsightFace模型,可按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)后,完成部署。 + + +## 支持模型列表 +目前FastDeploy支持如下模型的部署 +- ArcFace +- CosFace +- PartialFC +- VPL + + +## 导出ONNX模型 +以ArcFace为例: +访问[ArcFace](https://github.com/deepinsight/insightface/tree/master/recognition/arcface_torch)官方github库,按照指引下载安装,下载pt模型文件,利用 `torch2onnx.py` 得到`onnx`格式文件。 + +* 下载ArcFace模型文件 + ``` + Link: https://pan.baidu.com/share/init?surl=CL-l4zWqsI1oDuEEYVhj-g code: e8pw + ``` + +* 导出onnx格式文件 + ```bash + PYTHONPATH=. python ./torch2onnx.py ms1mv3_arcface_r100_fp16/backbone.pth --output ms1mv3_arcface_r100.onnx --network r100 --simplify 1 + ``` + +## 下载预训练ONNX模型 + +为了方便开发者的测试,下面提供了InsightFace导出的各系列模型,开发者可直接下载使用。 +其中精度指标来源于InsightFace中对各模型的介绍,详情各参考InsightFace中的说明 + +| 模型 | 大小 | 精度 (AgeDB_30) | +|:---------------------------------------------------------------- |:----- |:----- | +| [CosFace-r18](https://bj.bcebos.com/paddlehub/fastdeploy/glint360k_cosface_r18.onnx) | 92MB | 97.7 | +| [CosFace-r34](https://bj.bcebos.com/paddlehub/fastdeploy/glint360k_cosface_r34.onnx) | 131MB | 98.3| +| [CosFace-r50](https://bj.bcebos.com/paddlehub/fastdeploy/glint360k_cosface_r50.onnx) | 167MB | 98.3 | +| [CosFace-r100](https://bj.bcebos.com/paddlehub/fastdeploy/glint360k_cosface_r100.onnx) | 249MB | 98.4 | +| [ArcFace-r18](https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r18.onnx) | 92MB | 97.7 | +| [ArcFace-r34](https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r34.onnx) | 131MB | 98.1| +| [ArcFace-r50](https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r50.onnx) | 167MB | - | +| [ArcFace-r100](https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_arcface_r100.onnx) | 249MB | 98.4 | +| [ArcFace-r100_lr0.1](https://bj.bcebos.com/paddlehub/fastdeploy/ms1mv3_r100_lr01.onnx) | 249MB | 98.4 | +| [PartialFC-r34](https://bj.bcebos.com/paddlehub/fastdeploy/partial_fc_glint360k_r50.onnx) | 167MB | -| +| [PartialFC-r50](https://bj.bcebos.com/paddlehub/fastdeploy/partial_fc_glint360k_r100.onnx) | 249MB | - | + + + + +## 详细部署文档 + +- [Python部署](python) +- [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[InsightFace CommitID:babb9a5](https://github.com/deepinsight/insightface/commit/babb9a5) 编写 diff --git a/examples/vision/faceid/insightface/cpp/CMakeLists.txt b/examples/vision/faceid/insightface/cpp/CMakeLists.txt new file mode 100644 index 0000000000..56c1d4cb9e --- /dev/null +++ b/examples/vision/faceid/insightface/cpp/CMakeLists.txt @@ -0,0 +1,22 @@ +PROJECT(infer_demo C CXX) +CMAKE_MINIMUM_REQUIRED (VERSION 3.12) + +# 指定下载解压后的fastdeploy库路径 +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) + +# 添加FastDeploy依赖头文件 +include_directories(${FASTDEPLOY_INCS}) + +add_executable(infer_arcface_demo ${PROJECT_SOURCE_DIR}/infer_arcface.cc) +target_link_libraries(infer_arcface_demo ${FASTDEPLOY_LIBS}) + +add_executable(infer_cosface_demo ${PROJECT_SOURCE_DIR}/infer_cosface.cc) +target_link_libraries(infer_cosface_demo ${FASTDEPLOY_LIBS}) + +add_executable(infer_vpl_demo ${PROJECT_SOURCE_DIR}/infer_vpl.cc) +target_link_libraries(infer_vpl_demo ${FASTDEPLOY_LIBS}) + +add_executable(infer_partial_fc_demo ${PROJECT_SOURCE_DIR}/infer_partial_fc.cc) +target_link_libraries(infer_partial_fc_demo ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/faceid/insightface/cpp/README.md b/examples/vision/faceid/insightface/cpp/README.md new file mode 100644 index 0000000000..56af498c2d --- /dev/null +++ b/examples/vision/faceid/insightface/cpp/README.md @@ -0,0 +1,129 @@ +# InsightFace C++部署示例 +本目录下提供infer_xxx.cc快速完成InsighFace模型包括ArcFace\CosFace\VPL\Partial_FC在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 +以ArcFace为例提供`infer_arcface.cc`快速完成ArcFace在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) +- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/compile/prebuild_libraries.md) + +以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试 + +``` +mkdir build +cd build +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz +tar xvf fastdeploy-linux-x64-0.2.0.tgz +cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 +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 + + +# 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 +# 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 +# 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 +``` + +运行完成可视化结果如下图所示 + +
+ + + +
+ +## InsightFace C++接口 + +### ArcFace类 + +``` +fastdeploy::vision::faceid::ArcFace( + const string& model_file, + const string& params_file = "", + const RuntimeOption& runtime_option = RuntimeOption(), + const Frontend& model_format = Frontend::ONNX) +``` + +ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式。 + +### CosFace类 + +``` +fastdeploy::vision::faceid::CosFace( + const string& model_file, + const string& params_file = "", + const RuntimeOption& runtime_option = RuntimeOption(), + const Frontend& model_format = Frontend::ONNX) +``` + +CosFace模型加载和初始化,其中model_file为导出的ONNX模型格式。 + +### PartialFC类 + +``` +fastdeploy::vision::faceid::PartialFC( + const string& model_file, + const string& params_file = "", + const RuntimeOption& runtime_option = RuntimeOption(), + const Frontend& model_format = Frontend::ONNX) +``` + +PartialFC模型加载和初始化,其中model_file为导出的ONNX模型格式。 + +### VPL类 + +``` +fastdeploy::vision::faceid::VPL( + const string& model_file, + const string& params_file = "", + const RuntimeOption& runtime_option = RuntimeOption(), + const Frontend& model_format = Frontend::ONNX) +``` + +VPL模型加载和初始化,其中model_file为导出的ONNX模型格式。 +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX时,此参数传入空字符串即可 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX格式 + +#### Predict函数 + +> ``` +> ArcFace::Predict(cv::Mat* im, FaceRecognitionResult* result, +> float conf_threshold = 0.25, +> float nms_iou_threshold = 0.5) +> ``` +> +> 模型预测接口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **im**: 输入图像,注意需为HWC,BGR格式 +> > * **result**: 检测结果,包括检测框,各个框的置信度, FaceRecognitionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **conf_threshold**: 检测框置信度过滤阈值 +> > * **nms_iou_threshold**: NMS处理过程中iou阈值 + +### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + + +> > * **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 + +- [模型介绍](../../) +- [Python部署](../python) +- [视觉模型预测结果](../../../../../docs/api/vision_results/) diff --git a/examples/vision/faceid/insightface/cpp/infer_arcface.cc b/examples/vision/faceid/insightface/cpp/infer_arcface.cc new file mode 100644 index 0000000000..0c8824f0e9 --- /dev/null +++ b/examples/vision/faceid/insightface/cpp/infer_arcface.cc @@ -0,0 +1,156 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto model = fastdeploy::vision::faceid::ArcFace(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void GpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::faceid::ArcFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void TrtInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("data", {1, 3, 112, 112}); + auto model = fastdeploy::vision::faceid::ArcFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +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" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + std::vector image_files = {argv[2], argv[3], argv[4]}; + if (std::atoi(argv[5]) == 0) { + CpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 1) { + GpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 2) { + TrtInfer(argv[1], image_files); + } + return 0; +} diff --git a/examples/vision/faceid/insightface/cpp/infer_cosface.cc b/examples/vision/faceid/insightface/cpp/infer_cosface.cc new file mode 100644 index 0000000000..d08e71d513 --- /dev/null +++ b/examples/vision/faceid/insightface/cpp/infer_cosface.cc @@ -0,0 +1,156 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto model = fastdeploy::vision::faceid::CosFace(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void GpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::faceid::CosFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void TrtInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("data", {1, 3, 112, 112}); + auto model = fastdeploy::vision::faceid::CosFace(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +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" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + std::vector image_files = {argv[2], argv[3], argv[4]}; + if (std::atoi(argv[5]) == 0) { + CpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 1) { + GpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 2) { + TrtInfer(argv[1], image_files); + } + return 0; +} diff --git a/examples/vision/faceid/insightface/cpp/infer_partial_fc.cc b/examples/vision/faceid/insightface/cpp/infer_partial_fc.cc new file mode 100644 index 0000000000..762276ccff --- /dev/null +++ b/examples/vision/faceid/insightface/cpp/infer_partial_fc.cc @@ -0,0 +1,156 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto model = fastdeploy::vision::faceid::PartialFC(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void GpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::faceid::PartialFC(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void TrtInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("data", {1, 3, 112, 112}); + auto model = fastdeploy::vision::faceid::PartialFC(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +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" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + std::vector image_files = {argv[2], argv[3], argv[4]}; + if (std::atoi(argv[5]) == 0) { + CpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 1) { + GpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 2) { + TrtInfer(argv[1], image_files); + } + return 0; +} diff --git a/examples/vision/faceid/insightface/cpp/infer_vpl.cc b/examples/vision/faceid/insightface/cpp/infer_vpl.cc new file mode 100644 index 0000000000..7f570966fb --- /dev/null +++ b/examples/vision/faceid/insightface/cpp/infer_vpl.cc @@ -0,0 +1,156 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto model = fastdeploy::vision::faceid::VPL(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void GpuInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::faceid::VPL(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +void TrtInfer(const std::string& model_file, + const std::vector& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("data", {1, 3, 112, 112}); + auto model = fastdeploy::vision::faceid::VPL(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + cv::Mat face0 = cv::imread(image_file[0]); + cv::Mat face1 = cv::imread(image_file[1]); + cv::Mat face2 = cv::imread(image_file[2]); + + fastdeploy::vision::FaceRecognitionResult res0; + fastdeploy::vision::FaceRecognitionResult res1; + fastdeploy::vision::FaceRecognitionResult res2; + + if ((!model.Predict(&face0, &res0)) || (!model.Predict(&face1, &res1)) || + (!model.Predict(&face2, &res2))) { + std::cerr << "Prediction Failed." << std::endl; + } + + std::cout << "Prediction Done!" << std::endl; + + // 输出预测框结果 + std::cout << "--- [Face 0]:" << res0.Str(); + std::cout << "--- [Face 1]:" << res1.Str(); + std::cout << "--- [Face 2]:" << res2.Str(); + + // 计算余弦相似度 + float cosine01 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res1.embedding, model.l2_normalize); + float cosine02 = fastdeploy::vision::utils::CosineSimilarity( + res0.embedding, res2.embedding, model.l2_normalize); + std::cout << "Detect Done! Cosine 01: " << cosine01 + << ", Cosine 02:" << cosine02 << std::endl; +} + +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" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + std::vector image_files = {argv[2], argv[3], argv[4]}; + if (std::atoi(argv[5]) == 0) { + CpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 1) { + GpuInfer(argv[1], image_files); + } else if (std::atoi(argv[5]) == 2) { + TrtInfer(argv[1], image_files); + } + return 0; +} diff --git a/examples/vision/faceid/insightface/python/README.md b/examples/vision/faceid/insightface/python/README.md new file mode 100644 index 0000000000..f53e516355 --- /dev/null +++ b/examples/vision/faceid/insightface/python/README.md @@ -0,0 +1,100 @@ +# InsightFace Python部署示例 +本目录下提供infer_xxx.py快速完成InsighFace模型包括ArcFace\CosFace\VPL\Partial_FC在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md) +- 2. FastDeploy Python whl包安装,参考[FastDeploy Python安装](../../../../../docs/quick_start/install.md) + +以ArcFace为例子, 提供`infer_arcface.py`快速完成ArcFace在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 + +``` +#下载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 + + +#下载部署示例代码 +git clone https://github.com/PaddlePaddle/FastDeploy.git +cd examples/vison/faceid/insightface/python/ + +# 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 +# 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 +# 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 +``` + +运行完成可视化结果如下图所示 + +
+ + + +
+ +``` +Prediction Done! +--- [Face 0]:FaceRecognitionResult: [Dim(512), Min(-2.309220), Max(2.372197), Mean(0.016987)] +--- [Face 1]:FaceRecognitionResult: [Dim(512), Min(-2.288258), Max(1.995104), Mean(-0.003400)] +--- [Face 2]:FaceRecognitionResult: [Dim(512), Min(-3.243411), Max(3.875866), Mean(-0.030682)] +Detect Done! Cosine 01: 0.814385, Cosine 02:-0.059388 + +``` + +## InsightFace Python接口 + +``` +fastdeploy.vision.faceid.ArcFace(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +fastdeploy.vision.faceid.CosFace(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +fastdeploy.vision.faceid.PartialFC(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +fastdeploy.vision.faceid.VPL(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +``` + +ArcFace模型加载和初始化,其中model_file为导出的ONNX模型格式 + +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX格式时,此参数无需设定 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX + +### predict函数 + +> ``` +> ArcFace.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) +> ``` +> +> 模型预测结口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **image_data**(np.ndarray): 输入数据,注意需为HWC,BGR格式 +> > * **conf_threshold**(float): 检测框置信度过滤阈值 +> > * **nms_iou_threshold**(float): NMS处理过程中iou阈值 + +> **返回** +> +> > 返回`fastdeploy.vision.FaceRecognitionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) + +### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + + +> > * **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 +> > * **l2_normalize**(bool): 输出人脸向量之前是否执行l2归一化,默认False + + +## 其它文档 + +- [InsightFace 模型介绍](..) +- [InsightFace C++部署](../cpp) +- [模型预测结果说明](../../../../../docs/api/vision_results/) diff --git a/examples/vision/faceid/insightface/python/infer_arcface.py b/examples/vision/faceid/insightface/python/infer_arcface.py new file mode 100644 index 0000000000..2d725026e1 --- /dev/null +++ b/examples/vision/faceid/insightface/python/infer_arcface.py @@ -0,0 +1,90 @@ +import fastdeploy as fd +import cv2 +import numpy as np + + +# 余弦相似度 +def cosine_similarity(a, b): + a = np.array(a) + b = np.array(b) + mul_a = np.linalg.norm(a, ord=2) + mul_b = np.linalg.norm(b, ord=2) + mul_ab = np.dot(a, b) + return mul_ab / (np.sqrt(mul_a) * np.sqrt(mul_b)) + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of scrfd onnx model.") + parser.add_argument( + "--face", required=True, help="Path of test face image file.") + parser.add_argument( + "--face_positive", + required=True, + help="Path of test face_positive image file.") + parser.add_argument( + "--face_negative", + required=True, + help="Path of test face_negative image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("data", [1, 3, 112, 112]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.faceid.ArcFace(args.model, runtime_option=runtime_option) + +# 加载图片 +face0 = cv2.imread(args.face) # 0,1 同一个人 +face1 = cv2.imread(args.face_positive) +face2 = cv2.imread(args.face_negative) # 0,2 不同的人 + +# 设置 l2 normalize +model.l2_normalize = True + +# 预测图片检测结果 +result0 = model.predict(face0) +result1 = model.predict(face1) +result2 = model.predict(face2) + +# 计算余弦相似度 +embedding0 = result0.embedding +embedding1 = result1.embedding +embedding2 = result2.embedding + +cosine01 = cosine_similarity(embedding0, embedding1) +cosine02 = cosine_similarity(embedding0, embedding2) + +# 打印结果 +print(result0, end="") +print(result1, end="") +print(result2, end="") +print("Cosine 01: ", cosine01) +print("Cosine 02: ", cosine02) +print(model.runtime_option) diff --git a/examples/vision/faceid/insightface/python/infer_cosface.py b/examples/vision/faceid/insightface/python/infer_cosface.py new file mode 100644 index 0000000000..07f1a0b14b --- /dev/null +++ b/examples/vision/faceid/insightface/python/infer_cosface.py @@ -0,0 +1,90 @@ +import fastdeploy as fd +import cv2 +import numpy as np + + +# 余弦相似度 +def cosine_similarity(a, b): + a = np.array(a) + b = np.array(b) + mul_a = np.linalg.norm(a, ord=2) + mul_b = np.linalg.norm(b, ord=2) + mul_ab = np.dot(a, b) + return mul_ab / (np.sqrt(mul_a) * np.sqrt(mul_b)) + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of scrfd onnx model.") + parser.add_argument( + "--face", required=True, help="Path of test face image file.") + parser.add_argument( + "--face_positive", + required=True, + help="Path of test face_positive image file.") + parser.add_argument( + "--face_negative", + required=True, + help="Path of test face_negative image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("data", [1, 3, 112, 112]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.faceid.CosFace(args.model, runtime_option=runtime_option) + +# 加载图片 +face0 = cv2.imread(args.face) # 0,1 同一个人 +face1 = cv2.imread(args.face_positive) +face2 = cv2.imread(args.face_negative) # 0,2 不同的人 + +# 设置 l2 normalize +model.l2_normalize = True + +# 预测图片检测结果 +result0 = model.predict(face0) +result1 = model.predict(face1) +result2 = model.predict(face2) + +# 计算余弦相似度 +embedding0 = result0.embedding +embedding1 = result1.embedding +embedding2 = result2.embedding + +cosine01 = cosine_similarity(embedding0, embedding1) +cosine02 = cosine_similarity(embedding0, embedding2) + +# 打印结果 +print(result0, end="") +print(result1, end="") +print(result2, end="") +print("Cosine 01: ", cosine01) +print("Cosine 02: ", cosine02) +print(model.runtime_option) diff --git a/examples/vision/faceid/insightface/python/infer_partial_fc.py b/examples/vision/faceid/insightface/python/infer_partial_fc.py new file mode 100644 index 0000000000..b931af0dff --- /dev/null +++ b/examples/vision/faceid/insightface/python/infer_partial_fc.py @@ -0,0 +1,90 @@ +import fastdeploy as fd +import cv2 +import numpy as np + + +# 余弦相似度 +def cosine_similarity(a, b): + a = np.array(a) + b = np.array(b) + mul_a = np.linalg.norm(a, ord=2) + mul_b = np.linalg.norm(b, ord=2) + mul_ab = np.dot(a, b) + return mul_ab / (np.sqrt(mul_a) * np.sqrt(mul_b)) + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of scrfd onnx model.") + parser.add_argument( + "--face", required=True, help="Path of test face image file.") + parser.add_argument( + "--face_positive", + required=True, + help="Path of test face_positive image file.") + parser.add_argument( + "--face_negative", + required=True, + help="Path of test face_negative image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("data", [1, 3, 112, 112]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.faceid.PartialFC(args.model, runtime_option=runtime_option) + +# 加载图片 +face0 = cv2.imread(args.face) # 0,1 同一个人 +face1 = cv2.imread(args.face_positive) +face2 = cv2.imread(args.face_negative) # 0,2 不同的人 + +# 设置 l2 normalize +model.l2_normalize = True + +# 预测图片检测结果 +result0 = model.predict(face0) +result1 = model.predict(face1) +result2 = model.predict(face2) + +# 计算余弦相似度 +embedding0 = result0.embedding +embedding1 = result1.embedding +embedding2 = result2.embedding + +cosine01 = cosine_similarity(embedding0, embedding1) +cosine02 = cosine_similarity(embedding0, embedding2) + +# 打印结果 +print(result0, end="") +print(result1, end="") +print(result2, end="") +print("Cosine 01: ", cosine01) +print("Cosine 02: ", cosine02) +print(model.runtime_option) diff --git a/examples/vision/faceid/insightface/python/infer_vpl.py b/examples/vision/faceid/insightface/python/infer_vpl.py new file mode 100644 index 0000000000..14c25bfb47 --- /dev/null +++ b/examples/vision/faceid/insightface/python/infer_vpl.py @@ -0,0 +1,90 @@ +import fastdeploy as fd +import cv2 +import numpy as np + + +# 余弦相似度 +def cosine_similarity(a, b): + a = np.array(a) + b = np.array(b) + mul_a = np.linalg.norm(a, ord=2) + mul_b = np.linalg.norm(b, ord=2) + mul_ab = np.dot(a, b) + return mul_ab / (np.sqrt(mul_a) * np.sqrt(mul_b)) + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of scrfd onnx model.") + parser.add_argument( + "--face", required=True, help="Path of test face image file.") + parser.add_argument( + "--face_positive", + required=True, + help="Path of test face_positive image file.") + parser.add_argument( + "--face_negative", + required=True, + help="Path of test face_negative image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("data", [1, 3, 112, 112]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.faceid.VPL(args.model, runtime_option=runtime_option) + +# 加载图片 +face0 = cv2.imread(args.face) # 0,1 同一个人 +face1 = cv2.imread(args.face_positive) +face2 = cv2.imread(args.face_negative) # 0,2 不同的人 + +# 设置 l2 normalize +model.l2_normalize = True + +# 预测图片检测结果 +result0 = model.predict(face0) +result1 = model.predict(face1) +result2 = model.predict(face2) + +# 计算余弦相似度 +embedding0 = result0.embedding +embedding1 = result1.embedding +embedding2 = result2.embedding + +cosine01 = cosine_similarity(embedding0, embedding1) +cosine02 = cosine_similarity(embedding0, embedding2) + +# 打印结果 +print(result0, end="") +print(result1, end="") +print(result2, end="") +print("Cosine 01: ", cosine01) +print("Cosine 02: ", cosine02) +print(model.runtime_option) diff --git a/examples/vision/faceid/partial_fc/README.md b/examples/vision/faceid/partial_fc/README.md deleted file mode 100644 index ca03ba2e79..0000000000 --- a/examples/vision/faceid/partial_fc/README.md +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - -## 下载预训练ONNX模型 - -为了方便开发者的测试,下面提供了RetinaFace导出的各系列模型,开发者可直接下载使用。 - -| 模型 | 大小 | 精度 | -|:---------------------------------------------------------------- |:----- |:----- | -| [partial_fc_glint360k_r50](https://bj.bcebos.com/paddlehub/fastdeploy/partial_fc_glint360k_r50.onnx) | 167MB | - | -| [partial_fc_glint360k_r100](https://bj.bcebos.com/paddlehub/fastdeploy/partial_fc_glint360k_r100.onnx) | 249MB | -| - - - -## 详细部署文档 - -- [Python部署](python) -- [C++部署](cpp) diff --git a/examples/vision/matting/README.md b/examples/vision/matting/README.md new file mode 100644 index 0000000000..1fba41f3e8 --- /dev/null +++ b/examples/vision/matting/README.md @@ -0,0 +1,7 @@ +人脸检测模型 + +FastDeploy目前支持如下人脸识别模型部署 + +| 模型 | 说明 | 模型格式 | 版本 | +| :--- | :--- | :------- | :--- | +| [modnet](./modnet) | MODNet系列模型 | ONNX | CommitID:28165a4 | diff --git a/examples/vision/matting/modnet/README.md b/examples/vision/matting/modnet/README.md index fc3f7c0080..dbeb901fed 100644 --- a/examples/vision/matting/modnet/README.md +++ b/examples/vision/matting/modnet/README.md @@ -2,7 +2,7 @@ ## 模型版本说明 -- [MODNet CommitID:28165a4](https://github.com/ZHKKKe/MODNet/commit/28165a4) +- [MODNet](https://github.com/ZHKKKe/MODNet/commit/28165a4) - (1)[链接中](https://github.com/ZHKKKe/MODNet/commit/28165a4)的*.pt通过[导出ONNX模型](#导出ONNX模型)操作后,可进行部署; - (2)开发者基于自己数据训练的MODNet CommitID:b984b4b模型,可按照[导出ONNX模型](#%E5%AF%BC%E5%87%BAONNX%E6%A8%A1%E5%9E%8B)后,完成部署。 @@ -40,3 +40,8 @@ - [Python部署](python) - [C++部署](cpp) + + +## 版本说明 + +- 本版本文档和代码基于[MODNet CommitID:28165a4](https://github.com/ZHKKKe/MODNet/commit/28165a4) 编写 diff --git a/examples/vision/matting/modnet/cpp/README.md b/examples/vision/matting/modnet/cpp/README.md index 82226ae4c8..64da2e9407 100644 --- a/examples/vision/matting/modnet/cpp/README.md +++ b/examples/vision/matting/modnet/cpp/README.md @@ -12,27 +12,29 @@ ``` mkdir build cd build -wget https://xxx.tgz +wget https://https://bj.bcebos.com/paddlehub/fastdeploy/cpp/fastdeploy-linux-x64-gpu-0.2.0.tgz tar xvf fastdeploy-linux-x64-0.2.0.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 make -j #下载官方转换好的MODNet模型文件和测试图片 -wget https://bj.bcebos.com/paddlehub/fastdeploy/modnet_photographic__portrait_matting.onnx -wget todo + +wget https://bj.bcebos.com/paddlehub/fastdeploy/modnet_photographic_portrait_matting.onnx +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_matting_input.jpg # CPU推理 -./infer_demo modnet_photographic__portrait_matting.onnx todo 0 +./infer_demo modnet_photographic_portrait_matting.onnx test_lite_matting_input.jpg 0 # GPU推理 -./infer_demo modnet_photographic__portrait_matting.onnx todo 1 +./infer_demo modnet_photographic_portrait_matting.onnx test_lite_matting_input.jpg 1 # GPU上TensorRT推理 -./infer_demo modnet_photographic__portrait_matting.onnx todo 2 +./infer_demo modnet_photographic_portrait_matting.onnx test_lite_matting_input.jpg 2 ``` 运行完成可视化结果如下图所示 - + + ## MODNet C++接口 @@ -58,7 +60,7 @@ MODNet模型加载和初始化,其中model_file为导出的ONNX模型格式。 #### Predict函数 > ``` -> MODNet::Predict(cv::Mat* im, DetectionResult* result, +> MODNet::Predict(cv::Mat* im, MattingResult* result, > float conf_threshold = 0.25, > float nms_iou_threshold = 0.5) > ``` @@ -68,17 +70,19 @@ MODNet模型加载和初始化,其中model_file为导出的ONNX模型格式。 > **参数** > > > * **im**: 输入图像,注意需为HWC,BGR格式 -> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **result**: 检测结果,包括检测框,各个框的置信度, MattingResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) > > * **conf_threshold**: 检测框置信度过滤阈值 > > * **nms_iou_threshold**: NMS处理过程中iou阈值 ### 类成员变量 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + -> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(vector<float>): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=ture` 表示不使用填充的方式,默认值为`is_no_pad=false` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=false` -> > * **stride**(int): 配合`stris_mini_pad`成员变量使用, 默认值为`stride=32` +> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[256, 256] +> > * **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 - [模型介绍](../../) - [Python部署](../python) diff --git a/examples/vision/matting/modnet/cpp/infer.cc b/examples/vision/matting/modnet/cpp/infer.cc new file mode 100644 index 0000000000..dbc0e38fa6 --- /dev/null +++ b/examples/vision/matting/modnet/cpp/infer.cc @@ -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. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::matting::MODNet(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + // 设置推理size, 必须和模型文件一致 + model.size = {256, 256}; + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::MattingResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + + auto vis_im = fastdeploy::vision::Visualize::VisMattingAlpha(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::matting::MODNet(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + // 设置推理size, 必须和模型文件一致 + model.size = {256, 256}; + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::MattingResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + + auto vis_im = fastdeploy::vision::Visualize::VisMattingAlpha(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("input", {1, 3, 256, 256}); + auto model = fastdeploy::vision::matting::MODNet(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + // 设置推理size, 必须和模型文件一致 + model.size = {256, 256}; + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::MattingResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + + auto vis_im = fastdeploy::vision::Visualize::VisMattingAlpha(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./modnet_photographic_portrait_matting.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/matting/modnet/python/README.md b/examples/vision/matting/modnet/python/README.md index d7b1149f8e..1b38262a80 100644 --- a/examples/vision/matting/modnet/python/README.md +++ b/examples/vision/matting/modnet/python/README.md @@ -9,25 +9,28 @@ ``` #下载modnet模型文件和测试图片 -wget https://bj.bcebos.com/paddlehub/fastdeploy/modnet_photographic__portrait_matting.onnx -wget todo + +wget https://bj.bcebos.com/paddlehub/fastdeploy/modnet_photographic_portrait_matting.onnx +wget https://raw.githubusercontent.com/DefTruth/lite.ai.toolkit/main/examples/lite/resources/test_lite_matting_input.jpg #下载部署示例代码 git clone https://github.com/PaddlePaddle/FastDeploy.git -cd examples/vison/detection/modnet/python/ +cd examples/vison/matting/modnet/python/ # CPU推理 -python infer.py --model modnet_photographic__portrait_matting.onnx --image todo --device cpu +python infer.py --model modnet_photographic_portrait_matting.onnx --image test_lite_matting_input.jpg --device cpu # GPU推理 -python infer.py --model modnet_photographic__portrait_matting.onnx --image todo --device gpu +python infer.py --model modnet_photographic_portrait_matting.onnx --image test_lite_matting_input.jpg --device gpu # GPU上使用TensorRT推理 -python infer.py --model modnet_photographic__portrait_matting.onnx --image todo --device gpu --use_trt True +python infer.py --model modnet_photographic_portrait_matting.onnx --image test_lite_matting_input.jpg --device gpu --use_trt True + ``` 运行完成可视化结果如下图所示 - + + ## MODNet Python接口 @@ -60,15 +63,17 @@ MODNet模型加载和初始化,其中model_file为导出的ONNX模型格式 > **返回** > -> > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > 返回`fastdeploy.vision.MattingResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) ### 类成员属性 +#### 预处理参数 +用户可按照自己的实际需求,修改下列预处理参数,从而影响最终的推理和部署效果 + -> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] -> > * **padding_value**(list[float]): 通过此参数可以修改图片在resize时候做填充(padding)的值, 包含三个浮点型元素, 分别表示三个通道的值, 默认值为[114, 114, 114] -> > * **is_no_pad**(bool): 通过此参数让图片是否通过填充的方式进行resize, `is_no_pad=True` 表示不使用填充的方式,默认值为`is_no_pad=False` -> > * **is_mini_pad**(bool): 通过此参数可以将resize之后图像的宽高这是为最接近`size`成员变量的值, 并且满足填充的像素大小是可以被`stride`成员变量整除的。默认值为`is_mini_pad=False` -> > * **stride**(int): 配合`stris_mini_padide`成员变量使用, 默认值为`stride=32` +> > * **size**(list[int]): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[256, 256] +> > * **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 diff --git a/examples/vision/matting/modnet/python/infer.py b/examples/vision/matting/modnet/python/infer.py new file mode 100644 index 0000000000..5980e1a1fc --- /dev/null +++ b/examples/vision/matting/modnet/python/infer.py @@ -0,0 +1,53 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of modnet onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("input", [1, 3, 256, 256]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.matting.MODNet(args.model, runtime_option=runtime_option) + +#设置推理size, 必须和模型文件一致 +model.size = (256, 256) +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im.copy()) + +# 预测结果可视化 +vis_im = fd.vision.vis_matting_alpha(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") diff --git a/fastdeploy/vision/evaluation/detection.py b/fastdeploy/vision/evaluation/detection.py index dc958c861e..c1de8ed19c 100644 --- a/fastdeploy/vision/evaluation/detection.py +++ b/fastdeploy/vision/evaluation/detection.py @@ -61,7 +61,7 @@ def eval_detection(model, im = cv2.imread(image_info["image"]) im_id = image_info["im_id"] if conf_threshold is None and nms_iou_threshold is None: - result = model.predict(im) + result = model.predict(im.copy()) else: result = model.predict(im, conf_threshold, nms_iou_threshold) pred = {