Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
minqiang.huang 757e0c0de7 | 1 year ago | |
---|---|---|
README.md | 1 year ago | |
convert_pt_onnx.py | 1 year ago | |
mnist_demo.py | 1 year ago | |
model.py | 1 year ago |
# 安装环境
pip3 install onnx
pip3 install onnxruntime
pip3 install torch
pip3 install torchvision
# 执行转换脚本
root@cse:/home/pcl/mnist# python3 convert_pt_onnx.py
device: cpu
weights_dict.keys(): odict_keys(['conv1.weight', 'conv1.bias', 'conv2.weight', 'conv2.bias', 'fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias', 'fc3.weight', 'fc3.bias'])
mnist pytorch2onnx success
def Onnx2Engines(args):
# 创建上下文管理器,并声明生成engine所使用的板卡和cluster
# 第一个参数为板卡ID, 第二个参数为cluster ID
with TopsInference.device(0, 0) as device:
# 创建ONNX解析器
onnx_parser = TopsInference.create_parser(TopsInference.ONNX_MODEL)
onnx_parser.set_input_names('Input3')
onnx_parser.set_output_names('Plus214_Output_0')
onnx_parser.set_input_shapes('1, 1, 28, 28')
# 读取ONNX模型
network = onnx_parser.read("mnist.onnx")
# 创建优化器
optimizer = TopsInference.create_optimizer()
# 设置精度模式:TopsInference.KDEFAULT(FP32)/TopsInference.KFP16_MIX(混精)/TopsInference.KFP16
optimizer.set_build_flag(TopsInference.KFP16_MIX)
# 根据读取到的ONNX模型,使用优化器生成推理engine
engine = optimizer.build(network)
# 将推理engine保存在本地
engine.save_executable("./mnist.exec")
def infer_test(args):
# pre-process
img = cv2.imread(args.image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.resize(gray, (28,28)).astype(np.float32)/255
input = np.reshape(gray, (1,1,28,28))
input = np.array(input, order='C')
inputs = [input]
# inference
TopsInference.create_error_manager()
with TopsInference.device(0, 0) as device:
engine = TopsInference.load("./mnist.exec")
outputs = []
engine.run(inputs, outputs, TopsInference.TIF_ENGINE_RSC_IN_HOST_OUT_HOST)
# post-process
print('output: {}'.format(outputs[0]))
print('predict result is: %d'%np.argmax(outputs[0]))
root@cse:/home/pcl/mnist# python3 mnist_demo.py --image testSample/img_2.jpg
save mnist.exec success
output: [[ 18.8125 -12.453125 -0.11877441 -5.671875 -6.1914062
-1.4794922 2.9414062 -1.6787109 -0.4326172 -0.5209961 ]]
predict result is: 0
本项目以PyTorch框架Mnist模型为例,介绍如何简单几步从原框架转换成GCU推理格式,到推理示例过程
Python
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》