From 38687970fa8883707dda16a34837cc1aae060ccc Mon Sep 17 00:00:00 2001 From: WangChen0902 <827913668@qq.com> Date: Wed, 20 Apr 2022 13:32:53 +0800 Subject: [PATCH] add HRNet export model tutorials (#5502) --- tutorials/pp-series/HRNet-Keypoint/README.md | 30 ++++++- .../HRNet-Keypoint/dataset/download_coco.py | 4 +- .../HRNet-Keypoint/tools/export_model.py | 83 +++++++++++++++++++ 3 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 tutorials/pp-series/HRNet-Keypoint/tools/export_model.py diff --git a/tutorials/pp-series/HRNet-Keypoint/README.md b/tutorials/pp-series/HRNet-Keypoint/README.md index 91bdd3da..e9f642b4 100644 --- a/tutorials/pp-series/HRNet-Keypoint/README.md +++ b/tutorials/pp-series/HRNet-Keypoint/README.md @@ -98,9 +98,9 @@ python -c "import paddle; print(paddle.__version__)" - If the coco dataset has been downloaded The files can be organized according to the above data file organization structure. -### 2.3 Training & Evaluation & Inference +### 2.3 Training & Evaluation & Test -We provides scripts for training, evalution and inference with various features according to different configure. +We provides scripts for training, evalution and test with various features according to different configure. ```bash # training on single-GPU @@ -115,7 +115,7 @@ python -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 tools/train.py -c con export CUDA_VISIBLE_DEVICES=0 python tools/eval.py -c configs/hrnet_w32_256x192.yml -o weights=https://paddledet.bj.bcebos.com/models/keypoint/hrnet_w32_256x192.pdparams -# Inference +# test python tools/infer.py -c configs/hrnet_w32_256x192.yml --infer_img=dataset/test_image/hrnet_demo.jpg -o weights=https://paddledet.bj.bcebos.com/models/keypoint/hrnet_w32_256x192.pdparams # training with distillation @@ -133,10 +133,32 @@ python -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 tools/train.py -c con export CUDA_VISIBLE_DEVICES=0 python tools/eval.py -c configs/lite_hrnet_30_256x192_coco_pact.yml -o weights=https://paddledet.bj.bcebos.com/models/keypoint/lite_hrnet_30_256x192_coco_pact.pdparams -# Inference with PACT quantization +# test with PACT quantization python tools/infer.py -c configs/lite_hrnet_30_256x192_coco_pact.yml --infer_img=dataset/test_image/hrnet_demo.jpg -o weights=https://paddledet.bj.bcebos.com/models/keypoint/lite_hrnet_30_256x192_coco_pact.pdparams +``` + +### 2.4 Export model & Inference + +```bash +# export model +python tools/export_model.py -c configs/hrnet_w32_256x192.yml -o weights=https://paddledet.bj.bcebos.com/models/keypoint/hrnet_w32_256x192_coco.pdparams + +# inference +python deploy/infer.py --model_dir=output_inference/hrnet_w32_256x192/ --image_file=dataset/test_image/hrnet_demo.jpg + +# export model with lite model +python tools/export_model.py -c configs/lite_hrnet_30_256x192_coco.yml -o weights=https://paddledet.bj.bcebos.com/models/keypoint/lite_hrnet_30_256x192_coco.pdparams + +# inference with lite model +python deploy/infer.py --model_dir=output_inference/lite_hrnet_30_256x192_coco/ --image_file=dataset/test_image/hrnet_demo.jpg + +# export model with PACT quantization +python tools/export_model.py -c configs/lite_hrnet_30_256x192_coco_pact.yml -o weights=https://paddledet.bj.bcebos.com/models/keypoint/lite_hrnet_30_256x192_coco_pact.pdparams + +# inference with PACT quantization +python deploy/infer.py --model_dir=output_inference/lite_hrnet_30_256x192_coco_pact/ --image_file=dataset/test_image/hrnet_demo.jpg ``` diff --git a/tutorials/pp-series/HRNet-Keypoint/dataset/download_coco.py b/tutorials/pp-series/HRNet-Keypoint/dataset/download_coco.py index 9809ecfb..9de6faa3 100644 --- a/tutorials/pp-series/HRNet-Keypoint/dataset/download_coco.py +++ b/tutorials/pp-series/HRNet-Keypoint/dataset/download_coco.py @@ -16,11 +16,11 @@ import sys import os.path as osp import logging # add python path of PadleDetection to sys.path -parent_path = osp.abspath(osp.join(__file__, *(['..'] * 3))) +parent_path = osp.abspath(osp.join(__file__, *(['..'] * 2))) if parent_path not in sys.path: sys.path.append(parent_path) -from ppdet.utils.download import download_dataset +from lib.utils.download import download_dataset logging.basicConfig(level=logging.INFO) diff --git a/tutorials/pp-series/HRNet-Keypoint/tools/export_model.py b/tutorials/pp-series/HRNet-Keypoint/tools/export_model.py new file mode 100644 index 00000000..2e0a4dfd --- /dev/null +++ b/tutorials/pp-series/HRNet-Keypoint/tools/export_model.py @@ -0,0 +1,83 @@ +# Copyright (c) 2020 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import sys + +# add python path of PadleDetection to sys.path +parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 2))) +sys.path.insert(0, parent_path) + +# ignore warning log +import warnings +warnings.filterwarnings('ignore') +import glob + +import paddle +from lib.utils.workspace import load_config, merge_config +from lib.slim import build_slim_model +from lib.core.trainer import Trainer +from lib.utils.check import check_gpu, check_version, check_config +from lib.utils.cli import ArgsParser +from lib.utils.logger import setup_logger + +logger = setup_logger('eval') + + +def parse_args(): + parser = ArgsParser() + parser.add_argument( + "--save-inference-dir", + default='output_inference', + type=str, + help="Evaluation directory, default is current directory.") + + args = parser.parse_args() + return args + + +def main(): + FLAGS = parse_args() + cfg = load_config(FLAGS.config) + # cfg['output_eval'] = FLAGS.output_eval + merge_config(FLAGS.opt) + + if cfg.use_gpu: + paddle.set_device('gpu') + else: + paddle.set_device('cpu') + + if 'slim' in cfg: + cfg = build_slim_model(cfg, mode='test') + + check_config(cfg) + check_gpu(cfg.use_gpu) + check_version() + + # build trainer + trainer = Trainer(cfg, mode='test') + + # load weights + trainer.load_weights(cfg.weights) + + # export model + trainer.export(output_dir=FLAGS.save_inference_dir) + + +if __name__ == '__main__': + main() -- GitLab