import numpy as np
import numpy as np
import cv2
from scipy.signal import convolve2d
def compute_convolution(matrix, kernel):
# 计算卷积
result = convolve2d(matrix, kernel, mode='valid')
return result
# calc float type
input_tensor = np.random.rand(500, 500).astype(np.float32)
def avg_to_int8(input):
min_v = input.min()
max_v = input.max()
sc = 256 / (max_v - min_v)
zero_point =(min_v+max_v)/2
return (input - zero_point) * sc
layer_nums = 10
kernel_size = 3
mode = 0 # 0->int8 2 ->int16
if mode == 0:
max_value = np.iinfo(np.int8).max
elif mode ==1:
max_value = np.iinfo(np.int16).max
input_sc = max_value / np.max(np.abs(input_tensor))
input_tensor_q = np.round(input_tensor * input_sc).astype(np.int32)
kernels = []
kernels_sc = []
kernels_q = []
for i in range(layer_nums):
kernels.append(np.random.rand(kernel_size, kernel_size).astype(np.float32))
kernels_sc.append(max_value / np.max(np.abs(kernels[i])))
kernels_q.append(np.round(kernels[i] * kernels_sc[i]).astype(np.int32))
def quan(input):
sc = max_value / np.max(np.abs(input))
input = np.round(sc * input)
return input
def get_sc(input):
return max_value / np.max(np.abs(input))
result = None
for i in range(layer_nums):
if i ==0:
input_data = input_tensor_q
else:
input_data = result
result = compute_convolution(input_data,kernels_q[i])
result = quan(result)
result_f = None
for i in range(layer_nums):
if i == 0:
input_data = input_tensor
else:
input_data = result_f
result_f = compute_convolution(input_data, kernels[i])
re_sc = get_sc(result_f)
result = result / re_sc
print("quan :" ,result[1:10,0])
print("float:" ,result_f[1:10,0])
mse = np.mean(np.square(result - result_f))
print("mse:",mse)
cv2.imshow("quan",avg_to_int8(result))
cv2.imshow("float",avg_to_int8(result_f))
cv2.imshow("cha",result - result_f)
cv2.waitKey(0)
# c1 = compute_convolution(a,k)
#
# # calc quan type
#
# mode = 0 # 0->int8 2 ->int16
# if mode == 0:
# max_value = np.iinfo(np.int8).max
# elif mode ==1:
# max_value = np.iinfo(np.int16).max
# scale_factor = max_value / max(np.max(np.abs(a)) ,np.max(np.abs(k)))
#
# print("sc",scale_factor)
#
# a_q = np.round(a * scale_factor).astype(np.int32)
# k_q = np.round(k * scale_factor).astype(np.int32)
#
# c_q = compute_convolution(a_q, k_q)
#
# c2 = c_q / scale_factor /scale_factor
#
# print("float:",c1)
# print("quan:",c2)
#
# mse = np.mean(np.square(c1 - c2))
#
# print("mse:",mse)
卷积量化测试
最新推荐文章于 2024-05-27 17:00:50 发布