嵌入式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
相关推荐
2401_8414956420 小时前
【自然语言处理】处理 GBK 编码汉字的算法设计
人工智能·python·自然语言处理·校验·文件读写·gbk编码与解码·批量过滤
怎么全是重名20 小时前
Survey on semantic segmentation using deep learning techniques
图像处理·人工智能·深度学习·图像分割
老蒋新思维20 小时前
创客匠人:工作流嵌入式智能体,重构知识变现的效率底层
大数据·服务器·人工智能·重构·创始人ip·创客匠人·知识变现
2501_9419820520 小时前
展望:RPA与AI在企业微信自动化领域的未来融合趋势
人工智能·企业微信·rpa
绵羊202320 小时前
索拉非尼作用机制
linux
小脉传媒GEO优化20 小时前
GEO优化数据统计系统DeepAnaX系统详细介绍:开启AI数据智能分析新范式
人工智能·信息可视化
爱笑的眼睛1120 小时前
MLflow Tracking API:超越实验记录,构建可复现的机器学习工作流
java·人工智能·python·ai
世岩清上20 小时前
以技术预研为引擎,驱动脑机接口等未来产业研发与应用创新发展
人工智能·脑机接口·未来产业
杜子不疼.20 小时前
【Linux】 进程控制(二):进程等待(wait/waitpid) 与 子进程获取(status)
linux·运维·服务器