[Backend] Add KunlunXin XPU deploy support (#894)

Revert "Revert "[Backend] Add KunlunXin XPU deploy support" (#893)"

This reverts commit 0990ab9b50.
This commit is contained in:
Jason
2022-12-16 11:22:30 +08:00
committed by GitHub
parent 0990ab9b50
commit 2d998223ac
39 changed files with 870 additions and 58 deletions

4
examples/vision/detection/yolov5/cpp/CMakeLists.txt Normal file → Executable file
View File

@@ -12,3 +12,7 @@ include_directories(${FASTDEPLOY_INCS})
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc)
# 添加FastDeploy库依赖
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
add_executable(infer_paddle_demo ${PROJECT_SOURCE_DIR}/infer_paddle_model.cc)
# 添加FastDeploy库依赖
target_link_libraries(infer_paddle_demo ${FASTDEPLOY_LIBS})

24
examples/vision/detection/yolov5/cpp/README.md Normal file → Executable file
View File

@@ -12,16 +12,33 @@
```bash
mkdir build
cd build
# 下载FastDeploy预编译库用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
# 下载 FastDeploy 预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
tar xvf fastdeploy-linux-x64-x.x.x.tgz
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
make -j
#下载官方转换好的yolov5模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx
#下载官方转换好的 yolov5 Paddle 模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s_infer.tar
tar -xvf yolov5s_infer.tar
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
# CPU推理
./infer_paddle_demo yolov5s_infer 000000014439.jpg 0
# GPU推理
./infer_paddle_demo yolov5s_infer 000000014439.jpg 1
# GPU上TensorRT推理
./infer_paddle_demo yolov5s_infer 000000014439.jpg 2
# XPU推理
./infer_paddle_demo yolov5s_infer 000000014439.jpg 3
```
上述的模型为 Paddle 模型的推理,如果想要做 ONNX 模型的推理,可以按照如下步骤:
```bash
# 1. 下载官方转换好的 yolov5 ONNX 模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
# CPU推理
./infer_demo yolov5s.onnx 000000014439.jpg 0
# GPU推理
@@ -29,7 +46,6 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000
# GPU上TensorRT推理
./infer_demo yolov5s.onnx 000000014439.jpg 2
```
运行完成可视化结果如下图所示
<img width="640" src="https://user-images.githubusercontent.com/67993288/184309358-d803347a-8981-44b6-b589-4608021ad0f4.jpg">

2
examples/vision/detection/yolov5/cpp/infer.cc Normal file → Executable file
View File

@@ -102,4 +102,4 @@ int main(int argc, char* argv[]) {
TrtInfer(argv[1], argv[2]);
}
return 0;
}
}

View File

@@ -0,0 +1,154 @@
// 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"
#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endif
void CpuInfer(const std::string& model_dir, const std::string& image_file) {
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
fastdeploy::RuntimeOption option;
option.UseCpu();
auto model = fastdeploy::vision::detection::YOLOv5(
model_file, params_file, option, fastdeploy::ModelFormat::PADDLE);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
auto im = cv::imread(image_file);
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::VisDetection(im, res);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}
void GpuInfer(const std::string& model_dir, const std::string& image_file) {
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
auto option = fastdeploy::RuntimeOption();
option.UseGpu();
auto model = fastdeploy::vision::detection::YOLOv5(
model_file, params_file, option, fastdeploy::ModelFormat::PADDLE);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
auto im = cv::imread(image_file);
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::VisDetection(im, res);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}
void TrtInfer(const std::string& model_dir, const std::string& image_file) {
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
auto option = fastdeploy::RuntimeOption();
option.UseGpu();
option.UseTrtBackend();
option.SetTrtInputShape("images", {1, 3, 640, 640});
auto model = fastdeploy::vision::detection::YOLOv5(
model_file, params_file, option, fastdeploy::ModelFormat::PADDLE);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
auto im = cv::imread(image_file);
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, res);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}
void XpuInfer(const std::string& model_dir, const std::string& image_file) {
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
fastdeploy::RuntimeOption option;
option.UseXpu();
auto model = fastdeploy::vision::detection::YOLOv5(
model_file, params_file, option, fastdeploy::ModelFormat::PADDLE);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
auto im = cv::imread(image_file);
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::VisDetection(im, res);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 4) {
std::cout << "Usage: infer_demo path/to/model path/to/image run_option, "
"e.g ./infer_model ./yolov5s_infer ./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; 3: run with KunlunXin XPU."
<< 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]);
} else if (std::atoi(argv[3]) == 3) {
XpuInfer(argv[1], argv[2]);
}
return 0;
}

11
examples/vision/detection/yolov5/python/README.md Normal file → Executable file
View File

@@ -13,15 +13,18 @@ git clone https://github.com/PaddlePaddle/FastDeploy.git
cd examples/vision/detection/yolov5/python/
#下载yolov5模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s_infer.tar
tar -xf yolov5s_infer.tar
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
# CPU推理
python infer.py --model yolov5s.onnx --image 000000014439.jpg --device cpu
python infer.py --model yolov5s_infer --image 000000014439.jpg --device cpu
# GPU推理
python infer.py --model yolov5s.onnx --image 000000014439.jpg --device gpu
python infer.py --model yolov5s_infer --image 000000014439.jpg --device gpu
# GPU上使用TensorRT推理
python infer.py --model yolov5s.onnx --image 000000014439.jpg --device gpu --use_trt True
python infer.py --model yolov5s_infer --image 000000014439.jpg --device gpu --use_trt True
# XPU推理
python infer.py --model yolov5s_infer --image 000000014439.jpg --device xpu
```
运行完成可视化结果如下图所示

21
examples/vision/detection/yolov5/python/infer.py Normal file → Executable file
View File

@@ -1,20 +1,20 @@
import fastdeploy as fd
import cv2
import os
def parse_arguments():
import argparse
import ast
parser = argparse.ArgumentParser()
parser.add_argument(
"--model", default=None, help="Path of yolov5 onnx model.")
parser.add_argument("--model", default=None, help="Path of yolov5 model.")
parser.add_argument(
"--image", default=None, help="Path of test image file.")
parser.add_argument(
"--device",
type=str,
default='cpu',
help="Type of inference device, support 'cpu' or 'gpu'.")
help="Type of inference device, support 'cpu' or 'gpu' or 'xpu'.")
parser.add_argument(
"--use_trt",
type=ast.literal_eval,
@@ -25,6 +25,8 @@ def parse_arguments():
def build_option(args):
option = fd.RuntimeOption()
if args.device.lower() == "xpu":
option.use_xpu()
if args.device.lower() == "gpu":
option.use_gpu()
@@ -37,14 +39,15 @@ def build_option(args):
args = parse_arguments()
if args.model is None:
model = fd.download_model(name='YOLOv5s')
else:
model = args.model
# 配置runtime加载模型
runtime_option = build_option(args)
model = fd.vision.detection.YOLOv5(model, runtime_option=runtime_option)
model_file = os.path.join(args.model, "model.pdmodel")
params_file = os.path.join(args.model, "model.pdiparams")
model = fd.vision.detection.YOLOv5(
model_file,
params_file,
runtime_option=runtime_option,
model_format=fd.ModelFormat.PADDLE)
# 预测图片检测结果
if args.image is None: