|
| 1 | +# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import os |
| 17 | +import os.path as osp |
| 18 | +import sys |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import paddle |
| 22 | +from paddleslim.quant import quant_post_static |
| 23 | + |
| 24 | +__dir__ = os.path.dirname(os.path.abspath(__file__)) |
| 25 | +sys.path.append(os.path.abspath(os.path.join(__dir__, '../../'))) |
| 26 | + |
| 27 | +from paddlevideo.loader.builder import build_dataloader, build_dataset |
| 28 | +from paddlevideo.utils import get_config, get_logger |
| 29 | + |
| 30 | + |
| 31 | +def parse_args(): |
| 32 | + def str2bool(v): |
| 33 | + return v.lower() in ("true", "t", "1") |
| 34 | + |
| 35 | + parser = argparse.ArgumentParser("PaddleVideo Inference model script") |
| 36 | + parser.add_argument( |
| 37 | + '-c', |
| 38 | + '--config', |
| 39 | + type=str, |
| 40 | + default= |
| 41 | + '../../configs/recognition/pptsm/pptsm_k400_frames_uniform_quantization.yaml', |
| 42 | + help='quantization config file path') |
| 43 | + parser.add_argument('-o', |
| 44 | + '--override', |
| 45 | + action='append', |
| 46 | + default=[], |
| 47 | + help='config options to be overridden') |
| 48 | + parser.add_argument("--use_gpu", |
| 49 | + type=str2bool, |
| 50 | + default=True, |
| 51 | + help="whether use gpui during quantization") |
| 52 | + |
| 53 | + return parser.parse_args() |
| 54 | + |
| 55 | + |
| 56 | +def post_training_quantization(cfg, use_gpu: bool = True): |
| 57 | + """Quantization entry |
| 58 | +
|
| 59 | + Args: |
| 60 | + cfg (dict): quntization configuration. |
| 61 | + use_gpu (bool, optional): whether to use gpu during quantization. Defaults to True. |
| 62 | + """ |
| 63 | + logger = get_logger("paddlevideo") |
| 64 | + |
| 65 | + place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace() |
| 66 | + |
| 67 | + # get defined params |
| 68 | + batch_size = cfg.DATASET.get('batch_size', 1) |
| 69 | + num_workers = cfg.DATASET.get('num_workers', 0) |
| 70 | + inference_file_name = cfg.get('model_name', 'inference') |
| 71 | + inference_model_dir = cfg.get('inference_model_dir', |
| 72 | + f'./inference/{inference_file_name}') |
| 73 | + quant_output_dir = cfg.get('quant_output_dir', |
| 74 | + osp.join(inference_model_dir, 'quant_model')) |
| 75 | + batch_nums = cfg.get('batch_nums', 10) |
| 76 | + |
| 77 | + # build dataloader for quantization, lite data is enough |
| 78 | + slim_dataset = build_dataset((cfg.DATASET.quant, cfg.PIPELINE.quant)) |
| 79 | + slim_dataloader_setting = dict(batch_size=batch_size, |
| 80 | + num_workers=num_workers, |
| 81 | + places=place, |
| 82 | + drop_last=False, |
| 83 | + shuffle=False) |
| 84 | + slim_loader = build_dataloader(slim_dataset, **slim_dataloader_setting) |
| 85 | + |
| 86 | + logger.info("Build slim_loader finished") |
| 87 | + |
| 88 | + def sample_generator(loader): |
| 89 | + def __reader__(): |
| 90 | + for indx, data in enumerate(loader): |
| 91 | + # must return np.ndarray, not paddle.Tensor |
| 92 | + videos = np.array(data[0]) |
| 93 | + yield videos |
| 94 | + |
| 95 | + return __reader__ |
| 96 | + |
| 97 | + # execute quantization in static graph mode |
| 98 | + paddle.enable_static() |
| 99 | + |
| 100 | + exe = paddle.static.Executor(place) |
| 101 | + |
| 102 | + logger.info("Staring Post-Training Quantization...") |
| 103 | + |
| 104 | + quant_post_static(executor=exe, |
| 105 | + model_dir=inference_model_dir, |
| 106 | + quantize_model_path=quant_output_dir, |
| 107 | + sample_generator=sample_generator(slim_loader), |
| 108 | + model_filename=f'{inference_file_name}.pdmodel', |
| 109 | + params_filename=f'{inference_file_name}.pdiparams', |
| 110 | + batch_size=batch_size, |
| 111 | + batch_nums=batch_nums, |
| 112 | + algo='KL') |
| 113 | + |
| 114 | + logger.info("Post-Training Quantization finished...") |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + args = parse_args() |
| 119 | + cfg = get_config(args.config, overrides=args.override) |
| 120 | + post_training_quantization(cfg, args.use_gpu) |
0 commit comments