From 3366d269c7ff06f91a726cf22bd1e5cece3c5d39 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Fri, 24 Mar 2023 21:22:30 +0800 Subject: [PATCH 01/18] fix reg test yolox --- configs/mmdet/_base_/base_openvino_dynamic-640x640.py | 6 ++++++ .../mmdet/detection/detection_openvino_dynamic-640x640.py | 1 + tests/regression/mmdet.yml | 4 +++- 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 configs/mmdet/_base_/base_openvino_dynamic-640x640.py create mode 100644 configs/mmdet/detection/detection_openvino_dynamic-640x640.py diff --git a/configs/mmdet/_base_/base_openvino_dynamic-640x640.py b/configs/mmdet/_base_/base_openvino_dynamic-640x640.py new file mode 100644 index 0000000000..29bb6712da --- /dev/null +++ b/configs/mmdet/_base_/base_openvino_dynamic-640x640.py @@ -0,0 +1,6 @@ +_base_ = ['./base_dynamic.py', '../../_base_/backends/openvino.py'] + +onnx_config = dict(input_shape=None) + +backend_config = dict( + model_inputs=[dict(opt_shapes=dict(input=[1, 3, 640, 640]))]) diff --git a/configs/mmdet/detection/detection_openvino_dynamic-640x640.py b/configs/mmdet/detection/detection_openvino_dynamic-640x640.py new file mode 100644 index 0000000000..bf3bea574e --- /dev/null +++ b/configs/mmdet/detection/detection_openvino_dynamic-640x640.py @@ -0,0 +1 @@ +_base_ = ['../_base_/base_openvino_dynamic-640x640.py'] diff --git a/tests/regression/mmdet.yml b/tests/regression/mmdet.yml index 4f2a938f19..e60aa4be06 100644 --- a/tests/regression/mmdet.yml +++ b/tests/regression/mmdet.yml @@ -250,7 +250,9 @@ models: - *pipeline_ort_dynamic_fp32 - *pipeline_trt_dynamic_fp32 - *pipeline_ncnn_static_fp32 - - *pipeline_openvino_dynamic_fp32 + - deploy_config: configs/mmdet/detection/detection_openvino_dynamic-640x640.py + convert_image: *convert_image + backend_test: False - name: Faster R-CNN metafile: configs/faster_rcnn/metafile.yml From d95b3023d7b48a4caabb996584a2405b77d8daa1 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Fri, 24 Mar 2023 21:32:21 +0800 Subject: [PATCH 02/18] fix detr --- .../mmdet/models/dense_heads/detr_head.py | 30 ++---- .../mmdet/models/detectors/__init__.py | 5 +- .../mmdet/models/detectors/base_detr.py | 94 +++++++++++++++++++ 3 files changed, 107 insertions(+), 22 deletions(-) create mode 100644 mmdeploy/codebase/mmdet/models/detectors/base_detr.py diff --git a/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py b/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py index 56ff40b7a0..f41816c246 100644 --- a/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py +++ b/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py @@ -9,28 +9,17 @@ @FUNCTION_REWRITER.register_rewriter( - 'mmdet.models.dense_heads.DETRHead.forward_single') -def detrhead__forward_single__default(self, x, img_metas): - """forward_single of DETRHead. + 'mmdet.models.dense_heads.DETRHead.forward') +def detrhead__forward__default(self, hidden_states): + """forward of DETRHead. Ease the mask computation """ - batch_size = x.size(0) - - x = self.input_proj(x) - # interpolate masks to have the same spatial shape with x - masks = x.new_zeros((batch_size, x.size(-2), x.size(-1))).to(torch.bool) - - # position encoding - pos_embed = self.positional_encoding(masks) # [bs, embed_dim, h, w] - # outs_dec: [nb_dec, bs, num_query, embed_dim] - outs_dec, _ = self.transformer(x, masks, self.query_embedding.weight, - pos_embed) - all_cls_scores = self.fc_cls(outs_dec) - all_bbox_preds = self.fc_reg(self.activate( - self.reg_ffn(outs_dec))).sigmoid() - return all_cls_scores, all_bbox_preds + layers_cls_scores = self.fc_cls(hidden_states) + layers_bbox_preds = self.fc_reg( + self.activate(self.reg_ffn(hidden_states))).sigmoid() + return layers_cls_scores, layers_bbox_preds @FUNCTION_REWRITER.register_rewriter( @@ -42,8 +31,8 @@ def detrhead__predict_by_feat__default(self, rescale: bool = True): """Rewrite `predict_by_feat` of `FoveaHead` for default backend.""" from mmdet.structures.bbox import bbox_cxcywh_to_xyxy - cls_scores = all_cls_scores_list[-1][-1] - bbox_preds = all_bbox_preds_list[-1][-1] + cls_scores = all_cls_scores_list[-1] + bbox_preds = all_bbox_preds_list[-1] img_shape = batch_img_metas[0]['img_shape'] max_per_img = self.test_cfg.get('max_per_img', len(cls_scores[0])) @@ -90,3 +79,4 @@ def detrhead__predict_by_feat__default(self, det_bboxes = torch.cat((det_bboxes, scores.unsqueeze(-1)), -1) return det_bboxes, det_labels + diff --git a/mmdeploy/codebase/mmdet/models/detectors/__init__.py b/mmdeploy/codebase/mmdet/models/detectors/__init__.py index 5b9df70a08..9d08e2dccd 100644 --- a/mmdeploy/codebase/mmdet/models/detectors/__init__.py +++ b/mmdeploy/codebase/mmdet/models/detectors/__init__.py @@ -1,4 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from . import single_stage, single_stage_instance_seg, two_stage +from . import base_detr, single_stage, single_stage_instance_seg, two_stage + +__all__ = ['base_detr', 'single_stage', 'single_stage_instance_seg', 'two_stage'] -__all__ = ['single_stage', 'single_stage_instance_seg', 'two_stage'] diff --git a/mmdeploy/codebase/mmdet/models/detectors/base_detr.py b/mmdeploy/codebase/mmdet/models/detectors/base_detr.py new file mode 100644 index 0000000000..ba3afeec1f --- /dev/null +++ b/mmdeploy/codebase/mmdet/models/detectors/base_detr.py @@ -0,0 +1,94 @@ +# Copyright (c) OpenMMLab. All rights reserved. +import copy + +import torch +from mmdet.models.detectors.base import ForwardResults +from mmdet.structures import DetDataSample +from mmdet.structures.det_data_sample import OptSampleList + +from mmdeploy.core import FUNCTION_REWRITER, mark +from mmdeploy.utils import is_dynamic_shape + + +@mark( + 'detr_predict', inputs=['input'], outputs=['dets', 'labels', 'masks']) +def __predict_impl(self, batch_inputs, data_samples, rescale): + """Rewrite and adding mark for `predict`. + + Encapsulate this function for rewriting `predict` of DetectionTransformer. + 1. Add mark for DetectionTransformer. + 2. Support both dynamic and static export to onnx. + """ + img_feats = self.extract_feat(batch_inputs) + head_inputs_dict = self.forward_transformer(img_feats, + data_samples) + results_list = self.bbox_head.predict( + **head_inputs_dict, + rescale=rescale, + batch_data_samples=data_samples) + return results_list + + +@torch.fx.wrap +def _set_metainfo(data_samples, img_shape): + """Set the metainfo. + + Code in this function cannot be traced by fx. + """ + + # fx can not trace deepcopy correctly + data_samples = copy.deepcopy(data_samples) + if data_samples is None: + data_samples = [DetDataSample()] + + # note that we can not use `set_metainfo`, deepcopy would crash the + # onnx trace. + for data_sample in data_samples: + data_sample.set_field( + name='img_shape', value=img_shape, field_type='metainfo') + + return data_samples + + +@FUNCTION_REWRITER.register_rewriter( + 'mmdet.models.detectors.base_detr.DetectionTransformer.predict') +def detection_transformer__predict(self, + batch_inputs: torch.Tensor, + data_samples: OptSampleList = None, + rescale: bool = True, + **kwargs) -> ForwardResults: + """Rewrite `predict` for default backend. + + Support configured dynamic/static shape for model input and return + detection result as Tensor instead of numpy array. + + Args: + batch_inputs (Tensor): Inputs with shape (N, C, H, W). + data_samples (List[:obj:`DetDataSample`]): The Data + Samples. It usually includes information such as + `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`. + rescale (Boolean): rescale result or not. + + Returns: + tuple[Tensor]: Detection results of the + input images. + - dets (Tensor): Classification bboxes and scores. + Has a shape (num_instances, 5) + - labels (Tensor): Labels of bboxes, has a shape + (num_instances, ). + """ + ctx = FUNCTION_REWRITER.get_context() + + deploy_cfg = ctx.cfg + + # get origin input shape as tensor to support onnx dynamic shape + is_dynamic_flag = is_dynamic_shape(deploy_cfg) + img_shape = torch._shape_as_tensor(batch_inputs)[2:] + if not is_dynamic_flag: + img_shape = [int(val) for val in img_shape] + + # set the metainfo + data_samples = _set_metainfo(data_samples, img_shape) + + return __predict_impl(self, batch_inputs, data_samples, rescale) + From 50c812f25dcc0b21211c5dd3570e43755f2a681f Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Mon, 27 Mar 2023 14:39:41 +0800 Subject: [PATCH 03/18] fix rtmdet-sdk reg --- tests/regression/mmdet.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/regression/mmdet.yml b/tests/regression/mmdet.yml index e60aa4be06..b7cf41aa22 100644 --- a/tests/regression/mmdet.yml +++ b/tests/regression/mmdet.yml @@ -337,7 +337,7 @@ models: - configs/rtmdet/rtmdet_s_8xb32-300e_coco.py pipelines: - *pipeline_ort_dynamic_fp32 - - deploy_config: configs/mmdet/detection/detection_tensorrt_static-640x640.py + - deploy_config: configs/mmdet/detection/detection_tensorrt_dynamic-64x64-800x800.py convert_image: *convert_image backend_test: *default_backend_test sdk_config: *sdk_dynamic From 54646810663ba2e1b692c3048d70e6a0770c5894 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Tue, 28 Mar 2023 17:32:32 +0800 Subject: [PATCH 04/18] fix conformer precision --- .../mmcls/models/backbones/__init__.py | 1 + .../mmcls/models/backbones/conformer.py | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 mmdeploy/codebase/mmcls/models/backbones/conformer.py diff --git a/mmdeploy/codebase/mmcls/models/backbones/__init__.py b/mmdeploy/codebase/mmcls/models/backbones/__init__.py index 62d47810cc..4e52aae1d7 100644 --- a/mmdeploy/codebase/mmcls/models/backbones/__init__.py +++ b/mmdeploy/codebase/mmcls/models/backbones/__init__.py @@ -1,3 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. +from . import conformer # noqa: F401,F403 from . import shufflenet_v2 # noqa: F401,F403 from . import vision_transformer # noqa: F401,F403 diff --git a/mmdeploy/codebase/mmcls/models/backbones/conformer.py b/mmdeploy/codebase/mmcls/models/backbones/conformer.py new file mode 100644 index 0000000000..fc6accdabd --- /dev/null +++ b/mmdeploy/codebase/mmcls/models/backbones/conformer.py @@ -0,0 +1,37 @@ +# Copyright (c) OpenMMLab. All rights reserved. +import torch.nn.functional as F + +from mmdeploy.core import FUNCTION_REWRITER +from mmdeploy.utils import Backend + + +@FUNCTION_REWRITER.register_rewriter( + func_name='mmcls.models.backbones.conformer.FCUUp.forward', + backend=Backend.TENSORRT.value) +def fcuup__forward__tensorrt(self, x, H, W): + """Rewrite `forward` of FCUUp used in conformer. + + FCUUp uses nearest interpolate while trt interpolate default + is bilinear. Set interpolate mode explicitly. + + Args: + ctx (ContextCaller): The context with additional information. + self (FCUUp): The instance of the class InvertedResidual. + x (Tensor): Input features of shape (N, Cin, H, W). + H (int): Feature map height + W (int): Feature map width + Returns: + out (Tensor): A feature map output from FCUUp. The tensor + shape (N, Cout, H * self.up_stride, W * self.up_stride). + """ + B, _, C = x.shape + # [N, 197, 384] -> [N, 196, 384] -> [N, 384, 196] -> [N, 384, 14, 14] + if self.with_cls_token: + x_r = x[:, 1:].transpose(1, 2).reshape(B, C, H, W) + else: + x_r = x.transpose(1, 2).reshape(B, C, H, W) + + x_r = self.act(self.bn(self.conv_project(x_r))) + + return F.interpolate( + x_r, size=(H * self.up_stride, W * self.up_stride), mode='nearest') From 3e7008902cc04646d73fb003c2536605c056746c Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Wed, 29 Mar 2023 19:47:15 +0800 Subject: [PATCH 05/18] add conformer_cls sdk --- .../mmdeploy/codebase/mmcls/conformer_cls.cpp | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp diff --git a/csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp b/csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp new file mode 100644 index 0000000000..0ab12867ca --- /dev/null +++ b/csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp @@ -0,0 +1,62 @@ +// Copyright (c) OpenMMLab. All rights reserved. + +#include +#include + +#include "mmdeploy/codebase/mmcls/mmcls.h" +#include "mmdeploy/core/tensor.h" +#include "mmdeploy/core/utils/device_utils.h" +#include "mmdeploy/core/utils/formatter.h" +#include "mmdeploy/experimental/module_adapter.h" + +using std::vector; + +namespace mmdeploy::mmcls { + +class ConformerHead : public MMClassification { + public: + explicit ConformerHead(const Value& cfg) : MMClassification(cfg) {} + Result operator()(const Value& infer_res) { + auto output = infer_res["output"].get(); + + if (!(output.shape().size() >= 2 && output.data_type() == DataType::kFLOAT)) { + MMDEPLOY_ERROR("unsupported `output` tensor, shape: {}, dtype: {}", output.shape(), + (int)output.data_type()); + return Status(eNotSupported); + } + + auto class_num = (int)output.shape(1); + OUTCOME_TRY(auto _scores, MakeAvailableOnDevice(output, kHost, stream())); + OUTCOME_TRY(stream().Wait()); + + return GetLabels(_scores, class_num); + } + + private: + Value GetLabels(const Tensor& scores, int class_num) const { + auto scores_data = scores.data(); + auto topk = std::min(topk_, class_num); + Labels output; + output.reserve(topk); + std::vector idx(class_num); + iota(begin(idx), end(idx), 0); + partial_sort(begin(idx), begin(idx) + topk, end(idx), + [&](int i, int j) { return scores_data[i] > scores_data[j]; }); + for (int i = 0; i < topk; ++i) { + float score = 0.f; + score = scores_data[idx[i]]; + + output.push_back({idx[i], score}); + } + return to_value(std::move(output)); + } + + private: + static constexpr const auto kHost = Device{0}; + + int topk_{1}; +}; + +MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMClassification, ConformerHead); + +} // namespace mmdeploy::mmcls From 71aaddb3bda789ccb33c1552f2b72041c453068f Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Wed, 29 Mar 2023 20:18:01 +0800 Subject: [PATCH 06/18] add mmcls ut --- .../test_mmcls/test_mmcls_models.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/test_codebase/test_mmcls/test_mmcls_models.py b/tests/test_codebase/test_mmcls/test_mmcls_models.py index a1037fc0c4..f11b0acd82 100644 --- a/tests/test_codebase/test_mmcls/test_mmcls_models.py +++ b/tests/test_codebase/test_mmcls/test_mmcls_models.py @@ -29,6 +29,14 @@ def get_invertedresidual_model(): return model +def get_fcuup_model(): + from mmcls.models.backbones.conformer import FCUUp + model = FCUUp(16, 16, 16) + + model.requires_grad_(False) + return model + + def get_vit_backbone(): from mmcls.models.classifiers.image import ImageClassifier model = ImageClassifier( @@ -279,3 +287,52 @@ def test_shift_windows_msa_cls(backend_type: Backend): model_inputs=rewrite_inputs, deploy_cfg=deploy_cfg, run_with_backend=False) + + +@pytest.mark.parametrize('backend_type', [Backend.TENSORRT]) +def test_fcuup__forward(backend_type: Backend): + + check_backend(backend_type, True) + model = get_fcuup_model() + model.cpu().eval() + if backend_type.value == 'tensorrt': + deploy_cfg = Config( + dict( + backend_config=dict( + type=backend_type.value, + model_inputs=[ + dict( + input_shapes=dict( + input=dict( + min_shape=[1, 197, 16], + opt_shape=[1, 197, 16], + max_shape=[1, 197, 16]))) + ]), + onnx_config=dict( + input_shape=[197, 16], output_names=['output']), + codebase_config=dict(type='mmcls', task='Classification'))) + else: + deploy_cfg = Config( + dict( + backend_config=dict(type=backend_type.value), + onnx_config=dict(input_shape=None, output_names=['output']), + codebase_config=dict(type='mmcls', task='Classification'))) + + imgs = torch.rand((1, 197, 16)) + model_outputs = model.forward(imgs, 14, 14) + wrapped_model = WrapModel(model, 'forward') + rewrite_inputs = {'x': imgs, 'H': 14, 'W': 14} + rewrite_outputs, is_backend_output = get_rewrite_outputs( + wrapped_model=wrapped_model, + model_inputs=rewrite_inputs, + deploy_cfg=deploy_cfg, + run_with_backend=False) + + if isinstance(rewrite_outputs, dict): + rewrite_outputs = rewrite_outputs['output'] + for model_output, rewrite_output in zip(model_outputs, rewrite_outputs): + model_output = model_output.cpu().numpy() + if isinstance(rewrite_output, torch.Tensor): + rewrite_output = rewrite_output.cpu().numpy() + assert np.allclose( + model_output, rewrite_output, rtol=1e-03, atol=1e-05) From 2156808452b6b3d405685128d2030b773c3aaea9 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Wed, 29 Mar 2023 20:31:47 +0800 Subject: [PATCH 07/18] fix detr ut --- tests/test_codebase/test_mmdet/test_mmdet_models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_codebase/test_mmdet/test_mmdet_models.py b/tests/test_codebase/test_mmdet/test_mmdet_models.py index 376806e793..c2af6c38f0 100644 --- a/tests/test_codebase/test_mmdet/test_mmdet_models.py +++ b/tests/test_codebase/test_mmdet/test_mmdet_models.py @@ -2009,9 +2009,9 @@ def test_detrhead__predict_by_feat(backend_type: Backend, ir_type: str): deploy_cfg = get_deploy_cfg(backend_type, ir_type) seed_everything(1234) - cls_score = [[torch.rand(1, 100, 5) for i in range(5, 0, -1)]] + cls_score = [torch.rand(1, 100, 5) for i in range(5, 0, -1)] seed_everything(5678) - bboxes = [[torch.rand(1, 100, 4) for i in range(5, 0, -1)]] + bboxes = [torch.rand(1, 100, 4) for i in range(5, 0, -1)] # to get outputs of onnx model after rewrite img_metas[0]['img_shape'] = torch.Tensor([s, s]) From e1ce38cb24b6f26ebe1b96784cba3e8e214e27b4 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Wed, 29 Mar 2023 20:41:07 +0800 Subject: [PATCH 08/18] fix detr ut --- .../test_mmdet/test_mmdet_models.py | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_codebase/test_mmdet/test_mmdet_models.py b/tests/test_codebase/test_mmdet/test_mmdet_models.py index c2af6c38f0..bebc2bb32e 100644 --- a/tests/test_codebase/test_mmdet/test_mmdet_models.py +++ b/tests/test_codebase/test_mmdet/test_mmdet_models.py @@ -1995,7 +1995,7 @@ def test_mlvl_point_generator__single_level_grid_priors__tensorrt( @pytest.mark.parametrize('backend_type, ir_type', [(Backend.ONNXRUNTIME, 'onnx')]) def test_detrhead__predict_by_feat(backend_type: Backend, ir_type: str): - """Test predict_by_feat rewrite of base dense head.""" + """Test predict_by_feat rewrite of detr head.""" check_backend(backend_type) dense_head = get_detrhead_model() dense_head.cpu().eval() @@ -2030,6 +2030,31 @@ def test_detrhead__predict_by_feat(backend_type: Backend, ir_type: str): assert rewrite_outputs is not None +@pytest.mark.parametrize('backend_type, ir_type', + [(Backend.ONNXRUNTIME, 'onnx')]) +def test_detrhead__forward(backend_type: Backend, ir_type: str): + """Test forward rewrite of detr.""" + check_backend(backend_type) + dense_head = get_detrhead_model() + dense_head.cpu().eval() + + deploy_cfg = get_deploy_cfg(backend_type, ir_type) + + seed_everything(1234) + hidden_states = torch.rand(1, 16, 4) + + # to get outputs of onnx model after rewrite + wrapped_model = WrapModel(dense_head, 'forward') + rewrite_inputs = {'hidden_states': hidden_states} + rewrite_outputs, _ = get_rewrite_outputs( + wrapped_model=wrapped_model, + model_inputs=rewrite_inputs, + deploy_cfg=deploy_cfg, + run_with_backend=False) + + assert rewrite_outputs is not None + + def get_solo_head_model(): test_cfg = Config( dict( From ce09ad317b6dbe3be84c64244149816b2598b197 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Wed, 29 Mar 2023 20:56:32 +0800 Subject: [PATCH 09/18] fix lint --- mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py | 1 - mmdeploy/codebase/mmdet/models/detectors/__init__.py | 4 ++-- mmdeploy/codebase/mmdet/models/detectors/base_detr.py | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py b/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py index f41816c246..0919b197cf 100644 --- a/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py +++ b/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py @@ -79,4 +79,3 @@ def detrhead__predict_by_feat__default(self, det_bboxes = torch.cat((det_bboxes, scores.unsqueeze(-1)), -1) return det_bboxes, det_labels - diff --git a/mmdeploy/codebase/mmdet/models/detectors/__init__.py b/mmdeploy/codebase/mmdet/models/detectors/__init__.py index 9d08e2dccd..32271acbd0 100644 --- a/mmdeploy/codebase/mmdet/models/detectors/__init__.py +++ b/mmdeploy/codebase/mmdet/models/detectors/__init__.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. from . import base_detr, single_stage, single_stage_instance_seg, two_stage -__all__ = ['base_detr', 'single_stage', 'single_stage_instance_seg', 'two_stage'] - +__all__ = ['base_detr', 'single_stage', 'single_stage_instance_seg', + 'two_stage'] diff --git a/mmdeploy/codebase/mmdet/models/detectors/base_detr.py b/mmdeploy/codebase/mmdet/models/detectors/base_detr.py index ba3afeec1f..128a955420 100644 --- a/mmdeploy/codebase/mmdet/models/detectors/base_detr.py +++ b/mmdeploy/codebase/mmdet/models/detectors/base_detr.py @@ -91,4 +91,3 @@ def detection_transformer__predict(self, data_samples = _set_metainfo(data_samples, img_shape) return __predict_impl(self, batch_inputs, data_samples, rescale) - From 8da76b5a4f6767c2844af14501c9ce7e80da195b Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Wed, 29 Mar 2023 21:07:47 +0800 Subject: [PATCH 10/18] fix yapf --- mmdeploy/codebase/mmdet/models/detectors/__init__.py | 5 +++-- mmdeploy/codebase/mmdet/models/detectors/base_detr.py | 10 +++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/mmdeploy/codebase/mmdet/models/detectors/__init__.py b/mmdeploy/codebase/mmdet/models/detectors/__init__.py index 32271acbd0..2c0a2f3ed5 100644 --- a/mmdeploy/codebase/mmdet/models/detectors/__init__.py +++ b/mmdeploy/codebase/mmdet/models/detectors/__init__.py @@ -1,5 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. from . import base_detr, single_stage, single_stage_instance_seg, two_stage -__all__ = ['base_detr', 'single_stage', 'single_stage_instance_seg', - 'two_stage'] +__all__ = [ + 'base_detr', 'single_stage', 'single_stage_instance_seg', 'two_stage' +] diff --git a/mmdeploy/codebase/mmdet/models/detectors/base_detr.py b/mmdeploy/codebase/mmdet/models/detectors/base_detr.py index 128a955420..3531c9183c 100644 --- a/mmdeploy/codebase/mmdet/models/detectors/base_detr.py +++ b/mmdeploy/codebase/mmdet/models/detectors/base_detr.py @@ -10,8 +10,7 @@ from mmdeploy.utils import is_dynamic_shape -@mark( - 'detr_predict', inputs=['input'], outputs=['dets', 'labels', 'masks']) +@mark('detr_predict', inputs=['input'], outputs=['dets', 'labels', 'masks']) def __predict_impl(self, batch_inputs, data_samples, rescale): """Rewrite and adding mark for `predict`. @@ -20,12 +19,9 @@ def __predict_impl(self, batch_inputs, data_samples, rescale): 2. Support both dynamic and static export to onnx. """ img_feats = self.extract_feat(batch_inputs) - head_inputs_dict = self.forward_transformer(img_feats, - data_samples) + head_inputs_dict = self.forward_transformer(img_feats, data_samples) results_list = self.bbox_head.predict( - **head_inputs_dict, - rescale=rescale, - batch_data_samples=data_samples) + **head_inputs_dict, rescale=rescale, batch_data_samples=data_samples) return results_list From c4f9308b375bf0b3fa50f02d40a336edd70a32da Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Thu, 30 Mar 2023 11:30:16 +0800 Subject: [PATCH 11/18] fix cls sdk --- .../mmdeploy/codebase/mmcls/conformer_cls.cpp | 62 ------------------- csrc/mmdeploy/codebase/mmcls/linear_cls.cpp | 2 + 2 files changed, 2 insertions(+), 62 deletions(-) delete mode 100644 csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp diff --git a/csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp b/csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp deleted file mode 100644 index 0ab12867ca..0000000000 --- a/csrc/mmdeploy/codebase/mmcls/conformer_cls.cpp +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) OpenMMLab. All rights reserved. - -#include -#include - -#include "mmdeploy/codebase/mmcls/mmcls.h" -#include "mmdeploy/core/tensor.h" -#include "mmdeploy/core/utils/device_utils.h" -#include "mmdeploy/core/utils/formatter.h" -#include "mmdeploy/experimental/module_adapter.h" - -using std::vector; - -namespace mmdeploy::mmcls { - -class ConformerHead : public MMClassification { - public: - explicit ConformerHead(const Value& cfg) : MMClassification(cfg) {} - Result operator()(const Value& infer_res) { - auto output = infer_res["output"].get(); - - if (!(output.shape().size() >= 2 && output.data_type() == DataType::kFLOAT)) { - MMDEPLOY_ERROR("unsupported `output` tensor, shape: {}, dtype: {}", output.shape(), - (int)output.data_type()); - return Status(eNotSupported); - } - - auto class_num = (int)output.shape(1); - OUTCOME_TRY(auto _scores, MakeAvailableOnDevice(output, kHost, stream())); - OUTCOME_TRY(stream().Wait()); - - return GetLabels(_scores, class_num); - } - - private: - Value GetLabels(const Tensor& scores, int class_num) const { - auto scores_data = scores.data(); - auto topk = std::min(topk_, class_num); - Labels output; - output.reserve(topk); - std::vector idx(class_num); - iota(begin(idx), end(idx), 0); - partial_sort(begin(idx), begin(idx) + topk, end(idx), - [&](int i, int j) { return scores_data[i] > scores_data[j]; }); - for (int i = 0; i < topk; ++i) { - float score = 0.f; - score = scores_data[idx[i]]; - - output.push_back({idx[i], score}); - } - return to_value(std::move(output)); - } - - private: - static constexpr const auto kHost = Device{0}; - - int topk_{1}; -}; - -MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMClassification, ConformerHead); - -} // namespace mmdeploy::mmcls diff --git a/csrc/mmdeploy/codebase/mmcls/linear_cls.cpp b/csrc/mmdeploy/codebase/mmcls/linear_cls.cpp index 3e1f171e3f..6d22a67b3a 100644 --- a/csrc/mmdeploy/codebase/mmcls/linear_cls.cpp +++ b/csrc/mmdeploy/codebase/mmcls/linear_cls.cpp @@ -85,6 +85,8 @@ class LinearClsHead : public MMClassification { }; MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMClassification, LinearClsHead); +using ConformerHead = LinearClsHead; +MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMClassification, ConformerHead); class CropBox { public: From 263dc3dbb66242fc5fd3bf69a738557955056f8c Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Thu, 30 Mar 2023 11:41:30 +0800 Subject: [PATCH 12/18] fix detr_head rewriter --- .../mmdet/models/dense_heads/detr_head.py | 14 ----------- .../test_mmdet/test_mmdet_models.py | 25 ------------------- 2 files changed, 39 deletions(-) diff --git a/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py b/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py index 0919b197cf..08121bdfbb 100644 --- a/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py +++ b/mmdeploy/codebase/mmdet/models/dense_heads/detr_head.py @@ -8,20 +8,6 @@ from mmdeploy.core import FUNCTION_REWRITER -@FUNCTION_REWRITER.register_rewriter( - 'mmdet.models.dense_heads.DETRHead.forward') -def detrhead__forward__default(self, hidden_states): - """forward of DETRHead. - - Ease the mask computation - """ - - layers_cls_scores = self.fc_cls(hidden_states) - layers_bbox_preds = self.fc_reg( - self.activate(self.reg_ffn(hidden_states))).sigmoid() - return layers_cls_scores, layers_bbox_preds - - @FUNCTION_REWRITER.register_rewriter( 'mmdet.models.dense_heads.DETRHead.predict_by_feat') def detrhead__predict_by_feat__default(self, diff --git a/tests/test_codebase/test_mmdet/test_mmdet_models.py b/tests/test_codebase/test_mmdet/test_mmdet_models.py index bebc2bb32e..1979f7df28 100644 --- a/tests/test_codebase/test_mmdet/test_mmdet_models.py +++ b/tests/test_codebase/test_mmdet/test_mmdet_models.py @@ -2030,31 +2030,6 @@ def test_detrhead__predict_by_feat(backend_type: Backend, ir_type: str): assert rewrite_outputs is not None -@pytest.mark.parametrize('backend_type, ir_type', - [(Backend.ONNXRUNTIME, 'onnx')]) -def test_detrhead__forward(backend_type: Backend, ir_type: str): - """Test forward rewrite of detr.""" - check_backend(backend_type) - dense_head = get_detrhead_model() - dense_head.cpu().eval() - - deploy_cfg = get_deploy_cfg(backend_type, ir_type) - - seed_everything(1234) - hidden_states = torch.rand(1, 16, 4) - - # to get outputs of onnx model after rewrite - wrapped_model = WrapModel(dense_head, 'forward') - rewrite_inputs = {'hidden_states': hidden_states} - rewrite_outputs, _ = get_rewrite_outputs( - wrapped_model=wrapped_model, - model_inputs=rewrite_inputs, - deploy_cfg=deploy_cfg, - run_with_backend=False) - - assert rewrite_outputs is not None - - def get_solo_head_model(): test_cfg = Config( dict( From 03d6a24c9d27b43a7bdc63041d458e4f21f448d5 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Thu, 30 Mar 2023 12:31:57 +0800 Subject: [PATCH 13/18] fix interpolate --- .../mmcls/models/backbones/__init__.py | 1 - .../mmcls/models/backbones/conformer.py | 37 -------------- mmdeploy/pytorch/functions/interpolate.py | 2 +- .../test_mmcls/test_mmcls_models.py | 49 ------------------- 4 files changed, 1 insertion(+), 88 deletions(-) delete mode 100644 mmdeploy/codebase/mmcls/models/backbones/conformer.py diff --git a/mmdeploy/codebase/mmcls/models/backbones/__init__.py b/mmdeploy/codebase/mmcls/models/backbones/__init__.py index 4e52aae1d7..62d47810cc 100644 --- a/mmdeploy/codebase/mmcls/models/backbones/__init__.py +++ b/mmdeploy/codebase/mmcls/models/backbones/__init__.py @@ -1,4 +1,3 @@ # Copyright (c) OpenMMLab. All rights reserved. -from . import conformer # noqa: F401,F403 from . import shufflenet_v2 # noqa: F401,F403 from . import vision_transformer # noqa: F401,F403 diff --git a/mmdeploy/codebase/mmcls/models/backbones/conformer.py b/mmdeploy/codebase/mmcls/models/backbones/conformer.py deleted file mode 100644 index fc6accdabd..0000000000 --- a/mmdeploy/codebase/mmcls/models/backbones/conformer.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) OpenMMLab. All rights reserved. -import torch.nn.functional as F - -from mmdeploy.core import FUNCTION_REWRITER -from mmdeploy.utils import Backend - - -@FUNCTION_REWRITER.register_rewriter( - func_name='mmcls.models.backbones.conformer.FCUUp.forward', - backend=Backend.TENSORRT.value) -def fcuup__forward__tensorrt(self, x, H, W): - """Rewrite `forward` of FCUUp used in conformer. - - FCUUp uses nearest interpolate while trt interpolate default - is bilinear. Set interpolate mode explicitly. - - Args: - ctx (ContextCaller): The context with additional information. - self (FCUUp): The instance of the class InvertedResidual. - x (Tensor): Input features of shape (N, Cin, H, W). - H (int): Feature map height - W (int): Feature map width - Returns: - out (Tensor): A feature map output from FCUUp. The tensor - shape (N, Cout, H * self.up_stride, W * self.up_stride). - """ - B, _, C = x.shape - # [N, 197, 384] -> [N, 196, 384] -> [N, 384, 196] -> [N, 384, 14, 14] - if self.with_cls_token: - x_r = x[:, 1:].transpose(1, 2).reshape(B, C, H, W) - else: - x_r = x.transpose(1, 2).reshape(B, C, H, W) - - x_r = self.act(self.bn(self.conv_project(x_r))) - - return F.interpolate( - x_r, size=(H * self.up_stride, W * self.up_stride), mode='nearest') diff --git a/mmdeploy/pytorch/functions/interpolate.py b/mmdeploy/pytorch/functions/interpolate.py index 39424b8a39..10340d37b7 100644 --- a/mmdeploy/pytorch/functions/interpolate.py +++ b/mmdeploy/pytorch/functions/interpolate.py @@ -81,7 +81,7 @@ def interpolate__tensorrt( size: Optional[Union[int, Tuple[int], Tuple[int, int], Tuple[int, int, int]]] = None, scale_factor: Optional[Union[float, Tuple[float]]] = None, - mode: str = 'bilinear', + mode: str = 'nearest', align_corners: Optional[bool] = None, recompute_scale_factor: Optional[bool] = None, ): diff --git a/tests/test_codebase/test_mmcls/test_mmcls_models.py b/tests/test_codebase/test_mmcls/test_mmcls_models.py index f11b0acd82..ba6a37b5e7 100644 --- a/tests/test_codebase/test_mmcls/test_mmcls_models.py +++ b/tests/test_codebase/test_mmcls/test_mmcls_models.py @@ -287,52 +287,3 @@ def test_shift_windows_msa_cls(backend_type: Backend): model_inputs=rewrite_inputs, deploy_cfg=deploy_cfg, run_with_backend=False) - - -@pytest.mark.parametrize('backend_type', [Backend.TENSORRT]) -def test_fcuup__forward(backend_type: Backend): - - check_backend(backend_type, True) - model = get_fcuup_model() - model.cpu().eval() - if backend_type.value == 'tensorrt': - deploy_cfg = Config( - dict( - backend_config=dict( - type=backend_type.value, - model_inputs=[ - dict( - input_shapes=dict( - input=dict( - min_shape=[1, 197, 16], - opt_shape=[1, 197, 16], - max_shape=[1, 197, 16]))) - ]), - onnx_config=dict( - input_shape=[197, 16], output_names=['output']), - codebase_config=dict(type='mmcls', task='Classification'))) - else: - deploy_cfg = Config( - dict( - backend_config=dict(type=backend_type.value), - onnx_config=dict(input_shape=None, output_names=['output']), - codebase_config=dict(type='mmcls', task='Classification'))) - - imgs = torch.rand((1, 197, 16)) - model_outputs = model.forward(imgs, 14, 14) - wrapped_model = WrapModel(model, 'forward') - rewrite_inputs = {'x': imgs, 'H': 14, 'W': 14} - rewrite_outputs, is_backend_output = get_rewrite_outputs( - wrapped_model=wrapped_model, - model_inputs=rewrite_inputs, - deploy_cfg=deploy_cfg, - run_with_backend=False) - - if isinstance(rewrite_outputs, dict): - rewrite_outputs = rewrite_outputs['output'] - for model_output, rewrite_output in zip(model_outputs, rewrite_outputs): - model_output = model_output.cpu().numpy() - if isinstance(rewrite_output, torch.Tensor): - rewrite_output = rewrite_output.cpu().numpy() - assert np.allclose( - model_output, rewrite_output, rtol=1e-03, atol=1e-05) From 67c44c6478bbf25fef36490bdb28dd369349a782 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Thu, 30 Mar 2023 15:17:15 +0800 Subject: [PATCH 14/18] complement the mmdet ut --- .../test_mmdet/data/detr_model.json | 129 ++++++++++++++++++ .../test_mmdet/test_mmdet_models.py | 39 ++++++ 2 files changed, 168 insertions(+) create mode 100644 tests/test_codebase/test_mmdet/data/detr_model.json diff --git a/tests/test_codebase/test_mmdet/data/detr_model.json b/tests/test_codebase/test_mmdet/data/detr_model.json new file mode 100644 index 0000000000..0d4c2ba6b6 --- /dev/null +++ b/tests/test_codebase/test_mmdet/data/detr_model.json @@ -0,0 +1,129 @@ +{ + "type": "DETR", + "num_queries": 100, + "data_preprocessor": { + "type": "DetDataPreprocessor", + "mean": [123.675, 116.28, 103.53], + "std": [58.395, 57.12, 57.375], + "bgr_to_rgb": true, + "pad_size_divisor": 1 + }, + "backbone": { + "type": "ResNet", + "depth": 50, + "num_stages": 4, + "out_indices": [3], + "frozen_stages": 1, + "norm_cfg": { + "type": "BN", + "requires_grad": false + }, + "norm_eval": true, + "style": "pytorch", + "init_cfg": { + "type": "Pretrained", + "checkpoint": "torchvision://resnet50" + } + }, + "neck": { + "type": "ChannelMapper", + "in_channels": [2048], + "kernel_size": 1, + "out_channels": 256, + "num_outs": 1 + }, + "encoder": { + "num_layers": 6, + "layer_cfg": { + "self_attn_cfg": { + "embed_dims": 256, + "num_heads": 8, + "dropout": 0.1, + "batch_first": true + }, + "ffn_cfg": { + "embed_dims": 256, + "feedforward_channels": 2048, + "num_fcs": 2, + "ffn_drop": 0.1, + "act_cfg": { + "type": "ReLU", + "inplace": true + } + } + } + }, + "decoder": { + "num_layers": 6, + "layer_cfg": { + "self_attn_cfg": { + "embed_dims": 256, + "num_heads": 8, + "dropout": 0.1, + "batch_first": true + }, + "cross_attn_cfg": { + "embed_dims": 256, + "num_heads": 8, + "dropout": 0.1, + "batch_first": true + }, + "ffn_cfg": { + "embed_dims": 256, + "feedforward_channels": 2048, + "num_fcs": 2, + "ffn_drop": 0.1, + "act_cfg": { + "type": "ReLU", + "inplace": true + } + } + }, + "return_intermediate": true + }, + "positional_encoding": { + "num_feats": 128, + "normalize": true + }, + "bbox_head": { + "type": "DETRHead", + "num_classes": 80, + "embed_dims": 256, + "loss_cls": { + "type": "CrossEntropyLoss", + "bg_cls_weight": 0.1, + "use_sigmoid": false, + "loss_weight": 1.0, + "class_weight": 1.0 + }, + "loss_bbox": { + "type": "L1Loss", + "loss_weight": 5.0 + }, + "loss_iou": { + "type": "GIoULoss", + "loss_weight": 2.0 + } + }, + "train_cfg": { + "assigner": { + "type": + "HungarianAssigner", + "match_costs": [{ + "type": "ClassificationCost", + "weight": 1.0 + }, { + "type": "BBoxL1Cost", + "weight": 5.0, + "box_format": "xywh" + }, { + "type": "IoUCost", + "iou_mode": "giou", + "weight": 2.0 + }] + } + }, + "test_cfg": { + "max_per_img": 100 + } +} diff --git a/tests/test_codebase/test_mmdet/test_mmdet_models.py b/tests/test_codebase/test_mmdet/test_mmdet_models.py index 1979f7df28..109f41a728 100644 --- a/tests/test_codebase/test_mmdet/test_mmdet_models.py +++ b/tests/test_codebase/test_mmdet/test_mmdet_models.py @@ -691,6 +691,45 @@ def test_forward_of_base_detector(model_cfg_path, backend): assert rewrite_outputs is not None +@pytest.mark.parametrize('backend', [Backend.ONNXRUNTIME]) +@pytest.mark.parametrize( + 'model_cfg_path', ['tests/test_codebase/test_mmdet/data/detr_model.json']) +def test_predict_of_detr_detector(model_cfg_path, backend): + check_backend(backend) + deploy_cfg = Config( + dict( + backend_config=dict(type=backend.value), + onnx_config=dict( + output_names=['dets', 'labels'], input_shape=None), + codebase_config=dict( + type='mmdet', + task='ObjectDetection', + post_processing=dict( + score_threshold=0.05, + iou_threshold=0.5, + max_output_boxes_per_class=200, + pre_top_k=-1, + keep_top_k=100, + background_label_id=-1, + export_postprocess_mask=False, + )))) + model_cfg = Config(dict(model=mmengine.load(model_cfg_path))) + from mmdet.apis import init_detector + model = init_detector(model_cfg, None, device='cpu', palette='coco') + + img = torch.randn(1, 3, 64, 64) + from mmdet.structures import DetDataSample + data_sample = DetDataSample(metainfo=dict(batch_input_shape=(64, 64))) + rewrite_inputs = {'batch_inputs': img} + wrapped_model = WrapModel(model, 'predict', data_samples=[data_sample]) + rewrite_outputs, _ = get_rewrite_outputs( + wrapped_model=wrapped_model, + model_inputs=rewrite_inputs, + deploy_cfg=deploy_cfg) + + assert rewrite_outputs is not None + + @pytest.mark.parametrize('backend_type', [Backend.ONNXRUNTIME, Backend.OPENVINO]) def test_single_roi_extractor(backend_type: Backend): From 742c29a5933600d1ca3b88eb4a1050fca8fee545 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Thu, 30 Mar 2023 19:11:37 +0800 Subject: [PATCH 15/18] fix regression DETR" --- tests/regression/mmdet.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/regression/mmdet.yml b/tests/regression/mmdet.yml index b7cf41aa22..947dfe93cc 100644 --- a/tests/regression/mmdet.yml +++ b/tests/regression/mmdet.yml @@ -300,7 +300,10 @@ models: - configs/detr/detr_r50_8xb2-150e_coco.py pipelines: - *pipeline_ort_dynamic_fp32 - - *pipeline_trt_dynamic_fp16 + - deploy_config: configs/mmdet/detection/detection_tensorrt-fp16_dynamic-64x64-800x800.py + convert_image: *convert_image + backend_test: *default_backend_test + sdk_config: *sdk_dynamic - name: CenterNet metafile: configs/centernet/metafile.yml From 785fca063240534b5384a914651e522d3df7ce5b Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Fri, 31 Mar 2023 11:30:13 +0800 Subject: [PATCH 16/18] fix ut --- tests/test_codebase/test_mmdet/test_mmdet_models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_codebase/test_mmdet/test_mmdet_models.py b/tests/test_codebase/test_mmdet/test_mmdet_models.py index 109f41a728..487c12867e 100644 --- a/tests/test_codebase/test_mmdet/test_mmdet_models.py +++ b/tests/test_codebase/test_mmdet/test_mmdet_models.py @@ -692,9 +692,14 @@ def test_forward_of_base_detector(model_cfg_path, backend): @pytest.mark.parametrize('backend', [Backend.ONNXRUNTIME]) +@pytest.mark.skipif( + reason='mha only support torch greater than 1.10.0', + condition=torch.__version__ < '1.10.0') @pytest.mark.parametrize( 'model_cfg_path', ['tests/test_codebase/test_mmdet/data/detr_model.json']) def test_predict_of_detr_detector(model_cfg_path, backend): + # Skip test when torch.__version__ < 1.10.0 + # See https://github.com/open-mmlab/mmdeploy/discussions/1434 check_backend(backend) deploy_cfg = Config( dict( From 9e1d9bf00ca1de4d707b8c974dec16b835a2c5ba Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Fri, 31 Mar 2023 12:00:17 +0800 Subject: [PATCH 17/18] fix ut version --- tests/test_codebase/test_mmdet/test_mmdet_models.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/test_codebase/test_mmdet/test_mmdet_models.py b/tests/test_codebase/test_mmdet/test_mmdet_models.py index 487c12867e..7a5eb0a897 100644 --- a/tests/test_codebase/test_mmdet/test_mmdet_models.py +++ b/tests/test_codebase/test_mmdet/test_mmdet_models.py @@ -9,21 +9,21 @@ import numpy as np import pytest import torch +from packaging import version try: from torch.testing import assert_close as torch_assert_close except Exception: from torch.testing import assert_allclose as torch_assert_close -from mmengine import Config -from mmengine.config import ConfigDict - from mmdeploy.codebase import import_codebase from mmdeploy.core.rewriters.rewriter_manager import RewriterContext from mmdeploy.utils import Backend, Codebase from mmdeploy.utils.test import (WrapFunction, WrapModel, backend_checker, check_backend, get_model_outputs, get_onnx_model, get_rewrite_outputs) +from mmengine import Config +from mmengine.config import ConfigDict try: import_codebase(Codebase.MMDET) @@ -222,13 +222,12 @@ def test_multiclass_nms_with_keep_top_k(pre_top_k): @backend_checker(Backend.TENSORRT) def test__anchorgenerator__single_level_grid_priors(): backend_type = 'tensorrt' + import mmdeploy.codebase.mmdet.models.task_modules.prior_generators.anchor # noqa import onnx + from mmdeploy.apis.onnx import export from mmdet.models.task_modules.prior_generators.anchor_generator import \ AnchorGenerator - import mmdeploy.codebase.mmdet.models.task_modules.prior_generators.anchor # noqa - from mmdeploy.apis.onnx import export - generator = AnchorGenerator( scales=[8], ratios=[0.5, 1.0, 2.0], strides=[4]) @@ -694,7 +693,7 @@ def test_forward_of_base_detector(model_cfg_path, backend): @pytest.mark.parametrize('backend', [Backend.ONNXRUNTIME]) @pytest.mark.skipif( reason='mha only support torch greater than 1.10.0', - condition=torch.__version__ < '1.10.0') + condition=version.parse(torch.__version__) < version.parse('1.10.0')) @pytest.mark.parametrize( 'model_cfg_path', ['tests/test_codebase/test_mmdet/data/detr_model.json']) def test_predict_of_detr_detector(model_cfg_path, backend): From d2650b2ca2f421a9265a058bcc84e0bcc52dee13 Mon Sep 17 00:00:00 2001 From: hanrui1sensetime Date: Fri, 31 Mar 2023 12:04:01 +0800 Subject: [PATCH 18/18] fix lint --- tests/test_codebase/test_mmdet/test_mmdet_models.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_codebase/test_mmdet/test_mmdet_models.py b/tests/test_codebase/test_mmdet/test_mmdet_models.py index 7a5eb0a897..32110c2b01 100644 --- a/tests/test_codebase/test_mmdet/test_mmdet_models.py +++ b/tests/test_codebase/test_mmdet/test_mmdet_models.py @@ -16,14 +16,15 @@ except Exception: from torch.testing import assert_allclose as torch_assert_close +from mmengine import Config +from mmengine.config import ConfigDict + from mmdeploy.codebase import import_codebase from mmdeploy.core.rewriters.rewriter_manager import RewriterContext from mmdeploy.utils import Backend, Codebase from mmdeploy.utils.test import (WrapFunction, WrapModel, backend_checker, check_backend, get_model_outputs, get_onnx_model, get_rewrite_outputs) -from mmengine import Config -from mmengine.config import ConfigDict try: import_codebase(Codebase.MMDET) @@ -222,12 +223,13 @@ def test_multiclass_nms_with_keep_top_k(pre_top_k): @backend_checker(Backend.TENSORRT) def test__anchorgenerator__single_level_grid_priors(): backend_type = 'tensorrt' - import mmdeploy.codebase.mmdet.models.task_modules.prior_generators.anchor # noqa import onnx - from mmdeploy.apis.onnx import export from mmdet.models.task_modules.prior_generators.anchor_generator import \ AnchorGenerator + import mmdeploy.codebase.mmdet.models.task_modules.prior_generators.anchor # noqa + from mmdeploy.apis.onnx import export + generator = AnchorGenerator( scales=[8], ratios=[0.5, 1.0, 2.0], strides=[4])