Skip to content
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

[Feature] Support centernet dev1.x #1219

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3435296
support centernet head
hanrui1sensetime Oct 18, 2022
756fa08
add centernet head ut
hanrui1sensetime Oct 18, 2022
96644a3
add centernet
hanrui1sensetime Oct 18, 2022
fe97bd1
add centernet
hanrui1sensetime Oct 18, 2022
2f20161
add support models
hanrui1sensetime Oct 18, 2022
b23ab0f
fix mdformat
hanrui1sensetime Oct 19, 2022
3bd715f
fix reg test
hanrui1sensetime Oct 19, 2022
ba46743
fix scale
hanrui1sensetime Oct 19, 2022
6004ea6
fix conflicts
hanrui1sensetime Oct 24, 2022
fecd330
fix test.py show_dir kwargs
hanrui1sensetime Oct 31, 2022
77fec5c
fix for profile in T4
hanrui1sensetime Oct 31, 2022
d042cf8
fix dynamic shape
hanrui1sensetime Nov 1, 2022
adbf53e
fix lint
hanrui1sensetime Nov 1, 2022
3a53adc
move rescale and border to outside
hanrui1sensetime Nov 2, 2022
5269e8f
fix ut
hanrui1sensetime Nov 2, 2022
58755e1
fix lint
hanrui1sensetime Nov 2, 2022
a18754b
update ort torchscript benchmark
hanrui1sensetime Nov 2, 2022
9314817
fix centernet
hanrui1sensetime Nov 3, 2022
94cf03c
fix ut
hanrui1sensetime Nov 3, 2022
5ee8bd8
remove unused file
hanrui1sensetime Nov 4, 2022
1d49368
support centernet sdk
hanrui1sensetime Nov 7, 2022
b772d66
remove unused rewriter
hanrui1sensetime Nov 7, 2022
4621aa5
fix lint
hanrui1sensetime Nov 7, 2022
0cd31a8
fix flake8
hanrui1sensetime Nov 7, 2022
ce16874
remove unused line
hanrui1sensetime Nov 8, 2022
f034022
fix lint
hanrui1sensetime Nov 8, 2022
0cd10e3
fix lint
hanrui1sensetime Nov 8, 2022
deec016
fix conflict
hanrui1sensetime Nov 8, 2022
8e8706b
fix doc links
hanrui1sensetime Nov 8, 2022
ab8ce5a
fix mdformat
hanrui1sensetime Nov 8, 2022
22a0069
fix scale_factor as default
hanrui1sensetime Nov 8, 2022
7208924
fix conflict of docs
hanrui1sensetime Nov 9, 2022
106a5f9
apart random pad and pad
hanrui1sensetime Nov 9, 2022
e6d4d6c
fix sdk
hanrui1sensetime Nov 9, 2022
29c0bb1
fix centernet docs
hanrui1sensetime Nov 10, 2022
dc6694e
fix code style of cpp
hanrui1sensetime Nov 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/en/03-benchmark/benchmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,20 @@ Users can directly test the performance through [how_to_evaluate_a_model.md](../
<td align="center">37.4</td>
<td align="center">-</td>
</tr>
<tr>
<td align="center"><a href="https://github.com/open-mmlab/mmdetection/tree/v3.0.0rc2/configs/centernet/centernet_r18_8xb16-crop512-140e_coco.py">CenterNet</a></td>
hanrui1sensetime marked this conversation as resolved.
Show resolved Hide resolved
<td align="center">Object Detection</td>
<td align="center">COCO2017</td>
<td align="center">box AP</td>
<td align="center">25.9</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">26.0</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
</tr>
<tr>
<td align="center"><a href="https://github.com/open-mmlab/mmdetection/tree/master/configs/yolox/yolox_s_8x8_300e_coco.py">YOLOX</a></td>
<td align="center">Object Detection</td>
Expand Down
14 changes: 14 additions & 0 deletions docs/zh_cn/03-benchmark/benchmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,20 @@ GPU: ncnn, TensorRT, PPLNN
<td align="center">37.4</td>
<td align="center">-</td>
</tr>
<tr>
<td align="center"><a href="https://github.com/open-mmlab/mmdetection/tree/v3.0.0rc2/configs/centernet/centernet_r18_8xb16-crop512-140e_coco.py">CenterNet</a></td>
hanrui1sensetime marked this conversation as resolved.
Show resolved Hide resolved
<td align="center">Object Detection</td>
<td align="center">COCO2017</td>
<td align="center">box AP</td>
<td align="center">25.9</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">26.0</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
</tr>
<tr>
<td align="center"><a href="https://github.com/open-mmlab/mmdetection/tree/master/configs/yolox/yolox_s_8x8_300e_coco.py">YOLOX</a></td>
<td align="center">Object Detection</td>
Expand Down
26 changes: 20 additions & 6 deletions mmdeploy/codebase/mmdet/deploy/object_detection_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,18 @@ def forward(self,

bboxes = dets[:, :4]
scores = dets[:, 4]

# perform rescale
if rescale and 'scale_factor' in img_metas[i]:
scale_factor = img_metas[i]['scale_factor']
if rescale:
if 'scale_factor' in img_metas[i]:
scale_factor = img_metas[i]['scale_factor']
else:
# Some models like CenterNet use rescale with out scale_factor
# We multiply the feat_size inside the model, and divide the
# image shape outside the model to adapt dynamic shape.
scale_factor = np.array([
1.0 / img_metas[i]['batch_input_shape'][1],
1.0 / img_metas[i]['batch_input_shape'][0]
])
if isinstance(scale_factor, (list, tuple, np.ndarray)):
if len(scale_factor) == 2:
scale_factor = np.array(scale_factor)
Expand All @@ -200,12 +208,18 @@ def forward(self,
scale_factor = torch.from_numpy(scale_factor).to(dets)
bboxes /= scale_factor

if 'pad_param' in img_metas[i]:
if 'pad_param' in img_metas[i] or 'border' in img_metas[i]:
hanrui1sensetime marked this conversation as resolved.
Show resolved Hide resolved
# Most of models in mmdetection 3.x use `pad_param`, but some
# models like CenterNet uses `border`.
# offset pixel of the top-left corners between original image
# and padded/enlarged image, 'pad_param' is used when exporting
# CornerNet and CentripetalNet to onnx
x_off = img_metas[i]['pad_param'][2]
y_off = img_metas[i]['pad_param'][0]
if 'pad_param' in img_metas[i]:
x_off = img_metas[i]['pad_param'][2]
y_off = img_metas[i]['pad_param'][0]
else:
x_off = img_metas[i]['border'][2]
y_off = img_metas[i]['border'][0]
bboxes[:, ::2] -= x_off
bboxes[:, 1::2] -= y_off
bboxes *= (bboxes > 0)
Expand Down
6 changes: 4 additions & 2 deletions mmdeploy/codebase/mmdet/models/dense_heads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from . import detr_head
from .base_dense_head import (base_dense_head__predict_by_feat,
base_dense_head__predict_by_feat__ncnn)
from .centernet_head import centernet_head__predict_by_feat__default
from .centernet_head import (centernet_head__decode_heatmap__default,
centernet_head__predict_by_feat__default)
from .fovea_head import fovea_head__predict_by_feat
from .gfl_head import gfl_head__predict_by_feat
from .reppoints_head import reppoints_head__predict_by_feat
Expand All @@ -20,5 +21,6 @@
'fovea_head__predict_by_feat', 'base_dense_head__predict_by_feat__ncnn',
'yolox_head__predict_by_feat__ncnn', 'gfl_head__predict_by_feat',
'reppoints_head__predict_by_feat', 'detr_head',
'centernet_head__predict_by_feat__default', 'rtmdet_head__predict_by_feat'
'centernet_head__predict_by_feat__default', 'rtmdet_head__predict_by_feat',
'centernet_head__decode_heatmap__default'
]
45 changes: 43 additions & 2 deletions mmdeploy/codebase/mmdet/models/dense_heads/centernet_head.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Copyright (c) OpenMMLab. All rights reserved.
from typing import List
from typing import List, Tuple

from torch import Tensor
import torch

from mmdeploy.core import FUNCTION_REWRITER
from mmdet.models.utils import (get_local_maximum, get_topk_from_heatmap,
transpose_and_gather_feat)


@FUNCTION_REWRITER.register_rewriter(
Expand Down Expand Up @@ -37,14 +40,52 @@ def centernet_head__predict_by_feat__default(
kernel=self.test_cfg.local_maximum_kernel)
det_bboxes = batch_det_bboxes.reshape([batch_size, -1, 5])
det_labels = batch_labels.reshape(batch_size, -1)
'''
batch_border = det_bboxes.new_tensor(
batch_img_metas[0]['border'])[..., [2, 0, 2, 0]]
det_bboxes[..., :4] -= batch_border
print(f'debugging scale: {batch_img_metas[0]}')
hanrui1sensetime marked this conversation as resolved.
Show resolved Hide resolved
if rescale and 'scale_factor' in batch_img_metas[0]:
det_bboxes[..., :4] /= det_bboxes.new_tensor(
batch_img_metas[0]['scale_factor']).repeat((1, 2))

'''
if with_nms:
det_bboxes, det_labels = self._bboxes_nms(det_bboxes, det_labels,
self.test_cfg)
return det_bboxes, det_labels


@FUNCTION_REWRITER.register_rewriter(
'mmdet.models.dense_heads.centernet_head.CenterNetHead._decode_heatmap')
def centernet_head__decode_heatmap__default(
hanrui1sensetime marked this conversation as resolved.
Show resolved Hide resolved
ctx,
self,
center_heatmap_pred: Tensor,
wh_pred: Tensor,
offset_pred: Tensor,
img_shape: tuple,
k: int = 100,
kernel: int = 3) -> Tuple[Tensor, Tensor]:
"""Rewrite `_decode_heatmap` of `CenterNetHead` for default backend."""

# Rewrite this function to move the img_shape calculation outside the
# model for dynamic shape deployment.
height, width = center_heatmap_pred.shape[2:]
hanrui1sensetime marked this conversation as resolved.
Show resolved Hide resolved
center_heatmap_pred = get_local_maximum(center_heatmap_pred, kernel=kernel)

*batch_dets, topk_ys, topk_xs = get_topk_from_heatmap(
center_heatmap_pred, k=k)
batch_scores, batch_index, batch_topk_labels = batch_dets

wh = transpose_and_gather_feat(wh_pred, batch_index)
offset = transpose_and_gather_feat(offset_pred, batch_index)
topk_xs = topk_xs + offset[..., 0]
topk_ys = topk_ys + offset[..., 1]
tl_x = (topk_xs - wh[..., 0] / 2) / width
tl_y = (topk_ys - wh[..., 1] / 2) / height
br_x = (topk_xs + wh[..., 0] / 2) / width
br_y = (topk_ys + wh[..., 1] / 2) / height

batch_bboxes = torch.stack([tl_x, tl_y, br_x, br_y], dim=2)
batch_bboxes = torch.cat((batch_bboxes, batch_scores[..., None]), dim=-1)
return batch_bboxes, batch_topk_labels