MobileNetV2使用“有校准数据训练后量化”精度丢失严重
Created by: yeyupiaoling
训练环境
- Python 3.7
- windows 10
- PaddlePaddle 1.7.0
- MobileNet V2
转换环境
- Ubuntu 16.04
- Python 3.5
- Paddle-Lite 2.3.0
模型: infer_model.zip
图片(提供几张): images.zip
数据预处理:
import os
import random
from multiprocessing import cpu_count
import numpy as np
import paddle
from PIL import Image
# 测试图片的预处理
def test_mapper(sample):
img, label, crop_size = sample
img = Image.open(img)
# 统一图像大小
img = img.resize((crop_size, crop_size), Image.ANTIALIAS)
# 转换成numpy值
img = np.array(img).astype(np.float32)
# 转换成CHW
img = img.transpose((2, 0, 1))
img = (img - 128.0) * 0.0078125
return img, int(label)
# 测试的图片reader
def test_reader(test_list_path, crop_size):
def reader():
with open(test_list_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
img, label = line.split('\t')
img = os.path.join('..', img)
yield img, label, crop_size
return paddle.reader.xmap_readers(test_mapper, reader, cpu_count(), 1024)
量化训练:
from paddle.fluid.contrib.slim.quantization import PostTrainingQuantization, WeightQuantization
import paddle.fluid as fluid
import my_reader
exe = fluid.Executor(fluid.CPUPlace())
model_dir = 'infer_model'
save_model_path = 'mobilenet_v2_quant'
sample_generator = my_reader.test_reader('../dataset/test_list.txt', 224)
batch_size = 10
batch_nums = 10
algo = "KL"
quantizable_op_type = ["conv2d", "depthwise_conv2d", "mul"]
ptq = PostTrainingQuantization(
executor=exe,
sample_generator=sample_generator,
model_dir=model_dir,
batch_size=batch_size,
batch_nums=batch_nums,
algo=algo,
quantizable_op_type=quantizable_op_type)
ptq.quantize()
ptq.save_quantized_model(save_model_path)
转换命令:
./opt --model_dir=./mobilenet_v2_quant \
--optimize_out_type=naive_buffer \
--optimize_out=mobilenet_v2_quant \
--valid_targets=arm \
--prefer_int8_kernel=true