当前位置:网站首页>[pytorch pre training model modification, addition and deletion of specific layers]
[pytorch pre training model modification, addition and deletion of specific layers]
2022-07-05 11:48:00 【Network starry sky (LUOC)】
List of articles
One 、 The introduction
In the process of building a deep learning network , It is often necessary to modify the pre training model and add or delete specific layers .
torchvision.models It provides a variety of models to meet the choice of different tasks , So when building the network structure , There is no need to reproduce a network structure from scratch , Just modify it on the basis of the official library .
Two 、 Official model library
pytorch The provided model can be queried through the following links :https://pytorch.org/vision/stable/models.html, Classification 、 Division 、 Object detection instance segmentation, key point detection and video classification 4 A classification , You can find the model you need .
Take the classification task as an example , What we use is resnet.torchvision.models Provides resnet18,resnet34,resnet50,resnet101,resnet152. The two columns on the right are where they are ImageNet Upper top1 Accuracy and top5 Accuracy.
Here we use resnet50 For example . The function is described as follows :
import torchvision.models as models
def Net(nn.Module):
def __init__(self, input_ch, num_class,pretrained=True):
super(Net,self).__init__()
self.model = models.resnet50(pretrained=pretrained)
def forward(self,x):
x = self.model(x)
return x
such , We define a Net, This Net Is a pre training weight used resnet50.
3、 ... and 、 Modify a specific layer
In use , One of the problems we may often encounter is , The number of input channels is inconsistent with that of the first layer of the network . Here, the first floor needs to be checked conv Make changes . If we initialize a conv layer , And I want to use the weight of pre training , What do you do then ? We can do this in the following ways .
resnet50 Of conv1 Weight dimension is [64,3,7,7], It means that the input image needs to be 3 passageway . Suppose the image we want to input is a grayscale image , that conv1 The number of input channels should be changed to 1.
Put the original nn.Conv2d(3, 64, kernel_size=(7,7), stride=(2,2), padding=(3,3), bias=False), Replace with nn.Conv2d(1, 64, kernel_size=(7,7), stride=(2,2), padding=(3,3), bias=False).
def Net(nn.Module):
def __init__(self, input_ch, num_class,pretrained=True):
super(Net,self).__init__()
self.model = models.resnet50(pretrained=pretrained)
conv1 = nn.Conv2d(1, 64, kernel_size=(7,7), stride=(2,2), padding=(3,3), bias=False) # new conv1 layer
self.model.conv1 = conv1 # Replace the original conv1
def forward(self,x):
x = self.model(x)
return x
Follow the operation above , be conv1 The pre training weight of cannot be utilized . In order to make use of conv1 Pre training weight of , We walked along dim=1 Draw , Expand the average weight to new conv1 The weight dimension is consistent .
def Net(nn.Module):
def __init__(self, input_ch, num_class,pretrained=True):
super(Net,self).__init__()
self.model = models.resnet50(pretrained=pretrained)
conv1_weight = torch.mean(self.model.conv1.weight,dim=1,keepdim=True).repeat(1,input_ch,1,1)# Remove from conv1 Weight and average and expand
conv1 = nn.Conv2d(input_ch, 64, kernel_size=(7,7), stride=(2,2), padding=(3,3), bias=False) # new conv1 layer
model_dict = self.model.state_dict()# Get the pre training weight of the whole network
self.model.conv1 = conv1 # Replace the original conv1
model_dict['conv1.weight'] = conv1_weight # take conv1 Replace the weight with a new one conv1 The weight
model_dict.update(model_dict)# Update the pre training weight of the whole network
self.model.load_state_dict(model_dict)# Load new pre training weights
def forward(self,x):
x = self.model(x)
return x
Four 、 Add or delete specific layers
We also often encounter the need to delete the last few layers of the network structure . Or to resnet50 For example . Suppose you want to complete a multi label classification task , To increase classifier.
import torchvision.models as models
class classifer(nn.Module):
def __init__(self,in_ch,num_classes):
super(classification_head,self).__init__()
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.fc = nn.Linear(in_ch,num_classes)
def forward(self, x):
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
# import pdb;pdb.set_trace()
return x
class Net(nn.Module):
def __init__(self, input_ch, num_class,pretrained=True):
super(Net,self).__init__()
model = models.resnet50(pretrained=pretrained)
self.backbone = nn.Sequential(*list(model.children())[:-3])# Put the last layer4,Avgpool and Fully Connected Layer Remove
self.classification_head1 = nn.Sequential(*list(model.children())[-3],
classifier(2048,3))
self.classification_head2 = nn.Sequential(*list(model.children())[-3],
classifier(2048,5))
def forward(self,x):
x = self.backbone(x)
output1 = self.classification_head1(x)
output2 = self.classification_head2(x)
return [output1,putput2]
take layer4 Also from the backbone It is separated and belongs to two classifer In order to avoid the mutual interference between the two classification tasks , Keep only the lower levels 、 Feature extraction for the network part with high commonality , The higher-level network carries out .
边栏推荐
- Open3D 欧式聚类
- 【PyTorch预训练模型修改、增删特定层】
- COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
- 7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
- Solve readobjectstart: expect {or N, but found n, error found in 1 byte of
- Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
- 【无标题】
- Pytorch training process was interrupted
- codeforces每日5题(均1700)-第五天
- 【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
猜你喜欢
Pytorch weight decay and dropout
如何让你的产品越贵越好卖
13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
7.2 daily study 4
【TFLite, ONNX, CoreML, TensorRT Export】
codeforces每日5题(均1700)-第五天
【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
【 YOLOv3中Loss部分计算】
一次生产环境redis内存占用居高不下问题排查
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
随机推荐
12.(地图数据篇)cesium城市建筑物贴图
The ninth Operation Committee meeting of dragon lizard community was successfully held
Network five whip
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
How to make your products as expensive as possible
Redirection of redis cluster
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
【 YOLOv3中Loss部分计算】
Manage multiple instagram accounts and share anti Association tips
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
C#实现WinForm DataGridView控件支持叠加数据绑定
redis主从模式
12. (map data) cesium city building map
1个插件搞定网页中的广告
How to understand super browser? What scenarios can it be used in? What brands are there?
COMSOL -- three-dimensional graphics random drawing -- rotation
Solve the grpc connection problem. Dial succeeds with transientfailure
liunx禁ping 详解traceroute的不同用法
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
View all processes of multiple machines