概述
本示例工程中我们会在 tensorflow2 下使用 keras api 创建一个自定义 cnn 网络,在 vitis-ai 1.3 环境下编译成 xilinx dpu 上运行的模型文件,并在 xilinx zynqmp 上部署运行。
该示例工程应在 vitis-ai1.3 docker,vitis-ai-tensorflow2conda 环境中运行。
有关mnist手写数字数据集,请参见http://yann.lecun.com/exdb/mnist/。
在该例程中我们会演示以下的步骤:
使用 tensorflow2 训练和评估小型自定义卷积神经网络
对浮点模型进行量化 (quantization)
对量化模型进行评估
使用校准数据集对训练后的模型进行微调 (finetuning)
编译量化和微调的模型,以创建 .xmodel 文件,准备在 dpu 加速器 ip 上执行在 zynqmp 评估板上下载并运行该模型
代码说明
可从下面的 github 页面获得源码。
https://github.com/lobster1989/mnist-classification-vitis-ai-1.3-tensorflow2
python 及 shell 脚本源码说明如下:
load_data.py:下载 mnist 数据集;
generate_images.py:从 keras 在线数据集生成本地图像文件;
train.py:创建和训练用于 mnist 分类的简单 cnn 模型, 经过训练的浮点模型将被保存到本地;
quantize.py:使用 vitisquantizer 量化前一个步骤保存的浮点模型,量化后的模型将被保存到本地;
eval_quantized.py:评估量化模型的 accuracy;
finetune.py:利用 vitis-ai 进行模型微调(仅作演示,后续编译部署的为 quantize 步骤保存的模型);
compile_zcu102.sh/compile_zcu104.sh:启动 vai_c_tensorflow2 命令以将量化或微调的模型编译为供 dpu 运行的 .xmodel 文件。两个文件分别对应目标为 zcu102 和 zcu104 开发板;
make_target_zcu102.sh/make_target_zcu104.sh:将.xmodel和图像复制到 。/target_zcu10x 文件夹中,准备好将其复制到 zcu102/zcu104 评估板的 sd 卡中。
模型创建/量化/编译
在运行此部分之前,我们应在 vitis-ai docker 环境下激活 vitis-ai-tensorflow2anaconda 环境。有关更多详细信息,请参阅最新版本的 ug1414。
(vitis-ai-tensorflow2) vitis-ai /workspace/myproj/tf2-mnist-end-to-end 》 python train.py
load mnist dataset.。
create custom cnn.。
model: “mnist_customcnn_model”
_________________________________________________________________
layer (type) output shape param #
=================================================================
input_1 (inputlayer) [(none, 28, 28, 1)] 0
_________________________________________________________________
conv2d (conv2d) (none, 26, 26, 32) 320
_________________________________________________________________
max_pooling2d (maxpooling2d) (none, 13, 13, 32) 0
_________________________________________________________________
conv2d_1 (conv2d) (none, 11, 11, 64) 18496
_________________________________________________________________
max_pooling2d_1 (maxpooling2 (none, 5, 5, 64) 0
_________________________________________________________________
conv2d_2 (conv2d) (none, 3, 3, 64) 36928
_________________________________________________________________
flatten (flatten) (none, 576) 0
_________________________________________________________________
dense (dense) (none, 64) 36928
_________________________________________________________________
dense_1 (dense) (none, 10) 650
=================================================================
total params: 93,322
trainable params: 93,322
non-trainable params: 0
_________________________________________________________________
fit on dataset.。
epoch 1/10
782/782 [==============================] - 13s 16ms/step - loss: 0.1843 - accuracy: 0.9427 - val_loss: 0.0701 - val_accuracy: 0.9813
epoch 2/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0529 - accuracy: 0.9835 - val_loss: 0.0543 - val_accuracy: 0.9855
epoch 3/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0346 - accuracy: 0.9894 - val_loss: 0.0472 - val_accuracy: 0.9877
epoch 4/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0252 - accuracy: 0.9929 - val_loss: 0.0463 - val_accuracy: 0.9878
epoch 5/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0188 - accuracy: 0.9945 - val_loss: 0.0494 - val_accuracy: 0.9896
epoch 6/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0147 - accuracy: 0.9956 - val_loss: 0.0513 - val_accuracy: 0.9901
epoch 7/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0121 - accuracy: 0.9966 - val_loss: 0.0452 - val_accuracy: 0.9920
epoch 8/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0096 - accuracy: 0.9973 - val_loss: 0.0542 - val_accuracy: 0.9906
epoch 9/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0088 - accuracy: 0.9976 - val_loss: 0.0640 - val_accuracy: 0.9893
epoch 10/10
782/782 [==============================] - 5s 7ms/step - loss: 0.0073 - accuracy: 0.9978 - val_loss: 0.0709 - val_accuracy: 0.9893
save trained model to./models/float_model.h5.
evaluate model on test dataset.。
157/157 [==============================] - 1s 3ms/step - loss: 0.0426 - accuracy: 0.9911
loss: 0.043
acc: 0.991
(vitis-ai-tensorflow2) vitis-ai /workspace/myproj/tf2-mnist-end-to-end 》 python quantize.py
load float model.。
load mnist dataset.。
run quantization.。
[info] start crosslayerequalization.。.
10/10 [==============================] - 0s 32ms/step
[info] crosslayerequalization done.
[info] start quantize calibration.。.
157/157 [==============================] - 12s 77ms/step
[info] quantize calibration done.
[info] start generating quantized model.。.
[warning] skip quantize pos adjustment for layer quant_dense, its quantize pos is [i=none, w=8.0, b=9.0, o=3.0]
[info] generating quantized model done.
saved quantized model as 。/models/quantized_model.h5
(vitis-ai-tensorflow2) vitis-ai /workspace/myproj/tf2-mnist-end-to-end 》 python eval_quantized.py
load quantized model.。
warningno training configuration found in the save file, so the model was *not* compiled. compile it manually.
load mnist dataset.。
compile model.。
evaluate model on test dataset
157/157 [==============================] - 4s 22ms/step - loss: 0.0417 - accuracy: 0.9913
loss: 0.042
acc: 0.991
(vitis-ai-tensorflow2) vitis-ai /workspace/myproj/tf2-mnist-end-to-end 》 python finetune.py
load float model.。
load mnist dataset.。
create quantize training model.。
[info] start crosslayerequalization.。.
10/10 [==============================] - 0s 33ms/step
[info] crosslayerequalization done.
compiling model.。
fit on dataset.。
epoch 1/10
782/782 [==============================] - 48s 61ms/step - loss: 0.0077 - accuracy: 0.9978 - val_loss: 0.0738 - val_accuracy: 0.9882
epoch 2/10
782/782 [==============================] - 39s 50ms/step - loss: 0.0062 - accuracy: 0.9980 - val_loss: 0.0845 - val_accuracy: 0.9888
epoch 3/10
782/782 [==============================] - 40s 51ms/step - loss: 0.0058 - accuracy: 0.9983 - val_loss: 0.0810 - val_accuracy: 0.9885
epoch 4/10
782/782 [==============================] - 40s 51ms/step - loss: 0.0061 - accuracy: 0.9982 - val_loss: 0.0744 - val_accuracy: 0.9902
epoch 5/10
782/782 [==============================] - 40s 51ms/step - loss: 0.0048 - accuracy: 0.9984 - val_loss: 0.0834 - val_accuracy: 0.9911
epoch 6/10
782/782 [==============================] - 39s 50ms/step - loss: 0.0047 - accuracy: 0.9986 - val_loss: 0.0807 - val_accuracy: 0.9893
epoch 7/10
782/782 [==============================] - 39s 50ms/step - loss: 0.0039 - accuracy: 0.9987 - val_loss: 0.0894 - val_accuracy: 0.9903
epoch 8/10
782/782 [==============================] - 39s 50ms/step - loss: 0.0034 - accuracy: 0.9989 - val_loss: 0.0863 - val_accuracy: 0.9904
epoch 9/10
782/782 [==============================] - 39s 49ms/step - loss: 0.0042 - accuracy: 0.9989 - val_loss: 0.1043 - val_accuracy: 0.9893
epoch 10/10
782/782 [==============================] - 39s 50ms/step - loss: 0.0044 - accuracy: 0.9986 - val_loss: 0.0994 - val_accuracy: 0.9908
saved finetuned model as 。/models/finetuned_model.h5
evaluate model on test dataset.。
157/157 [==============================] - 1s 7ms/step - loss: 0.0675 - accuracy: 0.9920
loss: 0.068
acc: 0.992
(vitis-ai-tensorflow2) vitis-ai /workspace/myproj/tf2-mnist-end-to-end 》 bash -x compile_zcu102.sh
+ arch=/opt/vitis_ai/compiler/arch/dpuczdx8g/zcu102/arch.json
+ outdir=。/compiled_model
+ net_name=customcnn
+ model=。/models/finetuned_model.h5
+ echo -----------------------------------------
-----------------------------------------
+ echo ‘compiling model for zcu102.。’
compiling model for zcu102.。
+ echo -----------------------------------------
-----------------------------------------
+ compile
+ tee compile.log
+ vai_c_tensorflow2 --model 。/models/finetuned_model.h5 --arch /opt/vitis_ai/compiler/arch/dpuczdx8g/zcu102/arch.json --output_dir 。/compiled_model --net_name customcnn
/opt/vitis_ai/conda/envs/vitis-ai-tensorflow2/lib/python3.7/site-packages/xnnc/translator/tensorflow_translator.py h5pydeprecationwarning: dataset.value has been deprecated. use dataset[()] instead.
value = param.get(group).get(ds).value
[info] parse raw model :100%|██████████| 10/10 [00:00《00:00, 16871.70it/s]
[info] infer shape (nhwc) :100%|██████████| 26/26 [00:00《00:00, 2956.30it/s]
[info] generate xmodel :100%|██████████| 26/26 [00:00《00:00, 5561.60it/s]
[info] namespace(inputs_shape=none, layout=‘nhwc’, model_files=[‘。/models/finetuned_model.h5’], model_type=‘tensorflow2’, out_filename=‘。/compiled_model/customcnn_org.xmodel’, proto=none)
[info] tensorflow2 model: models/finetuned_model.h5
[opt] no optimization method available for xir-level optimization.
[info] generate xmodel: /workspace/myproj/tf2-mnist-end-to-end/compiled_model/customcnn_org.xmodel
[unilog][info] the compiler log will be dumped at “/tmp/vitis-ai-user/log/xcompiler-20210325-093926-3120”
[unilog][info] target architecture: dpuczdx8g_isa0_b4096_max_bg2
[unilog][info] compile mode: dpu
[unilog][info] debug mode: function
[unilog][info] target architecture: dpuczdx8g_isa0_b4096_max_bg2
[unilog][info] graph name: mnist_customcnn_model, with op num: 42
[unilog][info] begin to compile.。.
[unilog][info] total device subgraph number 3, dpu subgraph number 1
[unilog][info] compile done.
[unilog][info] the meta json is saved to “/workspace/myproj/tf2-mnist-end-to-end/。/compiled_model/meta.json”
[unilog][info] the compiled xmodel is saved to “/workspace/myproj/tf2-mnist-end-to-end/。/compiled_model/customcnn.xmodel”
[unilog][info] the compiled xmodel‘s md5sum is 764142e83d074ea9470b9eb9d0757f68, and been saved to “/workspace/myproj/tf2-mnist-end-to-end/。/compiled_model/md5sum.txt”
**************************************************
* vitis_ai compilation - xilinx inc.
**************************************************
+ echo -----------------------------------------
-----------------------------------------
+ echo ’model compiled‘
model compiled
+ echo -----------------------------------------
-----------------------------------------
(vitis-ai-tensorflow2) vitis-ai /workspace/myproj/tf2-mnist-end-to-end 》 bash -x make_target_zcu102.sh
+ echo -----------------------------------------
-----------------------------------------
+ echo ’make target zcu102 started.。‘
make target zcu102 started.。
+ echo -----------------------------------------
-----------------------------------------
+ target_zcu102=。/target_zcu102
+ compile_zcu102=。/compiled_model
+ app=。/application
+ net_name=customcnn
+ rm -rf 。/target_zcu102
+ mkdir -p 。/target_zcu102/model_dir
+ cp 。/application/app_mt.py 。/target_zcu102
+ echo ’ copied application to target_zcu102 folder‘
copied application to target_zcu102 folder
+ cp 。/compiled_model/customcnn.xmodel 。/target_zcu102/model_dir/。
+ echo ’ copied xmodel file(s) to target_zcu102 folder‘
copied xmodel file(s) to target_zcu102 folder
+ mkdir -p 。/target_zcu102/images
+ python generate_images.py --dataset=mnist --image_dir=。/target_zcu102/images --image_format=jpg --max_images=10000
2021-03-25 0934.445257: i tensorflow/stream_executor/platform/default/dso_loader.cc:48] successfully opened dynamic library libcudart.so.10.1
command line options:
--dataset : mnist
--subset : test
--image_dir : 。/target_zcu102/images
--image_list :
--label_list :
--image_format : jpg
--max_images : 10000
+ echo ’ copied images to target_zcu102 folder‘
copied images to target_zcu102 folder
+ echo -----------------------------------------
-----------------------------------------
+ echo ’make target zcu102 completed‘
make target zcu102 completed
+ echo -----------------------------------------
-----------------------------------------
在 zynqmp 评估板上运行
进行电路板设置。
之后,将 target_zcu10x 目录中的所有文件复制到 sd 卡。从 sd 卡启动 zcu10x(下面实例运行的是 zcu104),运行 app_mt.py。结果显示,实际运行模型的精度为 99.13%.
root@xilinx-zcu104-2020_2:~/target_zcu104# python3 app_mt.py --threads 4
command line options:
--image_dir : images
--threads : 4
--model : model_dir/customcnn.xmodel
pre-processing 10000 images.。.
starting 4 threads.。.
throughput=5622.61 fps, total frames = 10000, time=1.7785 seconds
correct:9913, wrong:87, accuracy:0.9913
原文标题:开发者分享 | vitis-ai 1.3/tensorflow2 环境下使用自定义 cnn 模型进行 mnist 分类
文章出处:【微信公众号:xilinx技术社区】欢迎添加关注!文章转载请注明出处。
血氧仪都用到哪些芯片?
高效氮化镓电源设计方案 GaN在基于图腾柱PFC的电源设计中实现高效率
过了2021年中国民用无人机行业将没有了门槛
无人机中超声波原理
Apple猛挖墙脚:iPhone7高通要悲剧
如何在TensorFlow2里使用Keras API创建一个自定义CNN网络?
平衡的界面离子浓度和迁移空间障碍促进高效沉积/溶解电池化学
90后可以通过哪一些形式来抓住互联网的好机遇
系统封装图文教程(精华)
从八个步骤分析无人驾驶系统安全
骁龙888 5G移动平台即将迎来首秀
关于玻璃纤维的制造和应用
2014年音视频及家电频道最受关注热文TOP20
以摩洛哥后翻蜘蛛为灵感的行走机器人
自动变速箱降油耗方法分享
魅族这个视频破三观,这些妹子懂得真多
Allegro新产品设计用于需要高精度和高分辨率的应用
什么是电子增材制造?|一种得益于新材料的加成法电子制造工艺
闻亭数字成为TI DLP中国区分销商
不同种类液位计在液LV储罐液位测量中的应用