From db5f32955a58e3a1e704fc3fd2b86ade6c63aa36 Mon Sep 17 00:00:00 2001 From: Liufang Sang Date: Tue, 15 Oct 2019 10:19:34 +0800 Subject: [PATCH] [PaddleSlim] save infer model for each epoch (#3570) --- .../slim/quantization/README.md | 1 - .../slim/quantization/compress.py | 1 + .../slim/quantization/freeze.py | 2 +- .../classification/quantization/compress.py | 1 + .../classification/quantization/freeze.py | 26 ++++++++++++++----- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/PaddleCV/PaddleDetection/slim/quantization/README.md b/PaddleCV/PaddleDetection/slim/quantization/README.md index ad178fd9..fe69186a 100644 --- a/PaddleCV/PaddleDetection/slim/quantization/README.md +++ b/PaddleCV/PaddleDetection/slim/quantization/README.md @@ -44,7 +44,6 @@ step1: 设置gpu卡 export CUDA_VISIBLE_DEVICES=0 ``` step2: 开始训练 -``` 使用PaddleDetection提供的配置文件在用8卡进行训练: ``` diff --git a/PaddleCV/PaddleDetection/slim/quantization/compress.py b/PaddleCV/PaddleDetection/slim/quantization/compress.py index 7c4dbead..6e3156ac 100644 --- a/PaddleCV/PaddleDetection/slim/quantization/compress.py +++ b/PaddleCV/PaddleDetection/slim/quantization/compress.py @@ -239,6 +239,7 @@ def main(): eval_feed_list=test_feed, eval_func={'map': eval_func}, eval_fetch_list=[eval_fetch_list[0]], + prune_infer_model=[["image", "im_size"],["multiclass_nms_0.tmp_0"]], train_optimizer=None) com.config(FLAGS.slim_file) com.run() diff --git a/PaddleCV/PaddleDetection/slim/quantization/freeze.py b/PaddleCV/PaddleDetection/slim/quantization/freeze.py index 04271a4f..85906fa8 100644 --- a/PaddleCV/PaddleDetection/slim/quantization/freeze.py +++ b/PaddleCV/PaddleDetection/slim/quantization/freeze.py @@ -142,7 +142,7 @@ def main(): assert os.path.exists(FLAGS.model_path) infer_prog, feed_names, fetch_targets = fluid.io.load_inference_model( dirname=FLAGS.model_path, executor=exe, - model_filename='__model__', + model_filename='__model__.infer', params_filename='__params__') eval_keys = ['bbox', 'gt_box', 'gt_label', 'is_difficult'] diff --git a/PaddleSlim/classification/quantization/compress.py b/PaddleSlim/classification/quantization/compress.py index 4894684f..41b76788 100644 --- a/PaddleSlim/classification/quantization/compress.py +++ b/PaddleSlim/classification/quantization/compress.py @@ -95,6 +95,7 @@ def compress(args): eval_fetch_list=val_fetch_list, teacher_programs=[], train_optimizer=opt, + prune_infer_model=[[image.name], [out.name]], distiller_optimizer=None) com_pass.config(args.config_file) com_pass.run() diff --git a/PaddleSlim/classification/quantization/freeze.py b/PaddleSlim/classification/quantization/freeze.py index 396875c7..6e97e024 100644 --- a/PaddleSlim/classification/quantization/freeze.py +++ b/PaddleSlim/classification/quantization/freeze.py @@ -53,20 +53,32 @@ def eval(args): val_program, feed_names, fetch_targets = fluid.io.load_inference_model(args.model_path, exe, - model_filename="__model__", + model_filename="__model__.infer", params_filename="__params__") val_reader = paddle.batch(reader.val(), batch_size=128) feeder = fluid.DataFeeder(place=place, feed_list=feed_names, program=val_program) results=[] for batch_id, data in enumerate(val_reader()): + image = [[d[0]] for d in data] + label = [[d[1]] for d in data] + feed_data = feeder.feed(image) + pred = exe.run(val_program, + feed=feed_data, + fetch_list=fetch_targets) + pred = np.array(pred[0]) + label = np.array(label) + sort_array = pred.argsort(axis=1) + top_1_pred = sort_array[:, -1:][:, ::-1] + top_1 = np.mean(label == top_1_pred) + top_5_pred = sort_array[:, -5:][:, ::-1] + acc_num = 0 + for i in range(len(label)): + if label[i][0] in top_5_pred[i]: + acc_num += 1 + top_5 = acc_num / len(label) + results.append([top_1, top_5]) - # top1_acc, top5_acc - result = exe.run(val_program, - feed=feeder.feed(data), - fetch_list=fetch_targets) - result = [np.mean(r) for r in result] - results.append(result) result = np.mean(np.array(results), axis=0) print("top1_acc/top5_acc= {}".format(result)) sys.stdout.flush() -- GitLab