mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-06 00:57:33 +08:00
[Example] Merge Download Paddle Model, Paddle->ONNX->MLIR->BModel (#1643)
* fix infer.py and README * [Example] Merge Download Paddle Model, Paddle->Onnx->Mlir->Bmodel and inference into infer.py. Modify README.md * modify pp_liteseg sophgo infer.py and README.md * fix PPOCR,PPYOLOE,PICODET,LITESEG sophgo infer.py and README.md * fix memory overflow problem while inferring with sophgo backend * fix memory overflow problem while inferring with sophgo backend --------- Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: xuyizhou <yizhou.xu@sophgo.com>
This commit is contained in:
@@ -15,13 +15,21 @@ cd FastDeploy/examples/vision/detection/paddledetection/sophgo/python
|
||||
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
|
||||
|
||||
# 推理
|
||||
#ppyoloe推理示例
|
||||
python3 infer_ppyoloe.py --model_file model/ppyoloe_crn_s_300e_coco_1684x_f32.bmodel --config_file model/infer_cfg.yml --image ./000000014439.jpg
|
||||
# ppyoloe推理示例
|
||||
# 指定--auto True,自动完成模型准备、转换和推理,需要指定PaddleDetection路径
|
||||
python3 infer_ppyoloe.py --auto True --pp_detect_path {Path to PaddleDetection} --model_file '' --config_file '' --image ''
|
||||
|
||||
#picodet推理示例
|
||||
python3 infer_picodet.py --model_file model/picodet_s_416_coco_lcnet_1684x_f32.bmodel --config_file model/infer_cfg.yml --image ./000000014439.jpg
|
||||
# 指定--auto False,需要用户指定模型、配置文件和图片路径,不需要指定PaddleDetection路径。
|
||||
python3 infer_ppyoloe.py --auto False --pp_detect_path '' --model_file model/ppyoloe_crn_s_300e_coco_1684x_f32.bmodel --config_file model/infer_cfg.yml --image ./000000014439.jpg
|
||||
|
||||
#yolov8推理示例
|
||||
# picodet推理示例
|
||||
# 指定--auto True,自动完成模型准备、转换和推理,需要指定PaddleDetection路径
|
||||
python3 infer_picodet.py --auto True --pp_detect_path {Path to PaddleDetection} --model_file '' --config_file '' --image ''
|
||||
|
||||
# 指定--auto False,需要用户指定模型、配置文件和图片路径,不需要指定PaddleDetection路径。
|
||||
python3 infer_picodet.py --auto False --pp_detect_path '' --model_file model/ppyoloe_crn_s_300e_coco_1684x_f32.bmodel --config_file model/infer_cfg.yml --image ./000000014439.jpg
|
||||
|
||||
# yolov8推理示例
|
||||
python3 infer_yolov8.py --model_file model/yolov8s_s_300e_coco_1684x_f32.bmodel --config_file model/infer_cfg.yml --image ./000000014439.jpg
|
||||
# 运行完成后返回结果如下所示
|
||||
可视化结果存储在sophgo_result.jpg中
|
||||
|
@@ -14,12 +14,101 @@
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
from subprocess import run
|
||||
from prepare_npz import prepare
|
||||
|
||||
def export_model(args):
|
||||
PPDetection_path = args.pp_detect_path
|
||||
|
||||
export_str = 'python3 tools/export_model.py \
|
||||
-c configs/picodet/picodet_s_320_coco_lcnet.yml \
|
||||
--output_dir=output_inference \
|
||||
-o weights=https://paddledet.bj.bcebos.com/models/picodet_s_320_coco_lcnet.pdparams'
|
||||
cur_path = os.getcwd()
|
||||
os.chdir(PPDetection_path)
|
||||
print(export_str)
|
||||
run(export_str, shell=True)
|
||||
cp_str = 'cp -r ./output_inference/picodet_s_320_coco_lcnet ' + cur_path
|
||||
print(cp_str)
|
||||
run(cp_str, shell=True)
|
||||
os.chdir(cur_path)
|
||||
|
||||
def paddle2onnx():
|
||||
convert_str = 'paddle2onnx --model_dir picodet_s_320_coco_lcnet/ \
|
||||
--model_filename model.pdmodel \
|
||||
--params_filename model.pdiparams \
|
||||
--save_file picodet_s_320_coco_lcnet.onnx \
|
||||
--enable_dev_version True'
|
||||
print(convert_str)
|
||||
run(convert_str, shell=True)
|
||||
fix_shape_str = 'python3 -m paddle2onnx.optimize \
|
||||
--input_model picodet_s_320_coco_lcnet.onnx \
|
||||
--output_model picodet_s_320_coco_lcnet.onnx \
|
||||
--input_shape_dict "{\'image\':[1,3,640,640]}"'
|
||||
print(fix_shape_str)
|
||||
run(fix_shape_str, shell=True)
|
||||
|
||||
def mlir_prepare():
|
||||
mlir_path = os.getenv("MODEL_ZOO_PATH")
|
||||
mlir_path = mlir_path[:-13]
|
||||
regression_path = os.path.join(mlir_path, 'regression')
|
||||
mv_str_list = ['mkdir picodet',
|
||||
'cp -rf ' + os.path.join(regression_path, 'dataset/COCO2017/') + ' ./picodet',
|
||||
'cp -rf ' + os.path.join(regression_path, 'image/') + ' ./picodet',
|
||||
'cp picodet_s_320_coco_lcnet.onnx ./picodet',
|
||||
'mkdir ./picodet/workspace']
|
||||
for str in mv_str_list:
|
||||
print(str)
|
||||
run(str, shell=True)
|
||||
|
||||
def image_prepare():
|
||||
img_str = 'wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg'
|
||||
if not os.path.exists('000000014439.jpg'):
|
||||
print(img_str)
|
||||
run(img_str, shell=True)
|
||||
prepare('000000014439.jpg', [320, 320])
|
||||
cp_npz_str = 'cp ./inputs.npz ./picodet'
|
||||
print(cp_npz_str)
|
||||
run(cp_npz_str, shell=True)
|
||||
|
||||
def onnx2mlir():
|
||||
transform_str = 'model_transform.py \
|
||||
--model_name picodet_s_320_coco_lcnet \
|
||||
--model_def ../picodet_s_320_coco_lcnet.onnx \
|
||||
--input_shapes [[1,3,320,320],[1,2]] \
|
||||
--keep_aspect_ratio \
|
||||
--pixel_format rgb \
|
||||
--output_names p2o.Div.79,p2o.Concat.9 \
|
||||
--test_input ../inputs.npz \
|
||||
--test_result picodet_s_320_coco_lcnet_top_outputs.npz \
|
||||
--mlir picodet_s_320_coco_lcnet.mlir'
|
||||
os.chdir('./picodet/workspace')
|
||||
print(transform_str)
|
||||
run(transform_str, shell=True)
|
||||
os.chdir('../../')
|
||||
|
||||
def mlir2bmodel():
|
||||
deploy_str = 'model_deploy.py \
|
||||
--mlir picodet_s_320_coco_lcnet.mlir \
|
||||
--quantize F32 \
|
||||
--chip bm1684x \
|
||||
--test_input picodet_s_320_coco_lcnet_in_f32.npz \
|
||||
--test_reference picodet_s_320_coco_lcnet_top_outputs.npz \
|
||||
--model picodet_s_320_coco_lcnet_1684x_f32.bmodel'
|
||||
os.chdir('./picodet/workspace')
|
||||
print(deploy_str)
|
||||
run(deploy_str, shell=True)
|
||||
os.chdir('../../')
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--auto", required=True, help="Auto download, convert, compile and infer if True")
|
||||
parser.add_argument(
|
||||
"--pp_detect_path", default='/workspace/PaddleDetection', help="Path of PaddleDetection folder")
|
||||
parser.add_argument(
|
||||
"--model_file", required=True, help="Path of sophgo model.")
|
||||
parser.add_argument("--config_file", required=True, help="Path of config.")
|
||||
@@ -31,10 +120,18 @@ def parse_arguments():
|
||||
if __name__ == "__main__":
|
||||
args = parse_arguments()
|
||||
|
||||
model_file = args.model_file
|
||||
params_file = ""
|
||||
config_file = args.config_file
|
||||
if args.auto:
|
||||
export_model()
|
||||
paddle2onnx()
|
||||
mlir_prepare()
|
||||
image_prepare()
|
||||
onnx2mlir()
|
||||
mlir2bmodel()
|
||||
|
||||
model_file = './picodet/workspace/picodet_s_320_coco_lcnet_1684x_f32.bmodel' if args.auto else args.model_file
|
||||
params_file = ""
|
||||
config_file = './picodet_s_320_coco_lcnet/infer_cfg.yml' if args.auto else args.config_file
|
||||
img_file = './000000014439.jpg' if args.auto else args.image
|
||||
# 配置runtime,加载模型
|
||||
runtime_option = fd.RuntimeOption()
|
||||
runtime_option.use_sophgo()
|
||||
@@ -46,14 +143,14 @@ if __name__ == "__main__":
|
||||
runtime_option=runtime_option,
|
||||
model_format=fd.ModelFormat.SOPHGO)
|
||||
|
||||
model.postprocessor.apply_decode_and_nms()
|
||||
model.postprocessor.apply_nms()
|
||||
|
||||
# 预测图片分割结果
|
||||
im = cv2.imread(args.image)
|
||||
im = cv2.imread(img_file)
|
||||
result = model.predict(im)
|
||||
print(result)
|
||||
|
||||
# 可视化结果
|
||||
vis_im = fd.vision.vis_detection(im, result, score_threshold=0.5)
|
||||
cv2.imwrite("sophgo_result.jpg", vis_im)
|
||||
print("Visualized result save in ./sophgo_result.jpg")
|
||||
print("Visualized result save in ./sophgo_result_picodet.jpg")
|
||||
|
@@ -14,12 +14,17 @@
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
|
||||
from subprocess import run
|
||||
from prepare_npz import prepare
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--auto", required=True, help="Auto download, convert, compile and infer if True")
|
||||
parser.add_argument(
|
||||
"--pp_detect_path", default='/workspace/PaddleDetection', help="Path of PaddleDetection folder")
|
||||
parser.add_argument(
|
||||
"--model_file", required=True, help="Path of sophgo model.")
|
||||
parser.add_argument("--config_file", required=True, help="Path of config.")
|
||||
@@ -27,13 +32,102 @@ def parse_arguments():
|
||||
"--image", type=str, required=True, help="Path of test image file.")
|
||||
return parser.parse_args()
|
||||
|
||||
def export_model(args):
|
||||
PPDetection_path = args.pp_detect_path
|
||||
export_str = 'python3 tools/export_model.py \
|
||||
-c configs/ppyoloe/ppyoloe_crn_s_300e_coco.yml \
|
||||
-output_dir=output_inference \
|
||||
-o weights=https://paddledet.bj.bcebos.com/models/ppyoloe_crn_s_300e_coco.pdparams'
|
||||
cur_path = os.getcwd()
|
||||
os.chdir(PPDetection_path)
|
||||
print(export_str)
|
||||
run(export_str, shell=True)
|
||||
cp_str = 'cp -r ./output_inference/ppyoloe_crn_s_300e_coco ' + cur_path
|
||||
print(cp_str)
|
||||
run(cp_str, shell=True)
|
||||
os.chdir(cur_path)
|
||||
|
||||
def paddle2onnx():
|
||||
convert_str = 'paddle2onnx --model_dir ppyoloe_crn_s_300e_coco \
|
||||
--model_filename model.pdmodel \
|
||||
--params_filename model.pdiparams \
|
||||
--save_file ppyoloe_crn_s_300e_coco.onnx \
|
||||
--enable_dev_version True'
|
||||
print(convert_str)
|
||||
run(convert_str, shell=True)
|
||||
fix_shape_str = 'python3 -m paddle2onnx.optimize --input_model ppyoloe_crn_s_300e_coco.onnx \
|
||||
--output_model ppyoloe_crn_s_300e_coco.onnx \
|
||||
--input_shape_dict "{\'image\':[1,3,640,640]}"'
|
||||
print(fix_shape_str)
|
||||
run(fix_shape_str, shell=True)
|
||||
|
||||
def mlir_prepare():
|
||||
mlir_path = os.getenv("MODEL_ZOO_PATH")
|
||||
mlir_path = mlir_path[:-13]
|
||||
regression_path = os.path.join(mlir_path, 'regression')
|
||||
mv_str_list = ['mkdir ppyoloe',
|
||||
'cp -rf ' + os.path.join(regression_path, 'dataset/COCO2017/') + ' ./ppyoloe',
|
||||
'cp -rf ' + os.path.join(regression_path, 'image/') + ' ./ppyoloe',
|
||||
'cp ppyoloe_crn_s_300e_coco.onnx ./ppyoloe',
|
||||
'mkdir ./ppyoloe/workspace']
|
||||
for str in mv_str_list:
|
||||
print(str)
|
||||
run(str, shell=True)
|
||||
|
||||
def image_prepare():
|
||||
img_str = 'wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg'
|
||||
if not os.path.exists('000000014439.jpg'):
|
||||
print(img_str)
|
||||
run(img_str, shell=True)
|
||||
prepare('000000014439.jpg', [640, 640])
|
||||
cp_npz_str = 'cp ./inputs.npz ./ppyoloe'
|
||||
print(cp_npz_str)
|
||||
run(cp_npz_str, shell=True)
|
||||
|
||||
def onnx2mlir():
|
||||
transform_str = 'model_transform.py \
|
||||
--model_name ppyoloe_crn_s_300e_coco \
|
||||
--model_def ../ppyoloe_crn_s_300e_coco.onnx \
|
||||
--input_shapes [[1,3,640,640],[1,2]] \
|
||||
--keep_aspect_ratio \
|
||||
--pixel_format rgb \
|
||||
--output_names p2o.Div.1,p2o.Concat.29 \
|
||||
--test_input ../inputs.npz \
|
||||
--test_result ppyoloe_crn_s_300e_coco_top_outputs.npz \
|
||||
--mlir ppyoloe_crn_s_300e_coco.mlir'
|
||||
os.chdir('./ppyoloe/workspace')
|
||||
print(transform_str)
|
||||
run(transform_str, shell=True)
|
||||
os.chdir('../../')
|
||||
|
||||
def mlir2bmodel():
|
||||
deploy_str = 'model_deploy.py \
|
||||
--mlir ppyoloe_crn_s_300e_coco.mlir \
|
||||
--quantize F32 \
|
||||
--chip bm1684x \
|
||||
--test_input ppyoloe_crn_s_300e_coco_in_f32.npz \
|
||||
--test_reference ppyoloe_crn_s_300e_coco_top_outputs.npz \
|
||||
--model ppyoloe_crn_s_300e_coco_1684x_f32.bmodel'
|
||||
os.chdir('./ppyoloe/workspace')
|
||||
print(deploy_str)
|
||||
run(deploy_str, shell=True)
|
||||
os.chdir('../../')
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = parse_arguments()
|
||||
|
||||
model_file = args.model_file
|
||||
if args.auto:
|
||||
export_model(args)
|
||||
paddle2onnx()
|
||||
mlir_prepare()
|
||||
image_prepare()
|
||||
onnx2mlir()
|
||||
mlir2bmodel()
|
||||
|
||||
model_file = './ppyoloe/workspace/ppyoloe_crn_s_300e_coco_1684x_f32.bmodel' if args.auto else args.model_file
|
||||
params_file = ""
|
||||
config_file = args.config_file
|
||||
config_file = './ppyoloe_crn_s_300e_coco/infer_cfg.yml' if args.auto else args.config_file
|
||||
image_file = './000000014439.jpg' if args.auto else args.image
|
||||
|
||||
# 配置runtime,加载模型
|
||||
runtime_option = fd.RuntimeOption()
|
||||
@@ -46,14 +140,14 @@ if __name__ == "__main__":
|
||||
runtime_option=runtime_option,
|
||||
model_format=fd.ModelFormat.SOPHGO)
|
||||
|
||||
model.postprocessor.apply_decode_and_nms()
|
||||
model.postprocessor.apply_nms()
|
||||
|
||||
# 预测图片分割结果
|
||||
im = cv2.imread(args.image)
|
||||
im = cv2.imread(image_file)
|
||||
result = model.predict(im)
|
||||
print(result)
|
||||
|
||||
# 可视化结果
|
||||
vis_im = fd.vision.vis_detection(im, result, score_threshold=0.5)
|
||||
cv2.imwrite("sophgo_result.jpg", vis_im)
|
||||
print("Visualized result save in ./sophgo_result.jpg")
|
||||
print("Visualized result save in ./sophgo_result_ppyoloe.jpg")
|
||||
|
@@ -0,0 +1,17 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
def prepare(img_path, sz):
|
||||
im = cv2.imread(img_path)
|
||||
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
||||
im = cv2.resize(im, sz)
|
||||
im = im.transpose((2,0,1))
|
||||
im = im[np.newaxis,...]
|
||||
im_scale_y = sz[0] / float(im.shape[2])
|
||||
im_scale_x = sz[1] / float(im.shape[3])
|
||||
inputs = {}
|
||||
inputs['image'] = np.array(im).astype('float32')
|
||||
# scale = np.array([im_scale_y, im_scale_x])
|
||||
# scale = scale[np.newaxis,...]
|
||||
inputs['scale_factor'] = np.array(([im_scale_y, im_scale_x], )).astype('float32')
|
||||
np.savez('inputs.npz', image=inputs['image'], scale_factor=inputs['scale_factor'])
|
Reference in New Issue
Block a user