-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d641b6
commit de6f4bd
Showing
10 changed files
with
177 additions
and
28 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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from .upernet import UPerHead | ||
from .segformer import SegFormerHead | ||
from .sfnet import SFHead | ||
from .fpn import FPNHead | ||
from .fapn import FaPNHead | ||
|
||
__all__ = ['UPerHead', 'SegFormerHead', 'SFHead'] | ||
__all__ = ['UPerHead', 'SegFormerHead', 'SFHead', 'FPNHead', 'FaPNHead'] |
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,102 @@ | ||
import torch | ||
from torch import nn, Tensor | ||
from torch.nn import functional as F | ||
from torchvision.ops import DeformConv2d | ||
|
||
|
||
class ConvModule(nn.Sequential): | ||
def __init__(self, c1, c2, k, s=1, p=0): | ||
super().__init__( | ||
nn.Conv2d(c1, c2, k, s, p, bias=False), | ||
nn.BatchNorm2d(c2), | ||
nn.ReLU(True) | ||
) | ||
|
||
|
||
class DCNv2(nn.Module): | ||
def __init__(self, c1, c2, k, s, p, g=1): | ||
super().__init__() | ||
self.dcn = DeformConv2d(c1, c2, k, s, p, groups=g) | ||
self.offset_mask = nn.Conv2d(c2, g* 3 * k * k, k, s, p) | ||
self._init_offset() | ||
|
||
def _init_offset(self): | ||
self.offset_mask.weight.data.zero_() | ||
self.offset_mask.bias.data.zero_() | ||
|
||
def forward(self, x, offset): | ||
out = self.offset_mask(offset) | ||
o1, o2, mask = torch.chunk(out, 3, dim=1) | ||
offset = torch.cat([o1, o2], dim=1) | ||
mask = mask.sigmoid() | ||
return self.dcn(x, offset, mask) | ||
|
||
|
||
class FSM(nn.Module): | ||
def __init__(self, c1, c2): | ||
super().__init__() | ||
self.conv_atten = nn.Conv2d(c1, c1, 1, bias=False) | ||
self.conv = nn.Conv2d(c1, c2, 1, bias=False) | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
atten = self.conv_atten(F.avg_pool2d(x, x.shape[2:])).sigmoid() | ||
feat = torch.mul(x, atten) | ||
x = x + feat | ||
return self.conv(x) | ||
|
||
|
||
class FAM(nn.Module): | ||
def __init__(self, c1, c2): | ||
super().__init__() | ||
self.lateral_conv = FSM(c1, c2) | ||
self.offset = nn.Conv2d(c2*2, c2, 1, bias=False) | ||
self.dcpack_l2 = DCNv2(c2, c2, 3, 1, 1, 8) | ||
|
||
def forward(self, feat_l, feat_s): | ||
feat_up = feat_s | ||
if feat_l.shape[2:] != feat_s.shape[2:]: | ||
feat_up = F.interpolate(feat_s, size=feat_l.shape[2:], mode='bilinear', align_corners=False) | ||
|
||
feat_arm = self.lateral_conv(feat_l) | ||
offset = self.offset(torch.cat([feat_arm, feat_up*2], dim=1)) | ||
|
||
feat_align = F.relu(self.dcpack_l2(feat_up, offset)) | ||
return feat_align + feat_arm | ||
|
||
|
||
class FaPNHead(nn.Module): | ||
def __init__(self, in_channels, channel=128, num_classes=19): | ||
super().__init__() | ||
in_channels = in_channels[::-1] | ||
self.align_modules = nn.ModuleList([ConvModule(in_channels[0], channel, 1)]) | ||
self.output_convs = nn.ModuleList([]) | ||
|
||
for ch in in_channels[1:]: | ||
self.align_modules.append(FAM(ch, channel)) | ||
self.output_convs.append(ConvModule(channel, channel, 3, 1, 1)) | ||
|
||
self.conv_seg = nn.Conv2d(channel, num_classes, 1) | ||
self.dropout = nn.Dropout2d(0.1) | ||
|
||
def forward(self, features) -> Tensor: | ||
features = features[::-1] | ||
out = self.align_modules[0](features[0]) | ||
|
||
for feat, align_module, output_conv in zip(features[1:], self.align_modules[1:], self.output_convs): | ||
out = align_module(feat, out) | ||
out = output_conv(out) | ||
out = self.conv_seg(self.dropout(out)) | ||
return out | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
sys.path.insert(0, '.') | ||
from models.backbones.resnet import ResNet | ||
backbone = ResNet('50') | ||
head = FaPNHead([256, 512, 1024, 2048], 128, 19) | ||
x = torch.randn(2, 3, 224, 224) | ||
features = backbone(x) | ||
out = head(features) | ||
out = F.interpolate(out, size=x.shape[-2:], mode='bilinear', align_corners=False) | ||
print(out.shape) |
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,53 @@ | ||
import torch | ||
from torch import nn, Tensor | ||
from torch.nn import functional as F | ||
|
||
|
||
class ConvModule(nn.Sequential): | ||
def __init__(self, c1, c2, k, s=1, p=0): | ||
super().__init__( | ||
nn.Conv2d(c1, c2, k, s, p, bias=False), | ||
nn.BatchNorm2d(c2), | ||
nn.ReLU(True) | ||
) | ||
|
||
|
||
class FPNHead(nn.Module): | ||
"""Panoptic Feature Pyramid Networks | ||
https://arxiv.org/abs/1901.02446 | ||
""" | ||
def __init__(self, in_channels, channel=128, num_classes=19): | ||
super().__init__() | ||
self.lateral_convs = nn.ModuleList([]) | ||
self.output_convs = nn.ModuleList([]) | ||
|
||
for ch in in_channels[::-1]: | ||
self.lateral_convs.append(ConvModule(ch, channel, 1)) | ||
self.output_convs.append(ConvModule(channel, channel, 3, 1, 1)) | ||
|
||
self.conv_seg = nn.Conv2d(channel, num_classes, 1) | ||
self.dropout = nn.Dropout2d(0.1) | ||
|
||
def forward(self, features) -> Tensor: | ||
features = features[::-1] | ||
out = self.lateral_convs[0](features[0]) | ||
|
||
for i in range(1, len(features)): | ||
out = F.interpolate(out, scale_factor=2.0, mode='nearest') | ||
out = out + self.lateral_convs[i](features[i]) | ||
out = self.output_convs[i](out) | ||
out = self.conv_seg(self.dropout(out)) | ||
return out | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
sys.path.insert(0, '.') | ||
from models.backbones.resnet import ResNet | ||
backbone = ResNet('50') | ||
head = FPNHead([256, 512, 1024, 2048], 128, 19) | ||
x = torch.randn(2, 3, 224, 224) | ||
features = backbone(x) | ||
out = head(features) | ||
out = F.interpolate(out, size=x.shape[-2:], mode='bilinear', align_corners=False) | ||
print(out.shape) |
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