Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Support saving and loading models in different formats #3758

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
24 changes: 13 additions & 11 deletions deploy/python/collect_dynamic_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatter自动更新,与主题无关。

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()

Expand All @@ -62,7 +61,10 @@ def collect_dynamic_shape(args):

# prepare config
cfg = DeployConfig(args.config)
pred_cfg = PredictConfig(cfg.model, cfg.params)
if paddle.__version__.split('.')[0] == '2':
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

兼容paddle 2.x

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)

Expand Down
12 changes: 10 additions & 2 deletions deploy/python/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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, cfg.params)
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()
Expand Down Expand Up @@ -139,7 +143,11 @@ def __init__(self, args):
logger=logger)

def _init_base_config(self):
self.pred_cfg = PredictConfig(self.cfg.model, self.cfg.params)
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()
Expand Down
116 changes: 56 additions & 60 deletions deploy/python/infer_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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, '
Expand All @@ -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',
Expand All @@ -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()

Expand All @@ -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 = {
Expand Down Expand Up @@ -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, cfg.params)
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()
Expand Down Expand Up @@ -224,6 +219,7 @@ def auto_tune(args, dataset, img_nums):


class DatasetPredictor(Predictor):

def __init__(self, args):
super().__init__(args)

Expand Down
28 changes: 24 additions & 4 deletions paddleseg/deploy/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,45 @@


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):
return os.path.join(self._dir, self.dic['Deploy']['model'])
if self._is_old_format:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

兼容旧版本导出模型格式。

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):
return os.path.join(self._dir, self.dic['Deploy']['params'])
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
def load_transforms(t_list):
Expand Down
2 changes: 1 addition & 1 deletion paddleseg/models/backbones/efficientformerv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

顺带修复bug,与PR主题无关。

x = [
paddle.concat(
[
Expand Down
3 changes: 1 addition & 2 deletions tools/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down