本次实验均在官方提供的docker容器内部进行,使用的模型为tensorflow提供的MobileNet_v2_1.0_224 预训练模型。
环境准备
docker环境
# 方法一,从docker hub下载,需要梯子 sudo docker pull zepan/zhouyi 方法二,百度云下载镜像文件(压缩包约2.9GB,解压后约5.3GB) 链接:https://pan.baidu.com/s/1yaKBPDxR_oakdTnqgyn5fg 提取码:f8dr gunzip zhouyi_docker.tar.gz sudo docker load --input zhouyi_docker.tar docker run -it zepan/zhouyi /bin/bash cd ~/demos mkdir mobile_v2_1 cd mobile_v2_1
材料准备
模型
常见预训练模型文件在 github上可以下载:https://aijishu.com/link?targ...mkdir model mv mobilenet_v2_1.4_224.tgz ./model tar -xzvf mobilenet_v2_1.4_224.tgz """ mobilenet_v2_1.4_224.ckpt.index ... mobilenet_v2_1.4_224_frozen.pb """
数据集
docker 容器内部提供了测试数据集(ILSVRC2012)/root/demos/tflite/dataset
,生成npy文件的数据集和标注文件,用于校正int8预测结果cp -r ../tflite/dataset ./ vi dataset/preprocess_dataset.py """ # mean = [123.68, 116.78, 103.94] mean = [127.5, 127.5, 127.5] """ cd dataset python preprocess_dataset.py
编译
编写配置文件 pb_mobilenet_v2_run.cfg
"""
[Common]
mode = run
[Parser]
model_name = mobilenet_v2
detection_postprocess =
model_domain = image_classification
input_model = ./model/mobilenet_v2_1_frozen.pb
input = input
input_shape = [1, 224, 224, 3]
output = MobilenetV2/Predictions/Softmax # 可以通过netron查看输出节点
output_dir = ./
[AutoQuantizationTool]
quantize_method = SYMMETRIC
quant_precision = int8
ops_per_channel = DepthwiseConv
reverse_rgb = False
label_id_offset = 0
dataset_name =
detection_postprocess =
anchor_generator =
log = False
calibration_data = ./dataset/dataset.npy
calibration_label = ./dataset/label.npy
preprocess_mode = normalize
[GBuilder]
inputs=./model/input.bin
simulator=aipu_simulator_z1
outputs=mobilenet_v2_output.bin
profile= True
target=Z1_0701
"""
将测试图片(长臂猿)转成二进制文件
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
input_height=224
input_width=224
input_channel = 3
mean = [127.5, 127.5, 127.5]
var = 1
img_name = "1.jpg"
img = Image.open(img_name)
plt.imshow(img)
img_w, img_h = img.size
if img_w/img_h > input_width/input_height :
tmp_h = input_height
tmp_w = int(input_height/img_h*img_w)
oft_y = 0
oft_x = (tmp_w-input_width)/2
else:
tmp_w = input_width
tmp_h = int(input_width/img_w*img_h)
oft_x = 0
oft_y = (tmp_h-input_height)/2
img1 = img.resize((tmp_w, tmp_h),Image.ANTIALIAS)
plt.imshow(img1)
img2 = img1.crop((oft_x,oft_y,oft_x+input_width,oft_y+input_height))
plt.imshow(img2)
img_arr = (np.array(img2)-mean)/var
img_arr=img_arr.astype(np.int8)
# 保存成仿真需要的bin文件
import struct
data=b''
for y in range(img_arr.shape[1]):
for x in range(img_arr.shape[0]):
data += struct.pack('bbb',img_arr[y,x,0],img_arr[y,x,1],img_arr[y,x,2])
fw = open("input.bin", "wb")
fw.write(data)
fw.close()
print("save to input.bin OK")
NN compiler编译 同时推理测试图片结果
aipubuild ./config/pb_mobilenet_v2_run.cfg
返回结果:
"""
[I] [main.cpp : 135] Simulator finished.
Total errors: 0, warnings: 0
"""
解码预测结果-mobilenet_v2_output.bin
编写quant_predict.py
from PIL import Image
import cv2
from matplotlib import pyplot as plt
import matplotlib.patches as patches
import numpy as np
import os
import imagenet_classes as class_name
current_dir = os.getcwd()
label_offset = 1
outputfile = current_dir + '/mobilenet_v2_output.bin'
npyoutput = np.fromfile(outputfile, dtype=np.uint8)
outputclass = npyoutput.argmax()
head5p = npyoutput.argsort()[-5:][::-1]
# labelfile = current_dir + '/output_ref.bin'
# npylabel = np.fromfile(labelfile, dtype=np.int8)
# labelclass = npylabel.argmax()
# head5t = npylabel.argsort()[-5:][::-1]
print("predict first 5 label:")
for i in head5p:
print(" index %4d, prob %3d, name: %s"%(i, npyoutput[i], class_name.class_names[i-label_offset]))
# print("true first 5 label:")
# for i in head5t:
# print(" index %4d, prob %3d, name: %s"%(i, npylabel[i], class_name.class_names[i-label_offset]))
# Show input picture
print('Detect picture save to result.jpeg')
input_path = './model/input.bin'
npyinput = np.fromfile(input_path, dtype=np.int8)
image = np.clip(np.round(npyinput)+128, 0, 255).astype(np.uint8)
image = np.reshape(image, (224, 224, 3))
im = Image.fromarray(image)
im.save('result.jpeg')
"""
predict first 5 label:
index 918, prob 27, name: comic book
index 819, prob 23, name: spotlight, spot
index 368, prob 9, name: chimpanzee, chimp, Pan troglodytes
index 372, prob 8, name: patas, hussar monkey, Erythrocebus patas
index 367, prob 6, name: gorilla, Gorilla gorilla
Detect picture save to result.jpeg
"""
百度随机抓取的一个猴子图片,在top-5内有三类猴子。
工程文件夹链接:https://pan.baidu.com/s/1X_yoCDPghqBp84qjvwyJZg
提取码:JJJR