From d8755b530ae24ae163d3adb670c7543a0865e66c Mon Sep 17 00:00:00 2001 From: chenxinfeng Date: Thu, 20 Apr 2023 12:05:45 +0000 Subject: [PATCH 1/5] embed the preprocess into the easydeploy onnx/tensorrt model --- projects/easydeploy/tools/export_onnx.py | 34 ++++++++++++++++++++++-- projects/easydeploy/tools/image-demo.py | 25 +---------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/projects/easydeploy/tools/export_onnx.py b/projects/easydeploy/tools/export_onnx.py index b937cc8a7..e3160e403 100644 --- a/projects/easydeploy/tools/export_onnx.py +++ b/projects/easydeploy/tools/export_onnx.py @@ -7,8 +7,9 @@ import onnx import torch +from torch.nn.parameter import Parameter from mmdet.apis import init_detector -from mmengine.config import ConfigDict +from mmengine.config import Config, ConfigDict from mmengine.logging import print_log from mmengine.utils.path import mkdir_or_exist @@ -23,6 +24,30 @@ warnings.filterwarnings(action='ignore', category=ResourceWarning) +def preprocess(config:Config, model:torch.nn.Module): + data_preprocess = config.get('model', {}).get('data_preprocessor', {}) + mean = data_preprocess.get('mean', [0., 0., 0.]) + std = data_preprocess.get('std', [1., 1., 1.]) + mean_value = torch.tensor(mean, dtype=torch.float32).reshape(1, 3, 1, 1) + std_value = torch.tensor(std, dtype=torch.float32).reshape(1, 3, 1, 1) + + class PreProcess(torch.nn.Module): + def __init__(self): + super().__init__() + self.mean_value = Parameter(mean_value, requires_grad=False) + self.std_value = Parameter(std_value, requires_grad=False) + self.core_model = model + + def forward(self, x:torch.Tensor): + assert x.ndim==4 + x = x.float() + y = (x - self.mean_value) / self.std_value + y = self.core_model(y) + return y + + return PreProcess().eval() + + def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('config', help='Config file') @@ -110,8 +135,13 @@ def main(): baseModel=baseModel, backend=backend, postprocess_cfg=postprocess_cfg) deploy_model.eval() + # embed the preprocess into the model + cfg = Config.fromfile(args.config) + deploy_model = preprocess(cfg, deploy_model) + deploy_model.eval() + fake_input = torch.randn(args.batch_size, 3, - *args.img_size).to(args.device) + *args.img_size).type(torch.uint8).to(args.device) # dry run deploy_model(fake_input) diff --git a/projects/easydeploy/tools/image-demo.py b/projects/easydeploy/tools/image-demo.py index c85f31a02..edcd5d9f7 100644 --- a/projects/easydeploy/tools/image-demo.py +++ b/projects/easydeploy/tools/image-demo.py @@ -33,27 +33,6 @@ def parse_args(): return args -def preprocess(config): - data_preprocess = config.get('model', {}).get('data_preprocessor', {}) - mean = data_preprocess.get('mean', [0., 0., 0.]) - std = data_preprocess.get('std', [1., 1., 1.]) - mean = torch.tensor(mean, dtype=torch.float32).reshape(1, 3, 1, 1) - std = torch.tensor(std, dtype=torch.float32).reshape(1, 3, 1, 1) - - class PreProcess(torch.nn.Module): - - def __init__(self): - super().__init__() - - def forward(self, x): - x = x[None].float() - x -= mean.to(x.device) - x /= std.to(x.device) - return x - - return PreProcess().eval() - - def main(): args = parse_args() @@ -80,8 +59,6 @@ def main(): test_pipeline[0] = ConfigDict({'type': 'mmdet.LoadImageFromNDArray'}) test_pipeline = Compose(test_pipeline) - pre_pipeline = preprocess(cfg) - if not args.show: path.mkdir_or_exist(args.out_dir) @@ -102,7 +79,7 @@ def main(): device=args.device) scale_factor = samples.get('scale_factor', [1., 1]) scale_factor = torch.asarray(scale_factor * 2, device=args.device) - data = pre_pipeline(data).to(args.device) + data = data[None].type(torch.uint8).to(args.device) result = model(data) if source_type['is_dir']: From 1a01316f168dba75b835f3b0db904bc4dfb05023 Mon Sep 17 00:00:00 2001 From: chenxinfeng Date: Thu, 20 Apr 2023 12:43:44 +0000 Subject: [PATCH 2/5] fix linting --- projects/easydeploy/tools/export_onnx.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/projects/easydeploy/tools/export_onnx.py b/projects/easydeploy/tools/export_onnx.py index e3160e403..18882c5cb 100644 --- a/projects/easydeploy/tools/export_onnx.py +++ b/projects/easydeploy/tools/export_onnx.py @@ -24,7 +24,7 @@ warnings.filterwarnings(action='ignore', category=ResourceWarning) -def preprocess(config:Config, model:torch.nn.Module): +def preprocess(config: Config, model: torch.nn.Module): data_preprocess = config.get('model', {}).get('data_preprocessor', {}) mean = data_preprocess.get('mean', [0., 0., 0.]) std = data_preprocess.get('std', [1., 1., 1.]) @@ -38,8 +38,8 @@ def __init__(self): self.std_value = Parameter(std_value, requires_grad=False) self.core_model = model - def forward(self, x:torch.Tensor): - assert x.ndim==4 + def forward(self, x: torch.Tensor): + assert x.ndim == 4 x = x.float() y = (x - self.mean_value) / self.std_value y = self.core_model(y) From 88db2930eb28bfe1242480d5ef1371e86dad1aab Mon Sep 17 00:00:00 2001 From: chenxinfeng Date: Thu, 20 Apr 2023 12:52:22 +0000 Subject: [PATCH 3/5] fix linting by isort and yapf --- projects/easydeploy/tools/export_onnx.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/projects/easydeploy/tools/export_onnx.py b/projects/easydeploy/tools/export_onnx.py index 18882c5cb..629ca0981 100644 --- a/projects/easydeploy/tools/export_onnx.py +++ b/projects/easydeploy/tools/export_onnx.py @@ -7,11 +7,11 @@ import onnx import torch -from torch.nn.parameter import Parameter from mmdet.apis import init_detector from mmengine.config import Config, ConfigDict from mmengine.logging import print_log from mmengine.utils.path import mkdir_or_exist +from torch.nn.parameter import Parameter # Add MMYOLO ROOT to sys.path sys.path.append(str(Path(__file__).resolve().parents[3])) @@ -32,6 +32,7 @@ def preprocess(config: Config, model: torch.nn.Module): std_value = torch.tensor(std, dtype=torch.float32).reshape(1, 3, 1, 1) class PreProcess(torch.nn.Module): + def __init__(self): super().__init__() self.mean_value = Parameter(mean_value, requires_grad=False) From d383dad208ed13c25dfc75f4fdd507da6aa70c74 Mon Sep 17 00:00:00 2001 From: chenxinfeng Date: Fri, 28 Apr 2023 15:21:24 +0000 Subject: [PATCH 4/5] change deploy input from uint8 to float --- projects/easydeploy/tools/export_onnx.py | 2 +- projects/easydeploy/tools/image-demo.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/easydeploy/tools/export_onnx.py b/projects/easydeploy/tools/export_onnx.py index 629ca0981..028dead21 100644 --- a/projects/easydeploy/tools/export_onnx.py +++ b/projects/easydeploy/tools/export_onnx.py @@ -142,7 +142,7 @@ def main(): deploy_model.eval() fake_input = torch.randn(args.batch_size, 3, - *args.img_size).type(torch.uint8).to(args.device) + *args.img_size).float().to(args.device) # dry run deploy_model(fake_input) diff --git a/projects/easydeploy/tools/image-demo.py b/projects/easydeploy/tools/image-demo.py index edcd5d9f7..bed64166f 100644 --- a/projects/easydeploy/tools/image-demo.py +++ b/projects/easydeploy/tools/image-demo.py @@ -79,7 +79,7 @@ def main(): device=args.device) scale_factor = samples.get('scale_factor', [1., 1]) scale_factor = torch.asarray(scale_factor * 2, device=args.device) - data = data[None].type(torch.uint8).to(args.device) + data = data[None].float().to(args.device) result = model(data) if source_type['is_dir']: From 9e766b4e186b0adfa05be6679a860e8865904bee Mon Sep 17 00:00:00 2001 From: chenxinfeng Date: Fri, 28 Apr 2023 16:07:30 +0000 Subject: [PATCH 5/5] fix bug --- projects/easydeploy/tools/export_onnx.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/projects/easydeploy/tools/export_onnx.py b/projects/easydeploy/tools/export_onnx.py index 028dead21..ec79b8e60 100644 --- a/projects/easydeploy/tools/export_onnx.py +++ b/projects/easydeploy/tools/export_onnx.py @@ -30,6 +30,7 @@ def preprocess(config: Config, model: torch.nn.Module): std = data_preprocess.get('std', [1., 1., 1.]) mean_value = torch.tensor(mean, dtype=torch.float32).reshape(1, 3, 1, 1) std_value = torch.tensor(std, dtype=torch.float32).reshape(1, 3, 1, 1) + device = next(model.parameters()).device class PreProcess(torch.nn.Module): @@ -46,7 +47,7 @@ def forward(self, x: torch.Tensor): y = self.core_model(y) return y - return PreProcess().eval() + return PreProcess().to(device).eval() def parse_args(): @@ -142,7 +143,7 @@ def main(): deploy_model.eval() fake_input = torch.randn(args.batch_size, 3, - *args.img_size).float().to(args.device) + *args.img_size).to(args.device) # dry run deploy_model(fake_input)