From d44eab3abc9780815e1b9bd150e180184ccc1852 Mon Sep 17 00:00:00 2001 From: Bobholamovic Date: Wed, 24 Jul 2024 16:28:37 +0800 Subject: [PATCH 1/2] Support PIR --- README.md | 2 +- README_CN.md | 2 +- README_EN.md | 2 +- deploy/python/collect_dynamic_shape.py | 2 +- deploy/python/infer.py | 4 ++-- deploy/python/infer_dataset.py | 2 +- docs/install.md | 6 +++--- docs/install_cn.md | 6 +++--- paddleseg/deploy/infer.py | 8 ++++---- paddleseg/models/backbones/efficientformerv2.py | 2 +- tools/export.py | 3 +-- 11 files changed, 19 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e8f014f6ff..bacd3186b4 120000 --- a/README.md +++ b/README.md @@ -1 +1 @@ -README_CN.md +README_CN.md \ No newline at end of file diff --git a/README_CN.md b/README_CN.md index b62759c596..87d1084ef9 100644 --- a/README_CN.md +++ b/README_CN.md @@ -11,7 +11,7 @@ [![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE) [![Version](https://img.shields.io/github/release/PaddlePaddle/PaddleSeg.svg)](https://github.com/PaddlePaddle/PaddleSeg/releases) -![python version](https://img.shields.io/badge/python-3.6+-orange.svg) +![python version](https://img.shields.io/badge/python-3.8+-orange.svg) ![support os](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg) ![stars](https://img.shields.io/github/stars/PaddlePaddle/PaddleSeg?color=ccf) diff --git a/README_EN.md b/README_EN.md index b600f9fd99..86085e6731 100644 --- a/README_EN.md +++ b/README_EN.md @@ -10,7 +10,7 @@ English | [简体中文](README_CN.md) [![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE) [![Version](https://img.shields.io/github/release/PaddlePaddle/PaddleSeg.svg)](https://github.com/PaddlePaddle/PaddleSeg/releases) -![python version](https://img.shields.io/badge/python-3.6+-orange.svg) +![python version](https://img.shields.io/badge/python-3.8+-orange.svg) ![support os](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg) ![stars](https://img.shields.io/github/stars/PaddlePaddle/PaddleSeg?color=ccf) diff --git a/deploy/python/collect_dynamic_shape.py b/deploy/python/collect_dynamic_shape.py index 276e7c1844..4626fc3877 100644 --- a/deploy/python/collect_dynamic_shape.py +++ b/deploy/python/collect_dynamic_shape.py @@ -62,7 +62,7 @@ def collect_dynamic_shape(args): # prepare config cfg = DeployConfig(args.config) - pred_cfg = PredictConfig(cfg.model, cfg.params) + pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) pred_cfg.enable_use_gpu(1000, 0) pred_cfg.collect_shape_range_info(args.dynamic_shape_path) diff --git a/deploy/python/infer.py b/deploy/python/infer.py index 736e250196..f133d652d7 100644 --- a/deploy/python/infer.py +++ b/deploy/python/infer.py @@ -52,7 +52,7 @@ def auto_tune(args, imgs, img_nums): num = min(len(imgs), img_nums) cfg = DeployConfig(args.cfg) - pred_cfg = PredictConfig(cfg.model, cfg.params) + pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) pred_cfg.enable_use_gpu(100, 0) if not args.print_detail: pred_cfg.disable_glog_info() @@ -139,7 +139,7 @@ def __init__(self, args): logger=logger) def _init_base_config(self): - self.pred_cfg = PredictConfig(self.cfg.model, self.cfg.params) + self.pred_cfg = PredictConfig(self.cfg.model_dir, self.cfg.model_prefix) if not self.args.print_detail: self.pred_cfg.disable_glog_info() self.pred_cfg.enable_memory_optim() diff --git a/deploy/python/infer_dataset.py b/deploy/python/infer_dataset.py index a469e4c9ec..21364dd223 100644 --- a/deploy/python/infer_dataset.py +++ b/deploy/python/infer_dataset.py @@ -191,7 +191,7 @@ def auto_tune(args, dataset, img_nums): num = min(len(dataset), img_nums) cfg = DeployConfig(args.cfg) - pred_cfg = PredictConfig(cfg.model, cfg.params) + pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) pred_cfg.enable_use_gpu(100, 0) if not args.print_detail: pred_cfg.disable_glog_info() diff --git a/docs/install.md b/docs/install.md index da3d4f0064..aa684edeb4 100644 --- a/docs/install.md +++ b/docs/install.md @@ -8,20 +8,20 @@ English | [简体中文](install_cn.md) - pip/pip3(9.0.1+),64-bit version - CUDA >= 10.2 - cuDNN >= 7.6 -- PaddlePaddle (the version >= 2.4) +- PaddlePaddle (the version >= 3.0.0b0) ## 2 Installation ### 2.1 Install PaddlePaddle -Please refer to the [installation doc](https://www.paddlepaddle.org.cn/documentation/docs/en/install/index_en.html) to install PaddlePaddle (the version >= 2.4). +Please refer to the [installation doc](https://www.paddlepaddle.org.cn/documentation/docs/en/install/index_en.html) to install PaddlePaddle (the version >= 3.0.0b0). Highly recommend you install the GPU version of PaddlePaddle, due to the large overhead of segmentation models, otherwise, it could be out of memory while running the models. For example, run the following command to install Paddle with pip for Linux, CUDA 10.2. ``` -python -m pip install paddlepaddle-gpu==2.4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple +python -m pip install paddlepaddle-gpu==3.0.0b0 -i https://pypi.tuna.tsinghua.edu.cn/simple ``` diff --git a/docs/install_cn.md b/docs/install_cn.md index e0809e36ac..259b058390 100644 --- a/docs/install_cn.md +++ b/docs/install_cn.md @@ -9,18 +9,18 @@ - pip/pip3(9.0.1+),64位版本 - CUDA >= 10.2 - cuDNN >= 7.6 -- PaddlePaddle (版本==2.5) +- PaddlePaddle (版本>=3.0.0b0) ## 2 本地安装说明 ### 2.1 安装PaddlePaddle -请参考[快速安装文档](https://www.paddlepaddle.org.cn/install/quick)或者[详细安装文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html),安装PaddlePaddle (要求不低于2.4版本,推荐安装最新版本)。 +请参考[快速安装文档](https://www.paddlepaddle.org.cn/install/quick)或者[详细安装文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html),安装PaddlePaddle (要求不低于3.0.0b0版本,推荐安装最新版本)。 比如Linux、CUDA 10.2,使用pip安装GPU版本,执行如下命令。 ``` -python -m pip install paddlepaddle-gpu==2.4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple +python -m pip install paddlepaddle-gpu==3.0.0b0 -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 使用如下命令验证PaddlePaddle是否安装成功,并且查看版本。 diff --git a/paddleseg/deploy/infer.py b/paddleseg/deploy/infer.py index 40038e1445..b143b80702 100644 --- a/paddleseg/deploy/infer.py +++ b/paddleseg/deploy/infer.py @@ -34,12 +34,12 @@ def transforms(self): return self._transforms @property - def model(self): - return os.path.join(self._dir, self.dic['Deploy']['model']) + def model_dir(self): + return self._dir @property - def params(self): - return os.path.join(self._dir, self.dic['Deploy']['params']) + def model_prefix(self): + return self.dic['Deploy']['model_prefix'] @staticmethod def load_transforms(t_list): diff --git a/paddleseg/models/backbones/efficientformerv2.py b/paddleseg/models/backbones/efficientformerv2.py index 57b310a29f..eb1547e952 100644 --- a/paddleseg/models/backbones/efficientformerv2.py +++ b/paddleseg/models/backbones/efficientformerv2.py @@ -203,7 +203,7 @@ def forward_tokens(self, x): def forward(self, x): x = self.patch_embed(x) x = self.forward_tokens(x) - if self.mode is not 'multi_scale': + if self.mode != 'multi_scale': x = [ paddle.concat( [ diff --git a/tools/export.py b/tools/export.py index 589b6af580..938b81a703 100644 --- a/tools/export.py +++ b/tools/export.py @@ -97,8 +97,7 @@ def main(args): # TODO add test config deploy_info = { 'Deploy': { - 'model': save_name + '.pdmodel', - 'params': save_name + '.pdiparams', + 'model_prefix': save_name, 'transforms': transforms, 'input_shape': shape, 'output_op': args.output_op, From deb357349837d9dd43d177d20a71c0e5596b9dd2 Mon Sep 17 00:00:00 2001 From: Bobholamovic Date: Wed, 24 Jul 2024 17:48:05 +0800 Subject: [PATCH 2/2] Fix compatibility --- README_CN.md | 2 +- README_EN.md | 2 +- deploy/python/collect_dynamic_shape.py | 24 ++--- deploy/python/infer.py | 12 ++- deploy/python/infer_dataset.py | 116 ++++++++++++------------- docs/install.md | 6 +- docs/install_cn.md | 6 +- paddleseg/deploy/infer.py | 24 ++++- 8 files changed, 109 insertions(+), 83 deletions(-) diff --git a/README_CN.md b/README_CN.md index 87d1084ef9..b62759c596 100644 --- a/README_CN.md +++ b/README_CN.md @@ -11,7 +11,7 @@ [![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE) [![Version](https://img.shields.io/github/release/PaddlePaddle/PaddleSeg.svg)](https://github.com/PaddlePaddle/PaddleSeg/releases) -![python version](https://img.shields.io/badge/python-3.8+-orange.svg) +![python version](https://img.shields.io/badge/python-3.6+-orange.svg) ![support os](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg) ![stars](https://img.shields.io/github/stars/PaddlePaddle/PaddleSeg?color=ccf) diff --git a/README_EN.md b/README_EN.md index 86085e6731..b600f9fd99 100644 --- a/README_EN.md +++ b/README_EN.md @@ -10,7 +10,7 @@ English | [简体中文](README_CN.md) [![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE) [![Version](https://img.shields.io/github/release/PaddlePaddle/PaddleSeg.svg)](https://github.com/PaddlePaddle/PaddleSeg/releases) -![python version](https://img.shields.io/badge/python-3.8+-orange.svg) +![python version](https://img.shields.io/badge/python-3.6+-orange.svg) ![support os](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg) ![stars](https://img.shields.io/github/stars/PaddlePaddle/PaddleSeg?color=ccf) diff --git a/deploy/python/collect_dynamic_shape.py b/deploy/python/collect_dynamic_shape.py index 4626fc3877..8916ea6c0c 100644 --- a/deploy/python/collect_dynamic_shape.py +++ b/deploy/python/collect_dynamic_shape.py @@ -16,6 +16,7 @@ import os import numpy as np +import paddle from paddle.inference import create_predictor from paddle.inference import Config as PredictConfig @@ -29,22 +30,20 @@ def parse_args(): parser = argparse.ArgumentParser(description='Test') - parser.add_argument( - "--config", - help="The deploy config generated by exporting model.", - type=str, - required=True) + parser.add_argument("--config", + help="The deploy config generated by exporting model.", + type=str, + required=True) parser.add_argument( '--image_path', help='The directory or path or file list of the images to be predicted.', type=str, required=True) - parser.add_argument( - '--dynamic_shape_path', - type=str, - default="./dynamic_shape.pbtxt", - help='The path to save dynamic shape.') + parser.add_argument('--dynamic_shape_path', + type=str, + default="./dynamic_shape.pbtxt", + help='The path to save dynamic shape.') return parser.parse_args() @@ -62,7 +61,10 @@ def collect_dynamic_shape(args): # prepare config cfg = DeployConfig(args.config) - pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) + if paddle.__version__.split('.')[0] == '2': + pred_cfg = PredictConfig(cfg.model, cfg.params) + else: + pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) pred_cfg.enable_use_gpu(1000, 0) pred_cfg.collect_shape_range_info(args.dynamic_shape_path) diff --git a/deploy/python/infer.py b/deploy/python/infer.py index f133d652d7..6f1373b4b9 100644 --- a/deploy/python/infer.py +++ b/deploy/python/infer.py @@ -16,6 +16,7 @@ import os import numpy as np +import paddle from paddle.inference import create_predictor, PrecisionType from paddle.inference import Config as PredictConfig @@ -52,7 +53,10 @@ def auto_tune(args, imgs, img_nums): num = min(len(imgs), img_nums) cfg = DeployConfig(args.cfg) - pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) + if paddle.__version__.split('.')[0] == '2': + pred_cfg = PredictConfig(cfg.model, cfg.params) + else: + pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) pred_cfg.enable_use_gpu(100, 0) if not args.print_detail: pred_cfg.disable_glog_info() @@ -139,7 +143,11 @@ def __init__(self, args): logger=logger) def _init_base_config(self): - self.pred_cfg = PredictConfig(self.cfg.model_dir, self.cfg.model_prefix) + if paddle.__version__.split('.')[0] == '2': + self.pred_cfg = PredictConfig(self.cfg.model, self.cfg.params) + else: + self.pred_cfg = PredictConfig(self.cfg.model_dir, + self.cfg.model_prefix) if not self.args.print_detail: self.pred_cfg.disable_glog_info() self.pred_cfg.enable_memory_optim() diff --git a/deploy/python/infer_dataset.py b/deploy/python/infer_dataset.py index 21364dd223..db38244e8f 100644 --- a/deploy/python/infer_dataset.py +++ b/deploy/python/infer_dataset.py @@ -32,13 +32,12 @@ def parse_args(): parser = argparse.ArgumentParser(description='Model Infer') - parser.add_argument( - "--config", - dest="cfg", - help="The config file.", - default=None, - type=str, - required=True) + parser.add_argument("--config", + dest="cfg", + help="The config file.", + default=None, + type=str, + required=True) parser.add_argument( '--dataset_type', @@ -53,11 +52,10 @@ def parse_args(): type=str, default=None, required=True) - parser.add_argument( - '--dataset_mode', - help='The dataset mode, such as train, val.', - type=str, - default="val") + parser.add_argument('--dataset_mode', + help='The dataset mode, such as train, val.', + type=str, + default="val") parser.add_argument( '--resize_width', help='Set the resize width to acclerate the test. In default, it is 0, ' @@ -70,11 +68,10 @@ def parse_args(): 'which means use the origin height.', type=int, default=0) - parser.add_argument( - '--batch_size', - help='Mini batch size of one gpu or cpu.', - type=int, - default=1) + parser.add_argument('--batch_size', + help='Mini batch size of one gpu or cpu.', + type=int, + default=1) parser.add_argument( '--device', @@ -88,51 +85,45 @@ def parse_args(): type=eval, choices=[True, False], help='Whether to use Nvidia TensorRT to accelerate prediction.') - parser.add_argument( - "--precision", - default="fp32", - type=str, - choices=["fp32", "fp16", "int8"], - help='The tensorrt precision.') + parser.add_argument("--precision", + default="fp32", + type=str, + choices=["fp32", "fp16", "int8"], + help='The tensorrt precision.') parser.add_argument( '--enable_auto_tune', default=False, type=eval, choices=[True, False], - help='Whether to enable tuned dynamic shape. We uses some images to collect ' + help= + 'Whether to enable tuned dynamic shape. We uses some images to collect ' 'the dynamic shape for trt sub graph, which avoids setting dynamic shape manually.' ) - parser.add_argument( - '--auto_tuned_shape_file', - type=str, - default="auto_tune_tmp.pbtxt", - help='The temp file to save tuned dynamic shape.') - parser.add_argument( - '--min_subgraph_size', - default=3, - type=int, - help='The min subgraph size in tensorrt prediction.') - - parser.add_argument( - '--cpu_threads', - default=10, - type=int, - help='Number of threads to predict when using cpu.') - parser.add_argument( - '--enable_mkldnn', - default=False, - type=eval, - choices=[True, False], - help='Enable to use mkldnn to speed up when using cpu.') - - parser.add_argument( - '--with_argmax', - help='Perform argmax operation on the predict result.', - action='store_true') - parser.add_argument( - '--print_detail', - help='Print GLOG information of Paddle Inference.', - action='store_true') + parser.add_argument('--auto_tuned_shape_file', + type=str, + default="auto_tune_tmp.pbtxt", + help='The temp file to save tuned dynamic shape.') + parser.add_argument('--min_subgraph_size', + default=3, + type=int, + help='The min subgraph size in tensorrt prediction.') + + parser.add_argument('--cpu_threads', + default=10, + type=int, + help='Number of threads to predict when using cpu.') + parser.add_argument('--enable_mkldnn', + default=False, + type=eval, + choices=[True, False], + help='Enable to use mkldnn to speed up when using cpu.') + + parser.add_argument('--with_argmax', + help='Perform argmax operation on the predict result.', + action='store_true') + parser.add_argument('--print_detail', + help='Print GLOG information of Paddle Inference.', + action='store_true') return parser.parse_args() @@ -152,10 +143,11 @@ def get_dataset(args): with codecs.open(args.cfg, 'r', 'utf-8') as file: dic = yaml.load(file, Loader=yaml.FullLoader) transforms_dic = dic['Deploy']['transforms'] - transforms_dic.insert(0, { - "type": "Resize", - 'target_size': [args.resize_width, args.resize_height] - }) + transforms_dic.insert( + 0, { + "type": "Resize", + 'target_size': [args.resize_width, args.resize_height] + }) transforms = DeployConfig.load_transforms(transforms_dic).transforms kwargs = { @@ -191,7 +183,10 @@ def auto_tune(args, dataset, img_nums): num = min(len(dataset), img_nums) cfg = DeployConfig(args.cfg) - pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) + if paddle.__version__.split('.')[0] == '2': + pred_cfg = PredictConfig(cfg.model, cfg.params) + else: + pred_cfg = PredictConfig(cfg.model_dir, cfg.model_prefix) pred_cfg.enable_use_gpu(100, 0) if not args.print_detail: pred_cfg.disable_glog_info() @@ -224,6 +219,7 @@ def auto_tune(args, dataset, img_nums): class DatasetPredictor(Predictor): + def __init__(self, args): super().__init__(args) diff --git a/docs/install.md b/docs/install.md index aa684edeb4..da3d4f0064 100644 --- a/docs/install.md +++ b/docs/install.md @@ -8,20 +8,20 @@ English | [简体中文](install_cn.md) - pip/pip3(9.0.1+),64-bit version - CUDA >= 10.2 - cuDNN >= 7.6 -- PaddlePaddle (the version >= 3.0.0b0) +- PaddlePaddle (the version >= 2.4) ## 2 Installation ### 2.1 Install PaddlePaddle -Please refer to the [installation doc](https://www.paddlepaddle.org.cn/documentation/docs/en/install/index_en.html) to install PaddlePaddle (the version >= 3.0.0b0). +Please refer to the [installation doc](https://www.paddlepaddle.org.cn/documentation/docs/en/install/index_en.html) to install PaddlePaddle (the version >= 2.4). Highly recommend you install the GPU version of PaddlePaddle, due to the large overhead of segmentation models, otherwise, it could be out of memory while running the models. For example, run the following command to install Paddle with pip for Linux, CUDA 10.2. ``` -python -m pip install paddlepaddle-gpu==3.0.0b0 -i https://pypi.tuna.tsinghua.edu.cn/simple +python -m pip install paddlepaddle-gpu==2.4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple ``` diff --git a/docs/install_cn.md b/docs/install_cn.md index 259b058390..e0809e36ac 100644 --- a/docs/install_cn.md +++ b/docs/install_cn.md @@ -9,18 +9,18 @@ - pip/pip3(9.0.1+),64位版本 - CUDA >= 10.2 - cuDNN >= 7.6 -- PaddlePaddle (版本>=3.0.0b0) +- PaddlePaddle (版本==2.5) ## 2 本地安装说明 ### 2.1 安装PaddlePaddle -请参考[快速安装文档](https://www.paddlepaddle.org.cn/install/quick)或者[详细安装文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html),安装PaddlePaddle (要求不低于3.0.0b0版本,推荐安装最新版本)。 +请参考[快速安装文档](https://www.paddlepaddle.org.cn/install/quick)或者[详细安装文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html),安装PaddlePaddle (要求不低于2.4版本,推荐安装最新版本)。 比如Linux、CUDA 10.2,使用pip安装GPU版本,执行如下命令。 ``` -python -m pip install paddlepaddle-gpu==3.0.0b0 -i https://pypi.tuna.tsinghua.edu.cn/simple +python -m pip install paddlepaddle-gpu==2.4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 使用如下命令验证PaddlePaddle是否安装成功,并且查看版本。 diff --git a/paddleseg/deploy/infer.py b/paddleseg/deploy/infer.py index b143b80702..7cfe4a7ec4 100644 --- a/paddleseg/deploy/infer.py +++ b/paddleseg/deploy/infer.py @@ -21,24 +21,44 @@ class DeployConfig: + def __init__(self, path): with codecs.open(path, 'r', 'utf-8') as file: self.dic = yaml.load(file, Loader=yaml.FullLoader) - self._transforms = self.load_transforms(self.dic['Deploy'][ - 'transforms']) + self._transforms = self.load_transforms( + self.dic['Deploy']['transforms']) self._dir = os.path.dirname(path) + self._is_old_format = 'model_prefix' not in self.dic['Deploy'] @property def transforms(self): return self._transforms + @property + def model(self): + if self._is_old_format: + return os.path.join(self._dir, self.dic['Deploy']['model']) + else: + return os.path.join(self._dir, + self.dic['Deploy']['model_prefix'] + '.pdmodel') + + @property + def params(self): + if self._is_old_format: + return os.path.join(self._dir, self.dic['Deploy']['params']) + else: + return os.path.join( + self._dir, self.dic['Deploy']['model_prefix'] + '.pdiparams') + @property def model_dir(self): return self._dir @property def model_prefix(self): + if self._is_old_format: + return self.dic['Deploy']['model'][:-8] return self.dic['Deploy']['model_prefix'] @staticmethod