1. 模型转换
按照R329教程一|周易 AIPU 部署及仿真教程将tensorflow的模型转换成pb模型。这里对vgg16进行转换,转换后的模型在附件一的百度网盘。
这里选择vgg16,因为vgg16和resnet50的数据预处理方式一样
2. 矫正数据集
这里我们是针对imagenet分类的模型进行量化的。我们量化矫正的数据集采用imagenet_1k.zip(可以自行百度下载),然后将数据制作成npy文件。制作好的npy文件在附件一的百度网盘。
3. NN compiler的cfg文件
这里我们因为是在docker中进行仿真测试的,所以我们采用run的模式,具体的配置如下。需要根据模型,数据的位置进行修改。
vgg_16_run.cfg
[Common]
mode=run
[Parser]
model_name = MobilenetV2
detection_postprocess =
model_domain = image_classification
output = vgg_16/fc8/squeezed
input_model = pretrained/vgg_16_frozen.pb
input = input
input_shape = [1,224,224,3]
output_dir = ./
[AutoQuantizationTool]
model_name = MobilenetV2
quantize_method = SYMMETRIC
ops_per_channel = DepthwiseConv
calibration_data = ./dataset/dataset1000.npy
calibration_label = ./dataset/label1000.npy
preprocess_mode = normalize
quant_precision=int8
reverse_rgb = False
label_id_offset = 0
[GBuilder]
inputs=./output/input.bin
outputs=./output/vgg_16.bin
simulator=aipu_simulator_z1
profile= True
target=Z1_0701
simulator得到的输出为./output/vgg_16.bin
4. simulator执行的输入输出结果,比较运算量化误差
准备好:
- pb模型
- 校准数据集
- cfg配置文件
之后就可以build了。直接在docker容器中使用如下命令:
aipubuild config/vgg_16_run.cfg
打印的日志信息:
[INFO]:========================================================================
[INFO]: STATIC CHECK END
[INFO]:========================================================================
[INFO]:AIPU START RUNNING: BIN[0]
[INFO]:TOTAL TIME: 18.984778s.
[INFO]:SIMULATOR EXIT!
[I] [main.cpp : 135] Simulator finished.
Total errors: 0, warnings: 0
4.2 用quant_predict.py
对输出结果进行解析,结果为:
root@35da802afbd8:/tmp# python quant_predict.py
Predict shape (1000,)
Predict Class is 230
Ref shape: (1000,)
Ref Class is 230
predict first 5 label:
index 230, prob 106, name: Old English sheepdog, bobtail
index 231, prob 101, name: Shetland sheepdog, Shetland sheep dog, Shetland
index 157, prob 69, name: Blenheim spaniel
index 169, prob 54, name: redbone
index 259, prob 53, name: Samoyed, Samoyede
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
pred shape: (1000,)
***************** TOP 1 result ***************
Predict Class is 230
True Class is 230
可以看到针对分类任务,预测为230,目标也是230类别。