模型压缩之8位量化——徒手造轮子

这里简单说明情况/流程即可,不再实际对模型进行压缩。

1-设定最大最小范围,将参数clip到此范围内;

2-采用255对参数进行最大最小值系数化处理;

3-数据格式转换为8位即可;

举例如下:

1-设定最大最小值为2,-2,然后对随机数进行clip

>>> xx
array([-0.03792056, -0.03543376,  2.11450531, -1.00187846, -1.6370846 ,
        0.13144204, -1.14044352, -0.47598372,  0.48249807,  0.41293063,
        0.50319045, -0.33351765,  1.99534621,  1.90570351, -0.07295837,
        0.96069915, -0.82314341, -1.49708989,  1.25026652, -1.37752694,
       -0.4253239 , -0.95655893,  0.92019954,  0.53596921,  1.64408076,
       -1.0236392 , -0.16911002, -0.09163271, -0.25296542,  0.11552916,
        0.91452231,  1.05104281,  0.22929848,  0.08540769, -0.70410114,
        0.89512494,  2.4170672 ,  2.17545606,  2.19707369,  1.43511977,
        0.27938961,  0.15911593, -0.98390229,  0.44097581, -0.00609449,
       -0.66568627,  1.02575084,  0.4165646 , -0.060003  , -0.52152193,
        0.02118122,  0.96681046, -1.37796988, -0.69395938, -0.74742486,
       -0.56772941,  0.21670969, -0.4735466 ,  0.58769068,  0.23123573,
       -0.42057946,  0.1423509 , -1.76617659, -0.44750992])

>>> xxc
array([-0.03792056, -0.03543376,  2.        , -1.00187846, -1.6370846 ,
        0.13144204, -1.14044352, -0.47598372,  0.48249807,  0.41293063,
        0.50319045, -0.33351765,  1.99534621,  1.90570351, -0.07295837,
        0.96069915, -0.82314341, -1.49708989,  1.25026652, -1.37752694,
       -0.4253239 , -0.95655893,  0.92019954,  0.53596921,  1.64408076,
       -1.0236392 , -0.16911002, -0.09163271, -0.25296542,  0.11552916,
        0.91452231,  1.05104281,  0.22929848,  0.08540769, -0.70410114,
        0.89512494,  2.        ,  2.        ,  2.        ,  1.43511977,
        0.27938961,  0.15911593, -0.98390229,  0.44097581, -0.00609449,
       -0.66568627,  1.02575084,  0.4165646 , -0.060003  , -0.52152193,
        0.02118122,  0.96681046, -1.37796988, -0.69395938, -0.74742486,
       -0.56772941,  0.21670969, -0.4735466 ,  0.58769068,  0.23123573,
       -0.42057946,  0.1423509 , -1.76617659, -0.44750992])

2-用255对clip后的数据进行系数化处理

>>> xxcq
array([125.08256445, 125.24109803, 255.        ,  63.63024847,
        23.13585678, 135.87943035,  54.79672542,  97.15603773,
       158.25925223, 153.82432771, 159.57839132, 106.23824956,
       254.70332079, 248.98859847, 122.84890363, 188.74457097,
        75.02460772,  32.06051958, 207.20449052,  39.68265748,
       100.38560141,  66.51936849, 186.16272094, 161.66803721,
       232.31014875,  62.24300076, 116.7192365 , 121.65841478,
       111.37345418, 134.86498408, 185.80079698, 194.50397909,
       142.11777816, 132.94474001,  82.61355219, 184.56421515,
       255.        , 255.        , 255.        , 218.98888561,
       145.31108735, 137.64364051,  64.77622883, 155.61220775,
       127.11147656,  85.0625004 , 192.89161598, 154.05599334,
       123.67480881,  94.25297671, 128.85030291, 189.13416688,
        39.65442003,  83.26008966,  79.85166508,  91.30724993,
       141.31524297,  97.31140419, 164.96528094, 142.24127778,
       100.68805967, 136.57486981,  14.90624219,  98.97124266])

3-格式转换,这个很简单

array([125, 125, 255,  63,  23, 135,  54,  97, 158, 153, 159, 106, 254,
       248, 122, 188,  75,  32, 207,  39, 100,  66, 186, 161, 232,  62,
       116, 121, 111, 134, 185, 194, 142, 132,  82, 184, 255, 255, 255,
       218, 145, 137,  64, 155, 127,  85, 192, 154, 123,  94, 128, 189,
        39,  83,  79,  91, 141,  97, 164, 142, 100, 136,  14,  98],
      dtype=uint8)

另外有相关问题可以加入QQ群讨论,不设微信群

QQ群:868373192 

语音图像视频深度-学习群

你可能感兴趣的:(python,8bit量化,模型压缩)