-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Lite R-ASPP with MobileNetV3 backbone.
- Loading branch information
Showing
5 changed files
with
120 additions
and
9 deletions.
There are no files selected for viewing
Binary file not shown.
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,6 +1,5 @@ | ||
from collections import OrderedDict | ||
|
||
import torch | ||
from torch import nn | ||
from torch.nn import functional as F | ||
|
||
|
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,67 @@ | ||
from collections import OrderedDict | ||
|
||
from torch import nn, Tensor | ||
from torch.nn import functional as F | ||
from typing import Dict | ||
|
||
|
||
__all__ = ["LRASPP"] | ||
|
||
|
||
class LRASPP(nn.Module): | ||
""" | ||
Implements a Lite R-ASPP Network for semantic segmentation. | ||
Args: | ||
backbone (nn.Module): the network used to compute the features for the model. | ||
The backbone should return an OrderedDict[Tensor], with the key being | ||
"high" for the high level feature map and "low" for the low level feature map. | ||
low_channels (int): the number of channels of the low level features. | ||
high_channels (int): the number of channels of the high level features. | ||
num_classes (int): number of output classes of the model (including the background). | ||
inter_channels (int, optional): the number of channels for intermediate computations. | ||
""" | ||
|
||
def __init__(self, backbone, low_channels, high_channels, num_classes, inter_channels=128): | ||
super().__init__() | ||
self.backbone = backbone | ||
self.classifier = LRASPPHead(low_channels, high_channels, num_classes, inter_channels) | ||
|
||
def forward(self, input): | ||
features = self.backbone(input) | ||
out = self.classifier(features) | ||
out = F.interpolate(out, size=input.shape[-2:], mode='bilinear', align_corners=False) | ||
|
||
result = OrderedDict() | ||
result["out"] = out | ||
|
||
return result | ||
|
||
|
||
class LRASPPHead(nn.Module): | ||
|
||
def __init__(self, low_channels, high_channels, num_classes, inter_channels): | ||
super().__init__() | ||
self.cbr = nn.Sequential( | ||
nn.Conv2d(high_channels, inter_channels, 1, bias=False), | ||
nn.BatchNorm2d(inter_channels), | ||
nn.ReLU(inplace=True) | ||
) | ||
self.scale = nn.Sequential( | ||
nn.AdaptiveAvgPool2d(1), | ||
nn.Conv2d(high_channels, inter_channels, 1, bias=False), | ||
nn.Sigmoid(), | ||
) | ||
self.low_classifier = nn.Conv2d(low_channels, num_classes, 1) | ||
self.high_classifier = nn.Conv2d(inter_channels, num_classes, 1) | ||
|
||
def forward(self, input: Dict[str, Tensor]) -> Tensor: | ||
low = input["low"] | ||
high = input["high"] | ||
|
||
x = self.cbr(high) | ||
s = self.scale(high) | ||
x = x * s | ||
x = F.interpolate(x, size=low.shape[-2:], mode='bilinear', align_corners=False) | ||
|
||
return self.low_classifier(low) + self.high_classifier(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