32

nihui · 2021年07月18日

【周易AIPU 仿真】一篇折腾r329的记录

一篇折腾r329的记录

0x0 直播玩r329多次翻车后的小结

https://aijishu.com/e/1120000000214336

为了让开发者更快得体验搭载周易AIPU处理器的R329芯片,极术社区联合矽速科技共同推出了R329开发板申请活动。该R329开发板由矽速科技开发,支持智能语音和视频,同时提供周易AIPU SDK下载。

好,是白嫖开发板活动!

系统:fedora 34

python 3.9 版本太新,装不了 tensorflow 1.15,又不喜欢 anaconda 环境,被迫学习了下怎么配 docker。直播玩 r329 仿真,结果因为代理问题,网速问题,tensorflow问题,各种各样的踩坑,导致直播翻车了好几天。今天终于把 mnasnet 的 aipu 仿真弄出来了,写这篇文件记录过程。

吐嘈:为什么校准图片需要提供label呢?有原始float模型直接可以在线算出来的呀 quq

太难了

0x1 安装 docker

dnf install moby-engine docker-compose
systemctl start docker

0x2 使用矽速科技提供的docker环境

docker pull zepan/zhouyi

docker pull 不吃 http_proxy/https_proxy 环境变量,要改配置文件

https://docs.docker.com/netwo...

但是这个 image 有 2g 多,mirror 都不缓存,即使配置了代理还是挺慢的,建议让别人帮你下载好发你

docker load --input zhouyi.tar
docker run -i -t zepan/zhouyi /bin/bash

0x3 导出tensorflow pb

https://github.com/tensorflow... 下载 master 最新的 zip

https://github.com/tensorflow... 下载 v1.15.0 的 zip

放到 docker 里面解压缩到 models-master 和 tensorflow-1.15.0

https://github.com/tensorflow... 下载 NASNet-A_Mobile_224 对应的 checkpoint 压缩包,解压到 docker 里的 /tmp/nasnet-a_mobile_04_10_2017

配置 pip 代理,代理地址一定要加 http:// 否则 urllib 会报错,后面安装 slim 和导出 pb 都需要这个代理

export http_proxy="http://192.168.31.210:4568"
export https_proxy="http://192.168.31.210:4568"

安装 slim 模块

cd models-master/research/slim
python setup.py install --user
cd models-master/research/slim
python3 export_inference_graph.py --alsologtostderr --model_name=nasnet_mobile --image_size=224 --output_file=/tmp/nasnet_mobile_inf.pb

用文本编辑器打开 /tmp/nasnet_mobile_inf.pb,是乱码,翻到最后面,看到这样的,说明最后输出的 node 是 final_layer/predictions

final_layer/predictionsSoftmaxfinal_layer/FC/BiasAdd*

配合 checkpoint 导出 frozen pb 到 /tmp/nasnet_mobile_frozen.pb

cd tensorflow-1.15.0/tensorflow/python/tools
python3 freeze_graph.py --input_graph=/tmp/nasnet_mobile_inf.pb --input_checkpoint=/tmp/nasnet-a_mobile_04_10_2017/model.ckpt --input_binary=true --output_graph=/tmp/nasnet_mobile_frozen.pb --output_node_names=final_layer/predictions

0x4 准备校准数据集

这里下载1000类的1000张图片

https://github.com/nihui/imag...

生成每行都是图片路径 label的imagelabel.txt

ls imagenet-sample-images | sort > image.txt
seq 0 999 > label.txt
paste -d ' ' image.txt label.txt > imagelabel.txt

把 demos/pb/dataset/ 里的 preprocess_dataset.py 拿出来改改开头

https://github.com/tensorflow... 得知,mnasnet 预处理采用 inception_preprocessing,即把 0~255 调整为 -1~1

img_dir='./imagenet-sample-images/'
label_file='./imagelabel.txt'

#MNASNET PARAM
input_height=224
input_width=224
input_channel = 3
mean = 127.5
var = 127.5

从 1000 张图生成 dataset.npy 和 label.npy 文件

python preprocess_dataset.py

0x5 准备 input.bin 和 output_ref.bin

把 demos/pb/model/ 里的 gen_inputbin.py 拿出来改改

这里有个大坑,mean 和 val 是不能照着模型的预处理写的,必须要写 mean=127.5 var=1 才可以,似乎npu总是输入 -127~127 范围的数值

input_height=224
input_width=224
input_channel = 3
mean = [127.5, 127.5, 127.5]
var = 1

测试图片偷懒,就直接用 demos 的 result.jpeg,于是 output_ref.bin 就复用了,执行这个脚本,生成 mnasnet 所需的 input.bin

python gen_inputbin.py

0x6 配置 aipubuild run.cfg

从官方步骤可知,aipubuild build.cfg 会生成 aipu.bin 放在板子上跑,aipubuild run.cfg 是直接在仿真器里跑

所以这次就直接配置 run.cfg 就可以了

把 demos/pb/config/ 里的 resnet_50_run.cfg 拿出来改改 nasnet_mobile_run.cfg

主要改的是 output input_model input 这几项,其他调整下路径就好,这里要特别注意,output 要用 softmax 前的 node,否则最后结果会错误

mnasnet softmax 前是 final_layer/FC/BiasAdd,不应该用最后的 final_layer/predictions

[Common]
mode=run

[Parser]
model_name = nasnet_mobile
detection_postprocess =
model_domain = image_classification
output = final_layer/FC/BiasAdd
input_model = ./tmp/nasnet_mobile_frozen.pb
input = input
input_shape = [1,224,224,3]
output_dir = ./

[AutoQuantizationTool]
model_name = nasnet_mobile
quantize_method = SYMMETRIC
ops_per_channel = DepthwiseConv
calibration_data = ./dataset.npy
calibration_label = ./label.npy
preprocess_mode = normalize
quant_precision=int8
reverse_rgb = False
label_id_offset = 0

[GBuilder]
inputs=./input.bin
simulator=aipu_simulator_z1
outputs=output_nasnet_mobile.bin
profile= True
target=Z1_0701

执行 aipubuild 运行仿真

aipubuild nasnet_mobile_run.cfg

输出log,并输出 output_nasnet_mobile.bin 结果文件,是仿真器npu跑出来的结果

[I] ==== auto-quantization ======
[I]     step1: get max/min statistic value DONE
[W] shift value is discrete in Depthwise, layer cell_stem_1/comb_iter_4/left/separable_3x3_1/separable_conv2d/depthwise_0, fixed by constraining shift value, may lead to acc drop
[W] shift value is discrete in Depthwise, layer cell_stem_1/comb_iter_4/left/separable_3x3_2/separable_conv2d/depthwise_0, fixed by constraining shift value, may lead to acc drop
[I]     step2: quantization each op DONE
[I]     step3: build quantization forward DONE
[I]     step4: show output scale of end node:
[I]             layer_id:643, layer_top:final_layer/FC/BiasAdd_0, output_scale:[13.73145]
[I] ==== auto-quantization DONE =
[I] Quantize model complete
[I] Building ...

[I] [IRChecker] Start to check IR: /tmp/AIPUBuilder_1626582538.8113065/nasnet_mobile_int8.txt
[I] [IRChecker] model_name: nasnet_mobile
[I] [IRChecker] IRChecker: All IR pass
[I] [graph.cpp : 846] loading graph weight: /tmp/AIPUBuilder_1626582538.8113065/nasnet_mobile_int8.bin size: 0x533efe
[I] [main.cpp  : 135] Simulator finished.
Total errors: 0,  warnings: 0

上面的log省略了一些不重要的信息,注意 Start to check IR: /tmp/AIPUBuilder_1626582538.8113065/nasnet_mobile_int8.txt 是ir文件,打开这个文件,拉到最后看到

layer_id=643
layer_name=final_layer/FC/MatMul
layer_type=FullyConnected
layer_bottom=[final_layer/Mean_0]
layer_bottom_shape=[[1,1056]]
layer_bottom_type=[uint8]
layer_top=[final_layer/FC/BiasAdd_0]
layer_top_shape=[[1,1001]]
layer_top_type=[int8]
weights_type=int8
weights_offset=4394554
weights_size=1057056
weights_shape=[1001,1056]
biases_type=int32
biases_offset=5451610
biases_size=4004
biases_shape=[1001]
with_activation=NONE
scale_type=uint8
scale_value=151
shift_type=int8
shift_value=17
num_output=1001

layer_top_type 的类型是 int8,这个很重要,记下来

0x7 npu量化推理结果比对

把 demos/pb/ 里的 quant_predict.py 和 imagenet_classes.py 拿出来改改

主要是改 outputfile 路径,以及 npyoutput 的数据类型,int8 改成 np.int8,uint8 改成 np.uint8

前面得知是 int8,要用 np.int8

outputfile = current_dir + '/output_nasnet_mobile.bin'
npyoutput = np.fromfile(outputfile, dtype=np.int8)

运行 quant_predict.py

python quant_predict.py

输出如下,可以看到 top3 有 231 232,还行

predict first 5 label:
index  231, prob 127, name: Shetland sheepdog, Shetland sheep dog, Shetland
index  232, prob 120, name: collie
index  158, prob  46, name: papillon
index  170, prob  41, name: borzoi, Russian wolfhound
index  194, prob  35, name: Australian terrier
true first 5 label:
index  230, prob 109, name: Old English sheepdog, bobtail
index  231, prob  96, name: Shetland sheepdog, Shetland sheep dog, Shetland
index  232, prob  57, name: collie
index  226, prob  54, name: malinois
index  263, prob  53, name: Brabancon griffon
Detect picture save to result.jpeg
推荐阅读
关注数
16354
内容数
1226
嵌入式端AI,包括AI算法在推理框架Tengine,MNN,NCNN,PaddlePaddle及相关芯片上的实现。欢迎加入微信交流群,微信号:aijishu20(备注:嵌入式)
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息