pytorch对权重文件(model.pth / model.weights)的处理方式
pytorch 打印模型层的名字的多个方式,以及对应显示,删除最后多个层的两种方式
def forward(self, x, last_cont=None): x = self.model(x) if self.use_dcl: mask = self.Convmask(x) mask = self.avgpool2(mask) mask = torch.tanh(mask) mask = mask.view(mask.size(0), -1) x = self.avgpool(x) x = x.view(x.size(0), -1) out = [] out.append(self.classifier(x)) if self.use_dcl: out.append(self.classifier_swap(x)) out.append(mask)
1、 for name, module in model._modules.items():
print (name," : ",module)
这里的名字模型定义的时候,前向传播的一个大块,每个大块里面的是多个小块包含在module中
for name, module in model._modules.items(): print (name," : ",module) print ("**********") for name, module in model._modules.items(): print (name) 打印################################## (relu): ReLU(inplace) (se_module): SEModule( (avg_pool): AdaptiveAvgPool2d(output_size=1) (fc1): Conv2d(2048, 128, kernel_size=(1, 1), stride=(1, 1)) (relu): ReLU(inplace) (fc2): Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1)) (sigmoid): Sigmoid() ) ) ) ) avgpool : AdaptiveAvgPool2d(output_size=1) classifier : Linear(in_features=2048, out_features=402, bias=False) classifier_swap : Linear(in_features=2048, out_features=804, bias=False) Convmask : Conv2d(2048, 1, kernel_size=(1, 1), stride=(1, 1)) avgpool2 : AvgPool2d(kernel_size=2, stride=2, padding=0) ********** model avgpool classifier classifier_swap Convmask avgpool2
1、 for n in model.named_modules():
print (n)
打印是一个元组,层的名字和对应的类型: ... ('model.4.2.se_module.avg_pool', AdaptiveAvgPool2d(output_size=1)) ('model.4.2.se_module.fc1', Conv2d(2048, 128, kernel_size=(1, 1), stride=(1, 1))) ('model.4.2.se_module.relu', ReLU(inplace)) ('model.4.2.se_module.fc2', Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1))) ('model.4.2.se_module.sigmoid', Sigmoid()) ('avgpool', AdaptiveAvgPool2d(output_size=1)) ('classifier', Linear(in_features=2048, out_features=402, bias=False)) ('classifier_swap', Linear(in_features=2048, out_features=804, bias=False)) ('Convmask', Conv2d(2048, 1, kernel_size=(1, 1), stride=(1, 1))) ('avgpool2', AvgPool2d(kernel_size=2, stride=2, padding=0))
2、 for n in (model.children()):
print (n)
打印的是所有层的类型,以及对应输入输出维度,参数
(2): SEResNetBottleneck( (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (relu): ReLU(inplace) (se_module): SEModule( (avg_pool): AdaptiveAvgPool2d(output_size=1) (fc1): Conv2d(2048, 128, kernel_size=(1, 1), stride=(1, 1)) (relu): ReLU(inplace) (fc2): Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1)) (sigmoid): Sigmoid() ) ) ) ) AdaptiveAvgPool2d(output_size=1) Linear(in_features=2048, out_features=402, bias=False) Linear(in_features=2048, out_features=804, bias=False) Conv2d(2048, 1, kernel_size=(1, 1), stride=(1, 1)) AvgPool2d(kernel_size=2, stride=2, padding=0)
3、 for n in (model.modules()):
print (n)
(se_module): SEModule( (avg_pool): AdaptiveAvgPool2d(output_size=1) (fc1): Conv2d(2048, 128, kernel_size=(1, 1), stride=(1, 1)) (relu): ReLU(inplace) (fc2): Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1)) (sigmoid): Sigmoid() ) ) Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False) BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False) BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ReLU(inplace) SEModule( (avg_pool): AdaptiveAvgPool2d(output_size=1) (fc1): Conv2d(2048, 128, kernel_size=(1, 1), stride=(1, 1)) (relu): ReLU(inplace) (fc2): Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1)) (sigmoid): Sigmoid() ) AdaptiveAvgPool2d(output_size=1) Conv2d(2048, 128, kernel_size=(1, 1), stride=(1, 1)) ReLU(inplace) Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1)) Sigmoid() AdaptiveAvgPool2d(output_size=1) Linear(in_features=2048, out_features=402, bias=False) Linear(in_features=2048, out_features=804, bias=False) Conv2d(2048, 1, kernel_size=(1, 1), stride=(1, 1)) AvgPool2d(kernel_size=2, stride=2, padding=0)
4、 for ind,i in model.state_dict().items():
print (ind,i.shape)
打印的是权重的层的名字和对应形状,顺序可能不是对的
model.4.2.bn1.num_batches_tracked torch.Size([]) model.4.2.conv2.weight torch.Size([512, 512, 3, 3]) model.4.2.bn2.weight torch.Size([512]) model.4.2.bn2.bias torch.Size([512]) model.4.2.bn2.running_mean torch.Size([512]) model.4.2.bn2.running_var torch.Size([512]) model.4.2.bn2.num_batches_tracked torch.Size([]) model.4.2.conv3.weight torch.Size([2048, 512, 1, 1]) model.4.2.bn3.weight torch.Size([2048]) model.4.2.bn3.bias torch.Size([2048]) model.4.2.bn3.running_mean torch.Size([2048]) model.4.2.bn3.running_var torch.Size([2048]) model.4.2.bn3.num_batches_tracked torch.Size([]) model.4.2.se_module.fc1.weight torch.Size([128, 2048, 1, 1]) model.4.2.se_module.fc1.bias torch.Size([128]) model.4.2.se_module.fc2.weight torch.Size([2048, 128, 1, 1]) model.4.2.se_module.fc2.bias torch.Size([2048]) classifier.weight torch.Size([402, 2048]) classifier_swap.weight torch.Size([804, 2048]) Convmask.weight torch.Size([1, 2048, 1, 1]) Convmask.bias torch.Size([1])
module 和 children返回的区别,mododule更多
最后删除层的方式两种
#resnet = models.resnet50(pretrained=True) modules = list(model.children())[:-4] # #删除最后四个个层 【-1】删除最后一个层 model = torch.nn.Sequential(*modules)
这种方式最后的层的名字会变成数字
(‘model.4.2.se_module.relu’, ReLU(inplace)) 会变成(‘0.4.2.se_module.relu’, ReLU(inplace))
(‘avgpool’, AdaptiveAvgPool2d(output_size=1))会变成(‘1’, AdaptiveAvgPool2d(output_size=1))
model>>0
avgpool>>1
把名字的 点 的第一个名字变成数字,没有点就是整体的名字变成数字
。。。。 ('0.4.2.se_module.relu', ReLU(inplace)) ('0.4.2.se_module.fc2', Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1))) ('0.4.2.se_module.sigmoid', Sigmoid()) ('1', AdaptiveAvgPool2d(output_size=1)) ('2', Linear(in_features=2048, out_features=402, bias=False)) ('3', Linear(in_features=2048, out_features=804, bias=False)) ('4', Conv2d(2048, 1, kernel_size=(1, 1), stride=(1, 1))) ####原来是按照模型结构定义的名字 ('model.4.2.se_module.relu', ReLU(inplace)) ('model.4.2.se_module.fc2', Conv2d(128, 2048, kernel_size=(1, 1), stride=(1, 1))) ('model.4.2.se_module.sigmoid', Sigmoid()) ('avgpool', AdaptiveAvgPool2d(output_size=1)) ('classifier', Linear(in_features=2048, out_features=402, bias=False)) ('classifier_swap', Linear(in_features=2048, out_features=804, bias=False)) ('Convmask', Conv2d(2048, 1, kernel_size=(1, 1), stride=(1, 1))) ('avgpool2', AvgPool2d(kernel_size=2, stride=2, padding=0))
方法2打印模型名字,不改变其他层名字
# del model.classifier # del model.classifier_swap # del model.Convmask # del model.avgpool2 直接对模型进行del ,不知道名字,先打印,名字,然后直接删除 for n in model.named_modules(): print (n)
————————————————
版权声明:本文为CSDN博主「shishi_m037192554」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37192554/article/details/104003947
如果这篇文章帮助到了你,你可以请作者喝一杯咖啡
![](https://img2020.cnblogs.com/blog/938105/202010/938105-20201013171833350-276632012.png)