将卷积运算放到初始化中,与模型初始化一块加载到cuda
原来代码:
class layer(nn.Module):
'''
x: input features with shape [N,C,H,W]
gamma, b: parameters of mapping function
'''
def __init__(self):
super(layer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.sigmoid = nn.Sigmoid()
def forward(self, x, gamma=2, b=1):
n,channel,h,w = x.size()
t = int(abs((math.log(channel, 2) + b) / gamma))
k = t if t % 2 else t + 1
conv = nn.Conv1d(1, 1, kernel_size=k, padding=int(k/2), bias=False)
y = self.avg_pool(x)
y = conv(y.squeeze(-1).transpose(-1, -2))
y = y.transpose(-1, -2).unsqueeze(-1)
y = self.sigmoid(y)
return x * y.expand_as(x)
修改后:
class layer(nn.Module):
'''
x: input features with shape [N,C,H,W]
gamma, b: parameters of mapping function
'''
def __init__(self, channel, gamma=2, b=1):
super(layer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
t = int(abs((math.log(channel, 2) + b) / gamma))
k = t if t % 2 else t + 1
self.conv = nn.Conv1d(1, 1, kernel_size=k, padding=int(k/2), bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
y = self.avg_pool(x)
y = self.conv(y.squeeze(-1).transpose(-1, -2))
y = y.transpose(-1, -2).unsqueeze(-1)
y = self.sigmoid(y)
return x * y.expand_as(x)