量化的两个文件
input_fn.py
# from resnet_v1_50_preprocessing import *
# def eval_input(iter, eval_image_dir, eval_image_list, class_num, eval_batch_size):
# images = []
# labels = []
# line = open(eval_image_list).readlines()
# for index in range(0, eval_batch_size):
# curline = line[iter * eval_batch_size + index]
# [image_name, label_id] = curline.split(' ')
# image = cv2.imread(eval_image_dir + image_name)
# image = central_crop(image, 224, 224)
# image = mean_image_subtraction(image, MEANS)
# images.append(image)
# labels.append(int(label_id))
# lb = preprocessing.LabelBinarizer()
# lb.fit(range(0, class_num))
# labels = lb.transform(labels)
# return {"input": images, "labels": labels}
import cv2
def mean_image_subtraction(image, means):
B, G, R = cv2.split(image)
B = B - means[0]
G = G - means[1]
R = R - means[2]
image = cv2.merge([R, G, B])
return image
def BGR2RGB(image):
B, G, R = cv2.split(image)
image = cv2.merge([R, G, B])
return image
def central_crop(image, crop_height, crop_width):
image_height = image.shape[0]
image_width = image.shape[1]
offset_height = (image_height - crop_height) // 2
offset_width = (image_width - crop_width) // 2
return image[offset_height:offset_height + crop_height, offset_width:
offset_width + crop_width, :]
_R_MEAN = 123.68
_G_MEAN = 116.78
_B_MEAN = 103.94
MEANS = [_B_MEAN,_G_MEAN,_R_MEAN]
calib_image_dir = "./Imagenet/calibration_images/"
calib_image_list = "./Imagenet/calibration.txt"
calib_batch_size = 50
def calib_input(iter):
images = []
line = open(calib_image_list).readlines()
for index in range(0, calib_batch_size):
curline = line[iter * calib_batch_size + index]
calib_image_name = curline.strip()
calib_image_name = calib_image_name.split(' ', 1)[0]
print(calib_image_dir + calib_image_name)
image = cv2.imread(calib_image_dir + calib_image_name)
image = central_crop(image, 224, 224)
image = mean_image_subtraction(image, MEANS)
print(f"image.shape={image.shape}")
images.append(image)
return {"input": images}
TensorFlow 1.x Version (vai_q_tensorflow)
quantize.sh
pb=./tf_resnetv1_50_imagenet_224_224_6.97G_1.3/float/resnet_v1_50_inference.pb
#input_nodes=resnet_v1_50_conv1_Conv2D(0)
input_shapes=?,224,224,3
output_nodes=resnet_v1_50_logits_Conv2D
vai_q_tensorflow quantize \
--input_frozen_graph ${pb} \
--input_shapes ?,224,224,3 \
--input_nodes input \
--output_nodes resnet_v1_50/predictions/Reshape_1 \
--input_fn input_fn.calib_input \
--calib_iter 2 \
--output_dir ./Ultra96_tf_resnetv1_build/quantize_results
Caffe Version (vai_q_caffe)
If the target hardware platform is DPUCAHX8H, the -keep_fixed_neuron option should be added to the command.
vai_q_caffe quantize -model float.prototxt -weights float.caffemodel -keep_fixed_neuron[options]
Chapter 4: Quantizing the Model UG1414