华为昇腾Atlas200边缘设备的CANN环境安装,边缘设备安装Anaconda,下载IDE(MindStudio),.pt转.onnx,.onnx转换.om,模型加载方法(拿到新设备后)
用户名:HwHiAiUser
密码:Mind@123
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
用nano打开,然后添加华为源。**注意!!**需要用华为的不然有些包无法下载。
2:安装CANN软件
注意!!!需要全程用HwHiAiUser用户
-
准备软件包的版本为: Ascend-cann-toolkit_6.0.1_linux-aarch64.run(下载链接)。上传到/home/HwHiAiUser目录下
-
安裝依赖参考链接,然后安装python3.7.5,最后配置pip的源(最好选择华为的)参考链接
**安装python注意事项!!**测试是否安装成功
#安装依赖 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!
-
安装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
**注意!!**需要先执行–check再安装–install
-
配置cann的环境变量参考链接,**注意:**查看链接的$HOME/Ascend是否是. /home/HwHiAiUser/Ascend,环境变量是. /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh
. /usr/local/Ascend/ascend-toolkit/set_env.sh
打开vi ~/.bashrc 添加. /usr/local/Ascend/ascend-toolkit/set_env.sh
安装成功后/home/HwHiAiUser目录下会出现Ascend文件
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
安装:
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
安装成功后conda无法调用问题:需要su进入root权限
4:配置加载模型所需的包和依赖
-
激活环境,下载加载模型所需包安装python-acllite。下载链接
安装完成后/home/HwHiAiUser/Ascend/目录下会出现thirdpart文件
5:下载IDE(MindStudio)
-
准备安装依赖,参考链接
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
-
下载版本为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
**注意!!!**需要在HwHiAiUser用户下,下载完成后进入jbr
-
执行命令启动
cd MindStudio/bin ./MindStudio.sh
启动:
6:代码转换
6.1:.pt转.onnx
#切换Anaconda后的虚拟环境
#下载安装库
pip install ultralytics
运行转换
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')
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
转换om需要在atlas设备上进行,先输入atc出现如下start说明可以运行,否则就是cann安装失败,需要重新安装cann
随后执行
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,])