class DyCAConv(nn.Module): def init(self, inp, oup, kernel_size, stride, reduction=32): super(DyCAConv, self).init() self.pool_h = nn.AdaptiveAvgPool2d((None, 1)) self.pool_w = nn.AdaptiveAvgPool2d((1, None)) mip = max(8, inp // reduction) self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0) self.bn1 = nn.BatchNorm2d(mip) self.act = h_swish() self.conv_h = nn.Conv2d(mip, inp, kernel_size=1, stride=1, padding=0) self.conv_w = nn.Conv2d(mip, inp, kernel_size=1, stride=1, padding=0) self.conv = nn.Sequential(nn.Conv2d(inp, oup, kernel_size, padding=kernel_size // 2, stride=stride), nn.BatchNorm2d(oup), nn.SiLU()) self.dynamic_weight_fc = nn.Sequential( nn.Linear(inp, 2), nn.Softmax(dim=1) ) def forward(self, x): identity = x n, c, h, w = x.size() x_h = self.pool_h(x) x_w = self.pool_w(x).permute(0, 1, 3, 2) y = torch.cat([x_h, x_w], dim=2) y = self.conv1(y) y = self.bn1(y) y = self.act(y) x_h, x_w = torch.split(y, [h, w], dim=2) x_w = x_w.permute(0, 1, 3, 2) a_h = self.conv_h(x_h).sigmoid() a_w = self.conv_w(x_w).sigmoid() # Compute dynamic weights x_avg_pool = nn.AdaptiveAvgPool2d(1)(x) x_avg_pool = x_avg_pool.view(x.size(0), -1) dynamic_weights = self.dynamic_weight_fc(x_avg_pool) out = identity * (dynamic_weights[:, 0].view(-1, 1, 1, 1) * a_w + dynamic_weights[:, 1].view(-1, 1, 1, 1) * a_h) return self.conv(out)用公式写出
时间: 2023-10-05 18:09:16 浏览: 206
这段代码实现了一个动态通道注意力卷积(Dynamic Channel Attention Convolution),其公式表示为:
$$\text{DyCAConv}(x) = \text{Conv}\left(\text{ReLU}\left(\text{BN}\left(\text{Conv}\left(\text{Concat}\left(\text{AdaptiveAvgPool2d}(x), \text{AdaptiveAvgPool2d}(x).\text{permute}(0,1,3,2)\right)\right)\right)\right)\right) \cdot \left(\text{Softmax}\left(\text{Linear}(\text{AdaptiveAvgPool2d}(x).\text{view}(n,-1)))\right)[:,0].view(-1,1,1,1] \cdot a_w + \text{Softmax}\left(\text{Linear}(\text{AdaptiveAvgPool2d}(x).\text{view}(n,-1)))\right)[:,1].view(-1,1,1,1] \cdot a_h\right)$$
其中,
- $x$:输入特征图;
- $\text{AdaptiveAvgPool2d}(x)$:对输入特征图进行自适应平均池化,得到全局平均特征向量;
- $\text{Concat}(x_h, x_w)$:将对 $x$ 进行水平和垂直方向的自适应平均池化后的结果在通道维度上合并起来;
- $\text{Conv}$:卷积操作;
- $\text{BN}$:批归一化;
- $\text{ReLU}$:激活函数;
- $a_h$:水平方向上的注意力矩阵;
- $a_w$:垂直方向上的注意力矩阵;
- $\text{Linear}$:全连接层;
- $\text{Softmax}$:softmax 归一化函数;
- $[:,0].view(-1,1,1,1)$ 和 $[:,1].view(-1,1,1,1)$:从计算出的动态权重向量中取出对应的权重,并将其扩展为与输入特征图相同的维度;
- $\cdot$:点乘操作。
注:该公式省略了一些细节,具体实现需要结合代码进行理解。
阅读全文