Safemotion Lib
Loading...
Searching...
No Matches
splat.py
Go to the documentation of this file.
1# encoding: utf-8
2"""
3@author: xingyu liao
4@contact: sherlockliao01@gmail.com
5"""
6
7import torch
8import torch.nn.functional as F
9from torch import nn
10from torch.nn import Conv2d, ReLU
11from torch.nn.modules.utils import _pair
12from fastreid.layers import get_norm
13
14
15class SplAtConv2d(nn.Module):
16 """Split-Attention Conv2d
17 """
18
19 def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0),
20 dilation=(1, 1), groups=1, bias=True,
21 radix=2, reduction_factor=4,
22 rectify=False, rectify_avg=False, norm_layer=None, num_splits=1,
23 dropblock_prob=0.0, **kwargs):
24 super(SplAtConv2d, self).__init__()
25 padding = _pair(padding)
26 self.rectify = rectify and (padding[0] > 0 or padding[1] > 0)
27 self.rectify_avg = rectify_avg
28 inter_channels = max(in_channels * radix // reduction_factor, 32)
29 self.radix = radix
30 self.cardinality = groups
31 self.channels = channels
32 self.dropblock_prob = dropblock_prob
33 if self.rectify:
34 from rfconv import RFConv2d
35 self.conv = RFConv2d(in_channels, channels * radix, kernel_size, stride, padding, dilation,
36 groups=groups * radix, bias=bias, average_mode=rectify_avg, **kwargs)
37 else:
38 self.conv = Conv2d(in_channels, channels * radix, kernel_size, stride, padding, dilation,
39 groups=groups * radix, bias=bias, **kwargs)
40 self.use_bn = norm_layer is not None
41 if self.use_bn:
42 self.bn0 = get_norm(norm_layer, channels * radix)
43 self.relu = ReLU(inplace=True)
44 self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
45 if self.use_bn:
46 self.bn1 = get_norm(norm_layer, inter_channels)
47 self.fc2 = Conv2d(inter_channels, channels * radix, 1, groups=self.cardinality)
48
49 self.rsoftmax = rSoftMax(radix, groups)
50
51 def forward(self, x):
52 x = self.conv(x)
53 if self.use_bn:
54 x = self.bn0(x)
55 if self.dropblock_prob > 0.0:
56 x = self.dropblock(x)
57 x = self.relu(x)
58
59 batch, rchannel = x.shape[:2]
60 if self.radix > 1:
61 splited = torch.split(x, rchannel // self.radix, dim=1)
62 gap = sum(splited)
63 else:
64 gap = x
65 gap = F.adaptive_avg_pool2d(gap, 1)
66 gap = self.fc1(gap)
67
68 if self.use_bn:
69 gap = self.bn1(gap)
70 gap = self.relu(gap)
71
72 atten = self.fc2(gap)
73 atten = self.rsoftmax(atten).view(batch, -1, 1, 1)
74
75 if self.radix > 1:
76 attens = torch.split(atten, rchannel // self.radix, dim=1)
77 out = sum([att * split for (att, split) in zip(attens, splited)])
78 else:
79 out = atten * x
80 return out.contiguous()
81
82
83class rSoftMax(nn.Module):
84 def __init__(self, radix, cardinality):
85 super().__init__()
86 self.radix = radix
87 self.cardinality = cardinality
88
89 def forward(self, x):
90 batch = x.size(0)
91 if self.radix > 1:
92 x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2)
93 x = F.softmax(x, dim=1)
94 x = x.reshape(batch, -1)
95 else:
96 x = torch.sigmoid(x)
97 return x
__init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0), dilation=(1, 1), groups=1, bias=True, radix=2, reduction_factor=4, rectify=False, rectify_avg=False, norm_layer=None, num_splits=1, dropblock_prob=0.0, **kwargs)
Definition splat.py:23
__init__(self, radix, cardinality)
Definition splat.py:84