|
| 1 | +""" Split Attention Conv2d (for ResNeSt Models) |
| 2 | +
|
| 3 | +Paper: `ResNeSt: Split-Attention Networks` - /https://arxiv.org/abs/2004.08955 |
| 4 | +
|
| 5 | +Adapted from original PyTorch impl at https://github.com/zhanghang1989/ResNeSt |
| 6 | +
|
| 7 | +Modified for torchscript compat, performance, and consistency with timm by Ross Wightman |
| 8 | +""" |
| 9 | +import torch |
| 10 | +import torch.nn.functional as F |
| 11 | +from torch import nn |
| 12 | + |
| 13 | + |
| 14 | +class RadixSoftmax(nn.Module): |
| 15 | + def __init__(self, radix, cardinality): |
| 16 | + super(RadixSoftmax, self).__init__() |
| 17 | + self.radix = radix |
| 18 | + self.cardinality = cardinality |
| 19 | + |
| 20 | + def forward(self, x): |
| 21 | + batch = x.size(0) |
| 22 | + if self.radix > 1: |
| 23 | + x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2) |
| 24 | + x = F.softmax(x, dim=1) |
| 25 | + x = x.reshape(batch, -1) |
| 26 | + else: |
| 27 | + x = torch.sigmoid(x) |
| 28 | + return x |
| 29 | + |
| 30 | + |
| 31 | +class SplitAttnConv2d(nn.Module): |
| 32 | + """Split-Attention Conv2d |
| 33 | + """ |
| 34 | + def __init__(self, in_channels, channels, kernel_size, stride=1, padding=0, |
| 35 | + dilation=1, groups=1, bias=False, radix=2, reduction_factor=4, |
| 36 | + act_layer=nn.ReLU, norm_layer=None, drop_block=None, **kwargs): |
| 37 | + super(SplitAttnConv2d, self).__init__() |
| 38 | + self.radix = radix |
| 39 | + self.cardinality = groups |
| 40 | + self.channels = channels |
| 41 | + mid_chs = channels * radix |
| 42 | + attn_chs = max(in_channels * radix // reduction_factor, 32) |
| 43 | + self.conv = nn.Conv2d( |
| 44 | + in_channels, mid_chs, kernel_size, stride, padding, dilation, |
| 45 | + groups=groups * radix, bias=bias, **kwargs) |
| 46 | + self.bn0 = norm_layer(mid_chs) if norm_layer is not None else None |
| 47 | + self.act0 = act_layer(inplace=True) |
| 48 | + self.fc1 = nn.Conv2d(channels, attn_chs, 1, groups=self.cardinality) |
| 49 | + self.bn1 = norm_layer(attn_chs) if norm_layer is not None else None |
| 50 | + self.act1 = act_layer(inplace=True) |
| 51 | + self.fc2 = nn.Conv2d(attn_chs, mid_chs, 1, groups=self.cardinality) |
| 52 | + self.drop_block = drop_block |
| 53 | + self.rsoftmax = RadixSoftmax(radix, groups) |
| 54 | + |
| 55 | + def forward(self, x): |
| 56 | + x = self.conv(x) |
| 57 | + if self.bn0 is not None: |
| 58 | + x = self.bn0(x) |
| 59 | + if self.drop_block is not None: |
| 60 | + x = self.drop_block(x) |
| 61 | + x = self.act0(x) |
| 62 | + |
| 63 | + B, RC, H, W = x.shape |
| 64 | + if self.radix > 1: |
| 65 | + x = x.reshape((B, self.radix, RC // self.radix, H, W)) |
| 66 | + x_gap = torch.sum(x, dim=1) |
| 67 | + else: |
| 68 | + x_gap = x |
| 69 | + x_gap = F.adaptive_avg_pool2d(x_gap, 1) |
| 70 | + x_gap = self.fc1(x_gap) |
| 71 | + if self.bn1 is not None: |
| 72 | + x_gap = self.bn1(x_gap) |
| 73 | + x_gap = self.act1(x_gap) |
| 74 | + x_attn = self.fc2(x_gap) |
| 75 | + |
| 76 | + x_attn = self.rsoftmax(x_attn).view(B, -1, 1, 1) |
| 77 | + if self.radix > 1: |
| 78 | + out = (x * x_attn.reshape((B, self.radix, RC // self.radix, 1, 1))).sum(dim=1) |
| 79 | + else: |
| 80 | + out = x * x_attn |
| 81 | + return out.contiguous() |
0 commit comments