From daf3524173a8dc933e60683b5738adf64b9c1749 Mon Sep 17 00:00:00 2001 From: lzzyzlbb <287246233@qq.com> Date: Mon, 7 Mar 2022 15:29:40 +0800 Subject: [PATCH] add benchmark_infer (#590) * add benchmark_infer --- ppgan/models/base_model.py | 8 +++++--- ppgan/models/firstorder_model.py | 2 +- ppgan/models/styleganv2_model.py | 7 +++++-- test_tipc/configs/CycleGAN/train_infer_python.txt | 10 ++++++---- test_tipc/configs/FOMM/train_infer_python.txt | 2 +- test_tipc/configs/Pix2pix/train_infer_python.txt | 10 ++++++---- test_tipc/configs/StyleGANv2/train_infer_python.txt | 10 ++++++---- test_tipc/configs/basicvsr/train_infer_python.txt | 10 ++++++---- test_tipc/configs/msvsr/train_infer_python.txt | 8 +++++--- test_tipc/test_train_inference_python.sh | 2 +- tools/export_model.py | 9 ++++++++- 11 files changed, 50 insertions(+), 28 deletions(-) diff --git a/ppgan/models/base_model.py b/ppgan/models/base_model.py index 8f8cf90..d4cd3ac 100755 --- a/ppgan/models/base_model.py +++ b/ppgan/models/base_model.py @@ -183,7 +183,7 @@ class BaseModel(ABC): for param in net.parameters(): param.trainable = requires_grad - def export_model(self, export_model, output_dir=None, inputs_size=[], export_serving_model=False): + def export_model(self, export_model, output_dir=None, inputs_size=[], export_serving_model=False, model_name=None): inputs_num = 0 for net in export_model: input_spec = [ @@ -196,11 +196,13 @@ class BaseModel(ABC): input_spec=input_spec) if output_dir is None: output_dir = 'inference_model' + if model_name is None: + model_name = '{}_{}'.format(self.__class__.__name__.lower(), + net["name"]) paddle.jit.save( static_model, os.path.join( - output_dir, '{}_{}'.format(self.__class__.__name__.lower(), - net["name"]))) + output_dir, model_name)) if export_serving_model: from paddle_serving_client.io import inference_model_to_serving model_name = '{}_{}'.format(self.__class__.__name__.lower(), diff --git a/ppgan/models/firstorder_model.py b/ppgan/models/firstorder_model.py index fb5afe2..e134862 100755 --- a/ppgan/models/firstorder_model.py +++ b/ppgan/models/firstorder_model.py @@ -214,7 +214,7 @@ class FirstOrderModel(BaseModel): kp_driving=kp_norm) return out['prediction'] - def export_model(self, export_model=None, output_dir=None, inputs_size=[], export_serving_model=False): + def export_model(self, export_model=None, output_dir=None, inputs_size=[], export_serving_model=False, model_name=None): source = paddle.rand(shape=inputs_size[0], dtype='float32') driving = paddle.rand(shape=inputs_size[1], dtype='float32') diff --git a/ppgan/models/styleganv2_model.py b/ppgan/models/styleganv2_model.py index c517864..2845258 100644 --- a/ppgan/models/styleganv2_model.py +++ b/ppgan/models/styleganv2_model.py @@ -310,13 +310,16 @@ class StyleGAN2Model(BaseModel): export_model=None, output_dir=None, inputs_size=[[1, 1, 512], [1, 1]], - export_serving_model=False): + export_serving_model=False, + model_name=None): infer_generator = self.InferGenerator() infer_generator.set_generator(self.nets['gen']) style = paddle.rand(shape=inputs_size[0], dtype='float32') truncation = paddle.rand(shape=inputs_size[1], dtype='float32') if output_dir is None: output_dir = 'inference_model' + if model_name is None: + model_name = "stylegan2model_gen" paddle.jit.save(infer_generator, - os.path.join(output_dir, "stylegan2model_gen"), + os.path.join(output_dir, model_name), input_spec=[style, truncation]) diff --git a/test_tipc/configs/CycleGAN/train_infer_python.txt b/test_tipc/configs/CycleGAN/train_infer_python.txt index ed3611b..b5a7f4e 100644 --- a/test_tipc/configs/CycleGAN/train_infer_python.txt +++ b/test_tipc/configs/CycleGAN/train_infer_python.txt @@ -4,7 +4,7 @@ python:python3.7 gpu_list:0|0,1 ## auto_cast:null -epochs:lite_train_lite_infer=1|whole_train_whole_infer=200 +epochs:lite_train_lite_infer=1|lite_train_whole_infer=1|whole_train_whole_infer=200 output_dir:./output/ dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 pretrained_model:null @@ -27,13 +27,13 @@ null:null ===========================infer_params=========================== --output_dir:./output/ load:null -norm_export:tools/export_model.py -c configs/cyclegan_horse2zebra.yaml --inputs_size="-1,3,-1,-1;-1,3,-1,-1" --load +norm_export:tools/export_model.py -c configs/cyclegan_horse2zebra.yaml --inputs_size="-1,3,-1,-1;-1,3,-1,-1" --model_name inference --load quant_export:null fpgm_export:null distill_export:null export1:null export2:null -inference_dir:cycleganmodel_netG_A +inference_dir:inference train_model:./inference/cyclegan_horse2zebra/cycleganmodel_netG_A infer_export:null infer_quant:False @@ -54,4 +54,6 @@ batch_size:1 fp_items:fp32 epoch:1 --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile -flags:null \ No newline at end of file +flags:null +===========================infer_benchmark_params========================== +random_infer_input:[{float32,[3,256,256]}] \ No newline at end of file diff --git a/test_tipc/configs/FOMM/train_infer_python.txt b/test_tipc/configs/FOMM/train_infer_python.txt index 172852a..dcae393 100644 --- a/test_tipc/configs/FOMM/train_infer_python.txt +++ b/test_tipc/configs/FOMM/train_infer_python.txt @@ -4,7 +4,7 @@ python:python3.7 gpu_list:0 ## auto_cast:null -epochs:lite_train_lite_infer=1|whole_train_whole_infer=100 +epochs:lite_train_lite_infer=1|lite_train_whole_infer=1|whole_train_whole_infer=100 output_dir:./output/ dataset.train.batch_size:lite_train_lite_infer=8|whole_train_whole_infer=8 pretrained_model:null diff --git a/test_tipc/configs/Pix2pix/train_infer_python.txt b/test_tipc/configs/Pix2pix/train_infer_python.txt index 7865857..32188b6 100644 --- a/test_tipc/configs/Pix2pix/train_infer_python.txt +++ b/test_tipc/configs/Pix2pix/train_infer_python.txt @@ -4,7 +4,7 @@ python:python3.7 gpu_list:0 ## auto_cast:null -epochs:lite_train_lite_infer=10|whole_train_whole_infer=200 +epochs:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 output_dir:./output/ dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 pretrained_model:null @@ -27,13 +27,13 @@ null:null ===========================infer_params=========================== --output_dir:./output/ load:null -norm_export:tools/export_model.py -c configs/pix2pix_facades.yaml --inputs_size="-1,3,-1,-1" --load +norm_export:tools/export_model.py -c configs/pix2pix_facades.yaml --inputs_size="-1,3,-1,-1" --model_name inference --load quant_export:null fpgm_export:null distill_export:null export1:null export2:null -inference_dir:pix2pixmodel_netG +inference_dir:inference train_model:./inference/pix2pix_facade/pix2pixmodel_netG infer_export:null infer_quant:False @@ -54,4 +54,6 @@ batch_size:1 fp_items:fp32 epoch:10 --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile -flags:null \ No newline at end of file +flags:null +===========================infer_benchmark_params========================== +random_infer_input:[{float32,[3,256,256]}] diff --git a/test_tipc/configs/StyleGANv2/train_infer_python.txt b/test_tipc/configs/StyleGANv2/train_infer_python.txt index 2d81aa0..423773b 100644 --- a/test_tipc/configs/StyleGANv2/train_infer_python.txt +++ b/test_tipc/configs/StyleGANv2/train_infer_python.txt @@ -4,7 +4,7 @@ python:python3.7 gpu_list:0 ## auto_cast:null -total_iters:lite_train_lite_infer=10|whole_train_whole_infer=800 +total_iters:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=800 output_dir:./output/ dataset.train.batch_size:lite_train_lite_infer=3|whole_train_whole_infer=3 pretrained_model:null @@ -27,13 +27,13 @@ null:null ===========================infer_params=========================== --output_dir:./output/ load:null -norm_export:tools/export_model.py -c configs/stylegan_v2_256_ffhq.yaml --inputs_size="1,1,512;1,1" --load +norm_export:tools/export_model.py -c configs/stylegan_v2_256_ffhq.yaml --inputs_size="1,1,512;1,1" --model_name inference --load quant_export:null fpgm_export:null distill_export:null export1:null export2:null -inference_dir:stylegan2model_gen +inference_dir:inference train_model:./inference/stylegan2/stylegan2model_gen infer_export:null infer_quant:False @@ -54,4 +54,6 @@ batch_size:8|16 fp_items:fp32 epoch:100 --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile -flags:null \ No newline at end of file +flags:null +===========================infer_benchmark_params========================== +random_infer_input:[{float32,[1,512]}, {float32,[1]}] \ No newline at end of file diff --git a/test_tipc/configs/basicvsr/train_infer_python.txt b/test_tipc/configs/basicvsr/train_infer_python.txt index 7d29c47..14316a1 100644 --- a/test_tipc/configs/basicvsr/train_infer_python.txt +++ b/test_tipc/configs/basicvsr/train_infer_python.txt @@ -4,7 +4,7 @@ python:python3.7 gpu_list:0 ## auto_cast:null -total_iters:lite_train_lite_infer=10|whole_train_whole_infer=200 +total_iters:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 output_dir:./output/ dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 pretrained_model:null @@ -27,13 +27,13 @@ null:null ===========================infer_params=========================== --output_dir:./output/ load:null -norm_export:tools/export_model.py -c configs/basicvsr_reds.yaml --inputs_size="1,6,3,180,320" --load +norm_export:tools/export_model.py -c configs/basicvsr_reds.yaml --inputs_size="1,6,3,180,320" --model_name inference --load quant_export:null fpgm_export:null distill_export:null export1:null export2:null -inference_dir:basicvsrmodel_generator +inference_dir:inference train_model:./inference/basicvsr/basicvsrmodel_generator infer_export:null infer_quant:False @@ -54,4 +54,6 @@ batch_size:2|4 fp_items:fp32 total_iters:50 --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile -flags:null \ No newline at end of file +flags:null +===========================infer_benchmark_params========================== +random_infer_input:[{float32,[6,3,180,320]}] \ No newline at end of file diff --git a/test_tipc/configs/msvsr/train_infer_python.txt b/test_tipc/configs/msvsr/train_infer_python.txt index 2e4ccef..288290e 100644 --- a/test_tipc/configs/msvsr/train_infer_python.txt +++ b/test_tipc/configs/msvsr/train_infer_python.txt @@ -4,7 +4,7 @@ python:python3.7 gpu_list:0 ## auto_cast:null -total_iters:lite_train_lite_infer=10|whole_train_whole_infer=200 +total_iters:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 output_dir:./output/ dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 pretrained_model:null @@ -27,13 +27,13 @@ null:null ===========================infer_params=========================== --output_dir:./output/ load:null -norm_export:tools/export_model.py -c configs/msvsr_reds.yaml --inputs_size="1,2,3,180,320" --load +norm_export:tools/export_model.py -c configs/msvsr_reds.yaml --inputs_size="1,2,3,180,320" --model_name inference --load quant_export:null fpgm_export:null distill_export:null export1:null export2:null -inference_dir:multistagevsrmodel_generator +inference_dir:inference train_model:./inference/msvsr/multistagevsrmodel_generator infer_export:null infer_quant:False @@ -49,3 +49,5 @@ null:null null:null --benchmark:True null:null +===========================infer_benchmark_params========================== +random_infer_input:[{float32,[2,3,180,320]}] diff --git a/test_tipc/test_train_inference_python.sh b/test_tipc/test_train_inference_python.sh index 63ec8e6..bc54fac 100644 --- a/test_tipc/test_train_inference_python.sh +++ b/test_tipc/test_train_inference_python.sh @@ -73,7 +73,7 @@ save_log_key=$(func_parser_key "${lines[48]}") infer_key1=$(func_parser_key "${lines[50]}") infer_value1=$(func_parser_value "${lines[50]}") -LOG_PATH="./test_tipc/output" +LOG_PATH="./test_tipc/output/${model_name}" mkdir -p ${LOG_PATH} status_log="${LOG_PATH}/results_python.log" diff --git a/tools/export_model.py b/tools/export_model.py index 2c055c1..ee2e563 100644 --- a/tools/export_model.py +++ b/tools/export_model.py @@ -57,6 +57,12 @@ def parse_args(): type=bool, help="export serving model.", ) + parser.add_argument( + "--model_name", + default=None, + type=str, + help="model_name.", + ) args = parser.parse_args() return args @@ -70,7 +76,8 @@ def main(args, cfg): for net_name, net in model.nets.items(): if net_name in state_dicts: net.set_state_dict(state_dicts[net_name]) - model.export_model(cfg.export_model, args.output_dir, inputs_size, args.export_serving_model) + model.export_model(cfg.export_model, args.output_dir, inputs_size, + args.export_serving_model, args.model_name) if __name__ == "__main__": -- GitLab