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
