quant_post.py 2.5 KB
Newer Older
S
slf12 已提交
1 2 3 4 5 6 7 8
import os
import sys
import logging
import paddle
import argparse
import functools
import math
import time
G
Guanghua Yu 已提交
9
import random
S
slf12 已提交
10
import numpy as np
G
Guanghua Yu 已提交
11
import paddle
S
slf12 已提交
12

W
whs 已提交
13 14
sys.path[0] = os.path.join(
    os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
S
slf12 已提交
15
from paddleslim.common import get_logger
X
XGZhang 已提交
16
from paddleslim.quant import quant_post_static
S
slf12 已提交
17
from utility import add_arguments, print_arguments
L
Liufang Sang 已提交
18
import imagenet_reader as reader
S
slf12 已提交
19 20 21 22 23
_logger = get_logger(__name__, level=logging.INFO)

parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
X
XGZhang 已提交
24 25
add_arg('batch_size',       int,  32,                 "Minibatch size.")
add_arg('batch_num',       int,  1,               "Batch number")
S
slf12 已提交
26 27 28 29 30
add_arg('use_gpu',          bool, True,                "Whether to use GPU or not.")
add_arg('model_path',       str,  "./inference_model/MobileNet/",  "model dir")
add_arg('save_path',       str,  "./quant_model/MobileNet/",  "model dir to save quanted model")
add_arg('model_filename',       str, None,                 "model file name")
add_arg('params_filename',      str, None,                 "params file name")
X
XGZhang 已提交
31 32
add_arg('algo',         str, 'hist',               "calibration algorithm")
add_arg('hist_percent',         float, 0.9999,             "The percentile of algo:hist")
X
XGZhang 已提交
33
add_arg('bias_correction',         bool, False,             "Whether to use bias correction")
G
Guanghua Yu 已提交
34
add_arg('ce_test',                 bool,   False,                                        "Whether to CE test.")
X
XGZhang 已提交
35

S
slf12 已提交
36 37 38 39
# yapf: enable


def quantize(args):
G
Guanghua Yu 已提交
40
    val_reader = reader.val()
S
slf12 已提交
41

B
Bai Yifan 已提交
42
    place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
S
slf12 已提交
43 44 45 46

    assert os.path.exists(args.model_path), "args.model_path doesn't exist"
    assert os.path.isdir(args.model_path), "args.model_path must be a dir"

B
Bai Yifan 已提交
47
    exe = paddle.static.Executor(place)
X
XGZhang 已提交
48
    quant_post_static(
S
slf12 已提交
49 50 51 52 53 54 55
        executor=exe,
        model_dir=args.model_path,
        quantize_model_path=args.save_path,
        sample_generator=val_reader,
        model_filename=args.model_filename,
        params_filename=args.params_filename,
        batch_size=args.batch_size,
X
XGZhang 已提交
56 57
        batch_nums=args.batch_num,
        algo=args.algo,
X
XGZhang 已提交
58 59
        hist_percent=args.hist_percent,
        bias_correction=args.bias_correction)
S
slf12 已提交
60 61 62 63 64


def main():
    args = parser.parse_args()
    print_arguments(args)
G
Guanghua Yu 已提交
65 66 67 68 69 70
    if args.ce_test:
        # set seed
        seed = 111
        np.random.seed(seed)
        paddle.seed(seed)
        random.seed(seed)
S
slf12 已提交
71 72 73 74
    quantize(args)


if __name__ == '__main__':
75
    paddle.enable_static()
S
slf12 已提交
76
    main()