【MindSpore】Ascend(昇腾)310推理 | 实现步骤

简介:本文介绍在Ascend 310平台对MindSpore框架实现的网络进行推理的流程。以CV语义分割网络(HRNet)为例,使用的数据集是Cityscapes,数据文件为PNG图片。使用的中间原语为MindIR,数据格式为二进制流

建议:可以参考MindSpore ModelZoo master分支下的ResNet系列网络的310推理脚本,是一个不错的完整示例。

注:本文目前仅介绍基于MindIR和二进制流文件的310推理,其他方法随缘更新。仅涉及实际操作流程,具体原理请查阅其他资料。

总体流程

在这里插入图片描述

相关脚本

推理过程涉及到一下相关脚本:

  • export.py
  • preprocess.py
  • main.cc
  • postprocess.py

接下来分别介绍各个脚本的作用。

Export

这一步的工作是将训练得到的checkpoint文件和网络模型转换为MindIR。

相关脚本:export.py

输入:checkpoint文件

输出(*.mindir)位置:自定义

def main():
    """Export mindir for 310 inference."""
    parser = argparse.ArgumentParser("HRNet Semantic Segmentation exporting.")
    parser.add_argument("--device_id", type=int, default=0, help="Device ID. ")
    parser.add_argument("--checkpoint_file", type=str, help="Checkpoint file path. ")
    parser.add_argument("--file_name", type=str, help="Output file name. ")
    parser.add_argument("--file_format", type=str, default="MINDIR",
                        choices=["AIR", "MINDIR"], help="Output file format. ")
    parser.add_argument("--device_target", type=str, default="Ascend",
                        choices=["Ascend", "GPU", "CPU"], help="Device target.")

    args = parser.parse_args()

    context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
    if args.device_target == "Ascend":
        context.set_context(device_id=args.device_id)
    # 将此前训练得到的checkpoint导入网络
    net = get_seg_model(config)
    params_dict = load_checkpoint(args.checkpoint_file)
    load_param_into_net(net, params_dict)
    net.set_train(False)
    # 构造一个推理时的模型输入
    height, width = config.eval.image_size[0], config.eval.image_size[1]
    input_data = Tensor(np.zeros([1, 3, height, width], dtype=np.float32))
    # 导出mindir文件
    export
为了安装MindSpore Ascend版本,以下是详细的安装步骤和注意事项: ### 系统要求 确保你的系统满足以下要求: - **操作系统**:Ubuntu 18.04、CentOS 7.6、EulerOS 2.8、openEuler 20.03 或 KylinV10 SP1。 - **Python版本**:3.9 到 3.11 之间的版本。 - **编译工具**:GCC 7.3.0,用于编译MindSpore的C++代码。 ### 安装步骤 #### 1. 安装依赖库 在安装MindSpore之前,需要先安装一些依赖库。假设你已经配置好了Ascend工具包的环境变量,可以使用以下命令安装必要的依赖库: ```bash cd /usr/local/Ascend/ascend-toolkit/latest/fwkacllib/lib64 pip3 install te-0.4.0-py3-none-any.whl pip3 install topi-0.4.0-py3-none-any.whl pip3 install hccl-0.1.0-py3-none-any.whl ``` #### 2. 安装MindSpore 接下来,使用`pip`命令安装MindSpore Ascend版本。你可以从华为云的镜像源下载并安装适合你系统的MindSpore版本。例如,安装1.2.1版本: ```bash pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.2.1/MindSpore/ascend/ubuntu_aarch64/mindspore_ascend-1.2.1-cp37-cp37m-linux_aarch64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 请根据你的Python版本和操作系统选择合适的Wheel文件。 #### 3. 验证安装 安装完成后,可以通过以下代码验证是否安装成功: ```python import mindspore as ms ms.set_context(device_target="Ascend") ms.set_context(device_id=0) mindspore.run_check() ``` 如果输出显示Ascend设备信息并且没有错误,说明安装成功。 ### 注意事项 - **版本兼容性**:确保MindSpore版本与CANN(Compute Acceleration Software Stack)版本对齐。例如,MindSpore 1.2.1需要CANN 6.0.RC1.alpha005。 - **硬件支持**:某些设备如Atlas 200 DK (Model 3000)可能无法直接安装MindSpore Ascend版本,建议使用其他支持的设备进行开发和测试。 ### 常见问题 1. **如何检查Ascend设备的状态**? 可以使用`npu-smi info`命令来查看Ascend设备的状态和配置。 2. **如何解决安装过程中出现的依赖问题**? 确保所有依赖库都已正确安装,并且版本匹配。可以尝试使用`pip`的`--no-cache-dir`选项来避免缓存问题。 3. **如何选择合适的MindSpore版本**? 根据你的操作系统和硬件平台选择合适的MindSpore版本。建议参考官方文档中的版本兼容性指南。 4. **如何配置Ascend工具链**? 确保Ascend工具链的环境变量已正确设置,并且所有必要的库文件都已安装。 5. **如何处理安装后的运行错误**? 检查日志文件,确保所有依赖项都已正确安装,并且系统环境变量已正确配置。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值