当前位置:   article > 正文

Atlas200安装CANN环境,安装Anaconda,安装MindStudio,.pt转.onnx,.onnx转换.om以及代码执行全套流程_pt转om

pt转om

华为昇腾Atlas200边缘设备的CANN环境安装,边缘设备安装Anaconda,下载IDE(MindStudio),.pt转.onnx,.onnx转换.om,模型加载方法(拿到新设备后)

用户名:HwHiAiUser

密码:Mind@123

image-20231128105151077

1:apt换源

#编辑sources.list文件
sudo nano /etc/apt/sources.list

deb https://repo.huaweicloud.com/ubuntu-ports/ bionic main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic main restricted universe multiverse

deb https://repo.huaweicloud.com/ubuntu-ports/ bionic-security main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic-security main restricted universe multiverse

deb https://repo.huaweicloud.com/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic-updates main restricted universe multiverse

deb https://repo.huaweicloud.com/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic-backports main restricted universe multiverse

保存文件:按下 Ctrl + O,然后按 Enter 确认文件名。
退出编辑器:按下 Ctrl + X。

#更新软件源:
sudo apt-get update

#升级软件包
sudo apt-get upgrade
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

nano打开,然后添加华为源。**注意!!**需要用华为的不然有些包无法下载。

image-20231229142201384

image-20231229142218971

2:安装CANN软件

注意!!!需要全程用HwHiAiUser用户

  1. 准备软件包的版本为: Ascend-cann-toolkit_6.0.1_linux-aarch64.run下载链接)。上传到/home/HwHiAiUser目录下

    image-20231229142253614

  2. 安裝依赖参考链接,然后安装python3.7.5,最后配置pip的源(最好选择华为的)参考链接

    image-20231229142332700

    **安装python注意事项!!**测试是否安装成功

    image-20231229142346416 image-20231229142401912

    #安装依赖
    sudo apt-get install -y gcc g++ make cmake zlib1g zlib1g-dev openssl libsqlite3-dev libssl-dev libffi-dev unzip pciutils net-tools libblas-dev gfortran libblas3                          
    
    #安装python,任意地址下载
    wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz
    
    #解压源码包
    tar -zxvf Python-3.7.5.tgz
    
    #进入解压后的文件夹,执行配置、编译和安装命令
    cd Python-3.7.5
    ./configure --prefix=/usr/local/python3.7.5 --enable-loadable-sqlite-extensions --enable-shared
    make
    sudo make install
    
    #设置python3.7.5环境变量。
    #用于设置python3.7.5库文件路径
    export LD_LIBRARY_PATH=/usr/local/python3.7.5/lib:$LD_LIBRARY_PATH
    export PATH=/usr/local/python3.7.5/bin:$PATH
    
    #配置pip的源
    mkdir ~/.pip 
    cd ~/.pip
    
    #编辑pip.conf文件
    vi pip.conf                      
    #写入以下内容:
    [global]
    #以华为源为例,请根据实际情况进行替换。
    index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
    trusted-host = mirrors.huaweicloud.com
    timeout = 120
    
    #保存
    :wq!                           
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  3. 安装cann开发套件包参考链接

    #以HwHiAiUser用户将开发套件包上传到Atlas 200 DK任意目录
    #执行如下命令为安装包增加可执行权限。
    chmod +x *.run
    
    #执行如下校验安装包的一致性和完整性。
    ./Ascend-cann-toolkit_{version}_linux-aarch64.run --check
    
    #执行如下命令进行Toolkit软件包的安装。
    ./Ascend-cann-toolkit_{version}_linux-aarch64.run --install
    
    #配置环境变量。
    vi ~/.bashrc
    #写入以下内容:
    . /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh    
    #保存
    :wq!
    
    #其立即生效
    source ~/.bashrc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    **注意!!**需要先执行–check再安装–install

    image-20231128112224261

  4. 配置cann的环境变量参考链接,**注意:**查看链接的$HOME/Ascend是否是. /home/HwHiAiUser/Ascend,环境变量是. /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh

    . /usr/local/Ascend/ascend-toolkit/set_env.sh    
    
    • 1

    打开vi ~/.bashrc 添加. /usr/local/Ascend/ascend-toolkit/set_env.sh

    image-20231229142425322

    安装成功后/home/HwHiAiUser目录下会出现Ascend文件

    image-20231128112719717

3:安装Anaconda

链接

下载软件:

wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh

wget -c https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-aarch64.sh
  • 1
  • 2
  • 3

安装:

chmod +x Anaconda3-2021.05-Linux-aarch64.sh
./Anaconda3-2021.05-Linux-aarch64.sh

vi ~/.bashrc
#添加环境变量,参考链接
export PATH=/root/anaconda3/bin:$PATH

#更新
source ~/.bashrc

#linux下启动环境
su
source activate
conda activate 200DK

#创建虚拟环境
conda create -n yourName python=3.7 
conda create -n 200 python=3.7 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

安装成功后conda无法调用问题:需要su进入root权限image-20231128112946424

4:配置加载模型所需的包和依赖

  1. 激活环境,下载加载模型所需包安装python-acllite。下载链接

    安装完成后/home/HwHiAiUser/Ascend/目录下会出现thirdpart文件

    image-20231128115704513

5:下载IDE(MindStudio)

  1. 准备安装依赖,参考链接

    sudo apt-get install -y gcc g++ make cmake zlib1g-dev libbz2-dev libsqlite3-dev libssl-dev libffi-dev unzip pciutils net-tools libblas-dev gfortran libblas3 liblapack-dev openssh-server xterm firefox xdg-utils libdbus-glib-1-dev gdb
    
    • 1

    image-20231229142443401

  2. 下载版本为3.0.4

    #下载MindStudio 3.0.4安装包:
    wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/MindStudio/MindStudio%203.0.4/MindStudio_3.0.4_linux.tar.gz
    
    #解压
    tar -zxvf MindStudio_3.0.4_linux.tar.gz
    
    #解压jbr至MindStudio安装根目录
    #下载
    wget https://cache-redirector.jetbrains.com/intellij-jbr/jbr-11_0_10-linux-aarch64-b1341.35.tar.gz
    #解压
    tar -zxvf jbr-11_0_10-linux-aarch64-b1341.35.tar.gz
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    **注意!!!**需要在HwHiAiUser用户下,下载完成后进入jbrimage-20231229142500530

    image-20231117155604084

  3. 执行命令启动

    cd MindStudio/bin
    ./MindStudio.sh
    
    • 1
    • 2

启动:image-20231229142514527

6:代码转换

6.1:.pt转.onnx

#切换Anaconda后的虚拟环境
#下载安装库
pip install ultralytics
  • 1
  • 2
  • 3

运行转换

import sys
from ultralytics import YOLO

#采用源码训练、验证、预测、导出模型,不需要依赖安装ultralytics
# Load a model
# model = YOLO(r"E:\weizhuang925new\camouflageRecognitionV2\static\ptFiles\BaseModel\BaseModel_RGB.pt")   # load a pretrained model (recommended for training)
model = YOLO(r"E:\weizhuang925new\camouflageRecognitionV2\static\defaultModelPt\yolov8l-seg.pt")   # load a pretrained model (recommended for training)

success = model.export(format="onnx",opset=12)  # export the model to ONNX format  #转换为onnx模型
print('model.export success')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6.2:.onnx转换.om

#在atlas200的设备上打开一个命令窗口,执行下面命令:
/usr/local/Ascend/ascend-toolkit/6.0.RC1/atc/bin/atc  --model=/home/rooty/yolo_model/BaseModel_HW.onnx --framework=5 --output=/home/rooty/yolo_model/BaseModel_HW --input_shape="images:1,3,640,640"  --soc_version=Ascend310


atc  --model="/home/HwHiAiUser/jieyidanao/tinityBuild3_100.onnx" --framework=5 --output=/home/HwHiAiUser/jieyidanao/tinityBuild3_100 --input_shape="input.1:1,3,256,256"  --soc_version=Ascend310
  • 1
  • 2
  • 3
  • 4
  • 5

转换om需要在atlas设备上进行,先输入atc出现如下start说明可以运行,否则就是cann安装失败,需要重新安装cann

image-20231229142526756

随后执行image-20231128114319688

7:代码执行

7.1:模型加载方法

参考链接

from acllite_model import AclLiteModel
from acllite_resource import AclLiteResource	

#初始化
acl_resource = AclLiteResource()
acl_resource.init()

#加载模型
model = AclLiteModel(MODEL_PATH)

#图像处理
def preprocess(img_path):
    image = Image.open(img_path)
    img_h = image.size[1]
    img_w = image.size[0]
    net_h = MODEL_HEIGHT
    net_w = MODEL_WIDTH

    image_ = image.resize((new_w, new_h))
    
    new_image= np.array(image_)
    new_image = new_image.astype(np.float32)
    new_image = new_image / 255
    print('new_image.shape', new_image.shape)
    new_image = new_image.transpose(2, 0, 1).copy()
    return new_image, image
    
#运行模型
result_list = model.execute([data,])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/338982
推荐阅读
相关标签
  

闽ICP备14008679号