-
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.
Add 'CBAM (Convolutional Block Attention Module)'
- Loading branch information
Showing
4 changed files
with
70 additions
and
7 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,65 @@ | ||
import torch | ||
from torch import nn | ||
from .vanilla_conv2d import Conv2d1x1 | ||
from .norm_act import normalizer_fn, activation_fn | ||
|
||
|
||
class ChannelAttention(nn.Module): | ||
def __init__( | ||
self, | ||
in_channels, | ||
rd_ratio, | ||
gate_fn: nn.Module = nn.Sigmoid | ||
) -> None: | ||
super().__init__() | ||
|
||
rd_channels = int(in_channels * rd_ratio) | ||
|
||
self.max_pool = nn.AdaptiveMaxPool2d((1, 1)) | ||
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1)) | ||
|
||
self.mlp = nn.Sequential( | ||
Conv2d1x1(in_channels, rd_channels, bias=True), | ||
activation_fn(), | ||
Conv2d1x1(rd_channels, in_channels, bias=True) | ||
) | ||
self.gate = gate_fn() | ||
|
||
def forward(self, x): | ||
return x * self.gate(self.mlp(self.max_pool(x)) + self.mlp(self.avg_pool(x))) | ||
|
||
|
||
class SpatialAttention(nn.Module): | ||
def __init__( | ||
self, | ||
kernel_size: int = 7, | ||
gate_fn: nn.Module = nn.Sigmoid | ||
) -> None: | ||
super().__init__() | ||
|
||
self.conv = nn.Conv2d(2, 1, kernel_size, padding=(kernel_size - 1) // 2, bias=False) | ||
self.norm = normalizer_fn(1) | ||
self.gate = gate_fn() | ||
|
||
def forward(self, x): | ||
s = torch.cat([torch.amax(x, dim=1, keepdim=True), torch.mean(x, dim=1, keepdim=True)], dim=1) | ||
return x * self.gate(self.norm(self.conv(s))) | ||
|
||
|
||
class CBAM(nn.Sequential): | ||
r""" | ||
Paper: CBAM: Convolutional Block Attention Module, https://arxiv.org/abs/1807.06521 | ||
Code: https://github.com/Jongchan/attention-module | ||
""" | ||
|
||
def __init__( | ||
self, | ||
in_channels, | ||
rd_ratio, | ||
kernel_size: int = 7, | ||
gate_fn: nn.Module = nn.Sigmoid | ||
) -> None: | ||
super().__init__( | ||
ChannelAttention(in_channels, rd_ratio, gate_fn=gate_fn), | ||
SpatialAttention(kernel_size=kernel_size, gate_fn=gate_fn) | ||
) |
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.25' | ||
__version__ = '0.0.26' |