甴尐 · 2021年07月17日

【周易AIPU 仿真】一个硬件工程师跑:R329 开发板docker下仿真流程

docker 环境搭建

镜像

image.png
24M网速 一分钟搞定.
copy到ubuntu 服务器
image.png

容器

image.png
之后所有操作都在容器中了.记住关键 CONTAINER ID
进入docker 环境
sudo docker exec -it e97638d58202 /bin/sh 可以开多窗口
sudo docker attach e97638d58202 好像只能开单窗口
docker 和主机传输文件
sudo docker cp e97638d58202:/share/resnet_101_mode/result.jpeg

跑通demo

`
cd ~/demos/tflite
./run_sim.sh
python3 quant_predict.py
`

跑通模型

构建自己的目录

mkdir /share
docker 没有安装git
apt-get install git
下载所需文件和仓库
image.png
image.png
备注
image.png
导出图需要安装
python3 setup.py install

下载模型

https://github.com/tensorflow...

创建模型文件夹
mkdir resnet_101_mode
docker 支持wget 直接在docker 下载
wget wget http://download.tensorflow.org/models/resnet_v1_101_2016_08_28.tar.gz
解压模型
tar -xvzf resnet_v1_101_2016_08_28.tar.gz

pb 文件生成

导出图
python ../models/research/slim/export_inference_graph.py -alsologtostderr -model_name=resnet_v1_101 -image_size=224 -labels_offset=1 -output_file=./resnet_v1_101_inf.pb
固化
python3 ../tensorflow/tensorflow/python/tools/freeze_graph.py --input_graph=./resnet_v1_101_inf.pb --input_checkpoint=./resnet_v1_101.ckpt --input_binary=true --output_graph=./resnet_v1_101_frozen.pb --output_node_names=resnet_v1_101/predictions/Reshape_1
image.png

image.png

数据集

模型原本就是使用模型使用 ILSVRC2012
docker 中 /root/demos/tflite/dataset/
直接使用dataset
cp /root/demos/tflite/dataset/ /share
python3 preprocess_dataset.py
image.png
里面就是dataset 原始数据和标签裁剪了5个.

cfg

build.cfg

`
[Common]
mode=build

[Parser]
model_name = resnet_101
detection_postprocess =
model_domain = image_classification
output = resnet_v1_101/predictions/Reshape_1
input_model = ./resnet_v1_101_frozen.pb
input = input
input_shape = [1,224,224,3]

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

[GBuilder]
outputs=./aipu.bin
profile= True
target=Z1_0701
`

执行过程

`
root@e97638d58202:/share/resnet_101_mode# aipubuild resnet_v1_101_build.cfg
WARNING:tensorflow:
The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:

If you depend on functionality not listed there, please file an issue.

[I] Parsing model....

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/AIPUBuilder/Parser/passes/tf/__init__.py:73: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

ck_v1/conv1/BatchNorm/FusedBatchNormV3) is fused into Linear Op (resnet_v1_101/block3/unit_1/bottleneck_v1/conv1/Conv2D)!
t_7/bottleneck_v1/conv2/Conv2D)!
(resnet_v1_101/block3/unit_9/bottleneck_v1/conv3/Conv2D)!

[I] Parse model complete
[I] Quantizing model....
[I] AQT start: model_name:resnet_101, calibration_method:MEAN, batch_size:1
[I] ==== read ir ================
[I] float32 ir txt: /tmp/AIPUBuilder_1626518697.8210652/resnet_101.txt
[I] float32 ir bin2: /tmp/AIPUBuilder_1626518697.8210652/resnet_101.bin
[I] ==== read ir DONE.===========
WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.nn.avg_pool is deprecated. Please use tf.nn.avg_pool2d instead.

[I] ==== auto-quantization ======
WARNING:tensorflow:From /usr/local/bin/aipubuild:8: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and:
tf.data.TFRecordDataset(path)
WARNING:tensorflow:Entity <bound method ImageNet.data_transform_fn of <AIPUBuilder.AutoQuantizationTool.auto_quantization.data_set.ImageNet object at 0x7f14cac790b8>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output. Cause: <cyfunction ImageNet.data_transform_fn at 0x7f153a50ff60> is not a module, class, method, function, traceback, frame, or code object
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/autograph/impl/api.py:330: The name tf.FixedLenFeature is deprecated. Please use tf.io.FixedLenFeature instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/autograph/impl/api.py:330: The name tf.parse_single_example is deprecated. Please use tf.io.parse_single_example instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/func_graph.py:915: The name tf.image.resize_images is deprecated. Please use tf.image.resize instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: DatasetV1.make_one_shot_iterator (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use for ... in dataset: to iterate over a dataset. If using tf.estimator, return the Dataset object directly from your input function. As a last resort, you can use tf.compat.v1.data.make_one_shot_iterator(dataset).
WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.global_variables_initializer is deprecated. Please use tf.compat.v1.global_variables_initializer instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.local_variables_initializer is deprecated. Please use tf.compat.v1.local_variables_initializer instead.

[I] step1: get max/min statistic value DONE
[I] step2: quantization each op DONE
[I] step3: build quantization forward DONE
[I] step4: show output scale of end node:
[I] layer_id:146, layer_top:resnet_v1_101/predictions/Reshape_1_0, output_scale:[375.57007]
[I] ==== auto-quantization DONE =
[I] Quantize model complete
[I] Building ...
[I] [common_options.h: 276] BuildTool version: 4.0.175. Build for target Z1_0701 at frequency 800MHz
[I] [common_options.h: 297] using default profile events to profile AIFF

[I] [IRChecker] Start to check IR: /tmp/AIPUBuilder_1626518697.8210652/resnet_101_int8.txt
[I] [IRChecker] model_name: resnet_101
[I] [IRChecker] IRChecker: All IR pass
[I] [graph.cpp : 846] loading graph weight: /tmp/AIPUBuilder_1626518697.8210652/resnet_101_int8.bin size: 0x2a971a4
[I] [builder.cpp:1059] Total memory for this graph: 0x3148da0 Bytes
[I] [builder.cpp:1060] Text section: 0x00046f20 Bytes
[I] [builder.cpp:1061] RO section: 0x00007100 Bytes
[I] [builder.cpp:1062] Desc section: 0x0000d200 Bytes
[I] [builder.cpp:1063] Data section: 0x02a80380 Bytes
[I] [builder.cpp:1064] BSS section: 0x0062d400 Bytes
[I] [builder.cpp:1065] Stack : 0x00040400 Bytes
[I] [builder.cpp:1066] Workspace(BSS): 0x000c4000 Bytes
Total errors: 0, warnings: 0
`

run.cfg

run

`
[Common]
mode=run

[Parser]
model_name = resnet_101
detection_postprocess =
model_domain = image_classification
output = resnet_v1_101/predictions/Reshape_1
input_model = ./resnet_v1_101_frozen.pb
input = input
input_shape = [1,224,224,3]
output_dir = ./

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

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

执行

`
root@e97638d58202:/share/resnet_101_mode# aipubuild resnet_v1_101_run.cfg
WARNING:tensorflow:
The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:

If you depend on functionality not listed there, please file an issue.

[I] Parsing model....

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/AIPUBuilder/Parser/passes/tf/__init__.py:73: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/AIPUBuilder/Parser/passes/tf/__init__.py:73: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2021-07-17 10:47:19.906208: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2021-07-17 10:47:19.928265: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 4200000000 Hz
2021-07-17 10:47:19.928792: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x14413940 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-07-17 10:47:19.928824: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version

[I] Parse model complete
[I] Quantizing model....
[I] AQT start: model_name:resnet_101, calibration_method:MEAN, batch_size:1
[I] ==== read ir ================
[I] float32 ir txt: /tmp/AIPUBuilder_1626518838.188256/resnet_101.txt
[I] float32 ir bin2: /tmp/AIPUBuilder_1626518838.188256/resnet_101.bin
[I] ==== read ir DONE.===========
WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.nn.avg_pool is deprecated. Please use tf.nn.avg_pool2d instead.

[I] ==== auto-quantization ======
WARNING:tensorflow:From /usr/local/bin/aipubuild:8: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and:
tf.data.TFRecordDataset(path)
WARNING:tensorflow:Entity <bound method ImageNet.data_transform_fn of <AIPUBuilder.AutoQuantizationTool.auto_quantization.data_set.ImageNet object at 0x7fd19bf50b38>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output. Cause: <cyfunction ImageNet.data_transform_fn at 0x7fd20f7f4f60> is not a module, class, method, function, traceback, frame, or code object
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/autograph/impl/api.py:330: The name tf.FixedLenFeature is deprecated. Please use tf.io.FixedLenFeature instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/autograph/impl/api.py:330: The name tf.parse_single_example is deprecated. Please use tf.io.parse_single_example instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/func_graph.py:915: The name tf.image.resize_images is deprecated. Please use tf.image.resize instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: DatasetV1.make_one_shot_iterator (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use for ... in dataset: to iterate over a dataset. If using tf.estimator, return the Dataset object directly from your input function. As a last resort, you can use tf.compat.v1.data.make_one_shot_iterator(dataset).
WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.global_variables_initializer is deprecated. Please use tf.compat.v1.global_variables_initializer instead.

WARNING:tensorflow:From /usr/local/bin/aipubuild:8: The name tf.local_variables_initializer is deprecated. Please use tf.compat.v1.local_variables_initializer instead.

[I] step1: get max/min statistic value DONE
[I] step2: quantization each op DONE
[I] step3: build quantization forward DONE
[I] step4: show output scale of end node:
[I] layer_id:146, layer_top:resnet_v1_101/predictions/Reshape_1_0, output_scale:[375.57007]
[I] ==== auto-quantization DONE =
[I] Quantize model complete
[I] Building ...
[I] [common_options.h: 276] BuildTool version: 4.0.175. Build for target Z1_0701 at frequency 800MHz
[I] [common_options.h: 297] using default profile events to profile AIFF

[I] [IRChecker] Start to check IR: /tmp/AIPUBuilder_1626518838.188256/resnet_101_int8.txt
[I] [IRChecker] model_name: resnet_101
[I] [IRChecker] IRChecker: All IR pass
[I] [graph.cpp : 846] loading graph weight: /tmp/AIPUBuilder_1626518838.188256/resnet_101_int8.bin size: 0x2a971a4
[I] [builder.cpp:1059] Total memory for this graph: 0x3148da0 Bytes
[I] [builder.cpp:1060] Text section: 0x00046f20 Bytes
[I] [builder.cpp:1061] RO section: 0x00007100 Bytes
[I] [builder.cpp:1062] Desc section: 0x0000d200 Bytes
[I] [builder.cpp:1063] Data section: 0x02a80380 Bytes
[I] [builder.cpp:1064] BSS section: 0x0062d400 Bytes
[I] [builder.cpp:1065] Stack : 0x00040400 Bytes
[I] [builder.cpp:1066] Workspace(BSS): 0x000c4000 Bytes
[I] [main.cpp : 467] # autogenrated by aipurun, do NOT modify!
LOG_FILE=log_default
FAST_FWD_INST=0
INPUT_INST_CNT=1
INPUT_DATA_CNT=2
CONFIG=Z1-0701
LOG_LEVEL=0
INPUT_INST_FILE0=/tmp/temp_3c12a6ed2bbd0b525c74f6ff0526d.text
INPUT_INST_BASE0=0x0
INPUT_INST_STARTPC0=0x0
INPUT_DATA_FILE0=/tmp/temp_3c12a6ed2bbd0b525c74f6ff0526d.ro
INPUT_DATA_BASE0=0x10000000
INPUT_DATA_FILE1=/tmp/temp_3c12a6ed2bbd0b525c74f6ff0526d.data
INPUT_DATA_BASE1=0x20000000
OUTPUT_DATA_CNT=2
OUTPUT_DATA_FILE0=output_resnet_101.bin
OUTPUT_DATA_BASE0=0x23200000
OUTPUT_DATA_SIZE0=0x3e8
OUTPUT_DATA_FILE1=profile_data.bin
OUTPUT_DATA_BASE1=0x22ac0780
OUTPUT_DATA_SIZE1=0xe00
RUN_DESCRIPTOR=BIN[0]

[I] [main.cpp : 118] run simulator:
aipu_simulator_z1 /tmp/temp_3c12a6ed2bbd0b525c74f6ff0526d.cfg

[I] [main.cpp : 135] Simulator finished.
Total errors: 0, warnings: 0
root@e97638d58202:/share/resnet_101_mode#
`

预测结果

先copy 代码和必须文件
`
cp /root/demos/tflite/output_ref.bin ./
cp /root/demos/tflite/imagenet_classes.py ./
cp /root/demos/tflite/quant_predict.py ./
cp ../resnet_v1_152_mode/model/input.bin ./model/
cp /root/demos/tflite/output_ref.bin ./
`
更改quant_predict.py 为自己output_resnet_101.bin

最后一步:
`
root@e97638d58202:/share/resnet_101_mode# ls^C
root@e97638d58202:/share/resnet_101_mode# python3 quant_predict.py
predict first 5 label:

index  230, prob 235, name: Old English sheepdog, bobtail
index  231, prob  19, name: Shetland sheepdog, Shetland sheep dog, Shetland
index  999, prob   0, name: ear, spike, capitulum
index  342, prob   0, name: hog, pig, grunter, squealer, Sus scrofa
index  340, prob   0, name: sorrel

true first 5 label:

index  232, prob  83, name: collie
index  231, prob  83, name: Shetland sheepdog, Shetland sheep dog, Shetland
index  158, prob  41, name: papillon
index  170, prob  40, name: borzoi, Russian wolfhound
index  161, prob  39, name: Afghan hound, Afghan

Detect picture save to result.jpeg
`
前5类正确. OK.
image.png

最后所有文件都在/share 中.传gitee.

image.png
image.png
https://gitee.com/ejuoy/r329_...

百度云连接

链接:https://pan.baidu.com/s/1uVfP...
提取码:g441

推荐阅读
关注数
7434
内容数
92
人工智能边缘计算软硬件解决方案,提供高性能、低成本、低功耗、易使用的硬件选型方案.
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息