-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring instance segmentation modules (#3696)
* Remove mmdet and torchvision directory & Refactor duplicated functions * Update docstring * Remove DictConfig
- Loading branch information
Showing
60 changed files
with
1,037 additions
and
1,587 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,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) |
This file contains 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 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 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 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 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
Oops, something went wrong.