嵌入式AI Arm_linux_第一个Demo_让IPU跑起来

嵌入式AI Arm_linux_第一个Demo_让IPU跑起来

使用Comake PI D1开发板需要使用星宸科技推出的适配Soc的sdk架构,在这里我们为了快速运行起来IPU,直接拉取整包的Comake PI D1的代码,在这个整包代码的基础上添加我们的第一个Demo程序,让IPU跑起来。

1. 配置环境

这里可以参考Comake PI D1环境搭建

如果没有服务器,可以在自己的windows电脑下安装虚拟机,可参考下面的虚拟机安装链接进行安装ubuntu

1.1 虚拟机安装

参考vmware虚拟机安装

清华镜像下载ubuntu镜像

vmware安装ubuntu

1.2 账号申请与git配置

在这里可直接参考git账号申请与配置

2. 拉取Comake PI D1 整包代码

2.1 拉取整包code

参考1.2的文章内容,下载整包code,记得将ipu资料也拉取下来

1.3 编译工具链配置

1.2顺利申请到git账号之后按照1.2配置好git,输入以下命令获取编译工具链

git clone "https://git03.sigmastar.com.cn:9083/sigmastar/release_tool"

然后参考Comake PI D1环境搭建

3. conda 安装配置

网上搜索miniconda的安装和配置即可

4. python环境搭建

复制代码
conda create -n classification python==3.10
conda activate classification
##此处建议在你的工作目录下创建一个文件夹
mkdir pyclass
cd pyclass
git clone https://github.com/WZMIAOMIAO/deep-learning-for-image-processing.git
cd deep-learning-for-image-processing/pytorch_classification/Test6_mobilenet

pytorch 安装配置(建议去官网选择合适的版本安装)
pip3 install torch torchvision

onnx安装

复制代码
pip install onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnx-simplifier -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxscript

5.以mobilenetv2为例实际操作

5.1 创建 mobilenetv2 文件夹

复制代码
mkdir mobilenetv2
cd mobilenetv2
git clone https://github.com/WZMIAOMIAO/deep-learning-for-image-processing.git
cd deep-learning-for-image-processing/pytorch_classification/Test6_mobilenet

5.2 验证模型

在这个


deep-learning-for-image-processing/pytorch_classification/Test6_mobilenet


路径下创建infer.py文件,验证模型正确与否

复制代码
vi infer.py

输入以下内容

复制代码
import os
import json

import torch
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt

from model_v2 import MobileNetV2

def main():
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    data_transform = transforms.Compose(
        [transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

    # load image
    img_path = "./apple.jpg"
    assert os.path.exists(img_path), "file: '{}' dose not exist.".format(img_path)
    img = Image.open(img_path)
    plt.imshow(img)
    # [N, C, H, W]
    img = data_transform(img)
    # expand batch dimension
    img = torch.unsqueeze(img, dim=0)

    # create model
    model = MobileNetV2(num_classes=1000).to(device)
    # load model weights
    model_weight_path = "./opendla/mobilenet_v2-b0353104.pth"
    model.load_state_dict(torch.load(model_weight_path, map_location=device))
    model.eval()
    with torch.no_grad():
        # predict class
        output = torch.squeeze(model(img.to(device))).cpu()
        predict = torch.softmax(output, dim=0)
        predict_cla = torch.argmax(predict).numpy()
        print("class id", predict_cla)

if __name__ == '__main__':
    main()

然后在 conda 的 classification 环境下运行

复制代码
python infer.py

5.3 转换模型为onnx模型

在这个


deep-learning-for-image-processing/pytorch_classification/Test6_mobilenet


路径下创建export.py文件,验证模型正确与否

复制代码
vi export.py

输入以下内容

复制代码
import os
import json

import torch
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt

from model_v2 import MobileNetV2

def main():
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    data_transform = transforms.Compose(
        [transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

    # load image
    img_path = "./apple.jpg"
    assert os.path.exists(img_path), "file: '{}' dose not exist.".format(img_path)
    img = Image.open(img_path)
    plt.imshow(img)
    # [N, C, H, W]
    img = data_transform(img)
    # expand batch dimension
    img = torch.unsqueeze(img, dim=0)

    # create model
    model = MobileNetV2(num_classes=1000).to(device)
    # load model weights
    model_weight_path = "./opendla/mobilenet_v2-b0353104.pth"
    model.load_state_dict(torch.load(model_weight_path, map_location=device))
    model.eval()
    torch.onnx.export(
        model,
        img.to(device),
        "./opendla/mobilenetv2.onnx",
        input_names=['images'],
        output_names=['output'],
        do_constant_folding=False
    )

if __name__ == '__main__':
    main()

优化图结构

复制代码
python -m onnxsim opendla/mobilenetv2.onnx opendla/mobilenetv2_sim.onnx

6.使用 comake 社区的端侧云ai仿真转换离线模型

详情可参考Comake 社区AI在线开发平台使用

7.拷贝到板端,离线运行模型

将6中转换好的模型img文件,拷贝至板端

这里先用sdk/verify/opendla/classification的代码验证一下

在板端执行:

复制代码
/customer/opendla/prog_classification -m mobilenet_v2_224.img -i apple.jpg
相关推荐
颜酱1 分钟前
# 02 | 搭骨架:用 LangGraph 编排 12 步工作流(思路)
前端·人工智能·后端
颜酱2 分钟前
02 | 搭骨架:用 LangGraph 编排 12 步工作流
前端·人工智能·后端
码上解惑4 分钟前
从 Dify 工作流说起:常用节点怎么选、怎样组合?
java·人工智能·ai·agent·dify·智能体·spring ai
无足鸟ICT7 分钟前
【RHCA+】查看变量
linux·运维·服务器
小陈phd8 分钟前
QAnything 阅读优化策略05——检索
人工智能·python·机器学习
Kina_C17 分钟前
Linux DNS 服务器-从高速缓存到辅助 DNS 部署指南
linux·运维·服务器·dns
suaizai_30 分钟前
TypeScript 7 正式发布!Vue 暂被 “拒之门外“ !!!
linux·运维·ubuntu
zhangfeng113335 分钟前
CVOCA 卷积模型,《Nature》子刊 特征提取技术突破性研究的综合分析报告
人工智能
炎火星40 分钟前
直播切片素材杂乱,怎么用 AI 剪辑做成可用带货视频?
人工智能