今天在进行torch模型的初始化的时候,发现报错:

Object has no attribute ‘weight’

回顾模型,发现在模型权重初始化函数,定义的带有conv的层的初始化是这样的。

def weights_init(m):
    """init the weight for a network"""
    classname=m.__class__.__name__
    # print(classname)
    if classname.find("conv")!=-1:
        nn.init.kaiming_normal_(
            m.weight.data,
            a=0,
            mode="fan_out"
        )
    elif classname.find("BatchNorm")!=-1:
        m.weight.data.fill_(1)
        m.bias.data.fill_(0)

然后回去看了一下模型的命名,发现定义的一个层,名字是conv_block,那么匹配到这个名字的时候,就会把conv_block当做卷积层进行初始化。

class conv_block(nn.Module):
    """
    Convolution Block 
    """
    def __init__(self, input_nc, output_nc):
        super(conv_block, self).__init__()
        
        self.conv = nn.Sequential(
            nn.Conv2d(input_nc, output_nc, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(output_nc),
            nn.ReLU(inplace=True),
            nn.Conv2d(output_nc, output_nc, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(output_nc),
            nn.ReLU(inplace=True))

    def forward(self, x):

        x = self.conv(x)
        return x


解决方法:,将conv具体制定为conv2d,问题解决。

def weights_init(m):
    """init the weight for a network"""
    classname=m.__class__.__name__
    # print(classname)
    if classname.find("conv2d")!=-1:
        nn.init.kaiming_normal_(
            m.weight.data,
            a=0,
            mode="fan_out"
        )
    elif classname.find("BatchNorm")!=-1:
        m.weight.data.fill_(1)
        m.bias.data.fill_(0)

类似问题参考:

AttributeError: ‘Sequential’ object has no attribute ‘weight’

Object has no attribute ‘weight’

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐