卷积量化测试

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)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值