MCPcopy Create free account
hub / github.com/HobbitLong/PyContrast / SplAtConv2d

Class SplAtConv2d

pycontrast/networks/resnest.py:19–84  ·  view source on GitHub ↗

Split-Attention Conv2d

Source from the content-addressed store, hash-verified

17
18
19class SplAtConv2d(Module):
20 """Split-Attention Conv2d
21 """
22 def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0),
23 dilation=(1, 1), groups=1, bias=True,
24 radix=2, reduction_factor=4,
25 rectify=False, rectify_avg=False, norm_layer=None,
26 dropblock_prob=0.0, **kwargs):
27 super(SplAtConv2d, self).__init__()
28 padding = _pair(padding)
29 self.rectify = rectify and (padding[0] > 0 or padding[1] > 0)
30 self.rectify_avg = rectify_avg
31 inter_channels = max(in_channels*radix//reduction_factor, 32)
32 self.radix = radix
33 self.cardinality = groups
34 self.channels = channels
35 self.dropblock_prob = dropblock_prob
36 if self.rectify:
37 from rfconv import RFConv2d
38 self.conv = RFConv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
39 groups=groups*radix, bias=bias, average_mode=rectify_avg, **kwargs)
40 else:
41 self.conv = Conv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
42 groups=groups*radix, bias=bias, **kwargs)
43 self.use_bn = norm_layer is not None
44 self.bn0 = norm_layer(channels*radix)
45 self.relu = ReLU(inplace=True)
46 self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
47 self.bn1 = norm_layer(inter_channels)
48 self.fc2 = Conv2d(inter_channels, channels*radix, 1, groups=self.cardinality)
49 if dropblock_prob > 0.0:
50 self.dropblock = DropBlock2D(dropblock_prob, 3)
51
52 def forward(self, x):
53 x = self.conv(x)
54 if self.use_bn:
55 x = self.bn0(x)
56 if self.dropblock_prob > 0.0:
57 x = self.dropblock(x)
58 x = self.relu(x)
59
60 batch, channel = x.shape[:2]
61 if self.radix > 1:
62 splited = torch.split(x, channel//self.radix, dim=1)
63 gap = sum(splited)
64 else:
65 gap = x
66 gap = F.adaptive_avg_pool2d(gap, 1)
67 gap = self.fc1(gap)
68
69 if self.use_bn:
70 gap = self.bn1(gap)
71 gap = self.relu(gap)
72
73 atten = self.fc2(gap).view((batch, self.radix, self.channels))
74 if self.radix > 1:
75 atten = F.softmax(atten, dim=1).view(batch, -1, 1, 1)
76 else:

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected