飞桨 x 昇腾生态适配方案:13_API 离线推理
作者:小顺637
- 2025-05-12 北京
本文字数:2358 字
阅读完需:约 8 分钟
ais_bench 提供的 python API 可供使能基于昇腾硬件的离线模型(.om 模型)推理。具体介绍可参考API_GUIDE下面列举几个常用的 API 推理场景使用方法。
静态 API 推理
单个 om 推理
api_1.py
import cv2
import numpy as np
from ais_bench.infer.interface import InferSession
from ais_bench.infer.common.utils import logger_print
def infer_api_static():
device_id = 0
model_path = '/home/w30067200/paddle_test/PaddleOCR/inference/om/inference.om'
image_path = '/home/w30067200/paddle_test/PaddleOCR/test/general_ocr_002.png'
image = cv2.imread(image_path)
# create session of om model for inference
session = InferSession(device_id, model_path)
# create new numpy data according inputs info
shape0 = session.get_inputs()[0].shape
print(shape0)
height, width = shape0[2], shape0[3]
resized_image = cv2.resize(image, (width, height))
image_array = np.array(resized_image).astype(np.float32)
feeds = [image_array]
# execute inference, inputs is ndarray list and outputs is ndarray list
outputs = session.infer(feeds, mode='static')
print(outputs[0].shape)
np.set_printoptions(threshold=np.inf)
print(outputs1[0])
# cv2.imwrite('output.png', outputs1[0])
logger_print("outputs: %s" % outputs)
# free model resource and device context of session
session0.free_resource()
infer_api_static()
复制代码
多个 om 推理
这里需要注意的是需要将前一个 om 推理结果输出的 shape 转化为下一个 om 推理模型输入的 shape:api_2.py
import cv2
import numpy as np
from ais_bench.infer.interface import InferSession
from ais_bench.infer.common.utils import logger_print
def infer_api_static():
device_id = 0
model_path0 = './det.om'
model_path = './rec.om'
image_path = './general_ocr_002.png'
image = cv2.imread(image_path)
# create session of om model for inference
session0 = InferSession(device_id, model_path0)
session = InferSession(device_id, model_path)
# create new numpy data according inputs info
shape0 = session0.get_inputs()[0].shape
shape1 = session.get_inputs()[0].shape
print("shape0:",shape0)
print("shape1:",shape1)
height, width = shape0[2], shape0[3]
resized_image = cv2.resize(image, (width, height))
image_array = np.array(resized_image).astype(np.float32)
feeds = [image_array]
# execute inference, inputs is ndarray list and outputs is ndarray list
outputs = session0.infer(feeds, mode='static')
print("outputs[0].shape:",outputs[0].shape)
# det.om推理结果输出的shape [1, 1, 640, 480] 转化为下一个rec.om推理模型输入的shape[1, 3, 48, 320]
height, width = shape1[2], shape1[3]
arr = np.repeat(outputs[0], 3, axis=1)
arr = arr.transpose(0, 2, 3, 1) # [1, 224, 224, 3]
resized = cv2.resize(arr[0], (width, height), interpolation=cv2.INTER_LINEAR)
resized = resized[np.newaxis, ...].transpose(0, 3, 1, 2) # [1, 3, 48, 680]
'''
resized_image1 = cv2.resize(arr, (width, height))
image_array1 = np.array(resized_image1).astype(np.float32)
'''
feeds = [resized]
print("feeds[0].shape",feeds[0].shape)
# execute inference, inputs is ndarray list and outputs is ndarray list
outputs1 = session.infer(feeds, mode='static')
np.set_printoptions(threshold=np.inf)
# print(outputs1[0].shape)
# cv2.imwrite('output.png', outputs1[0])
# logger_print("outputs1: %s" % outputs1)
# free model resource and device context of session
session0.free_resource()
infer_api_static()
复制代码
执行结果如下所示:

动态 API 推理
多个 om 推理
import onnx
import numpy as np
from ais_bench.infer.interface import InferSession
def infer_api_dymshape():
model_path0 = './model_dest_linux_x86_64.om'
model_path1 = './mode_loop_input2_i_cond_linux_x86_64.om'
# Load the model
device_id = 1
session0 = InferSession(device_id, model_path0)
session1 = InferSession(device_id, model_path1)
# 假设 batch 大小为 4
batch = 4
# 生成全 1 的 input1
input1 = np.ones((batch, 16, 32), dtype=np.float32)
# 生成全 2 的 input2
input2 = np.full((batch, 16, 32), 2, dtype=np.float32)
# print("input1 的形状:", input1.shape)
# print("input2 的形状:", input2.shape)
input = [input1, input2]
# 执行model_dest_linux_x86_64.om推理
output0 = session0.infer(input, mode='dymshape')
input3 = output0[0]
cnts = output0[1]
for i in range(cnts):
inputs = {"x.13": input3, "input2": input2}
# 执行mode_loop_input2_i_cond_linux_x86_64.om 推理
input3 = session1.infer(inputs, mode='dymshape')
print("input3", input3)
# free model resource and device context of session
session0.free_resource()
session1.free_resource()
infer_api_dymshape()
复制代码
执行结果:

划线
评论
复制
发布于: 刚刚阅读数: 2

小顺637
关注
还未添加个人签名 2023-01-19 加入
还未添加个人简介
评论