From 50e9014c5790375d075cb6b7b10cf28af6746494 Mon Sep 17 00:00:00 2001 From: RunningLeon Date: Fri, 8 Sep 2023 14:05:20 +0800 Subject: [PATCH] update --- ...ose-detection_simcc_onnxruntime_dynamic.py | 4 +++ mmdeploy/codebase/mmpose/codecs/__init__.py | 5 +++ .../codebase/mmpose/codecs/post_processing.py | 33 +++++++++++++++++++ .../codebase/mmpose/deploy/pose_detection.py | 6 +++- .../mmpose/deploy/pose_detection_model.py | 12 +++++-- .../codebase/mmpose/models/heads/__init__.py | 4 +-- .../mmpose/models/heads/simcc_head.py | 28 ++++++++++++++++ 7 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 mmdeploy/codebase/mmpose/codecs/__init__.py create mode 100644 mmdeploy/codebase/mmpose/codecs/post_processing.py create mode 100644 mmdeploy/codebase/mmpose/models/heads/simcc_head.py diff --git a/configs/mmpose/pose-detection_simcc_onnxruntime_dynamic.py b/configs/mmpose/pose-detection_simcc_onnxruntime_dynamic.py index 1ec49b90f9..6b9c3c891e 100644 --- a/configs/mmpose/pose-detection_simcc_onnxruntime_dynamic.py +++ b/configs/mmpose/pose-detection_simcc_onnxruntime_dynamic.py @@ -14,3 +14,7 @@ 0: 'batch' } }) + +codebase_config = dict( + export_postprocess=False # do not export get_simcc_maximum +) diff --git a/mmdeploy/codebase/mmpose/codecs/__init__.py b/mmdeploy/codebase/mmpose/codecs/__init__.py new file mode 100644 index 0000000000..ad861c8c27 --- /dev/null +++ b/mmdeploy/codebase/mmpose/codecs/__init__.py @@ -0,0 +1,5 @@ +# Copyright (c) OpenMMLab. All rights reserved. + +from .post_processing import get_simcc_maximum + +__all__ = ['get_simcc_maximum'] diff --git a/mmdeploy/codebase/mmpose/codecs/post_processing.py b/mmdeploy/codebase/mmpose/codecs/post_processing.py new file mode 100644 index 0000000000..aae5bc94be --- /dev/null +++ b/mmdeploy/codebase/mmpose/codecs/post_processing.py @@ -0,0 +1,33 @@ +# Copyright (c) OpenMMLab. All rights reserved. +import torch + + +def get_simcc_maximum(simcc_x: torch.Tensor, + simcc_y: torch.Tensor) -> torch.Tensor: + """Get maximum response location and value from simcc representations. + + rewrite to support `torch.Tensor` input type. + + Args: + simcc_x (torch.Tensor): x-axis SimCC in shape (N, K, Wx) + simcc_y (torch.Tensor): y-axis SimCC in shape (N, K, Wy) + + Returns: + tuple: + - locs (torch.Tensor): locations of maximum heatmap responses in shape + (N, K, 2) + - vals (torch.Tensor): values of maximum heatmap responses in shape + (N, K) + """ + N, K, _ = simcc_x.shape + simcc_x = simcc_x.flatten(0, 1) + simcc_y = simcc_y.flatten(0, 1) + x_locs = simcc_x.argmax(dim=1, keepdim=True) + y_locs = simcc_y.argmax(dim=1, keepdim=True) + locs = torch.cat((x_locs, y_locs), dim=1).to(torch.float32) + max_val_x, _ = simcc_x.max(dim=1, keepdim=True) + max_val_y, _ = simcc_y.max(dim=1, keepdim=True) + vals, _ = torch.cat([max_val_x, max_val_y], dim=1).min(dim=1) + locs = locs.reshape(N, K, 2) + vals = vals.reshape(N, K) + return locs, vals diff --git a/mmdeploy/codebase/mmpose/deploy/pose_detection.py b/mmdeploy/codebase/mmpose/deploy/pose_detection.py index 6584d995fb..86f2e4d09a 100644 --- a/mmdeploy/codebase/mmpose/deploy/pose_detection.py +++ b/mmdeploy/codebase/mmpose/deploy/pose_detection.py @@ -13,7 +13,8 @@ from mmengine.registry import Registry from mmdeploy.codebase.base import CODEBASE, BaseTask, MMCodebase -from mmdeploy.utils import Codebase, Task, get_input_shape, get_root_logger +from mmdeploy.utils import (Codebase, Task, get_codebase_config, + get_input_shape, get_root_logger) def process_model_config( @@ -362,6 +363,9 @@ def get_postprocess(self, *args, **kwargs) -> Dict: params['post_process'] = 'megvii' params['modulate_kernel'] = self.model_cfg.kernel_sizes[-1] elif codec.type == 'SimCCLabel': + export_postprocess = get_codebase_config(self.deploy_cfg).get( + 'export_postprocess', False) + params['export_postprocess'] = export_postprocess component = 'SimCCLabelDecode' elif codec.type == 'RegressionLabel': component = 'DeepposeRegressionHeadDecode' diff --git a/mmdeploy/codebase/mmpose/deploy/pose_detection_model.py b/mmdeploy/codebase/mmpose/deploy/pose_detection_model.py index 1686a089fe..be10a80e32 100644 --- a/mmdeploy/codebase/mmpose/deploy/pose_detection_model.py +++ b/mmdeploy/codebase/mmpose/deploy/pose_detection_model.py @@ -101,12 +101,20 @@ def forward(self, if self.model_cfg.model.type == 'YOLODetector': return self.pack_yolox_pose_result(batch_outputs, data_samples) + codebase_cfg = get_codebase_config(self.deploy_cfg) codec = self.model_cfg.codec if isinstance(codec, (list, tuple)): codec = codec[-1] if codec.type == 'SimCCLabel': - batch_pred_x, batch_pred_y = batch_outputs - preds = self.head.decode((batch_pred_x, batch_pred_y)) + export_postprocess = codebase_cfg.get('export_postprocess', False) + if export_postprocess: + keypoints, scores = [_.cpu().numpy() for _ in batch_outputs] + preds = [ + InstanceData(keypoints=keypoints, keypoint_scores=scores) + ] + else: + batch_pred_x, batch_pred_y = batch_outputs + preds = self.head.decode((batch_pred_x, batch_pred_y)) elif codec.type in ['RegressionLabel', 'IntegralRegressionLabel']: preds = self.head.decode(batch_outputs) else: diff --git a/mmdeploy/codebase/mmpose/models/heads/__init__.py b/mmdeploy/codebase/mmpose/models/heads/__init__.py index 9fb6239cdb..10bd18a0d9 100644 --- a/mmdeploy/codebase/mmpose/models/heads/__init__.py +++ b/mmdeploy/codebase/mmpose/models/heads/__init__.py @@ -1,4 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. -from . import mspn_head, yolox_pose_head # noqa: F401,F403 +from . import mspn_head, simcc_head, yolox_pose_head # noqa: F401,F403 -__all__ = ['mspn_head', 'yolox_pose_head'] +__all__ = ['mspn_head', 'yolox_pose_head', 'simcc_head'] diff --git a/mmdeploy/codebase/mmpose/models/heads/simcc_head.py b/mmdeploy/codebase/mmpose/models/heads/simcc_head.py new file mode 100644 index 0000000000..d568e42d62 --- /dev/null +++ b/mmdeploy/codebase/mmpose/models/heads/simcc_head.py @@ -0,0 +1,28 @@ +# Copyright (c) OpenMMLab. All rights reserved. +from mmdeploy.codebase.mmpose.codecs import get_simcc_maximum +from mmdeploy.core import FUNCTION_REWRITER +from mmdeploy.utils import get_codebase_config + + +@FUNCTION_REWRITER.register_rewriter('mmpose.models.heads.RTMCCHead.forward') +@FUNCTION_REWRITER.register_rewriter('mmpose.models.heads.SimCCHead.forward') +def simcc_head__forward(self, feats): + """Rewrite `forward` of SimCCHead for default backend. + + Args: + feats (tuple[Tensor]): Input features. + Returns: + key-points (torch.Tensor): Output keypoints in + shape of (N, K, 3) + """ + ctx = FUNCTION_REWRITER.get_context() + simcc_x, simcc_y = ctx.origin_func(self, feats) + codebase_cfg = get_codebase_config(ctx.cfg) + export_postprocess = codebase_cfg.get('export_postprocess', False) + if not export_postprocess: + return simcc_x, simcc_y + assert self.decoder.use_dark is False, \ + 'Do not support SimCCLabel with use_dark=True' + pts, scores = get_simcc_maximum(simcc_x, simcc_y) + pts /= self.decoder.simcc_split_ratio + return pts, scores