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

Refactoring instance segmentation modules #3696

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
42ad331
Update `__init__`
sungchul2 Jun 21, 2024
9dc54d1
Update `_create_model`
sungchul2 Jun 21, 2024
bcd9b4a
Update `_customize_inputs`
sungchul2 Jun 21, 2024
a465a29
Update `_customize_outputs`
sungchul2 Jun 21, 2024
1056862
Update `get_classification_layers`
sungchul2 Jun 24, 2024
6b9aca2
Update export related things
sungchul2 Jun 24, 2024
364a215
Update importing backbones
sungchul2 Jun 24, 2024
bf9d842
Update importing heads
sungchul2 Jun 24, 2024
7d74d52
Update `_build_model` and fix unit tests
sungchul2 Jun 24, 2024
815459c
Move `iou2d_calculator` to utils
sungchul2 Jun 24, 2024
1bce50d
Move `layers` into `utils`
sungchul2 Jun 24, 2024
e6b9aa5
Update importing losses
sungchul2 Jun 24, 2024
73a53ad
Update importing necks
sungchul2 Jun 24, 2024
e266189
Fix not updated things
sungchul2 Jun 24, 2024
7ba019f
Move `layers` out of `utils`
sungchul2 Jun 24, 2024
b33484e
Move tests
sungchul2 Jun 24, 2024
971afe7
Create `common` directory to manage common modules
sungchul2 Jun 25, 2024
31f221b
Remove unnecessary log
sungchul2 Jun 25, 2024
80a58d3
Refactoring & remove `DictConfig`
sungchul2 Jun 24, 2024
a4fb499
Update docstring and refactoring in utils
sungchul2 Jun 25, 2024
4d5db97
Revert unnecessarily updated docstring
sungchul2 Jun 25, 2024
4c1fc5a
Fix unit tests
sungchul2 Jun 25, 2024
e444ec3
Update directory names
sungchul2 Jun 25, 2024
9916e73
Merge branch 'releases/2.1.0' into refactoring-detection-modules
sungchul2 Jun 25, 2024
216857e
Update docstring
sungchul2 Jun 26, 2024
27e15dd
Move modules used across tasks to `common` directory
sungchul2 Jun 27, 2024
c04aa58
Update arguments by removing arguments that are same with default value
sungchul2 Jun 27, 2024
868c072
Fix unit test
sungchul2 Jun 27, 2024
b646589
Merge branch 'releases/2.1.0' into refactoring-detection-modules
sungchul2 Jun 27, 2024
961fbab
Move `SPPBottleneck` to common.layers
sungchul2 Jun 27, 2024
bf932a7
Update backbones
sungchul2 Jun 27, 2024
40495b3
Update heads
sungchul2 Jun 27, 2024
685f076
Update layers
sungchul2 Jun 27, 2024
59db509
Update additional heads
sungchul2 Jun 27, 2024
d8943b3
Update necks
sungchul2 Jun 27, 2024
ea8504a
Update roi_extractors
sungchul2 Jun 27, 2024
8293b0f
Update structures
sungchul2 Jun 27, 2024
ed5784d
Update ssd docstring
sungchul2 Jun 28, 2024
adfa34e
Update roi_heads
sungchul2 Jun 28, 2024
971e433
Remove `BaseDetector` and update `TwoStageDetector`
sungchul2 Jun 28, 2024
4969ee3
Remove mmdet directory
sungchul2 Jun 28, 2024
7f034e2
Merge branch 'releases/2.1.0' into refactoring-iseg-modules
sungchul2 Jun 28, 2024
413c8db
Integrate `MaskRCNN` to `TowStageDetector` and rename `OTXMaskRCNN` t…
sungchul2 Jul 1, 2024
d45adca
Move duplicated functions to `OTXInstanceSegModel` and update inherit…
sungchul2 Jul 1, 2024
750e89f
Remove torchvision directory
sungchul2 Jul 1, 2024
95b7400
Fix unit test
sungchul2 Jul 1, 2024
4ed7fda
(WIP) Update docstring
sungchul2 Jul 2, 2024
56ce17b
Update docstring
sungchul2 Jul 2, 2024
9cdb0dc
Remove `DictConfig`
sungchul2 Jul 2, 2024
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: 1 addition & 1 deletion src/otx/algo/common/backbones/cspnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import math
from typing import ClassVar

from otx.algo.detection.backbones.csp_darknet import SPPBottleneck # TODO (sungchul): move csp_darknet to common?
from otx.algo.common.layers import SPPBottleneck
from otx.algo.detection.layers import CSPLayer
from otx.algo.modules.base_module import BaseModule
from otx.algo.modules.conv_module import ConvModule
Expand Down
3 changes: 2 additions & 1 deletion src/otx/algo/common/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"""Custom layer implementations."""

from .res_layer import ResLayer
from .spp_layer import SPPBottleneck

__all__ = ["ResLayer"]
__all__ = ["ResLayer", "SPPBottleneck"]
7 changes: 1 addition & 6 deletions src/otx/algo/common/layers/res_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from otx.algo.modules.base_module import BaseModule, Sequential
from otx.algo.modules.conv import build_conv_layer
from otx.algo.modules.norm import build_norm_layer
from torch import nn

if TYPE_CHECKING:
from omegaconf import DictConfig


class ResLayer(Sequential):
"""ResLayer to build ResNet style backbone.
Expand Down Expand Up @@ -47,7 +42,7 @@ def __init__(
norm_cfg: dict,
stride: int = 1,
avg_down: bool = False,
conv_cfg: DictConfig | dict | None = None,
conv_cfg: dict | None = None,
downsample_first: bool = True,
**kwargs,
) -> None:
Expand Down
68 changes: 68 additions & 0 deletions src/otx/algo/common/layers/spp_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OpenMMLab. All rights reserved.
"""Implementation modified from mmdet.models.backbones.csp_darknet.py.

Reference : https://github.com/open-mmlab/mmdetection/blob/v3.2.0/mmdet/models/backbones/csp_darknet.py
"""

from __future__ import annotations

import torch
from otx.algo.modules.base_module import BaseModule
from otx.algo.modules.conv_module import ConvModule
from torch import Tensor, nn


class SPPBottleneck(BaseModule):
"""Spatial pyramid pooling layer used in YOLOv3-SPP.

Args:
in_channels (int): The input channels of this Module.
out_channels (int): The output channels of this Module.
kernel_sizes (tuple[int]): Sequential of kernel sizes of pooling
layers. Default: (5, 9, 13).
conv_cfg (dict): Config dict for convolution layer. Default: None,
which means using conv2d.
norm_cfg (dict): Config dict for normalization layer.
Default: dict(type='BN').
act_cfg (dict): Config dict for activation layer.
Default: dict(type='Swish').
init_cfg (dict, list[dict], optional): Initialization config dict.
Default: None.
"""

def __init__(
self,
in_channels: int,
out_channels: int,
kernel_sizes: tuple[int, ...] = (5, 9, 13),
conv_cfg: dict | None = None,
norm_cfg: dict | None = None,
act_cfg: dict | None = None,
init_cfg: dict | list[dict] | None = None,
):
super().__init__(init_cfg=init_cfg)
norm_cfg = norm_cfg or {"type": "BN", "momentum": 0.03, "eps": 0.001}
act_cfg = act_cfg or {"type": "Swish"}

mid_channels = in_channels // 2
self.conv1 = ConvModule(
in_channels,
mid_channels,
1,
stride=1,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
act_cfg=act_cfg,
)
self.poolings = nn.ModuleList([nn.MaxPool2d(kernel_size=ks, stride=1, padding=ks // 2) for ks in kernel_sizes])
conv2_channels = mid_channels * (len(kernel_sizes) + 1)
self.conv2 = ConvModule(conv2_channels, out_channels, 1, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)

def forward(self, x: Tensor) -> Tensor:
"""Forward."""
x = self.conv1(x)
with torch.cuda.amp.autocast(enabled=False):
x = torch.cat([x] + [pooling(x) for pooling in self.poolings], dim=1)
return self.conv2(x)
55 changes: 1 addition & 54 deletions src/otx/algo/detection/backbones/csp_darknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from torch import Tensor, nn
from torch.nn.modules.batchnorm import _BatchNorm

from otx.algo.common.layers import SPPBottleneck
from otx.algo.detection.layers import CSPLayer
from otx.algo.modules.base_module import BaseModule
from otx.algo.modules.conv_module import ConvModule
Expand Down Expand Up @@ -93,60 +94,6 @@ def export(self, x: Tensor) -> Tensor:
return self.conv(x)


class SPPBottleneck(BaseModule):
"""Spatial pyramid pooling layer used in YOLOv3-SPP.

Args:
in_channels (int): The input channels of this Module.
out_channels (int): The output channels of this Module.
kernel_sizes (tuple[int]): Sequential of kernel sizes of pooling
layers. Default: (5, 9, 13).
conv_cfg (dict): Config dict for convolution layer. Default: None,
which means using conv2d.
norm_cfg (dict): Config dict for normalization layer.
Default: dict(type='BN').
act_cfg (dict): Config dict for activation layer.
Default: dict(type='Swish').
init_cfg (dict, list[dict], optional): Initialization config dict.
Default: None.
"""

def __init__(
self,
in_channels: int,
out_channels: int,
kernel_sizes: tuple[int, ...] = (5, 9, 13),
conv_cfg: dict | None = None,
norm_cfg: dict | None = None,
act_cfg: dict | None = None,
init_cfg: dict | list[dict] | None = None,
):
super().__init__(init_cfg=init_cfg)
norm_cfg = norm_cfg or {"type": "BN", "momentum": 0.03, "eps": 0.001}
act_cfg = act_cfg or {"type": "Swish"}

mid_channels = in_channels // 2
self.conv1 = ConvModule(
in_channels,
mid_channels,
1,
stride=1,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
act_cfg=act_cfg,
)
self.poolings = nn.ModuleList([nn.MaxPool2d(kernel_size=ks, stride=1, padding=ks // 2) for ks in kernel_sizes])
conv2_channels = mid_channels * (len(kernel_sizes) + 1)
self.conv2 = ConvModule(conv2_channels, out_channels, 1, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)

def forward(self, x: Tensor) -> Tensor:
"""Forward."""
x = self.conv1(x)
with torch.cuda.amp.autocast(enabled=False):
x = torch.cat([x] + [pooling(x) for pooling in self.poolings], dim=1)
return self.conv2(x)


class CSPDarknet(BaseModule):
"""CSP-Darknet backbone used in YOLOv5 and YOLOX.

Expand Down
2 changes: 1 addition & 1 deletion src/otx/algo/detection/heads/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Intel Corporation
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
"""Custom head implementations for detection task."""

Expand Down
7 changes: 1 addition & 6 deletions src/otx/algo/detection/layers/channel_attention_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@

from __future__ import annotations

from typing import TYPE_CHECKING

import torch
from torch import Tensor, nn

from otx.algo.modules.base_module import BaseModule

if TYPE_CHECKING:
from omegaconf import DictConfig


class ChannelAttention(BaseModule):
"""Channel attention Module.
Expand All @@ -28,7 +23,7 @@ class ChannelAttention(BaseModule):
def __init__(
self,
channels: int,
init_cfg: DictConfig | dict | list[DictConfig] | list[dict] | None = None,
init_cfg: dict | list[dict] | None = None,
) -> None:
super().__init__(init_cfg=init_cfg)

Expand Down
35 changes: 14 additions & 21 deletions src/otx/algo/detection/layers/csp_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

from __future__ import annotations

from typing import TYPE_CHECKING

import torch
from torch import Tensor, nn

Expand All @@ -15,9 +13,6 @@
from otx.algo.modules.conv_module import ConvModule
from otx.algo.modules.depthwise_separable_conv_module import DepthwiseSeparableConvModule

if TYPE_CHECKING:
from omegaconf import DictConfig


class DarknetBottleneck(BaseModule):
"""The basic bottleneck block used in Darknet.
Expand Down Expand Up @@ -51,10 +46,10 @@ def __init__(
expansion: float = 0.5,
add_identity: bool = True,
use_depthwise: bool = False,
conv_cfg: DictConfig | dict | None = None,
norm_cfg: DictConfig | dict | None = None,
act_cfg: DictConfig | dict | None = None,
init_cfg: DictConfig | dict | list[DictConfig] | list[dict] | None = None,
conv_cfg: dict | None = None,
norm_cfg: dict | None = None,
act_cfg: dict | None = None,
init_cfg: dict | list[dict] | None = None,
) -> None:
if norm_cfg is None:
norm_cfg = {"type": "BN", "momentum": 0.03, "eps": 0.001}
Expand Down Expand Up @@ -109,8 +104,7 @@ class CSPNeXtBlock(BaseModule):
Defaults to dict(type='BN', momentum=0.03, eps=0.001).
act_cfg (dict): Config dict for activation layer.
Defaults to dict(type='SiLU').
init_cfg (:obj:`DictConfig` or dict or list[dict] or
list[:obj:`DictConfig`], optional): Initialization config dict.
init_cfg (dict or list[dict], optional): Initialization config dict.
Defaults to None.
"""

Expand All @@ -122,10 +116,10 @@ def __init__(
add_identity: bool = True,
use_depthwise: bool = False,
kernel_size: int = 5,
conv_cfg: DictConfig | dict | None = None,
norm_cfg: DictConfig | dict | None = None,
act_cfg: DictConfig | dict | None = None,
init_cfg: DictConfig | dict | list[DictConfig] | list[dict] | None = None,
conv_cfg: dict | None = None,
norm_cfg: dict | None = None,
act_cfg: dict | None = None,
init_cfg: dict | list[dict] | None = None,
) -> None:
if norm_cfg is None:
norm_cfg = {"type": "BN", "momentum": 0.03, "eps": 0.001}
Expand Down Expand Up @@ -184,8 +178,7 @@ class CSPLayer(BaseModule):
Defaults to dict(type='BN')
act_cfg (dict): Config dict for activation layer.
Defaults to dict(type='Swish')
init_cfg (:obj:`DictConfig` or dict or list[dict] or
list[:obj:`DictConfig`], optional): Initialization config dict.
init_cfg (dict or list[dict], optional): Initialization config dict.
Defaults to None.
"""

Expand All @@ -199,10 +192,10 @@ def __init__(
use_depthwise: bool = False,
use_cspnext_block: bool = False,
channel_attention: bool = False,
conv_cfg: DictConfig | dict | None = None,
norm_cfg: DictConfig | dict | None = None,
act_cfg: DictConfig | dict | None = None,
init_cfg: DictConfig | dict | list[DictConfig] | list[dict] | None = None,
conv_cfg: dict | None = None,
norm_cfg: dict | None = None,
act_cfg: dict | None = None,
init_cfg: dict | list[dict] | None = None,
) -> None:
if norm_cfg is None:
norm_cfg = {"type": "BN", "momentum": 0.03, "eps": 0.001}
Expand Down
12 changes: 7 additions & 5 deletions src/otx/algo/detection/ssd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
"""SSD object detector for the OTX detection."""
# Copyright (c) OpenMMLab. All rights reserved.
"""SSD object detector for the OTX detection.

Implementation modified from mmdet.models.detectors.single_stage.
Reference : https://github.com/open-mmlab/mmdetection/blob/v3.2.0/mmdet/models/detectors/single_stage.py
"""

from __future__ import annotations

Expand Down Expand Up @@ -34,10 +38,8 @@
logger = logging.getLogger()


# This class and its supporting functions below lightly adapted from the mmdet SingleStageDetector available at:
# https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/detectors/single_stage.py
class SingleStageDetector(BaseModule):
"""Single stage detector implementation from mmdet."""
"""Single stage detector implementation."""

def __init__(
self,
Expand Down
Loading
Loading