飞桨x昇腾生态适配方案:13_API离线推理

简介: ais_bench 提供了基于昇腾硬件的 Python API,用于离线模型(.om模型)推理。支持静态与动态API场景,如单个或多个OM模型推理。通过 `InferSession` 类加载模型并执行推理,可灵活处理输入输出形状转换。示例代码涵盖图片读取、形状调整、多模型串联推理及资源释放等操作,满足多样化推理需求。

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()

执行结果如下所示:
01_静态API推理结果.png

动态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()

执行结果:
02_动态API推理结果.png

目录
相关文章
|
4月前
|
缓存 边缘计算 监控
89_批量推理:异步API调用
在当今数据密集型应用和大模型部署的时代,批量推理已成为提升系统性能和资源利用率的关键技术。随着深度学习模型规模的不断扩大和应用场景的日益复杂,如何高效地处理大量推理请求成为技术团队面临的重要挑战。传统的同步API调用方式在面对高并发、大规模数据处理时,往往会遇到响应延迟高、资源利用不充分等问题。异步API调用作为一种更高效的处理模式,通过非阻塞操作和并发处理能力,为批量推理场景提供了理想的解决方案。
|
5月前
|
机器学习/深度学习 并行计算 小程序
DeepSeek-V3.2-Exp 发布,训练推理提效,API 同步降价
今天,我们正式发布 DeepSeek-V3.2-Exp 模型,这是一个实验性( Experimental)的版本。作为迈向新一代架构的中间步骤,V3.2-Exp 在 V3.1-Terminus 的基础上引入了 DeepSeek Sparse Attention(一种稀疏注意力机制…
781 0
DeepSeek-V3.2-Exp 发布,训练推理提效,API 同步降价
|
5月前
|
缓存 监控 供应链
亚马逊 MWS API 实战:商品详情精准获取与跨境电商数据整合方案
本文详细解析亚马逊MWS API接口的技术实现,重点解决跨境商品数据获取中的核心问题。文章首先介绍MWS接口体系的特点,包括多站点数据获取、AWS签名认证等关键环节,并对比普通电商接口的差异。随后深入拆解API调用全流程,提供签名工具类、多站点客户端等可复用代码。针对跨境业务场景,文章还给出数据整合工具实现方案,支持缓存、批量处理等功能。最后通过实战示例展示多站点商品对比和批量选品分析的应用,并附常见问题解决方案。该技术方案可直接应用于跨境选品、价格监控等业务场景,帮助开发者高效获取亚马逊商品数据。
监控 安全 API
392 0
|
5月前
|
人工智能 安全 API
API安全厂商F5首发后量子加密方案,为企业后量子时代加固防线
API安全厂商F5首发后量子加密方案,为企业后量子时代加固防线
150 1
|
5月前
|
供应链 数据可视化 BI
多平台数据整合工具新趋势:支持API实时联动的看板方案
在电商运营中,数据分散在多个平台导致效率低下、决策滞后。数据整合看板工具通过聚合多源数据,实现可视化、实时同步与低门槛操作,帮助团队高效利用数据,提升运营效率与决策质量。
|
6月前
|
JSON 缓存 API
孔夫子旧书网 API 实战:古籍与二手书数据获取及接口调用方案
孔夫子旧书网作为国内知名古籍与二手书交易平台,其数据对图书收藏、学术研究及电商系统具有重要价值。本文详解其API调用方法,涵盖认证机制、搜索参数、数据解析及反爬策略,并提供可直接使用的Python代码,助力开发者合规获取数据。
|
6月前
|
XML 缓存 API
eBay 商品详情 API 深度解析:从基础信息到变体数据获取全方案
本文详解如何通过 eBay 的 GetItem 和 GetMultipleItems 接口获取商品详情数据,涵盖基础属性、价格、变体、卖家信息等,并提供可复用的 Python 代码。内容包括 API 核心参数、响应结构、代码实现、实战注意事项及扩展方向,助力跨境电商开发。
|
6月前
|
前端开发 Java API
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
本文介绍了如何使用Spring WebFlux构建高效、可扩展的非阻塞API,涵盖响应式编程核心概念、技术方案设计及具体实现示例,适用于高并发场景下的API开发。
517 0
|
7月前
|
缓存 算法 API
从 0 实现 API 接口签名验证系统:基于 HMAC-SHA256 的防篡改方案(附 Python 全代码)
本文介绍基于 的 API 接口签名验证系统,实现防篡改与防重放攻击,包含完整设计原理、签名生成规则及可运行的 Python 客户端与服务端代码,并提供安全性优化与部署建议。