-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
5 changed files
with
64 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import torch | ||
from torch import nn | ||
from .vanilla_conv2d import Conv2d1x1 | ||
from .norm import LayerNorm2d | ||
|
||
|
||
class GlobalContextBlock(nn.Module): | ||
r""" | ||
Paper: GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond, https://arxiv.org/abs/1904.11492 | ||
""" | ||
|
||
def __init__( | ||
self, | ||
in_channels, | ||
rd_ratio | ||
) -> None: | ||
super().__init__() | ||
|
||
channels = int(in_channels * rd_ratio) | ||
|
||
self.conv1x1 = Conv2d1x1(in_channels, 1, bias=True) | ||
self.softmax = nn.Softmax(dim=1) | ||
|
||
self.transform = nn.Sequential( | ||
Conv2d1x1(in_channels, channels), | ||
LayerNorm2d(channels), | ||
nn.ReLU(inplace=True), | ||
Conv2d1x1(channels, in_channels) | ||
) | ||
|
||
def forward(self, x): | ||
# context modeling | ||
c = torch.einsum( | ||
"ncx, nxo -> nco", | ||
x.view(x.shape[0], x.shape[1], -1), | ||
self.softmax(self.conv1x1(x).view(x.shape[0], -1, 1)) | ||
) | ||
c = x * c.unsqueeze(-1) | ||
|
||
# transform | ||
return x + self.transform(c) |
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,18 @@ | ||
from torch import nn | ||
import torch.nn.functional as F | ||
|
||
|
||
class LayerNorm2d(nn.LayerNorm): | ||
""" LayerNorm for channels of '2D' spatial BCHW tensors """ | ||
|
||
def __init__( | ||
self, | ||
channels | ||
): | ||
super().__init__(channels) | ||
|
||
def forward(self, x): | ||
x = x.permute(0, 2, 3, 1) | ||
x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) | ||
x = x.permute(0, 3, 1, 2) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.0.26' | ||
__version__ = '0.0.27' |