-
Notifications
You must be signed in to change notification settings - Fork 675
[Fix] Fix errors about deploying MMYOLO-OpenVINO, DETR, ConvFormer and RTMDet #1919
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
Merged
RunningLeon
merged 18 commits into
open-mmlab:dev-1.x
from
hanrui1sensetime:fix_reg_2023_mar
Mar 31, 2023
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3366d26
fix reg test yolox
hanrui1sensetime d95b302
fix detr
hanrui1sensetime 50c812f
fix rtmdet-sdk reg
hanrui1sensetime 5464681
fix conformer precision
hanrui1sensetime 3e70089
add conformer_cls sdk
hanrui1sensetime 71aaddb
add mmcls ut
hanrui1sensetime 2156808
fix detr ut
hanrui1sensetime e1ce38c
fix detr ut
hanrui1sensetime ce09ad3
fix lint
hanrui1sensetime 8da76b5
fix yapf
hanrui1sensetime c4f9308
fix cls sdk
hanrui1sensetime 263dc3d
fix detr_head rewriter
hanrui1sensetime 03d6a24
fix interpolate
hanrui1sensetime 67c44c6
complement the mmdet ut
hanrui1sensetime 742c29a
fix regression DETR"
hanrui1sensetime 785fca0
fix ut
hanrui1sensetime 9e1d9bf
fix ut version
hanrui1sensetime d2650b2
fix lint
hanrui1sensetime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]))]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_base_ = ['../_base_/base_openvino_dynamic-640x640.py'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# 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__ = ['single_stage', 'single_stage_instance_seg', 'two_stage'] | ||
__all__ = [ | ||
'base_detr', 'single_stage', 'single_stage_instance_seg', 'two_stage' | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.