1.简介
海思平台在转模型量化时经常需要精度分析,可以参考官方文档《精度比对工具使用指南.pdf》,但是里面的一些细节描述还是不清楚,因此本文详细描述了精度分析对比的操作指南,方便新手入门。
2.对原始未量化的onnx模型进行dump
说明:原始模型为caffe的dump方法参考之前的博客
(1)准备工作
使用精度比对工具前,请参考《驱动和开发环境安装指南》手册完成开发环境搭建。本文这里不再赘述。
(2)使用脚本将图片数据转成可以用来dump的bin文件
脚本文件:
import os
import numpy as np
import cv2
def cv2_imread(path):
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), 1)
return img
def cv2_imwrite(image, image_path, type='jpg'):
cv2.imencode('.{}'.format(type), image)[1].tofile(image_path)
data_sets = []
sample_batch_input_bin_dir = "./"
for item in os.listdir(sample_batch_input_bin_dir):
if not item.endswith(".jpg"):
continue
print(item)
img = cv2_imread(item)
img = img[:, :, ::-1] #BGR2RGB
nw = 640
nh = 640
img = cv2.resize(img, (nw, nh))
#original_input_data = np.fromfile(img, dtype=np.uint8) # 读取bin文件时,bin文件内的dtype类型须根据模型的输入类型确定,下面以float32为例
# 将数据重新组织,具体根据模型输入中的shape值确定
#img.astype(np.float32)/ 255
img = img/255.0
img.astype(np.float32)
img = np.float32(img)
#print(img)
current_input_data = np.transpose(img, [2,0,1])
#current_input_data = img.reshape(3,640, 640)
# 将当前的数据添加到列表中
data_sets.append(current_input_data)
# 将每个batch的数据保存到一个输入bin文件中,从而得到一个包含多batch的输入bin文件
np.array(data_sets).tofile