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)) self.pool_h1 = nn.MaxPool2d((None, 1)) self.pool_w1 = nn.MaxPool2d((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) x_h1 = self.pool_h1(x) x_w1 = self.pool_w1(x).permute(0, 1, 3, 2) y = torch.cat([x_h, x_w, x_h1, x_w1], dim=2) y = self.conv1(y) y = self.bn1(y) y = self.act(y) x_h, x_w, _, _ = torch.split(y, [h, w, h, w], dim=2) x_w = x_w.permute(0, 1, 3, 2) x_w1 = x_w1.permute(0, 1, 3, 2) a_h = self.conv_h(x_h).sigmoid() a_w = self.conv_w(x_w).sigmoid() a_w1 = self.conv_w(x_w1).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 + dynamic_weights[:, 1].view(-1, 1, 1, 1) * a_w1) return self.conv(out)在里面修改一下,换成这个y = torch.cat([x_h+x_h1, x_w+x_w1], dim=2)
时间: 2023-11-15 16:06:26 浏览: 147
Inpout32_dll_source.rar_ Inpout32.dll_inpout32.dll_lpt_visual c
如果您想将原来代码中的y拼接成x_h+x_h1和x_w+x_w1,可以按照以下方式进行修改:
```
# 将原来的y拆分成x_h, x_w, x_h1, x_w1
x_h, x_w, x_h1, x_w1 = torch.split(y, [h, w, h, w], dim=2)
# 将x_h和x_h1相加,x_w和x_w1相加
x_h = x_h + x_h1
x_w = x_w + x_w1
# 将x_h和x_w拼接起来
y = torch.cat([x_h, x_w], dim=2)
```
这样就可以将x_h和x_h1相加,x_w和x_w1相加,并将它们拼接起来,作为新的y输出了。
阅读全文