pytorch解析.pth模型文件
pytorch训练出来的模型文件是.pth文件。
里面保存的是训练好的模型的参数,比如:权值(weight),偏置(bias)等。
.pth文件里面的数据结构类型是:collections.OrderedDict(有序字典)
解析pytorch模型文件的demo:
import torch
# 模型路径
pthfile = r'F:/experiment/image_identification/CCPD/code/RPnet/docs/model/wR2.pth'
# 由于模型原本是用GPU保存的,但我这台电脑上没有GPU,需要转化到CPU上
model = torch.load(pthfile, map_location=torch.device('cpu'))
print("type:")
# 类型是 dict
print(type(model))
print("length:")
# 查看模型字典长度
print(len(model))
print("key:")
# 查看模型字典里面的key
for k in model.keys():
print(k)
print("value:")
# 查看模型字典里面的value
for k in model:
print(k,model[k])