FPGA上利用Vitis AI部署resnet50 TensorFlow神经网络模型

参考Xilinx官方教程快速入门 • Vitis AI 用户指南 (UG1414) 克隆 Vitis AI 存储库以获取示例、参考代码和脚本(连接github失败可能需要科学上网)。

[Host]$ git clone https://github.com/Xilinx/Vitis-AI

[Host]$ cd Vitis-AI

安装Docker如何在 Ubuntu 20.04 上安装和使用 Docker 安装完docker后,下载最新Vitis AI Docker,

Host]$ docker pull xilinx/vitis-ai--cpu:latest

将官方的指令docker pull xilinx/vitis-ai--cpu:latest改为docker pull xilinx/vitis-ai-tensorflow2-cpu:latest,从tensorflow,pytorch,tensorflow2中三选一。遇到问题permission denied如下, 将当前用户添加到用户组,解决docker权限问题,运行的命令如下:

sudo groupadd docker #添加用户组

sudo gpasswd -a username docker #将当前用户添加至用户组,username写自己的用户名

newgrp docker #更新用户组

使用git checkout v3.0 切换到3.0版本 运行以下命令安装交叉编译系统环境

[Host]$ ./host_cross_compiler_setup.sh

如果提示host_cross_compiler_setup.sh找不到命令,需要使用chmod命令修改文件权限,使文件可执行

安装完交叉编译链后,有如下提示:

Please run the following command to enable Cross Compiler

source /root/petalinux_sdk_2022.2/environment-setup-cortexa72-cortexa53-xilinx-linux

If you run the above command failed, run the following commands to enable Cross Compiler

unset LD_LIBRARY_PATH

source /root/petalinux_sdk_2022.2/environment-setup-cortexa72-cortexa53-xilinx-linux

下载并设置开发板镜像 Design License Agreement (XEF) (xilinx.com) 使用Etcher将镜像文件烧写到SD卡上,将含镜像的SD卡插入目标开发板,通电并使用串行端口启动开发板以在系统上进行操作,使用串行端口来设置开发板的IP信息(ifconfig)。

下载tensorflow resnet50 模型

[Host]$ cd Vitis-AI

[Host]$ wget https://www.xilinx.com/bin/public/openDownload?filename=tf_resnetv1_50_imagenet_224_224_6.97G_3.0.zip -O tf_resnetv1_50_imagenet_224_224_6.97G_.0.zip

[Host]$ unzip tf_resnetv1_50_imagenet_224_224_6.97G_3.0.zip

量化模型

量化模型时 ,从 Imagenet_calib.tar.gz下载数据集,并复制到 Vitis-AI 文件夹中

启动 Docker 镜像./docker_run.sh xilinx/vitis-ai-tensorflow-cpu:latest

在 tf_resnetv1_50_imagenet_224_224_6.97G_3.0/code/quantize/config.ini 中,将 CALIB_BATCH_SIZE 设置为 5。然后运行

[Docker]$ conda activate vitis-ai-tensorflow

[Docker]$ tar -xzvf Imagenet_calib.tar.gz -C tf_resnetv1_50_imagenet_224_224_6.97G_3.0/data

[Docker]$ cd tf_resnetv1_50_imagenet_224_224_6.97G_3.0/code/quantize

[Docker]$ bash quantize.sh

量化需要1个小时45分钟, 运行 quantize.sh 后,已量化的模型包含在 tf_resnetv1_50_imagenet_224_224_6.97G_3.0/quantized 中

编译模型

vai_c_tensorflow -f ./quantized/quantized_baseline_6.96B_919.pb -a /opt/vitis_ai/compiler/arch/DPUCZDX8G/KV260/arch.json -o ./compiled -n resnet50_tf

修改ipv4的网络配置,输入命令sudo vim /etc/network/interfaces,编辑文件

重启网络服务:

sudo /etc/init.d/networking force-reload

sudo /etc/init.d/networking restart

此时IPv4的地址已经修改成功,可以和主机ping通。

将已经编译的模型复制到开发板上,要求输入root的密码,密码为root scp tf_resnetv1_50_imagenet_224_224_6.97G_3.0/compiled/resnet50_tf.xmodel root@[BOARD_IP]:~

下载 vitis_ai_runtime_r3.0.0_image_video.tar.gz 测试图,通过MobaXterm发送到开发板上,并在目标边缘平台上解压将下载好的测试图片。 解压图片压缩包tar -xzvf vitis_ai_runtime_r3.0.0_image_video.tar.gz -C Vitis-AI/examples/vai_runtime

运行 resnet50 示例

cd ~/Vitis-AI/examples/vai_runtime/resnet50

./resnet50 ~/resnet50_tf.xmodel

运行结果:

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: