嵌入式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
相关推荐
7***n753 小时前
API网关设计模式
linux·服务器·设计模式
m0_650108243 小时前
Gemini 2.5:重塑多模态 AI 边界的全面解读
论文阅读·人工智能·多模态大模型·gemini 2.5·跨模态融合
wuk9983 小时前
基于Matlab的彩色图像特征提取实现
人工智能·计算机视觉·matlab
GEO_NEWS3 小时前
2025下半年GEO服务商技术革命:万数科技以AI全链路优化定义行业标杆
人工智能
说私域3 小时前
智能名片链动2+1模式S2B2C商城小程序:构建私域生态“留”量时代的新引擎
大数据·人工智能·小程序
说私域3 小时前
基于开源AI大模型与AI智能名片S2B2C商城小程序的直播简介引流策略研究——以B站直播为例
人工智能·小程序
金紫火3 小时前
AiPPT 新功能体验:从“一键生成”到更智能的演示制作
人工智能·ppt
Dev7z3 小时前
基于计算机视觉与机器学习的课堂坐姿智能监测与预警系统
人工智能·机器学习·计算机视觉
哈里谢顿3 小时前
环境变量 HTTP_PROXY/HTTPS_PROXY 深度解析:为什么 cURL 行,Docker 不行?
linux
水如烟4 小时前
孤能子视角:“多劳多得”原则在知识经济时代的失效危机分析
人工智能