这里定义了一个模型,先进行卷积,再通过注意力通道,重复三次。
class Model(nn.Module):
def __init__(self, block_units, width_factor):
super().__init__()
self.ca = [CAttention(32), CAttention(128), CAttention(256)]
self.conv1 = nn.Conv2d(3, 32, 3, 2, 1)
self.conv2 = nn.Conv2d(32, 128, 3, 2, 1)
self.conv3 = nn.Conv2d(128, 256, 3, 2, 1)
def forward(self, x):
x = self.conv1(x)
x = self.ca[0](x)
x = self.conv2(x)
x = self.ca[1](x)
x = self.conv2(x)
x = self.ca[2](x)
可以看到,这里的注意力机制放在了一个List里。之后跑出来就是Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same。
去看大佬的讲解,说是一部分模型未加载到Cuda里,可能有的层没放到init下。但是我把所有的层都放在了主干网络里,还是报错。然后发现是因为放有三个注意力通道的List。
在__init__中用到List,tuple等对象时,一定要注意不要直接用。可以考虑是否能用nn.ModuleList()。
应改为:
class Model(nn.Module):
def __init__(self, block_units, width_factor):
super().__init__()
self.ca = nn.ModuleList([CAttention(in_planes) for in_planes in (32, 64, 256)])
self.conv1 = nn.Conv2d(3, 32, 3, 2, 1)
self.conv2 = nn.Conv2d(32, 128, 3, 2, 1)
self.conv3 = nn.Conv2d(128, 256, 3, 2, 1)
def forward(self, x):
x = self.conv1(x)
x = self.ca[0](x)
x = self.conv2(x)
x = self.ca[1](x)
x = self.conv2(x)
x = self.ca[2](x)
https://blog.csdn.net/qq_42902997/article/details/122594017
https://blog.csdn.net/weixin_36670529/article/details/105910767