本节内容来自:百度AIstudio课程
做一个记录。
资料
做作业时可以参考以下资料。
A.低内存带宽 B. 低功耗 C. 低计算资源 D. 低存储体积
A. 只有teacher model的参数需要更新
B. 只有student model的参数需要更新
C. teacher model和student model 的参数都需要更新
D.teacher model和student model 的参数都不需要更新
A: 能
B: 不能
A. 只对权重weight进行量化
B. 对ResNet50模型进行蒸馏提高精度
C. 对模型进行裁剪,减少通道数
D. 对权重weight和激活进行量化,预测采用INT8计算
A. 搜索空间
B. 搜索算法
C. 模型优化
D. 模型评估
(仅供参考)
请将每道选择题的答案写在这里:
该教程以图像分类模型MobileNetV1为例,说明如何快速使用量化训练接口。 该示例包含以下步骤:
以下章节依次介绍每个步骤的内容。
!pip install paddleslim
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/
Collecting paddleslim
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/69/3c/880afac020e3393da5a55b4e0b504d2b644a7ebe91092d953185f09660d1/paddleslim-1.0.1-py2.py3-none-any.whl (103kB)
100% |████████████████████████████████| 112kB 12.2MB/s ta 0:00:01
Requirement already satisfied: tqdm in /opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages (from paddleslim) (4.36.1)
Installing collected packages: paddleslim
Successfully installed paddleslim-1.0.1
PaddleSlim依赖Paddle1.7版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
import paddle
import paddle.fluid as fluid
import paddleslim as slim
import numpy as np
该章节构造一个用于对MNIST数据进行分类的分类模型,选用MobileNetV1
,并将输入大小设置为[1, 28, 28]
,输出类别数为10。 为了方便展示示例,我们在paddleslim.models
下预定义了用于构建分类模型的方法,执行以下代码构建分类模型:
为了快速执行该示例,我们选取简单的MNIST数据,Paddle框架的paddle.dataset.mnist包定义了MNIST数据的下载和读取。 代码如下:
import paddle.dataset.mnist as reader
train_reader = paddle.batch(
reader.train(), batch_size=128, drop_last=True)
test_reader = paddle.batch(
reader.test(), batch_size=128, drop_last=True)
data_feeder = fluid.DataFeeder(inputs, place)
Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-images-idx3-ubyte.gz
Begin to download
....................
Download finished
Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-labels-idx1-ubyte.gz
Begin to download
........
Download finished
Cache file /home/aistudio/.cache/paddle/dataset/mnist/t10k-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-images-idx3-ubyte.gz
Begin to download
....................
Download finished
Cache file /home/aistudio/.cache/paddle/dataset/mnist/t10k-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-labels-idx1-ubyte.gz
Begin to download
..
Download finished
先定义训练和测试函数,正常训练和量化训练时只需要调用函数即可。在训练函数中执行了一个epoch的训练,因为MNIST数据集数据较少,一个epoch就可将top1精度训练到95%以上。
def train(prog):
iter = 0
for data in train_reader():
acc1, acc5, loss = exe.run(prog, feed=data_feeder.feed(data), fetch_list=outputs)
if iter % 100 == 0:
print('train iter={}, top1={}, top5={}, loss={}'.format(iter, acc1.mean(), acc5.mean(), loss.mean()))
iter += 1
def test(prog):
iter = 0
res = [[], []]
for data in test_reader():
acc1, acc5, loss = exe.run(prog, feed=data_feeder.feed(data), fetch_list=outputs)
if iter % 100 == 0:
print('test iter={}, top1={}, top5={}, loss={}'.format(iter, acc1.mean(), acc5.mean(), loss.mean()))
res[0].append(acc1.mean())
res[1].append(acc5.mean())
iter += 1
print('final test result top1={}, top5={}'.format(np.array(res[0]).mean(), np.array(res[1]).mean()))
调用train函数训练分类网络,train_program是在第2步:构建网络中定义的
train(train_program)
train iter=0, top1=0.1328125, top5=0.5703125, loss=2.62163162231
train iter=100, top1=0.90625, top5=1.0, loss=0.227992996573
train iter=200, top1=0.9296875, top5=1.0, loss=0.184181377292
train iter=300, top1=0.9609375, top5=0.984375, loss=0.152075260878
train iter=400, top1=0.953125, top5=1.0, loss=0.155824646354
调用test函数测试分类网络,val_program是在第2步:构建网络中定义的。
test(val_program)
test iter=0, top1=0.96875, top5=1.0, loss=0.0912945345044
final test result top1=0.966846942902, top5=0.999699532986
按照配置在train_program和val_program中加入量化和反量化op.
place = exe.place
quant_program = slim.quant.quant_aware(train_program, exe.place, for_test=False)#请在次数添加你的代码
val_quant_program = slim.quant.quant_aware(val_program, exe.place, for_test=True)#请在次数添加你的代码
输出:
2020-04-05 12:49:39,794-INFO: quant_aware config {'moving_rate': 0.9, 'weight_quantize_type': 'channel_wise_abs_max', 'is_full_quantize': False, 'dtype': 'int8', 'weight_bits': 8, 'window_size': 10000, 'activation_bits': 8, 'quantize_op_types': ['conv2d', 'depthwise_conv2d', 'mul'], 'not_quant_pattern': ['skip_quant'], 'activation_quantize_type': 'moving_average_abs_max', 'for_tensorrt': False}
2020-04-05 12:49:40,854-INFO: quant_aware config {'moving_rate': 0.9, 'weight_quantize_type': 'channel_wise_abs_max', 'is_full_quantize': False, 'dtype': 'int8', 'weight_bits': 8, 'window_size': 10000, 'activation_bits': 8, 'quantize_op_types': ['conv2d', 'depthwise_conv2d', 'mul'], 'not_quant_pattern': ['skip_quant'], 'activation_quantize_type': 'moving_average_abs_max', 'for_tensorrt': False}
微调量化后的模型,训练一个epoch后测试。
train(quant_program)
输出:
train iter=0, top1=0.078125, top5=0.484375, loss=2.80661392212
train iter=100, top1=0.953125, top5=0.9921875, loss=0.158855527639
train iter=200, top1=0.953125, top5=1.0, loss=0.133928954601
train iter=300, top1=0.9609375, top5=0.9921875, loss=0.164225846529
train iter=400, top1=0.953125, top5=1.0, loss=0.187246501446
测试量化后的模型,和3.2 训练和测试中得到的测试结果相比,精度相近,达到了无损量化。
test(val_quant_program)
输出:
test iter=0, top1=0.9765625, top5=1.0, loss=0.0456145778298
final test result top1=0.970452725887, top5=0.99919873476