Pytorch在断网情况下加载pretrained model

在用resnet18时碰到这个问题。看下源码:

def resnet18(pretrained=False, **kwargs):
    """Constructs a ResNet-18 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
    return model

如果设定了pretrained=True,那么就会自动去model_urls这个地址下载。具体地址参考https://blog.csdn.net/sgfmby1994/article/details/103876681。

服务器断网情况下,可以先下载好模型,放到服务器指定路径/home/CN/myname/,然后用以下方式加载:

import torch #注意加上这一行

def resnet18(pretrained=False, **kwargs):
    """Constructs a ResNet-18 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
    if pretrained:
        # model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
        checkpoint=torch.load('/home/CN/myname/resnet18-5c106cde.pth') 
		model.load_state_dict(checkpoint)
    return model

你可能感兴趣的:(深度学习,pytorch,python)