AttributeError: ‘Functional‘ object has no attribute ‘load_state_dict‘

源代码
net = resnet101()
# net = resnet34(num_classes=5)
# load pretrain weights
model_weight_path = "./resnet101.pth"
#model_weight_path = "./resnet34-pre.pth"

missing_keys, unexpected_keys = net.load_state_dict(torch.load(model_weight_path), strict=False)#载入模型参数

出错AttributeError: 'Functional' object has no attribute 'load_state_dict'

找到定义

def resnet101(im_width=224, im_height=224, num_classes=1000, include_top=True):
    return _resnet(Bottleneck, [3, 4, 23, 3], im_width, im_height, num_classes, include_top)

 

def _resnet(block, blocks_num, im_width=224, im_height=224, num_classes=1000, include_top=True):
    # 定义输入(batch, 224, 224, 3)
    input_image = layers.Input(shape=(im_height, im_width, 3), dtype="float32")
    # 第一层conv1
    x = layers.Conv2D(filters=64, kernel_size=7, strides=2,

运行不行,改成以下

def resnet101(pretrained=False, **kwargs):
    """Constructs a ResNet-101 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet101']))
    return model
class ResNet(nn.Module):

    def __init__(self, block, layers, num_classes=1000):
        self.inplanes = 64
        super(ResNet, self).__init__()
       。。。

           def _make_layer(self, block, planes, blocks, stride=1):

错误解决

 

你可能感兴趣的:(pytorch)