CNN训练MNIST数据集tenflow2(中)

CNN训练MNIST数据集tenflow2(下)

紧接上文CNN训练MNIST数据集tenflow2(上),我们已经做好了训练,分别保存了32bit、16bit、8bit的量化模型。本篇的工作是加载模型,查看增加噪声、权重量化时的精度变化。数据流量化的结果将在下篇介绍。

一、加载数据集

# 1. 加载数据集
import tensorflow as tf
import numpy as np
minst = tf.keras.datasets.mnist
img_rows,img_cols = 28,28
(x_train, y_train), (x_test, y_test) = minst.load_data()
x_train = x_train.reshape(x_train.shape[0],img_rows,img_cols,1)
x_test = x_test.reshape(x_test.shape[0],img_rows,img_cols,1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train = x_train / 255
x_test = x_test / 255
y_train_onehot = tf.keras.utils.to_categorical(y_train)
y_test_onehot = tf.keras.utils.to_categorical(y_test)

二、加载模型和权重,测试准确率

# 2. 加载模型和权重,测试准确率
model = tf.keras.Sequential()
model = tf.keras.models.load_model('models/mnist_tf2_fw.h5')
score = model.evaluate(x_test, y_test_onehot, verbose=0)
print('Test accuracy:', "{:.5f}".format(score[1]))

三、增加噪声,测试准确率

model = tf.keras.models.load_model('models/mnist_tf2_fw.h5')
# 这里0,2,5层分别表示conv1,conv2,fc层的权重
for i in  [0,2,5]:
    # 通过get_weights 和setweights修改权重
    temp = model.layers[i].get_weights()[0]
    # 0.15表征噪声的标准差
    temp = temp + np.random.rand(*temp.shape)*0.15
    model.layers[i].set_weights([temp])
score = model.evaluate(x_test, y_test_onehot, verbose=0)
print('Test accuracy:', "{:.5f}".format(score[1]))

四、权重量化,测试准确率

# 8bit量化
bit = 8
model = tf.keras.models.load_model('models/mnist_tf2_fw.h5')
# 这里0,2,5层分别表示conv1,conv2,fc层的权重
for i in  [0,2,5]:
    temp = model.layers[i].get_weights()[0]
    temp = np.rint(temp * np.power(2,bit))/np.power(2,bit)
    model.layers[i].set_weights([temp])
score = model.evaluate(x_test, y_test_onehot, verbose=0)
print('Test accuracy:', "{:.5f}".format(score[1]))

统计结果如下:

噪声标准差00.040.060.080.090.100.16
准确率98.50097.73094.56087.51078.59070.18052.800
量化精度32bit16bit8bit4bit3bit2bit1bit
准确率98.50098.50098.49098.41097.38089.40066.250

特别说明的是:这里只是对权重做量化,推理计算过程中的数据还是32bit的浮点数,所以结果上看,3bit量化的精度下降不多,只有约1%。

有意思的一点是:3bit量化的准确率和0.04噪声接近;2bit量化准确率和0.08噪声相近,1bit量化的准确率和0.16的噪声相当。

分析原因:3bit量化相当于最小精度是0.125,可以将其等效为 3 σ 3\sigma 3σ的偏差,求得标准差为0.041,即为0.04的噪声。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值