[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:
Yi-sir
2023-03-31 15:08:01 +08:00
committed by GitHub
parent 8deb2ed179
commit 9e20dab0d6
15 changed files with 629 additions and 42 deletions

View File

@@ -31,7 +31,7 @@ void SophgoInfer(const std::string& model_dir, const std::string& image_file) {
auto model = fastdeploy::vision::detection::PicoDet(
model_file, params_file, config_file, option, format);
model.GetPostprocessor().ApplyDecodeAndNMS();
model.GetPostprocessor().ApplyNMS();
auto im = cv::imread(image_file);

View File

@@ -31,7 +31,7 @@ void SophgoInfer(const std::string& model_dir, const std::string& image_file) {
auto model = fastdeploy::vision::detection::PPYOLOE(
model_file, params_file, config_file, option, format);
model.GetPostprocessor().ApplyDecodeAndNMS();
model.GetPostprocessor().ApplyNMS();
auto im = cv::imread(image_file);

View File

@@ -30,7 +30,7 @@ void SophgoInfer(const std::string& model_dir, const std::string& image_file) {
auto model = fastdeploy::vision::detection::PaddleYOLOv8(model_file, params_file, config_file, option, format);
model.GetPostprocessor().ApplyDecodeAndNMS();
model.GetPostprocessor().ApplyNMS();
auto im = cv::imread(image_file);

View File

@@ -16,10 +16,18 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000
# 推理
# 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
# 指定--auto True自动完成模型准备、转换和推理需要指定PaddleDetection路径
python3 infer_ppyoloe.py --auto True --pp_detect_path {Path to PaddleDetection} --model_file '' --config_file '' --image ''
# 指定--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
# 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 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

View File

@@ -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")

View File

@@ -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")

View File

@@ -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'])

View File

@@ -15,7 +15,10 @@ cd FastDeploy/examples/vision/detection/yolov5/sophgo/python
# Download images.
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
# Inference.
# Set --auto True, automatic inference.
python3 infer.py --auto True
# Set --auto False, need to set the model path and image path manually.
python3 infer.py --model_file ./bmodel/yolov5s_1684x_f32.bmodel --image 000000014439.jpg
# The returned result.

View File

@@ -15,8 +15,11 @@ cd FastDeploy/examples/vision/detection/yolov5/sophgo/python
# 下载图片
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
# 推理
python3 infer.py --model_file ./bmodel/yolov5s_1684x_f32.bmodel --image 000000014439.jpg
# 指定--auto True自动完成下载数据、模型转换、推理
python3 infer.py --auto True
# 指定--auto False则需要设置模型目录、图片目录
python3 infer.py --auto False --model_file ./bmodel/yolov5s_1684x_f32.bmodel --image 000000014439.jpg
# 运行完成后返回结果如下所示
DetectionResult: [xmin, ymin, xmax, ymax, score, label_id]

View File

@@ -1,27 +1,94 @@
import fastdeploy as fd
import cv2
import os
from subprocess import run
def parse_arguments():
import argparse
import ast
parser = argparse.ArgumentParser()
parser.add_argument("--model", required=True, help="Path of model.")
parser.add_argument(
"--image", type=str, required=True, help="Path of test image file.")
"--auto", required=True, help="Auto download, convert, compile and infer if True")
parser.add_argument("--model", help="Path of model.")
parser.add_argument(
"--image", type=str, help="Path of test image file.")
return parser.parse_args()
def download():
download_model_str = 'wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx'
if not os.path.exists('yolov5s.onnx'):
print(download_model_str)
run(download_model_str, shell=True)
download_img_str = 'wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg'
if not os.path.exists('000000014439.jpg'):
print(download_img_str)
run(download_img_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 YOLOv5s',
'cp -rf ' + os.path.join(regression_path, 'dataset/COCO2017/') + ' ./YOLOv5s',
'cp -rf ' + os.path.join(regression_path, 'image/') + ' ./YOLOv5s',
'cp yolov5s.onnx ./YOLOv5s',
'mkdir ./YOLOv5s/workspace']
for str in mv_str_list:
print(str)
run(str, shell=True)
def onnx2mlir():
transform_str = 'model_transform.py \
--model_name yolov5s \
--model_def ../yolov5s.onnx \
--input_shapes [[1,3,640,640]] \
--mean 0.0,0.0,0.0 \
--scale 0.0039216,0.0039216,0.0039216 \
--keep_aspect_ratio \
--pixel_format rgb \
--output_names output0 \
--test_input ../image/dog.jpg \
--test_result yolov5s_top_outputs.npz \
--mlir yolov5s.mlir'
os.chdir('./YOLOv5s/workspace')
print(transform_str)
run(transform_str, shell=True)
os.chdir('../../')
def mlir2bmodel():
deploy_str = 'model_deploy.py \
--mlir yolov5s.mlir \
--quantize F32 \
--chip bm1684x \
--test_input yolov5s_in_f32.npz \
--test_reference yolov5s_top_outputs.npz \
--model yolov5s_1684x_f32.bmodel'
os.chdir('./YOLOv5s/workspace')
print(deploy_str)
run(deploy_str, shell=True)
os.chdir('../../')
args = parse_arguments()
if args.auto:
download()
mlir_prepare()
onnx2mlir()
mlir2bmodel()
# 配置runtime加载模型
runtime_option = fd.RuntimeOption()
runtime_option.use_sophgo()
model_file = args.model
model_file = './YOLOv5s/workspace/yolov5s_1684x_f32.bmodel' if args.auto else args.model
params_file = ""
img_file = './000000014439.jpg' if args.auto else args.image
model = fd.vision.detection.YOLOv5(
model_file,
@@ -29,8 +96,9 @@ model = fd.vision.detection.YOLOv5(
runtime_option=runtime_option,
model_format=fd.ModelFormat.SOPHGO)
# 预测图片分类结果
im = cv2.imread(args.image)
im = cv2.imread(img_file)
result = model.predict(im)
print(result)

View File

@@ -31,7 +31,12 @@ wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt
# 推理
python3 infer.py --det_model ocr_bmodel/ch_PP-OCRv3_det_1684x_f32.bmodel \
# --auto True自动完成下载数据、模型转换、推理
python3 infer.py --auto True --det_model '' --cls_model '' --rec_model '' --rec_label_file '' --image ''
# --auto False需要用户设置模型、字典、图片路径进行推理
python3 infer.py --auto False \
--det_model ocr_bmodel/ch_PP-OCRv3_det_1684x_f32.bmodel \
--cls_model ocr_bmodel/ch_ppocr_mobile_v2.0_cls_1684x_f32.bmodel \
--rec_model ocr_bmodel/ch_PP-OCRv3_rec_1684x_f32.bmodel \
--rec_label_file ../ppocr_keys_v1.txt \

View File

@@ -1,12 +1,15 @@
import fastdeploy as fd
import cv2
import os
from subprocess import run
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(
"--det_model", required=True, help="Path of Detection model of PPOCR.")
parser.add_argument(
@@ -27,22 +30,210 @@ def parse_arguments():
return parser.parse_args()
def getPPOCRv3():
cmd_str_det = 'wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar'
tar_str_det = 'tar xvf ch_PP-OCRv3_det_infer.tar'
cmd_str_cls = 'wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar'
tar_str_cls = 'tar xvf ch_ppocr_mobile_v2.0_cls_infer.tar'
cmd_str_rec = 'wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar'
tar_str_rec = 'tar xvf ch_PP-OCRv3_rec_infer.tar'
cmd_str_img = 'wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg'
cmd_str_label = 'wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt'
script_str = 'wget https://raw.githubusercontent.com/PaddlePaddle/Paddle2ONNX/develop/tools/paddle/paddle_infer_shape.py'
if not os.path.exists('ch_PP-OCRv3_det_infer.tar'):
print(cmd_str_det, tar_str_det)
run(cmd_str_det, shell=True)
run(tar_str_det, shell=True)
if not os.path.exists('ch_ppocr_mobile_v2.0_cls_infer.tar'):
print(cmd_str_cls, tar_str_cls)
run(cmd_str_cls, shell=True)
run(tar_str_cls, shell=True)
if not os.path.exists('ch_PP-OCRv3_rec_infer.tar'):
print(cmd_str_rec, tar_str_rec)
run(cmd_str_rec, shell=True)
run(tar_str_rec, shell=True)
if not os.path.exists('12.jpg'):
print(cmd_str_img)
run(cmd_str_img, shell=True)
if not os.path.exists('ppocr_keys_v1.txt'):
print(cmd_str_label)
run(cmd_str_label, shell=True)
if not os.path.exists('paddle_infer_shape.py'):
print(script_str)
run(script_str, shell=True)
def fix_input_shape():
fix_det_str = 'python paddle_infer_shape.py --model_dir ch_PP-OCRv3_det_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_dir ch_PP-OCRv3_det_infer_fix \
--input_shape_dict="{\'x\':[1,3,960,608]}"'
fix_rec_str = 'python paddle_infer_shape.py --model_dir ch_PP-OCRv3_rec_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_dir ch_PP-OCRv3_rec_infer_fix \
--input_shape_dict="{\'x\':[1,3,48,320]}"'
fix_cls_str = 'python paddle_infer_shape.py --model_dir ch_ppocr_mobile_v2.0_cls_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_dir ch_PP-OCRv3_cls_infer_fix \
--input_shape_dict="{\'x\':[1,3,48,192]}"'
print(fix_det_str)
run(fix_det_str, shell=True)
print(fix_rec_str)
run(fix_rec_str, shell=True)
print(fix_cls_str)
run(fix_cls_str, shell=True)
def paddle2onnx():
cmd_str_det = 'paddle2onnx --model_dir ch_PP-OCRv3_det_infer_fix \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ch_PP-OCRv3_det_infer.onnx \
--enable_dev_version True'
cmd_str_cls = 'paddle2onnx --model_dir ch_PP-OCRv3_cls_infer_fix \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ch_PP-OCRv3_cls_infer.onnx \
--enable_dev_version True'
cmd_str_rec = 'paddle2onnx --model_dir ch_PP-OCRv3_rec_infer_fix \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ch_PP-OCRv3_rec_infer.onnx \
--enable_dev_version True'
print(cmd_str_det)
run(cmd_str_det, shell=True)
print(cmd_str_cls)
run(cmd_str_cls, shell=True)
print(cmd_str_rec)
run(cmd_str_rec, 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 ch_PP-OCRv3',
'cp -rf ' + os.path.join(regression_path, 'dataset/COCO2017/') + ' ./ch_PP-OCRv3',
'cp -rf ' + os.path.join(regression_path, 'image/') + ' ./ch_PP-OCRv3',
'mv ch_PP-OCRv3_det_infer.onnx ./ch_PP-OCRv3',
'mv ch_PP-OCRv3_rec_infer.onnx ./ch_PP-OCRv3',
'mv ch_PP-OCRv3_cls_infer.onnx ./ch_PP-OCRv3',
'mkdir ./ch_PP-OCRv3/workspace']
for str in mv_str_list:
print(str)
run(str, shell=True)
def onnx2mlir():
transform_str_det = 'model_transform.py \
--model_name ch_PP-OCRv3_det \
--model_def ../ch_PP-OCRv3_det_infer.onnx \
--input_shapes [[1,3,960,608]] \
--mean 0.0,0.0,0.0 \
--scale 0.0039216,0.0039216,0.0039216 \
--keep_aspect_ratio \
--pixel_format rgb \
--output_names sigmoid_0.tmp_0 \
--test_input ../image/dog.jpg \
--test_result ch_PP-OCRv3_det_top_outputs.npz \
--mlir ./ch_PP-OCRv3_det.mlir'
transform_str_rec = 'model_transform.py \
--model_name ch_PP-OCRv3_rec \
--model_def ../ch_PP-OCRv3_rec_infer.onnx \
--input_shapes [[1,3,48,320]] \
--mean 0.0,0.0,0.0 \
--scale 0.0039216,0.0039216,0.0039216 \
--keep_aspect_ratio \
--pixel_format rgb \
--output_names softmax_5.tmp_0 \
--test_input ../image/dog.jpg \
--test_result ch_PP-OCRv3_rec_top_outputs.npz \
--mlir ./ch_PP-OCRv3_rec.mlir'
transform_str_cls = 'model_transform.py \
--model_name ch_PP-OCRv3_cls \
--model_def ../ch_PP-OCRv3_cls_infer.onnx \
--input_shapes [[1,3,48,192]] \
--mean 0.0,0.0,0.0 \
--scale 0.0039216,0.0039216,0.0039216 \
--keep_aspect_ratio \
--pixel_format rgb \
--output_names softmax_0.tmp_0 \
--test_input ../image/dog.jpg \
--test_result ch_PP-OCRv3_cls_top_outputs.npz \
--mlir ./ch_PP-OCRv3_cls.mlir'
os.chdir('./ch_PP-OCRv3/workspace/')
print(transform_str_det)
run(transform_str_det, shell=True)
print(transform_str_rec)
run(transform_str_rec, shell=True)
print(transform_str_cls)
run(transform_str_cls, shell=True)
os.chdir('../../')
def mlir2bmodel():
det_str = 'model_deploy.py \
--mlir ./ch_PP-OCRv3_det.mlir \
--quantize F32 \
--chip bm1684x \
--test_input ./ch_PP-OCRv3_det_in_f32.npz \
--test_reference ./ch_PP-OCRv3_det_top_outputs.npz \
--model ./ch_PP-OCRv3_det_1684x_f32.bmodel'
rec_str = 'model_deploy.py \
--mlir ./ch_PP-OCRv3_rec.mlir \
--quantize F32 \
--chip bm1684x \
--test_input ./ch_PP-OCRv3_rec_in_f32.npz \
--test_reference ./ch_PP-OCRv3_rec_top_outputs.npz \
--model ./ch_PP-OCRv3_rec_1684x_f32.bmodel'
cls_str = 'model_deploy.py \
--mlir ./ch_PP-OCRv3_cls.mlir \
--quantize F32 \
--chip bm1684x \
--test_input ./ch_PP-OCRv3_cls_in_f32.npz \
--test_reference ./ch_PP-OCRv3_cls_top_outputs.npz \
--model ./ch_PP-OCRv3_cls_1684x_f32.bmodel'
os.chdir('./ch_PP-OCRv3/workspace/')
print(det_str)
run(det_str, shell=True)
print(rec_str)
run(rec_str, shell=True)
print(cls_str)
run(cls_str, shell=True)
os.chdir('../../')
args = parse_arguments()
if (args.auto):
getPPOCRv3()
fix_input_shape()
paddle2onnx()
mlir_prepare()
onnx2mlir()
mlir2bmodel()
# 配置runtime加载模型
runtime_option = fd.RuntimeOption()
runtime_option.use_sophgo()
# Detection模型, 检测文字框
det_model_file = args.det_model
det_model_file = './ch_PP-OCRv3/workspace/ch_PP-OCRv3_det_1684x_f32.bmodel' if args.auto else args.det_model
det_params_file = ""
# Classification模型方向分类可选
cls_model_file = args.cls_model
cls_model_file = './ch_PP-OCRv3/workspace/ch_PP-OCRv3_cls_1684x_f32.bmodel' if args.auto else args.cls_model
cls_params_file = ""
# Recognition模型文字识别模型
rec_model_file = args.rec_model
rec_model_file = './ch_PP-OCRv3/workspace/ch_PP-OCRv3_rec_1684x_f32.bmodel' if args.auto else args.rec_model
rec_params_file = ""
rec_label_file = args.rec_label_file
rec_label_file = './ppocr_keys_v1.txt' if args.auto else args.rec_label_file
image_file = './12.jpg' if args.auto else args.image
# PPOCR的cls和rec模型现在已经支持推理一个Batch的数据
# 定义下面两个变量后, 可用于设置trt输入shape, 并在PPOCR模型初始化后, 完成Batch推理设置
@@ -92,9 +283,9 @@ rec_model = fd.vision.ocr.Recognizer(
ppocr_v3 = fd.vision.ocr.PPOCRv3(
det_model=det_model, cls_model=cls_model, rec_model=rec_model)
# 需要使用下行代码, 来启用rec模型的静态shape推理这里rec模型的静态输入为[3, 48, 584]
# 需要使用下行代码, 来启用rec模型的静态shape推理这里rec模型的静态输入为[3, 48, 320]
rec_model.preprocessor.static_shape_infer = True
rec_model.preprocessor.rec_image_shape = [3, 48, 584]
rec_model.preprocessor.rec_image_shape = [3, 48, 320]
# 给cls和rec模型设置推理时的batch size
# 此值能为-1, 和1到正无穷
@@ -103,7 +294,7 @@ ppocr_v3.cls_batch_size = cls_batch_size
ppocr_v3.rec_batch_size = rec_batch_size
# 预测图片准备
im = cv2.imread(args.image)
im = cv2.imread(image_file)
#预测并打印结果
result = ppocr_v3.predict(im)

View File

@@ -28,7 +28,11 @@ wget https://paddleseg.bj.bcebos.com/dygraph/demo/cityscapes_demo.png
将Paddle模型转换为SOPHGO bmodel模型转换步骤参考[文档](../README_CN.md)
# 推理
python3 infer.py --model_file ./bmodel/pp_liteseg_1684x_f32.bmodel --config_file ./bmodel/deploy.yaml --image cityscapes_demo.png
# --auto True: 自动完成下载数据、模型转换、推理
python3 infer.py --auto True --model '' --config_file '' --image ''
# --auto False需要用户设置模型、配置文件、图片路径进行推理
python3 infer.py --auto False --model_file ./bmodel/pp_liteseg_1684x_f32.bmodel --config_file ./bmodel/deploy.yaml --image cityscapes_demo.png
# 运行完成后返回结果如下所示
运行结果保存在sophgo_img.png中

View File

@@ -1,12 +1,14 @@
import fastdeploy as fd
import cv2
import os
from subprocess import run
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("--model", required=True, help="Path of model.")
parser.add_argument(
"--config_file", required=True, help="Path of config file.")
@@ -16,15 +18,102 @@ def parse_arguments():
return parser.parse_args()
def download():
download_model_str = 'wget https://bj.bcebos.com/paddlehub/fastdeploy/PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer.tgz'
if not os.path.exists('PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer.tgz'):
print(download_model_str)
run(download_model_str, shell=True)
tar_str = 'tar xvf PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer.tgz'
if not os.path.exists('./PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer'):
print(tar_str)
run(tar_str, shell=True)
download_script_str = 'wget https://raw.githubusercontent.com/PaddlePaddle/Paddle2ONNX/develop/tools/paddle/paddle_infer_shape.py'
if not os.path.exists('paddle_infer_shape.py'):
print(download_script_str)
run(download_script_str, shell=True)
download_img_str = 'wget https://paddleseg.bj.bcebos.com/dygraph/demo/cityscapes_demo.png'
if not os.path.exists('cityscapes_demo.png'):
print(download_img_str)
run(download_img_str, shell=True)
def paddle2onnx():
paddle_infer_shape_str = 'python3 paddle_infer_shape.py --model_dir PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer \
--model_filename model.pdmodel \
--params_filename model.pdiparams \
--save_dir pp_liteseg_fix \
--input_shape_dict="{\'x\':[1,3,512,512]}"'
print(paddle_infer_shape_str)
run(paddle_infer_shape_str, shell=True)
pp2onnx_str = 'paddle2onnx --model_dir pp_liteseg_fix \
--model_filename model.pdmodel \
--params_filename model.pdiparams \
--save_file pp_liteseg.onnx \
--enable_dev_version True'
print(pp2onnx_str)
run(pp2onnx_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 pp_liteseg',
'cp -rf ' + os.path.join(regression_path, 'dataset/COCO2017/') + ' ./pp_liteseg',
'cp -rf ' + os.path.join(regression_path, 'image/') + ' ./pp_liteseg',
'mv pp_liteseg.onnx ./pp_liteseg',
'mkdir ./pp_liteseg/workspace']
for str in mv_str_list:
print(str)
run(str, shell=True)
def onnx2mlir():
transform_str = 'model_transform.py \
--model_name pp_liteseg \
--model_def ../pp_liteseg.onnx \
--input_shapes [[1,3,512,512]] \
--mean 0.0,0.0,0.0 \
--scale 0.0039216,0.0039216,0.0039216 \
--keep_aspect_ratio \
--pixel_format rgb \
--output_names bilinear_interp_v2_6.tmp_0 \
--test_input ../image/dog.jpg \
--test_result pp_liteseg_top_outputs.npz \
--mlir pp_liteseg.mlir'
print(transform_str)
os.chdir('./pp_liteseg/workspace')
run(transform_str, shell=True)
os.chdir('../../')
def mlir2bmodel():
deploy_str = 'model_deploy.py \
--mlir pp_liteseg.mlir \
--quantize F32 \
--chip bm1684x \
--test_input pp_liteseg_in_f32.npz \
--test_reference pp_liteseg_top_outputs.npz \
--model pp_liteseg_1684x_f32.bmodel'
print(deploy_str)
os.chdir('./pp_liteseg/workspace')
run(deploy_str, shell=True)
os.chdir('../../')
args = parse_arguments()
if args.auto:
download()
paddle2onnx()
mlir_prepare()
onnx2mlir()
mlir2bmodel()
# 配置runtime加载模型
runtime_option = fd.RuntimeOption()
runtime_option.use_sophgo()
model_file = args.model
model_file = './pp_liteseg/workspace/pp_liteseg_1684x_f32.bmodel' if args.auto else args.model
params_file = ""
config_file = args.config_file
config_file = './PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer/deploy.yaml' if args.auto else args.config_file
img_file = './cityscapes_demo.png' if args.auto else args.image
model = fd.vision.segmentation.PaddleSegModel(
model_file,
@@ -34,7 +123,7 @@ model = fd.vision.segmentation.PaddleSegModel(
model_format=fd.ModelFormat.SOPHGO)
# 预测图片分类结果
im_org = cv2.imread(args.image)
im_org = cv2.imread(img_file)
#bmodel 是静态模型,模型输入固定,这里设置为[512, 512]
im = cv2.resize(im_org, [512, 512], interpolation=cv2.INTER_LINEAR)
result = model.predict(im)
@@ -42,4 +131,5 @@ print(result)
# 预测结果可视化
vis_im = fd.vision.vis_segmentation(im, result, weight=0.5)
vis_im = cv2.resize(vis_im, [im_org.shape[1], im_org.shape[0]], interpolation=cv2.INTER_LINEAR)
cv2.imwrite("sophgo_img.png", vis_im)

View File

@@ -225,6 +225,13 @@ bool SophgoBackend::Infer(std::vector<FDTensor>& inputs,
free(temp_out);
}
for (int i = 0; i < input_size; i++) {
bm_free_device(handle_, input_tensors[i].device_mem);
}
for (int i = 0; i < output_size; i++) {
bm_free_device(handle_, output_tensors[i].device_mem);
}
return true;
}