diff --git a/demo/quant/quant_aware/README.md b/demo/quant/quant_aware/README.md index 0f690179739ca5a9d0948e4302c79066bfbcabed..f7fa2686d52ae168f82840f6798fe4ebebb816d6 100644 --- a/demo/quant/quant_aware/README.md +++ b/demo/quant/quant_aware/README.md @@ -6,9 +6,29 @@ 请参考 量化API文档。 -## 分类模型的离线量化流程 +## 分类模型的量化训练流程 -### 1. 配置量化参数 +### 准备数据 + +在``demo``文件夹下创建``data``文件夹,将``ImageNet``数据集解压在``data``文件夹下,解压后``data``文件夹下应包含以下文件: +- ``'train'``文件夹,训练图片 +- ``'train_list.txt'``文件 +- ``'val'``文件夹,验证图片 +- ``'val_list.txt'``文件 + +### 准备需要量化的模型 + +使用以下命令下载训练好的模型并解压。 + +``` +mkdir pretrain +cd pretrain +wget http://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV1_pretrained.tar +tar xf MobileNetV1_pretrained.tar +cd .. +``` + +### 配置量化参数 ``` quant_config = { @@ -24,7 +44,7 @@ quant_config = { } ``` -### 2. 对训练和测试program插入可训练量化op +### 对训练和测试program插入可训练量化op ``` val_program = quant_aware(val_program, place, quant_config, scope=None, for_test=True) @@ -32,7 +52,7 @@ val_program = quant_aware(val_program, place, quant_config, scope=None, for_test compiled_train_prog = quant_aware(train_prog, place, quant_config, scope=None, for_test=False) ``` -### 3.关掉指定build策略 +### 关掉指定build策略 ``` build_strategy = fluid.BuildStrategy() @@ -45,32 +65,10 @@ compiled_train_prog = compiled_train_prog.with_data_parallel( exec_strategy=exec_strategy) ``` -### 4. freeze program - -``` -float_program, int8_program = convert(val_program, - place, - quant_config, - scope=None, - save_int8=True) -``` -### 5.保存预测模型 +### 训练命令 ``` -fluid.io.save_inference_model( - dirname=float_path, - feeded_var_names=[image.name], - target_vars=[out], executor=exe, - main_program=float_program, - model_filename=float_path + '/model', - params_filename=float_path + '/params') - -fluid.io.save_inference_model( - dirname=int8_path, - feeded_var_names=[image.name], - target_vars=[out], executor=exe, - main_program=int8_program, - model_filename=int8_path + '/model', - params_filename=int8_path + '/params') +python train.py --model MobileNet --pretrained_model ./pretrain/MobileNetV1_pretrained --checkpoint_dir ./output/mobilenetv1 --num_epochs 30 ``` +运行之后,可看到``best_model``的最后测试结果,和MobileNet量化前的精度top1=70.99%, top5=89.68%非常相近。 diff --git a/demo/quant/quant_aware/train.py b/demo/quant/quant_aware/train.py index 1933a7d88aafa5d8a4787dbf0a32a8907f9deeb1..7cb088b440f4037203c837830b1b853ad99ab100 100644 --- a/demo/quant/quant_aware/train.py +++ b/demo/quant/quant_aware/train.py @@ -8,8 +8,8 @@ import math import time import numpy as np import paddle.fluid as fluid -sys.path.append(sys.path[0] + "../../../") -sys.path.append(sys.path[0] + "../../") +sys.path.append(sys.path[0] + "/../../../") +sys.path.append(sys.path[0] + "/../../") from paddleslim.common import get_logger from paddleslim.analysis import flops from paddleslim.quant import quant_aware, quant_post, convert @@ -37,7 +37,7 @@ parser.add_argument('--step_epochs', nargs='+', type=int, default=[30, 60, 90], add_arg('config_file', str, None, "The config file for compression with yaml format.") add_arg('data', str, "imagenet", "Which data to use. 'mnist' or 'imagenet'") add_arg('log_period', int, 10, "Log period in batches.") -add_arg('test_period', int, 10, "Test period in epoches.") +add_arg('checkpoint_dir', str, "output", "checkpoint save dir") # yapf: enable model_list = [m for m in dir(models) if "__" not in m] @@ -192,16 +192,6 @@ def compress(args): return np.mean(np.array(acc_top1_ns)) def train(epoch, compiled_train_prog): - build_strategy = fluid.BuildStrategy() - build_strategy.memory_optimize = False - build_strategy.enable_inplace = False - build_strategy.fuse_all_reduce_ops = False - build_strategy.sync_batch_norm = False - exec_strategy = fluid.ExecutionStrategy() - compiled_train_prog = compiled_train_prog.with_data_parallel( - loss_name=avg_cost.name, - build_strategy=build_strategy, - exec_strategy=exec_strategy) batch_id = 0 for data in train_reader(): @@ -221,14 +211,41 @@ def compress(args): end_time - start_time)) batch_id += 1 + build_strategy = fluid.BuildStrategy() + build_strategy.memory_optimize = False + build_strategy.enable_inplace = False + build_strategy.fuse_all_reduce_ops = False + build_strategy.sync_batch_norm = False + exec_strategy = fluid.ExecutionStrategy() + compiled_train_prog = compiled_train_prog.with_data_parallel( + loss_name=avg_cost.name, + build_strategy=build_strategy, + exec_strategy=exec_strategy) + ############################################################################################################ # train loop ############################################################################################################ + best_acc1 = 0.0 + best_epoch = 0 for i in range(args.num_epochs): train(i, compiled_train_prog) - if i % args.test_period == 0: - test(i, val_program) - + acc1 = test(i, val_program) + fluid.io.save_persistables( + exe, + dirname=os.path.join(args.checkpoint_dir, str(i)), + main_program=val_program) + if acc1 > best_acc1: + best_acc1 = acc1 + best_epoch = i + fluid.io.save_persistables( + exe, + dirname=os.path.join(args.checkpoint_dir, 'best_model'), + main_program=val_program) + + fluid.io.load_persistables( + exe, + dirname=os.path.join(args.checkpoint_dir, 'best_model'), + main_program=val_program) ############################################################################################################ # 3. Freeze the graph after training by adjusting the quantize # operators' order for the inference. @@ -237,7 +254,8 @@ def compress(args): float_program, int8_program = convert(val_program, place, quant_config, \ scope=None, \ save_int8=True) - + print("eval best_model after convert") + final_acc1 = test(best_epoch, float_program) ############################################################################################################ # 4. Save inference model ############################################################################################################ diff --git a/demo/quant/quant_post/README.md b/demo/quant/quant_post/README.md index 72cd68781d6de71aca19d3b34f1daf187494f371..027bcf3dba6ba6079ab1da942ee393f6751010c0 100755 --- a/demo/quant/quant_post/README.md +++ b/demo/quant/quant_post/README.md @@ -10,7 +10,7 @@ ### 准备数据 -在当前文件夹下创建``data``文件夹,将``imagenet``数据集解压在``data``文件夹下,解压后``data``文件夹下应包含以下文件: +在``demo``文件夹下创建``data``文件夹,将``ImageNet``数据集解压在``data``文件夹下,解压后``data``文件夹下应包含以下文件: - ``'train'``文件夹,训练图片 - ``'train_list.txt'``文件 - ``'val'``文件夹,验证图片 @@ -69,4 +69,4 @@ python eval.py --model_path ./quant_model_train/MobileNet ``` top1_acc/top5_acc= [0.70141864 0.89086477] ``` -从以上精度对比可以看出,对``mobilenet``在``imagenet``上的分类模型进行离线量化后 ``top1``精度损失为``0.77%``, ``top5``精度损失为``0.46%``. +从以上精度对比可以看出,对``mobilenet``在``imagenet``上的分类模型进行离线量化后 ``top1``精度损失为``0.77%``, ``top5``精度损失为``0.46%``. diff --git a/demo/quant/quant_post/quant_post.py b/demo/quant/quant_post/quant_post.py index 5a2c1c834c82e125adad7a597f0d8667d8b19bfc..5691402122540f4426e1e1e63f6642cd10cfcb4c 100755 --- a/demo/quant/quant_post/quant_post.py +++ b/demo/quant/quant_post/quant_post.py @@ -9,13 +9,12 @@ import time import numpy as np import paddle.fluid as fluid -import reader sys.path.append(sys.path[0] + "/../../../") from paddleslim.common import get_logger from paddleslim.quant import quant_post sys.path.append(sys.path[0] + "/../../") from utility import add_arguments, print_arguments - +import imagenet_reader as reader _logger = get_logger(__name__, level=logging.INFO) parser = argparse.ArgumentParser(description=__doc__)