post_quant.py 2.8 KB
Newer Older
G
Guanghua Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import numpy as np
import argparse
import paddle
G
Guanghua Yu 已提交
20
from paddleslim.auto_compression.config_helpers import load_config
21
from paddleslim.common import load_onnx_model
G
Guanghua Yu 已提交
22 23
from paddleslim.quant import quant_post_static
from dataset import COCOTrainDataset
G
Guanghua Yu 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

def argsparser():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        '--config_path',
        type=str,
        default=None,
        help="path of compression strategy config.",
        required=True)
    parser.add_argument(
        '--save_dir',
        type=str,
        default='ptq_out',
        help="directory to save compressed model.")
    parser.add_argument(
        '--devices',
        type=str,
        default='gpu',
        help="which device used to compress.")
    parser.add_argument(
        '--algo', type=str, default='KL', help="post quant algo.")

    return parser


def main():
    global global_config
G
Guanghua Yu 已提交
51
    all_config = load_config(FLAGS.config_path)
G
Guanghua Yu 已提交
52 53
    global_config = all_config["Global"]

G
Guanghua Yu 已提交
54 55 56 57 58 59
    dataset = COCOTrainDataset(
        dataset_dir=global_config['dataset_dir'],
        image_dir=global_config['val_image_dir'],
        anno_path=global_config['val_anno_path'])
    train_loader = paddle.io.DataLoader(
        dataset, batch_size=1, shuffle=True, drop_last=True, num_workers=0)
G
Guanghua Yu 已提交
60 61 62

    place = paddle.CUDAPlace(0) if FLAGS.devices == 'gpu' else paddle.CPUPlace()
    exe = paddle.static.Executor(place)
63 64 65
    load_onnx_model(global_config["model_dir"])
    inference_model_path = global_config["model_dir"].rstrip().rstrip(
        '.onnx') + '_infer'
G
Guanghua Yu 已提交
66 67
    quant_post_static(
        executor=exe,
68
        model_dir=inference_model_path,
G
Guanghua Yu 已提交
69 70
        quantize_model_path=FLAGS.save_dir,
        data_loader=train_loader,
71 72
        model_filename='model.pdmodel',
        params_filename='model.pdiparams',
G
Guanghua Yu 已提交
73 74 75 76 77 78
        batch_size=32,
        batch_nums=10,
        algo=FLAGS.algo,
        hist_percent=0.999,
        is_full_quantize=False,
        bias_correction=False,
79
        onnx_format=True)
G
Guanghua Yu 已提交
80 81 82 83 84 85 86 87 88 89 90


if __name__ == '__main__':
    paddle.enable_static()
    parser = argsparser()
    FLAGS = parser.parse_args()

    assert FLAGS.devices in ['cpu', 'gpu', 'xpu', 'npu']
    paddle.set_device(FLAGS.devices)

    main()