MCPcopy Create free account
hub / github.com/QWTforGithub/T2LDM / SelectiveKernelAttn

Class SelectiveKernelAttn

timm/models/layers/selective_kernel.py:21–46  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19
20
21class SelectiveKernelAttn(nn.Module):
22 def __init__(self, channels, num_paths=2, attn_channels=32,
23 act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d):
24 """ Selective Kernel Attention Module
25
26 Selective Kernel attention mechanism factored out into its own module.
27
28 """
29 super(SelectiveKernelAttn, self).__init__()
30 self.num_paths = num_paths
31 self.fc_reduce = nn.Conv2d(channels, attn_channels, kernel_size=1, bias=False)
32 self.bn = norm_layer(attn_channels)
33 self.act = act_layer(inplace=True)
34 self.fc_select = nn.Conv2d(attn_channels, channels * num_paths, kernel_size=1, bias=False)
35
36 def forward(self, x):
37 assert x.shape[1] == self.num_paths
38 x = x.sum(1).mean((2, 3), keepdim=True)
39 x = self.fc_reduce(x)
40 x = self.bn(x)
41 x = self.act(x)
42 x = self.fc_select(x)
43 B, C, H, W = x.shape
44 x = x.view(B, self.num_paths, C // self.num_paths, H, W)
45 x = torch.softmax(x, dim=1)
46 return x
47
48
49class SelectiveKernel(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected