From 854314368ccb959d7e87808866bcbb489b663f6c Mon Sep 17 00:00:00 2001 From: wanziyu Date: Sat, 18 Mar 2023 11:04:27 +0800 Subject: [PATCH 1/2] [PaddlePaddle Hackathon4 No.186] Add PaddleDetection Models Deployment Go Examples Signed-off-by: wanziyu --- examples/application/go/ppyoloe/README.md | 57 ++++++ examples/application/go/ppyoloe/README_CN.md | 56 ++++++ examples/application/go/ppyoloe/infer.go | 185 +++++++++++++++++++ examples/application/go/yolov5/README.md | 56 ++++++ examples/application/go/yolov5/README_CN.md | 55 ++++++ examples/application/go/yolov5/infer.go | 144 +++++++++++++++ examples/application/go/yolov8/README.md | 56 ++++++ examples/application/go/yolov8/README_CN.md | 55 ++++++ examples/application/go/yolov8/infer.go | 150 +++++++++++++++ 9 files changed, 814 insertions(+) create mode 100644 examples/application/go/ppyoloe/README.md create mode 100644 examples/application/go/ppyoloe/README_CN.md create mode 100644 examples/application/go/ppyoloe/infer.go create mode 100644 examples/application/go/yolov5/README.md create mode 100644 examples/application/go/yolov5/README_CN.md create mode 100644 examples/application/go/yolov5/infer.go create mode 100644 examples/application/go/yolov8/README.md create mode 100644 examples/application/go/yolov8/README_CN.md create mode 100644 examples/application/go/yolov8/infer.go diff --git a/examples/application/go/ppyoloe/README.md b/examples/application/go/ppyoloe/README.md new file mode 100644 index 0000000000..906dae8b8d --- /dev/null +++ b/examples/application/go/ppyoloe/README.md @@ -0,0 +1,57 @@ +English | [简体中文](README_CN.md) +# PaddleDetection Golang Deployment Example + +This directory provides examples that `infer.go` uses CGO to call FastDeploy C API and fast finish the deployment of PaddleDetection models, including PPYOLOE on CPU/GPU. + +Before deployment, two steps require confirmation + +- 1. Software and hardware should meet the requirements. Please refer to [FastDeploy Environment Requirements](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) +- 2. Download the precompiled deployment library and samples code according to your development environment. Refer to [FastDeploy Precompiled Library](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) + +Taking inference on Linux as an example, the compilation test can be completed by executing the following command in this directory. FastDeploy version 1.0.4 above (x.x.x>1.0.4) or develop version (x.x.x=0.0.0) is required to support this model. + +### Use Golang and CGO to deploy PPYOLOE model + +Download the FastDeploy precompiled library. Users can choose your appropriate version in the `FastDeploy Precompiled Library` mentioned above. +```bash +wget https://fastdeploy.bj.bcebos.com/dev/cpp/fastdeploy-linux-x64-0.0.0.tgz +tar xvf fastdeploy-linux-x64-0.0.0.tgz +``` + +Copy FastDeploy C APIs from precompiled library to the current directory. +```bash +cp -r fastdeploy-linux-x64-0.0.0/include/fastdeploy_capi . +``` + +Download the PPYOLOE model file and test images. +```bash +wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +tar xvf ppyoloe_crn_l_300e_coco.tgz +``` + +Configure the `cgo CFLAGS: -I` to FastDeploy C API directory path and the `cgo LDFLAGS: -L` to FastDeploy dynamic library path. The FastDeploy dynamic library is located in the `/lib` directory. +```bash +cgo CFLAGS: -I./fastdeploy_capi +cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +``` + +Use the following command to add Fastdeploy library path to the environment variable. +```bash +source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh +``` + +Compile the Go file `infer.go`. +```bash +go build infer.go +``` + +After compiling, use the following command to obtain the predicted results. +```bash +# CPU inference +./infer -model ./ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device 0 +# GPU inference +./infer -model ./ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device 1 +``` + +Then visualized inspection result is saved in the local image `vis_result.jpg`. diff --git a/examples/application/go/ppyoloe/README_CN.md b/examples/application/go/ppyoloe/README_CN.md new file mode 100644 index 0000000000..695f836229 --- /dev/null +++ b/examples/application/go/ppyoloe/README_CN.md @@ -0,0 +1,56 @@ +[English](README.md) | 简体中文 +# PaddleDetection Golang 部署示例 + +本目录下提供`infer.go`, 使用CGO调用FastDeploy C API快速完成PaddleDetection模型PPYOLOE在CPU/GPU上部署的示例 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) +- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) + +以Linux上推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本1.0.4以上(x.x.x>1.0.4)或FastDeploy的Develop版本(x.x.x=0.0.0) +### 使用Golang和CGO工具进行PPYOLOE模型推理部署 + +在当前目录下,下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用 +```bash +wget https://fastdeploy.bj.bcebos.com/dev/cpp/fastdeploy-linux-x64-0.0.0.tgz +tar xvf fastdeploy-linux-x64-0.0.0.tgz +``` + +将FastDeploy C API文件拷贝至当前目录 +```bash +cp -r fastdeploy-linux-x64-0.0.0/include/fastdeploy_capi . +``` + +下载PPYOLOE模型文件和测试图片 +```bash +wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +tar xvf ppyoloe_crn_l_300e_coco.tgz +``` + +配置`infer.go`中的`cgo CFLAGS: -I`参数配置为C API文件路径,`cgo LDFLAGS: -L`参数配置为FastDeploy的动态库路径,动态库位于预编译库的`/lib`目录中 +```bash +cgo CFLAGS: -I./fastdeploy_capi +cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +``` + +将FastDeploy的库路径添加到环境变量 +```bash +source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh +``` + +编译Go文件`infer.go` +```bash +go build infer.go +``` + +编译完成后,使用如下命令执行可得到预测结果 +```bash +# CPU推理 +./infer -model ./ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device 0 +# GPU推理 +./infer -model ./ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device 1 +``` + +可视化的检测结果图片保存在本地`vis_result.jpg` diff --git a/examples/application/go/ppyoloe/infer.go b/examples/application/go/ppyoloe/infer.go new file mode 100644 index 0000000000..77d309885a --- /dev/null +++ b/examples/application/go/ppyoloe/infer.go @@ -0,0 +1,185 @@ +// Copyright (c) 2023 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. + +package main + +// #cgo CFLAGS: -I./fastdeploy_capi +// #cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +// #include +// #include +// #include +// #include +/* +#include +#ifdef WIN32 +const char sep = '\\'; +#else +const char sep = '/'; +#endif + +char* GetModelFilePath(char* model_dir, char* model_file, int max_size){ + snprintf(model_file, max_size, "%s%c%s", model_dir, sep, "model.pdmodel"); + return model_file; +} + +char* GetParametersFilePath(char* model_dir, char* params_file, int max_size){ + snprintf(params_file, max_size, "%s%c%s", model_dir, sep, "model.pdiparams"); + return params_file; +} + +char* GetConfigFilePath(char* model_dir, char* config_file, int max_size){ + snprintf(config_file, max_size, "%s%c%s", model_dir, sep, "infer_cfg.yml"); + return config_file; +} +*/ +import "C" +import ( + "flag" + "fmt" + "unsafe" +) + +func FDBooleanToGo(b C.FD_C_Bool) bool { + var cFalse C.FD_C_Bool + if b != cFalse { + return true + } + return false +} + +func CpuInfer(modelDir *C.char, imageFile *C.char) { + + var modelFile = (*C.char)(C.malloc(C.size_t(100))) + var paramsFile = (*C.char)(C.malloc(C.size_t(100))) + var configFile = (*C.char)(C.malloc(C.size_t(100))) + var maxSize = 99 + + modelFile = C.GetModelFilePath(modelDir, modelFile, C.int(maxSize)) + paramsFile = C.GetParametersFilePath(modelDir, paramsFile, C.int(maxSize)) + configFile = C.GetConfigFilePath(modelDir, configFile, C.int(maxSize)) + + var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() + C.FD_C_RuntimeOptionWrapperUseCpu(option) + + var model *C.FD_C_PPYOLOEWrapper = C.FD_C_CreatePPYOLOEWrapper( + modelFile, paramsFile, configFile, option, C.FD_C_ModelFormat_PADDLE) + + if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperInitialized(model)) { + fmt.Printf("Failed to initialize.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyPPYOLOEWrapper(model) + return + } + + var image C.FD_C_Mat = C.FD_C_Imread(imageFile) + + var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() + + if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperPredict(model, image, result)) { + fmt.Printf("Failed to predict.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyPPYOLOEWrapper(model) + C.FD_C_DestroyMat(image) + C.free(unsafe.Pointer(result)) + return + } + + var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) + + C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) + fmt.Printf("Visualized result saved in ./vis_result.jpg\n") + + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyPPYOLOEWrapper(model) + C.FD_C_DestroyDetectionResult(result) + C.FD_C_DestroyMat(image) + C.FD_C_DestroyMat(visImage) +} + +func GpuInfer(modelDir *C.char, imageFile *C.char) { + + var modelFile = (*C.char)(C.malloc(C.size_t(100))) + var paramsFile = (*C.char)(C.malloc(C.size_t(100))) + var configFile = (*C.char)(C.malloc(C.size_t(100))) + var maxSize = 99 + + modelFile = C.GetModelFilePath(modelDir, modelFile, C.int(maxSize)) + paramsFile = C.GetParametersFilePath(modelDir, paramsFile, C.int(maxSize)) + configFile = C.GetConfigFilePath(modelDir, configFile, C.int(maxSize)) + + var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() + C.FD_C_RuntimeOptionWrapperUseGpu(option, 0) + + var model *C.FD_C_PPYOLOEWrapper = C.FD_C_CreatePPYOLOEWrapper( + modelFile, paramsFile, configFile, option, C.FD_C_ModelFormat_PADDLE) + + if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperInitialized(model)) { + fmt.Printf("Failed to initialize.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyPPYOLOEWrapper(model) + return + } + + var image C.FD_C_Mat = C.FD_C_Imread(imageFile) + + var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() + + if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperPredict(model, image, result)) { + fmt.Printf("Failed to predict.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyPPYOLOEWrapper(model) + C.FD_C_DestroyMat(image) + C.free(unsafe.Pointer(result)) + return + } + + var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) + + C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) + fmt.Printf("Visualized result saved in ./vis_result.jpg\n") + + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyPPYOLOEWrapper(model) + C.FD_C_DestroyDetectionResult(result) + C.FD_C_DestroyMat(image) + C.FD_C_DestroyMat(visImage) +} + +var ( + modelDir string + imageFile string + deviceType int +) + +func init() { + flag.StringVar(&modelDir, "model", "", "paddle detection model to use") + flag.StringVar(&imageFile, "image", "", "image to predict") + flag.IntVar(&deviceType, "device", 0, "The data type of run_option is int, 0: run with cpu; 1: run with gpu") +} + +func main() { + flag.Parse() + + if modelDir != "" && imageFile != "" { + if deviceType == 0 { + CpuInfer(C.CString(modelDir), C.CString(imageFile)) + } else if deviceType == 1 { + GpuInfer(C.CString(modelDir), C.CString(imageFile)) + } + } else { + fmt.Printf("Usage: ./infer -model path/to/model_dir -image path/to/image -device run_option \n") + fmt.Printf("e.g ./infer -model ./ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device 0 \n") + } + +} diff --git a/examples/application/go/yolov5/README.md b/examples/application/go/yolov5/README.md new file mode 100644 index 0000000000..9d71cb1adc --- /dev/null +++ b/examples/application/go/yolov5/README.md @@ -0,0 +1,56 @@ +English | [简体中文](README_CN.md) +# YOLOv5 Golang Deployment Example + +This directory provides examples that `infer.go` uses CGO to call FastDeploy C API and finish the deployment of YOLOv5 model on CPU/GPU. + +Before deployment, two steps require confirmation + +- 1. Software and hardware should meet the requirements. Please refer to [FastDeploy Environment Requirements](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) +- 2. Download the precompiled deployment library and samples code according to your development environment. Refer to [FastDeploy Precompiled Library](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) + +Taking inference on Linux as an example, the compilation test can be completed by executing the following command in this directory. FastDeploy version 1.0.4 above (x.x.x>1.0.4) or develop version (x.x.x=0.0.0) is required to support this model. + +### Use Golang and CGO to deploy YOLOv5 model + +Download the FastDeploy precompiled library. Users can choose your appropriate version in the `FastDeploy Precompiled Library` mentioned above. +```bash +wget https://fastdeploy.bj.bcebos.com/dev/cpp/fastdeploy-linux-x64-0.0.0.tgz +tar xvf fastdeploy-linux-x64-0.0.0.tgz +``` + +Copy FastDeploy C APIs from precompiled library to the current directory. +```bash +cp -r fastdeploy-linux-x64-0.0.0/include/fastdeploy_capi . +``` + +Download the YOLOv5 ONNX model file and test images +```bash +wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +``` + +Configure the `cgo CFLAGS: -I` to FastDeploy C API directory path and the `cgo LDFLAGS: -L` to FastDeploy dynamic library path. The FastDeploy dynamic library is located in the `/lib` directory. +```bash +cgo CFLAGS: -I./fastdeploy_capi +cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +``` + +Use the following command to add Fastdeploy library path to the environment variable. +```bash +source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh +``` + +Compile the Go file `infer.go`. +```bash +go build infer.go +``` + +After compiling, use the following command to obtain the predicted results. +```bash +# CPU inference +./infer -model yolov5s.onnx -image 000000014439.jpg -device 0 +# GPU inference +./infer -model yolov5s.onnx -image 000000014439.jpg -device 1 +``` + +Then visualized inspection result is saved in the local image `vis_result.jpg`. diff --git a/examples/application/go/yolov5/README_CN.md b/examples/application/go/yolov5/README_CN.md new file mode 100644 index 0000000000..0da72e9eb4 --- /dev/null +++ b/examples/application/go/yolov5/README_CN.md @@ -0,0 +1,55 @@ +[English](README.md) | 简体中文 +# YOLOv5 Golang 部署示例 + +本目录下提供`infer.go`, 使用CGO调用FastDeploy C API快速完成YOLOv5模型在CPU/GPU上部署的示例 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) +- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) + +以Linux上推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本1.0.4以上(x.x.x>1.0.4)或FastDeploy的Develop版本(x.x.x=0.0.0) +### 使用Golang和CGO工具进行YOLOv5模型推理部署 + +在当前目录下,下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用 +```bash +wget https://fastdeploy.bj.bcebos.com/dev/cpp/fastdeploy-linux-x64-0.0.0.tgz +tar xvf fastdeploy-linux-x64-0.0.0.tgz +``` + +将FastDeploy C API文件拷贝至当前目录 +```bash +cp -r fastdeploy-linux-x64-0.0.0/include/fastdeploy_capi . +``` + +下载官方转换好的 YOLOv5 ONNX 模型文件和测试图片 +```bash +wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +``` + +配置`infer.go`中的`cgo CFLAGS: -I`参数配置为C API文件路径,`cgo LDFLAGS: -L`参数配置为FastDeploy的动态库路径,动态库位于预编译库的`/lib`目录中 +```bash +cgo CFLAGS: -I./fastdeploy_capi +cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +``` + +将FastDeploy的库路径添加到环境变量 +```bash +source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh +``` + +编译Go文件`infer.go` +```bash +go build infer.go +``` + +编译完成后,使用如下命令执行可得到预测结果 +```bash +# CPU推理 +./infer -model yolov5s.onnx -image 000000014439.jpg -device 0 +# GPU推理 +./infer -model yolov5s.onnx -image 000000014439.jpg -device 1 +``` + +可视化的检测结果图片保存在本地`vis_result.jpg` diff --git a/examples/application/go/yolov5/infer.go b/examples/application/go/yolov5/infer.go new file mode 100644 index 0000000000..4956eca465 --- /dev/null +++ b/examples/application/go/yolov5/infer.go @@ -0,0 +1,144 @@ +// Copyright (c) 2023 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. + +package main + +// #cgo CFLAGS: -I./fastdeploy_capi +// #cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +// #include +// #include +// #include +// #include +import "C" +import ( + "flag" + "fmt" + "unsafe" +) + +func FDBooleanToGo(b C.FD_C_Bool) bool { + var cFalse C.FD_C_Bool + if b != cFalse { + return true + } + return false +} + +func CpuInfer(modelFile *C.char, imageFile *C.char) { + + var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() + C.FD_C_RuntimeOptionWrapperUseCpu(option) + + var model *C.FD_C_YOLOv5Wrapper = C.FD_C_CreateYOLOv5Wrapper( + modelFile, C.CString(""), option, C.FD_C_ModelFormat_ONNX) + + if !FDBooleanToGo(C.FD_C_YOLOv5WrapperInitialized(model)) { + fmt.Printf("Failed to initialize.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv5Wrapper(model) + return + } + + var image C.FD_C_Mat = C.FD_C_Imread(imageFile) + + var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() + + if !FDBooleanToGo(C.FD_C_YOLOv5WrapperPredict(model, image, result)) { + fmt.Printf("Failed to predict.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv5Wrapper(model) + C.FD_C_DestroyMat(image) + C.free(unsafe.Pointer(result)) + return + } + + var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) + + C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) + fmt.Printf("Visualized result saved in ./vis_result.jpg\n") + + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv5Wrapper(model) + C.FD_C_DestroyDetectionResult(result) + C.FD_C_DestroyMat(image) + C.FD_C_DestroyMat(visImage) +} + +func GpuInfer(modelFile *C.char, imageFile *C.char) { + + var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() + C.FD_C_RuntimeOptionWrapperUseGpu(option, 0) + + var model *C.FD_C_YOLOv5Wrapper = C.FD_C_CreateYOLOv5Wrapper( + modelFile, C.CString(""), option, C.FD_C_ModelFormat_ONNX) + + if !FDBooleanToGo(C.FD_C_YOLOv5WrapperInitialized(model)) { + fmt.Printf("Failed to initialize.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv5Wrapper(model) + return + } + + var image C.FD_C_Mat = C.FD_C_Imread(imageFile) + + var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() + + if !FDBooleanToGo(C.FD_C_YOLOv5WrapperPredict(model, image, result)) { + fmt.Printf("Failed to predict.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv5Wrapper(model) + C.FD_C_DestroyMat(image) + C.free(unsafe.Pointer(result)) + return + } + + var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) + + C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) + fmt.Printf("Visualized result saved in ./vis_result.jpg\n") + + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv5Wrapper(model) + C.FD_C_DestroyDetectionResult(result) + C.FD_C_DestroyMat(image) + C.FD_C_DestroyMat(visImage) +} + +var ( + modelFile string + imageFile string + deviceType int +) + +func init() { + flag.StringVar(&modelFile, "model", "", "paddle detection model to use") + flag.StringVar(&imageFile, "image", "", "image to predict") + flag.IntVar(&deviceType, "device", 0, "The data type of run_option is int, 0: run with cpu; 1: run with gpu") +} + +func main() { + flag.Parse() + + if modelFile != "" && imageFile != "" { + if deviceType == 0 { + CpuInfer(C.CString(modelFile), C.CString(imageFile)) + } else if deviceType == 1 { + GpuInfer(C.CString(modelFile), C.CString(imageFile)) + } + } else { + fmt.Printf("Usage: ./infer -model path/to/model_dir -image path/to/image -device run_option \n") + fmt.Printf("e.g ./infer -model yolov5s.onnx -image 000000014439.jpg -device 0 \n") + } + +} diff --git a/examples/application/go/yolov8/README.md b/examples/application/go/yolov8/README.md new file mode 100644 index 0000000000..eeea436d78 --- /dev/null +++ b/examples/application/go/yolov8/README.md @@ -0,0 +1,56 @@ +English | [简体中文](README_CN.md) +# YOLOv8 Golang Deployment Example + +This directory provides examples that `infer.go` uses CGO to call FastDeploy C API and finish the deployment of YOLOv8 model on CPU/GPU. + +Before deployment, two steps require confirmation + +- 1. Software and hardware should meet the requirements. Please refer to [FastDeploy Environment Requirements](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) +- 2. Download the precompiled deployment library and samples code according to your development environment. Refer to [FastDeploy Precompiled Library](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) + +Taking inference on Linux as an example, the compilation test can be completed by executing the following command in this directory. FastDeploy version 1.0.4 above (x.x.x>1.0.4) or develop version (x.x.x=0.0.0) is required to support this model. + +### Use Golang and CGO to deploy YOLOv8 model + +Download the FastDeploy precompiled library. Users can choose your appropriate version in the `FastDeploy Precompiled Library` mentioned above. +```bash +wget https://fastdeploy.bj.bcebos.com/dev/cpp/fastdeploy-linux-x64-0.0.0.tgz +tar xvf fastdeploy-linux-x64-0.0.0.tgz +``` + +Copy FastDeploy C APIs from precompiled library to the current directory. +```bash +cp -r fastdeploy-linux-x64-0.0.0/include/fastdeploy_capi . +``` + +Download the YOLOv8 ONNX model file and test images +```bash +wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov8s.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +``` + +Configure the `cgo CFLAGS: -I` to FastDeploy C API directory path and the `cgo LDFLAGS: -L` to FastDeploy dynamic library path. The FastDeploy dynamic library is located in the `/lib` directory. +```bash +cgo CFLAGS: -I./fastdeploy_capi +cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +``` + +Use the following command to add Fastdeploy library path to the environment variable. +```bash +source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh +``` + +Compile the Go file `infer.go`. +```bash +go build infer.go +``` + +After compiling, use the following command to obtain the predicted results. +```bash +# CPU inference +./infer -model yolov8s.onnx -image 000000014439.jpg -device 0 +# GPU inference +./infer -model yolov8s.onnx -image 000000014439.jpg -device 1 +``` + +Then visualized inspection result is saved in the local image `vis_result.jpg`. diff --git a/examples/application/go/yolov8/README_CN.md b/examples/application/go/yolov8/README_CN.md new file mode 100644 index 0000000000..d556bb12ad --- /dev/null +++ b/examples/application/go/yolov8/README_CN.md @@ -0,0 +1,55 @@ +[English](README.md) | 简体中文 +# YOLOv8 Golang 部署示例 + +本目录下提供`infer.go`, 使用CGO调用FastDeploy C API快速完成YOLOv8模型在CPU/GPU上部署的示例 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) +- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) + +以Linux上推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本1.0.4以上(x.x.x>1.0.4)或FastDeploy的Develop版本(x.x.x=0.0.0) +### 使用Golang和CGO工具进行YOLOv8模型推理部署 + +在当前目录下,下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用 +```bash +wget https://fastdeploy.bj.bcebos.com/dev/cpp/fastdeploy-linux-x64-0.0.0.tgz +tar xvf fastdeploy-linux-x64-0.0.0.tgz +``` + +将FastDeploy C API文件拷贝至当前目录 +```bash +cp -r fastdeploy-linux-x64-0.0.0/include/fastdeploy_capi . +``` + +下载官方转换好的 YOLOv8 ONNX 模型文件和测试图片 +```bash +wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov8s.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +``` + +配置`infer.go`中的`cgo CFLAGS: -I`参数配置为C API文件路径,`cgo LDFLAGS: -L`参数配置为FastDeploy的动态库路径,动态库位于预编译库的`/lib`目录中 +```bash +cgo CFLAGS: -I./fastdeploy_capi +cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +``` + +将FastDeploy的库路径添加到环境变量 +```bash +source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh +``` + +编译Go文件`infer.go` +```bash +go build infer.go +``` + +编译完成后,使用如下命令执行可得到预测结果 +```bash +# CPU推理 +./infer -model yolov8s.onnx -image 000000014439.jpg -device 0 +# GPU推理 +./infer -model yolov8s.onnx -image 000000014439.jpg -device 1 +``` + +可视化的检测结果图片保存在本地`vis_result.jpg` diff --git a/examples/application/go/yolov8/infer.go b/examples/application/go/yolov8/infer.go new file mode 100644 index 0000000000..6959ff080b --- /dev/null +++ b/examples/application/go/yolov8/infer.go @@ -0,0 +1,150 @@ +// Copyright (c) 2023 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. + +package main + +// #cgo CFLAGS: -I./fastdeploy_capi +// #cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy +//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/onnxruntime/lib -lonnxruntime +//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/openvino/runtime/lib -lopenvino +//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/paddle2onnx/lib -lpaddle2onnx +//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/paddle_inference/paddle/lib -lpaddle_inference +//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/fast_tokenizer/lib -lcore_tokenizers +//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/examples/vision/detection/paddledetection/c/build/fastdeploy-linux-x64-1.0.4/third_libs/install/opencv/lib64 +// #include +// #include +// #include +// #include +import "C" +import ( + "flag" + "fmt" + "unsafe" +) + +func FDBooleanToGo(b C.FD_C_Bool) bool { + var cFalse C.FD_C_Bool + if b != cFalse { + return true + } + return false +} + +func CpuInfer(modelFile *C.char, imageFile *C.char) { + + var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() + C.FD_C_RuntimeOptionWrapperUseCpu(option) + + var model *C.FD_C_YOLOv8Wrapper = C.FD_C_CreateYOLOv8Wrapper( + modelFile, C.CString(""), option, C.FD_C_ModelFormat_ONNX) + + if !FDBooleanToGo(C.FD_C_YOLOv8WrapperInitialized(model)) { + fmt.Printf("Failed to initialize.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv8Wrapper(model) + return + } + + var image C.FD_C_Mat = C.FD_C_Imread(imageFile) + + var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() + + if !FDBooleanToGo(C.FD_C_YOLOv8WrapperPredict(model, image, result)) { + fmt.Printf("Failed to predict.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv8Wrapper(model) + C.FD_C_DestroyMat(image) + C.free(unsafe.Pointer(result)) + return + } + + var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) + + C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) + fmt.Printf("Visualized result saved in ./vis_result.jpg\n") + + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv8Wrapper(model) + C.FD_C_DestroyDetectionResult(result) + C.FD_C_DestroyMat(image) + C.FD_C_DestroyMat(visImage) +} + +func GpuInfer(modelFile *C.char, imageFile *C.char) { + + var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() + C.FD_C_RuntimeOptionWrapperUseGpu(option, 0) + + var model *C.FD_C_YOLOv8Wrapper = C.FD_C_CreateYOLOv8Wrapper( + modelFile, C.CString(""), option, C.FD_C_ModelFormat_ONNX) + + if !FDBooleanToGo(C.FD_C_YOLOv8WrapperInitialized(model)) { + fmt.Printf("Failed to initialize.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv8Wrapper(model) + return + } + + var image C.FD_C_Mat = C.FD_C_Imread(imageFile) + + var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() + + if !FDBooleanToGo(C.FD_C_YOLOv8WrapperPredict(model, image, result)) { + fmt.Printf("Failed to predict.\n") + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv8Wrapper(model) + C.FD_C_DestroyMat(image) + C.free(unsafe.Pointer(result)) + return + } + + var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) + + C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) + fmt.Printf("Visualized result saved in ./vis_result.jpg\n") + + C.FD_C_DestroyRuntimeOptionWrapper(option) + C.FD_C_DestroyYOLOv8Wrapper(model) + C.FD_C_DestroyDetectionResult(result) + C.FD_C_DestroyMat(image) + C.FD_C_DestroyMat(visImage) +} + +var ( + modelFile string + imageFile string + deviceType int +) + +func init() { + flag.StringVar(&modelFile, "model", "", "paddle detection model to use ") + flag.StringVar(&imageFile, "image", "", "image to predict") + flag.IntVar(&deviceType, "device", 0, "The data type of run_option is int, 0: run with cpu; 1: run with gpu") +} + +func main() { + flag.Parse() + + if modelFile != "" && imageFile != "" { + if deviceType == 0 { + CpuInfer(C.CString(modelFile), C.CString(imageFile)) + } else if deviceType == 1 { + GpuInfer(C.CString(modelFile), C.CString(imageFile)) + } + } else { + fmt.Printf("Usage: ./infer -model path/to/model_dir -image path/to/image -device run_option \n") + fmt.Printf("e.g ./infer -model yolov8s.onnx -image 000000014439.jpg -device 0 \n") + } + +} From 6353ff334f69abc5554ec486ddc81a3d2b7321b7 Mon Sep 17 00:00:00 2001 From: wanziyu Date: Sun, 19 Mar 2023 11:50:52 +0800 Subject: [PATCH 2/2] Fix YOLOv8 Deployment Go Example Signed-off-by: wanziyu --- examples/application/go/yolov8/infer.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/examples/application/go/yolov8/infer.go b/examples/application/go/yolov8/infer.go index 6959ff080b..4b100410d2 100644 --- a/examples/application/go/yolov8/infer.go +++ b/examples/application/go/yolov8/infer.go @@ -16,12 +16,6 @@ package main // #cgo CFLAGS: -I./fastdeploy_capi // #cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy -//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/onnxruntime/lib -lonnxruntime -//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/openvino/runtime/lib -lopenvino -//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/paddle2onnx/lib -lpaddle2onnx -//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/paddle_inference/paddle/lib -lpaddle_inference -//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/build/compiled_fastdeploy_sdk/third_libs/install/fast_tokenizer/lib -lcore_tokenizers -//// #cgo LDFLAGS: -L/home/wanziyu/Paddle/FastDeploy/examples/vision/detection/paddledetection/c/build/fastdeploy-linux-x64-1.0.4/third_libs/install/opencv/lib64 // #include // #include // #include