NCNN 下FP16、FP32速度耗时一般情况下:GPU使用FP16模型<CPU使用FP32模型<GPU使用FP32模型
ncnn int8量化后的改进, 参考链接: https://baijiahao.baidu.com/s?id=1699724039745016586&wfr=spider&for=pc
量化步骤参考 nihui 大佬教程: https://github.com/Tencent/ncnn/wiki/quantized-int8-inference
20220216-ncnn版本:
(1) 合并bn层 ncnn/build/tools 目录下
./ncnnoptimize xxx.param xxx.bin xxx_opt.param xxx_opt.bin 0 (0:f32, 1/65536: fp16)
(2) 生成量化图像集
find images/ -type f > imagelist.txt
注: images 为量化图像集, 生成的 imagelist.txt 为图像路径
(3) 生成量化表 ncnn/build/tools/quantize 目录下
./ncnn2table 1208_mbfacenet_ir_nobn.param 1208_mbfacenet_ir_nobn.bin imagelist.txt mbfacenet.table mean=[127.5, 127.5, 127.5] norm=[0.007843, 0.007843, 0.007843] shape=[112,112,3] pixel=BGR thread=4 method=kl/aciq
注: mean/norm 是使用ncnn推理时的参数((x-mean)*norm), shape 为网络的输入图像尺寸, method 为量化方式
(4) int8 量化
./ncnn2int8 1208_mbfacenet_ir_nobn.param 1208_mbfacenet_ir_nobn.bin 1208_mbfacenet_ir_int8.param 1208_mbfacenet_ir_int8.bin mbfacenet.table
注意事项:
1. 量化时, 方式有两种,kl和aciq,可以都尝试一下 (可能会导致推理结果不正确)
2. 生成量化表时,需确保.table非0非nan非inf,否则推理结果不正确
3. 量化时的图片应选择与测试集相似度较高的
4. int8量化后可能在pc端推理速度反而比float32慢,由于int8 主要是针对arm进行优化的,建议在目标平台测试速度
我最终三个模型均量化成功并推理成功啦! 过程中遇到了很多问题, 推理结果不正确, 量化表数值为0, 为NAN, 为inf 等, 大家不要放弃, 仔细检查自己的步骤是否有问题.