天天看點

【嵌入式AI】周易 AIPU 算法部署仿真測試

  本教程詳細記錄了使用周易 AIPU 進行算法部署仿真測試的方法,帶大家嘗嘗鮮。

  這裡以

resnet_v1_101

為例(原諒我在邊緣端用這麼大的模型,這裡純粹就是為了跑通)。

文章目錄

    • 1、安裝 docker 鏡像環境
    • 2、準備模型檔案
      • 2.1 下載下傳預訓練模型:
      • 2.2 export graph
      • 2.3 freeze graph
    • 3、準備量化校準資料集
    • 4、NN Compiler 配置
    • 5、AIPU 仿真
    • 5、仿真結果比對

1、安裝 docker 鏡像環境

# pull 鏡像
docker pull zepan/zhouyi

# run 容器
docker run -it zepan/zhouyi  /bin/bash

# 如果要vscode遠端開發的話,可以加端口
docker run -it -p 9999:22 -v /path/to/container:/path/to/host zepan/zhouyi  /bin/bash
           

  驗證容器環境是否正常:

cd ~/demos/tflite

# 執行仿真
./run_sim.sh
           
【嵌入式AI】周易 AIPU 算法部署仿真測試
# 驗證結果
python3 quant_predict.py
           
【嵌入式AI】周易 AIPU 算法部署仿真測試

2、準備模型檔案

2.1 下載下傳預訓練模型:

wget http://download.tensorflow.org/models/resnet_v1_101_2016_08_28.tar.gz
           

  解壓得到

resnet_v1_101.ckpt

tar zxvf resnet_v1_101_2016_08_28.tar.gz
           

2.2 export graph

vim export_graph.sh
           

  加入以下内容

# export graph
python3 export_inference_graph.py \
    --alsologtostderr \
    --model_name=resnet_v1_101 \
    --image_size=224 \
    --labels_offset=1 \ 
    --output_file=tmp/resnet_v1_101_inf.pb
           

  執行

./export_graph.sh

後将在 tmp檔案夾下生成

resnet_v1_101_inf.pb

2.3 freeze graph

vim freeze_graph.sh
           

  加入以下内容:

# freeze graph
python3 freeze_graph.py \
    --input_graph=tmp/resnet_v1_101_inf.pb \
    --input_checkpoint=tmp/resnet_v1_101.ckpt \
    --input_binary=true --output_graph=tmp/resnet_v1_101_frozen.pb \
    --output_node_names= resnet_v1_101/predictions/Reshape_1
           

  執行

./freeze_graph.sh

後将在 tmp 檔案夾下生成

resnet_v1_101_frozen.pb

  通過如上三步操作,将生成如下模型檔案,最終推理會用到的隻有

resnet_v1_101_frozen.pb

【嵌入式AI】周易 AIPU 算法部署仿真測試

3、準備量化校準資料集

  我這裡沒有自己去做資料集,使用了鏡像提供的圖檔和标簽。

【嵌入式AI】周易 AIPU 算法部署仿真測試

  制作量化校準資料集:

python3 preprocess_dataset.py
           

  執行如上操作會生成

dataset.npy

label.npy

【嵌入式AI】周易 AIPU 算法部署仿真測試

4、NN Compiler 配置

  在有

resnet_v1_101_frozen.pb

和 校準資料集之後,就可以編輯 NN Compiler 配置檔案了。

​  在 config 目錄下建立

resnet_101_build.cfg

resnet_101_run.cfg

配置檔案。

resnet_101_build.cfg

内容如下:

[Common]
mode=build

[Parser]
model_name = resnet_101
detection_postprocess = 
model_domain = image_classification
output = resnet_v1_101/predictions/Reshape
input_model = ./tmp/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
           

resnet_101_run.cfg

内容如下:

[Common]
mode=run

[Parser]
model_name = resnet_101
detection_postprocess = 
model_domain = image_classification
output = resnet_v1_101/predictions/Reshape
input_model = ./tmp/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
           

5、AIPU 仿真

  執行

build

aipubuild confg/resnet_101_build.cfg
           
【嵌入式AI】周易 AIPU 算法部署仿真測試

  執行

run

aipubuild config/resnet_101_run.cfg
           
【嵌入式AI】周易 AIPU 算法部署仿真測試

​  得到存儲模型輸出資料的檔案,後面驗證模型資料結果會用到。

【嵌入式AI】周易 AIPU 算法部署仿真測試

5、仿真結果比對

python3 quant_predict.py
           
【嵌入式AI】周易 AIPU 算法部署仿真測試

​  看結果,TOP1 已經可以對的上了,細節和更深的東西等拿到闆子再好好研究,先跑通再優秀~

  這篇先這樣了,在我的 github上可以拿到我上述測試過程的代碼,模型在這裡,提取碼:6666。

  先收工了~

掃描下方二維碼即可關注我的微信公衆号【極智視界】,擷取更多AI經驗分享,讓我們用極緻+極客的心态來迎接AI !

【嵌入式AI】周易 AIPU 算法部署仿真測試

繼續閱讀