AdaptiveAvgPool2d(x)用法

AdaptiveAvgPool2d(x)
自适应平均池化
只需给出输出的featere_map的size x
自动计算kenerl和stride

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) 在 self.pool_h = nn.AdaptiveAvgPool2d((None, 1)) self.pool_w = nn.AdaptiveAvgPool2d((1, None))这里继续添加 self.pool_w1 = nn.MaxPool2d((1, None)) self.pool_h1 = nn.MaxPool2d((None, 1))
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值