shangjl · 2021年07月25日

【R329模型仿真测试】周易 AIPU ResNet V1 152模型

1 搭建矽速科技提供的docker环境进行开发

`gunzip zhouyi_docker.tar.gz

sudo docker load --input zhouyi_docker.tar

docker ps

docker commit b4c875ede137 zepan/zhouyi`

2 准备量化矫正数据集,使用NN compiler转换自己的模型格式到对应格式

下载数据集:
数据集地址:http://www.image-net.org/chal...
下载label文件:
label文件的地址:http://dl.caffe.berkeleyvisio...
解压数据集和label文件:
解压label文件,可以看到以下文件:
1.png

3 准备矫正集的data.npy和label.npy

`import tensorflow as tf
import numpy as np
import sys
import os
import cv2

sys.path.append('/project/ai/scratch01/salyua01/sharing/guide_acc')
img_dir='./img/'
label_file='./label.txt'

RESNET PARAM

input_height=224
input_width=224
input_channel = 3
mean = [123.68, 116.78, 103.94]
var = 1

tf.enable_eager_execution()

def smallest_size_at_least(height, width, resize_min):

"""Computes new shape with the smallest side equal to `smallest_side`.

Computes new shape with the smallest side equal to `smallest_side` while
preserving the original aspect ratio.

Args:
  height: an int32 scalar tensor indicating the current height.
  width: an int32 scalar tensor indicating the current width.
  resize_min: A python integer or scalar `Tensor` indicating the size of
    the smallest side after resize.

Returns:
  new_height: an int32 scalar tensor indicating the new height.
  new_width: an int32 scalar tensor indicating the new width.
"""
resize_min = tf.cast(resize_min, tf.float32)

# Convert to floats to make subsequent calculations go smoothly.
height, width = tf.cast(height, tf.float32), tf.cast(width, tf.float32)

smaller_dim = tf.minimum(height, width)
scale_ratio = resize_min / smaller_dim

# Convert back to ints to make heights and widths that TF ops will accept.
new_height = tf.cast(tf.round(height * scale_ratio), tf.int32)
new_width = tf.cast(tf.round(width * scale_ratio), tf.int32)

return new_height, new_width

def resize_image(image, height, width, method='BILINEAR'):

"""Simple wrapper around tf.resize_images.

This is primarily to make sure we use the same `ResizeMethod` and other
details each time.

Args:
  image: A 3-D image `Tensor`.
  height: The target height for the resized image.
  width: The target width for the resized image.

Returns:
  resized_image: A 3-D tensor containing the resized image. The first two
    dimensions have the shape [height, width].
"""
resize_func = tf.image.ResizeMethod.NEAREST_NEIGHBOR if method == 'NEAREST' else tf.image.ResizeMethod.BILINEAR
return tf.image.resize_images(image, [height, width], method=resize_func, align_corners=False)

def aspect_preserving_resize(image, resize_min, channels=3, method='BILINEAR'):

"""Resize images preserving the original aspect ratio.

Args:
  image: A 3-D image `Tensor`.
  resize_min: A python integer or scalar `Tensor` indicating the size of
    the smallest side after resize.

Returns:
  resized_image: A 3-D tensor containing the resized image.
"""
shape = tf.shape(image)
height, width = shape[0], shape[1]
new_height, new_width = smallest_size_at_least(height, width, resize_min)
return resize_image(image, new_height, new_width, method)

def central_crop(image, crop_height, crop_width, channels=3):

"""Performs central crops of the given image list.

Args:
  image: a 3-D image tensor
  crop_height: the height of the image following the crop.
  crop_width: the width of the image following the crop.

Returns:
  3-D tensor with cropped image.
"""
shape = tf.shape(image)
height, width = shape[0], shape[1]
amount_to_be_cropped_h = height - crop_height
crop_top = amount_to_be_cropped_h // 2
amount_to_be_cropped_w = width - crop_width
crop_left = amount_to_be_cropped_w // 2
# return tf.image.crop_to_bounding_box(image, crop_top, crop_left, crop_height, crop_width)

size_assertion = tf.Assert(
    tf.logical_and(
        tf.greater_equal(height, crop_height),
        tf.greater_equal(width, crop_width)),
    ['Crop size greater than the image size.']
)
with tf.control_dependencies([size_assertion]):
    if channels == 1:
        image = tf.squeeze(image)
        crop_start = [crop_top, crop_left, ]
        crop_shape = [crop_height, crop_width, ]
    elif channels >= 3:
        crop_start = [crop_top, crop_left, 0]
        crop_shape = [crop_height, crop_width, -1]

    image = tf.slice(image, crop_start, crop_shape)

return tf.reshape(image, [crop_height, crop_width, -1])

label_data = open(label_file)
filename_list = []
label_list = []
for line in label_data:

filename_list.append(line.rstrip('\n').split(' ')[0])
label_list.append(int(line.rstrip('\n').split(' ')[1]))

label_data.close()
img_num = len(label_list)

images = np.zeros([img_num, input_height, input_width, input_channel], np.float32)
for file_name, img_idx in zip(filename_list, range(img_num)):

image_file = os.path.join(img_dir, file_name)
img_s = tf.gfile.GFile(image_file, 'rb').read()
image = tf.image.decode_jpeg(img_s)
image = tf.cast(image, tf.float32)
image = tf.clip_by_value(image, 0., 255.)
image = aspect_preserving_resize(image, min(input_height, input_width), input_channel) 
image = central_crop(image, input_height, input_width)
image = tf.image.resize_images(image, [input_height, input_width])
image = (image - mean) / var
image = image.numpy()
_, _, ch = image.shape
if ch == 1:
    image = tf.tile(image, multiples=[1,1,3])
    image = image.numpy()
images[img_idx] = image

np.save('dataset.npy', images)

labels = np.array(label_list)
np.save('label.npy', labels)

2.png

4 仿真配置文件 cfg

[Common]
mode = run
[Parser]
model_type = onnx
input_data_format = NCHW
model_name = resnet
detection_postprocess =
model_domain = image_classification
input_model = ./input/resnet.onnx
input = gpu_0/data_0
input_shape = [1, 3, 224, 224]
output = gpu_0/softmax_1

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

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

5仿真AIPU执行结果

执行后得到运算结果:output_ref.bin
以及在执行输出过程中可以得到最后一层的反量化系数:
`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 + '/output.bin'
npyoutput = np.fromfile(outputfile, dtype=np.uint8)
outputclass = npyoutput.argmax()
head5p = npyoutput.argsort()-5:

labelfile = current_dir + '/output_ref.bin'
npylabel = np.fromfile(labelfile, dtype=np.int8)
labelclass = npylabel.argmax()
head5t = npylabel.argsort()-5:

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 = './input/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')`
3.png

推荐阅读
关注数
0
文章数
2
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息