• Pytorch加载.pth文件


    1. .pth文件

    (The weights of the model have been saved in a .pth file, which is nothing but a pickle file of the model’s tensor parameters.

    We can load those into resnet18 using the model’s load _state_dict method.)

    .pth文件报存了模型的权重,这个文件只是一个模型张量参数的pickle文件。

    我们可以使用模型的load _state_dict方法将它们加载到 resnet18 中

    2. 加载

    2.1 如果.pth文件只保存了参数,则如下:

     1 import torch
     2 from torch.serialization import load
     3 import torchvision.models as models
     4 
     5 # pretrained=True使用预训练的模型
     6 resnet18 = models.resnet18(pretrained=True)#创建实例,模型下载.Pth文件
     7 model_path = 'D:/python_code/resnet18/resnet18-5c106cde.pth' 
     8 model_data = torch.load(model_path)
     9 resnet18.load_state_dict(model_data)
    10 print(resnet18)

    输出为:

    2.2 如果.pth文件保存的是整个网络结构+参数,则:

    1 import torchvision.models as models
    2 
    3 # pretrained=True就可以使用预训练的模型
    4 resnet18 = models.resnet18(pretrained=True)
    5 print(resnet18)

    输出为:

    参考:https://blog.csdn.net/u014264373/article/details/85332181

               https://blog.csdn.net/u013679159/article/details/104253030

  • 相关阅读:
    软件工程实践2019第一次作业
    SDN第三次作业
    SDN第二次作业
    SDN第一次作业
    软工第二次结对
    #软件工程第三次作业
    2019软件工程第一次作业
    php-长文章分页函数
    Sqlilab靶机配置
    目录遍历(复现及修复)
  • 原文地址:https://www.cnblogs.com/vvzhang/p/14111737.html
Copyright © 2020-2023  润新知