权重pt文件(.pth后缀)如何查看?
小罗碎碎念
我们在查看源码的时候,如果是预训练的模型,或者我们之前说的基础模型,那么就会涉及到一个内容——权重。但是可能有的人没有具体接触过,只是停留在“听说”。
今天另一篇推文介绍了三种UNet的变形,那么这里就以其中的一种作为分析,看一下作者提供的源码,里面就包含了权重文件.pth。
这篇推文先不教你怎么用这个文件,先教你怎么看这个文件的内容,这是以后也一直会用到的,建议收藏!!。
一、总代码
这段代码是使用PyTorch库加载一个预训练模型的权重文件(.pth文件),并打印出该模型的相关信息。
我是用Jupyter运行的代码,并导出的md文档,所以在第三部分会看到结果,并且我做了改动,删除了value值(太多了,也没有必要看,了解模型的架构就可以)。
# 导入PyTorch库
import torch
# 定义.pth文件的路径
pthfile = r'./last_ckpt.pt'
# 使用torch.load()函数加载.pth文件中的模型权重,指定在CPU环境下进行
model = torch.load(pthfile, torch.device('cpu'))
# 打印模型类型
print('type:')
print(type(model))
# 打印模型字典的长度
print('length:')
print(len(model))
# 遍历并打印模型字典中的所有键(key)
print('key:')
for k in model.keys():
print(k)
# 遍历并打印模型字典中的所有值(value)
print('value:')
for k in model:
print(k, model[k])
这段代码首先导入了PyTorch库,然后指定了一个.pth文件的路径。接着,使用torch.load()
函数加载了这个文件,并指定了在CPU环境下进行操作。之后,代码打印出了模型的类型、字典长度、所有的键和对应的值。
需要注意的是,torch.load()
函数返回的是一个包含模型状态的字典,而不是模型本身。如果你想要加载这个状态到一个已经定义好的模型实例中,你需要先创建模型实例,然后使用model.load_state_dict()
方法来加载状态字典。例如:
# 假设你已经定义了一个模型类MyModel
model_instance = MyModel()
# 加载状态字典到模型实例中
model_instance.load_state_dict(torch.load(pthfile, torch.device('cpu')))
这样,model_instance
就包含了从.pth文件中加载的权重和状态。
二、逐步输出
import torch
pthfile = r'/Users/luoxiaoluotongxue/Desktop/硕士课题进展记录/项目复现/TransAttUnet-main/pretrained-model.pth' # .pth文件的路径
model = torch.load(pthfile, torch.device('cpu')) # 设置在cpu环境下查询
print('type:')
print(type(model)) # 查看模型字典长度
print('length:')
print(len(model))
print('key:')
for k in model.keys(): # 查看模型字典里面的key
print(k)
print('value:')
for k in model: # 查看模型字典里面的value
print(k, model[k])
三、输出结果
这段输出展示了从.pth文件加载的模型权重的详细信息。
-
类型(type):模型权重以
collections.OrderedDict
的形式存储,这是一个有序字典,可以记住元素插入的顺序。 -
长度(length):模型权重字典包含229个条目。
-
键(key):字典中的键是模型中各个层的名称。这些名称通常由层的类型、层内的操作和层的序号组成。例如,
inc.double_conv.0.weight
表示输入卷积层(inc
)的第一个双卷积层(double_conv
)的权重。类似地,down1.maxpool_conv.1.double_conv.0.bias
表示第一个下采样(down1
)的最大池化卷积层(maxpool_conv
)的第一个双卷积层的偏置。 -
值(value):字典中的值是各个层的权重和偏置张量。这些张量的形状和值取决于层的类型和参数。
从输出中可以看出,这个模型包含多个卷积层、批量归一化层(running_mean
和running_var
)和全连接层(conv.weight
和conv.bias
)。此外,模型还包含一些特殊层,如位置嵌入层(pos.row_embed.weight
和pos.col_embed.weight
)和自注意力机制层(pam.*
)。
总之,这段输出展示了从.pth文件加载的模型权重的结构和内容。这些权重可以用于恢复模型的状态,从而在新数据上进行预测或继续训练。
type:
<class 'collections.OrderedDict'>
length:
229
key:
inc.double_conv.0.weight
inc.double_conv.0.bias
inc.double_conv.1.weight
inc.double_conv.1.bias
inc.double_conv.1.running_mean
inc.double_conv.1.running_var
inc.double_conv.1.num_batches_tracked
inc.double_conv.3.weight
inc.double_conv.3.bias
inc.double_conv.4.weight
inc.double_conv.4.bias
inc.double_conv.4.running_mean
inc.double_conv.4.running_var
inc.double_conv.4.num_batches_tracked
down1.maxpool_conv.1.double_conv.0.weight
down1.maxpool_conv.1.double_conv.0.bias
down1.maxpool_conv.1.double_conv.1.weight
down1.maxpool_conv.1.double_conv.1.bias
down1.maxpool_conv.1.double_conv.1.running_mean
down1.maxpool_conv.1.double_conv.1.running_var
down1.maxpool_conv.1.double_conv.1.num_batches_tracked
down1.maxpool_conv.1.double_conv.3.weight
down1.maxpool_conv.1.double_conv.3.bias
down1.maxpool_conv.1.double_conv.4.weight
down1.maxpool_conv.1.double_conv.4.bias
down1.maxpool_conv.1.double_conv.4.running_mean
down1.maxpool_conv.1.double_conv.4.running_var
down1.maxpool_conv.1.double_conv.4.num_batches_tracked
down2.maxpool_conv.1.double_conv.0.weight
down2.maxpool_conv.1.double_conv.0.bias
down2.maxpool_conv.1.double_conv.1.weight
down2.maxpool_conv.1.double_conv.1.bias
down2.maxpool_conv.1.double_conv.1.running_mean
down2.maxpool_conv.1.double_conv.1.running_var
down2.maxpool_conv.1.double_conv.1.num_batches_tracked
down2.maxpool_conv.1.double_conv.3.weight
down2.maxpool_conv.1.double_conv.3.bias
down2.maxpool_conv.1.double_conv.4.weight
down2.maxpool_conv.1.double_conv.4.bias
down2.maxpool_conv.1.double_conv.4.running_mean
down2.maxpool_conv.1.double_conv.4.running_var
down2.maxpool_conv.1.double_conv.4.num_batches_tracked
down3.maxpool_conv.1.double_conv.0.weight
down3.maxpool_conv.1.double_conv.0.bias
down3.maxpool_conv.1.double_conv.1.weight
down3.maxpool_conv.1.double_conv.1.bias
down3.maxpool_conv.1.double_conv.1.running_mean
down3.maxpool_conv.1.double_conv.1.running_var
down3.maxpool_conv.1.double_conv.1.num_batches_tracked
down3.maxpool_conv.1.double_conv.3.weight
down3.maxpool_conv.1.double_conv.3.bias
down3.maxpool_conv.1.double_conv.4.weight
down3.maxpool_conv.1.double_conv.4.bias
down3.maxpool_conv.1.double_conv.4.running_mean
down3.maxpool_conv.1.double_conv.4.running_var
down3.maxpool_conv.1.double_conv.4.num_batches_tracked
down4.maxpool_conv.1.double_conv.0.weight
down4.maxpool_conv.1.double_conv.0.bias
down4.maxpool_conv.1.double_conv.1.weight
down4.maxpool_conv.1.double_conv.1.bias
down4.maxpool_conv.1.double_conv.1.running_mean
down4.maxpool_conv.1.double_conv.1.running_var
down4.maxpool_conv.1.double_conv.1.num_batches_tracked
down4.maxpool_conv.1.double_conv.3.weight
down4.maxpool_conv.1.double_conv.3.bias
down4.maxpool_conv.1.double_conv.4.weight
down4.maxpool_conv.1.double_conv.4.bias
down4.maxpool_conv.1.double_conv.4.running_mean
down4.maxpool_conv.1.double_conv.4.running_var
down4.maxpool_conv.1.double_conv.4.num_batches_tracked
up1.conv.double_conv.0.weight
up1.conv.double_conv.0.bias
up1.conv.double_conv.1.weight
up1.conv.double_conv.1.bias
up1.conv.double_conv.1.running_mean
up1.conv.double_conv.1.running_var
up1.conv.double_conv.1.num_batches_tracked
up1.conv.double_conv.3.weight
up1.conv.double_conv.3.bias
up1.conv.double_conv.4.weight
up1.conv.double_conv.4.bias
up1.conv.double_conv.4.running_mean
up1.conv.double_conv.4.running_var
up1.conv.double_conv.4.num_batches_tracked
up2.conv.double_conv.0.weight
up2.conv.double_conv.0.bias
up2.conv.double_conv.1.weight
up2.conv.double_conv.1.bias
up2.conv.double_conv.1.running_mean
up2.conv.double_conv.1.running_var
up2.conv.double_conv.1.num_batches_tracked
up2.conv.double_conv.3.weight
up2.conv.double_conv.3.bias
up2.conv.double_conv.4.weight
up2.conv.double_conv.4.bias
up2.conv.double_conv.4.running_mean
up2.conv.double_conv.4.running_var
up2.conv.double_conv.4.num_batches_tracked
up3.conv.double_conv.0.weight
up3.conv.double_conv.0.bias
up3.conv.double_conv.1.weight
up3.conv.double_conv.1.bias
up3.conv.double_conv.1.running_mean
up3.conv.double_conv.1.running_var
up3.conv.double_conv.1.num_batches_tracked
up3.conv.double_conv.3.weight
up3.conv.double_conv.3.bias
up3.conv.double_conv.4.weight
up3.conv.double_conv.4.bias
up3.conv.double_conv.4.running_mean
up3.conv.double_conv.4.running_var
up3.conv.double_conv.4.num_batches_tracked
up4.conv.double_conv.0.weight
up4.conv.double_conv.0.bias
up4.conv.double_conv.1.weight
up4.conv.double_conv.1.bias
up4.conv.double_conv.1.running_mean
up4.conv.double_conv.1.running_var
up4.conv.double_conv.1.num_batches_tracked
up4.conv.double_conv.3.weight
up4.conv.double_conv.3.bias
up4.conv.double_conv.4.weight
up4.conv.double_conv.4.bias
up4.conv.double_conv.4.running_mean
up4.conv.double_conv.4.running_var
up4.conv.double_conv.4.num_batches_tracked
outc.conv.weight
outc.conv.bias
pos.row_embed.weight
pos.col_embed.weight
pam.gamma
pam.query_conv.weight
pam.query_conv.bias
pam.key_conv.weight
pam.key_conv.bias
pam.value_conv.weight
pam.value_conv.bias
fuse1.fuse_attn.0.weight
fuse1.fuse_attn.0.bias
fuse1.fuse_attn.1.weight
fuse1.fuse_attn.1.bias
fuse1.fuse_attn.1.running_mean
fuse1.fuse_attn.1.running_var
fuse1.fuse_attn.1.num_batches_tracked
fuse1.fuse_attn.2.weight
fuse1.fuse_attn.3.weight
fuse1.fuse_attn.3.bias
fuse1.fuse_attn.4.weight
fuse1.fuse_attn.4.bias
fuse1.fuse_attn.4.running_mean
fuse1.fuse_attn.4.running_var
fuse1.fuse_attn.4.num_batches_tracked
fuse1.fuse_attn.5.weight
fuse1.fuse_attn.6.weight
fuse1.fuse_attn.6.bias
fuse1.fuse_attn.7.weight
fuse1.fuse_attn.7.bias
fuse1.fuse_attn.7.running_mean
fuse1.fuse_attn.7.running_var
fuse1.fuse_attn.7.num_batches_tracked
fuse2.fuse_attn.0.weight
fuse2.fuse_attn.0.bias
fuse2.fuse_attn.1.weight
fuse2.fuse_attn.1.bias
fuse2.fuse_attn.1.running_mean
fuse2.fuse_attn.1.running_var
fuse2.fuse_attn.1.num_batches_tracked
fuse2.fuse_attn.2.weight
fuse2.fuse_attn.3.weight
fuse2.fuse_attn.3.bias
fuse2.fuse_attn.4.weight
fuse2.fuse_attn.4.bias
fuse2.fuse_attn.4.running_mean
fuse2.fuse_attn.4.running_var
fuse2.fuse_attn.4.num_batches_tracked
fuse2.fuse_attn.5.weight
fuse2.fuse_attn.6.weight
fuse2.fuse_attn.6.bias
fuse2.fuse_attn.7.weight
fuse2.fuse_attn.7.bias
fuse2.fuse_attn.7.running_mean
fuse2.fuse_attn.7.running_var
fuse2.fuse_attn.7.num_batches_tracked
fuse3.fuse_attn.0.weight
fuse3.fuse_attn.0.bias
fuse3.fuse_attn.1.weight
fuse3.fuse_attn.1.bias
fuse3.fuse_attn.1.running_mean
fuse3.fuse_attn.1.running_var
fuse3.fuse_attn.1.num_batches_tracked
fuse3.fuse_attn.2.weight
fuse3.fuse_attn.3.weight
fuse3.fuse_attn.3.bias
fuse3.fuse_attn.4.weight
fuse3.fuse_attn.4.bias
fuse3.fuse_attn.4.running_mean
fuse3.fuse_attn.4.running_var
fuse3.fuse_attn.4.num_batches_tracked
fuse3.fuse_attn.5.weight
fuse3.fuse_attn.6.weight
fuse3.fuse_attn.6.bias
fuse3.fuse_attn.7.weight
fuse3.fuse_attn.7.bias
fuse3.fuse_attn.7.running_mean
fuse3.fuse_attn.7.running_var
fuse3.fuse_attn.7.num_batches_tracked
fuse4.fuse_attn.0.weight
fuse4.fuse_attn.0.bias
fuse4.fuse_attn.1.weight
fuse4.fuse_attn.1.bias
fuse4.fuse_attn.1.running_mean
fuse4.fuse_attn.1.running_var
fuse4.fuse_attn.1.num_batches_tracked
fuse4.fuse_attn.2.weight
fuse4.fuse_attn.3.weight
fuse4.fuse_attn.3.bias
fuse4.fuse_attn.4.weight
fuse4.fuse_attn.4.bias
fuse4.fuse_attn.4.running_mean
fuse4.fuse_attn.4.running_var
fuse4.fuse_attn.4.num_batches_tracked
fuse4.fuse_attn.5.weight
fuse4.fuse_attn.6.weight
fuse4.fuse_attn.6.bias
fuse4.fuse_attn.7.weight
fuse4.fuse_attn.7.bias
fuse4.fuse_attn.7.running_mean
fuse4.fuse_attn.7.running_var
fuse4.fuse_attn.7.num_batches_tracked