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

RTMDet Inst Seg Explain Mode for 2.2 #4083

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ All notable changes to this project will be documented in this file.
(<https://github.com/openvinotoolkit/training_extensions/pull/4067>)
- Fix wrong model name in converter & template
(<https://github.com/openvinotoolkit/training_extensions/pull/4082>)
- Fix RTMDet Inst Explain Mode
(<https://github.com/openvinotoolkit/training_extensions/pull/4083>)

## \[v2.1.0\]

Expand Down
17 changes: 16 additions & 1 deletion src/otx/algo/detection/base_models/single_stage_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

from typing import TYPE_CHECKING

import torch

from otx.algo.instance_segmentation.heads.rtmdet_inst_head import RTMDetInstSepBNHead
from otx.algo.modules.base_module import BaseModule
from otx.algo.utils.mmengine_utils import InstanceData
from otx.core.data.entity.detection import DetBatchDataEntity

if TYPE_CHECKING:
import torch
from torch import Tensor, nn


Expand Down Expand Up @@ -210,6 +212,19 @@ def export(
backbone_feat = self.extract_feat(batch_inputs)
bbox_head_feat = self.bbox_head.forward(backbone_feat)
feature_vector = self.feature_vector_fn(backbone_feat)

if isinstance(self.bbox_head, RTMDetInstSepBNHead):
# create dummy saliency map as its implemented in ModelAPI
saliency_map = torch.zeros(1)
bboxes, labels, masks = self.bbox_head.export(backbone_feat, batch_img_metas, rescale=rescale) # type: ignore[misc]
return {
"bboxes": bboxes,
"labels": labels,
"masks": masks,
"feature_vector": feature_vector,
"saliency_map": saliency_map,
}

saliency_map = self.explain_fn(bbox_head_feat[0])
bboxes, labels = self.bbox_head.export(backbone_feat, batch_img_metas, rescale=rescale)
return {
Expand Down
4 changes: 2 additions & 2 deletions src/otx/algo/instance_segmentation/heads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .fcn_mask_head import FCNMaskHead
from .roi_head_tv import TVRoIHeads
from .rpn_head import RPNHead
from .rtmdet_ins_head import RTMDetInsSepBNHead
from .rtmdet_inst_head import RTMDetInstSepBNHead

__all__ = [
"Shared2FCBBoxHead",
Expand All @@ -16,5 +16,5 @@
"FCNMaskHead",
"TVRoIHeads",
"RPNHead",
"RTMDetInsSepBNHead",
"RTMDetInstSepBNHead",
]
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
# mypy: disable-error-code="call-overload, index, override, attr-defined, misc"


class RTMDetInsHead(RTMDetHead):
class RTMDetInstHead(RTMDetHead):
"""Detection Head of RTMDet-Ins.

Args:
Expand Down Expand Up @@ -764,7 +764,7 @@ def forward(self, features: tuple[Tensor, ...]) -> Tensor:
return self.projection(mask_features)


class RTMDetInsSepBNHead(RTMDetInsHead):
class RTMDetInstSepBNHead(RTMDetInstHead):
"""Detection Head of RTMDet-Ins with sep-bn layers.

Args:
Expand Down
4 changes: 2 additions & 2 deletions src/otx/algo/instance_segmentation/rtmdet_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from otx.algo.common.utils.samplers import PseudoSampler
from otx.algo.detection.base_models import SingleStageDetector
from otx.algo.detection.necks import CSPNeXtPAFPN
from otx.algo.instance_segmentation.heads import RTMDetInsSepBNHead
from otx.algo.instance_segmentation.heads import RTMDetInstSepBNHead
from otx.algo.instance_segmentation.losses import DiceLoss
from otx.algo.modules.norm import build_norm_layer
from otx.core.config.data import TileConfig
Expand Down Expand Up @@ -155,7 +155,7 @@ def _build_model(self, num_classes: int) -> SingleStageDetector:
activation=partial(nn.SiLU, inplace=True),
)

bbox_head = RTMDetInsSepBNHead(
bbox_head = RTMDetInstSepBNHead(
num_classes=num_classes,
in_channels=96,
stacked_convs=2,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/api/test_xai.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_predict_with_explain(
if "dino" in model_name:
pytest.skip("DINO is not supported.")

if any(keyword in recipe for keyword in ["rtmdet_inst_tiny", "maskdino", "maskrcnn_r50_tv"]):
if any(keyword in recipe for keyword in ["maskdino", "maskrcnn_r50_tv"]):
# TODO(Eugene): inst-seg models not fully support yet.
pytest.skip(f"There's issue with inst-seg: {recipe}. Skip for now.")

Expand Down
Loading