当前位置:网站首页>Freezing and thawing of pytoch
Freezing and thawing of pytoch
2022-07-28 07:53:00 【Hyacinth's cat redamancy】
Load part of the pre training model
In fact, most of the time, we need to adjust our model according to our tasks , Therefore, it is difficult to ensure that the model is exactly the same as the public model , But the parameters of the pre training model do help to improve the accuracy of training , In order to combine the advantages of the two , We need to load some pre training models .
pretrained_dict = model_zoo.load_url(model_urls['resnet152'])
model_dict = model.state_dict()
# take pretrained_dict It doesn't belong to model_dict Key out of
pretrained_dict = {
k: v for k, v in pretrained_dict.items() if k in model_dict}
# Update existing model_dict
model_dict.update(pretrained_dict)
# Load what we really need state_dict
model.load_state_dict(model_dict)
Because we need to eliminate the mismatched keys in the original model , That is, the name of the layer , Therefore, the changed layer of our new model needs to be different from the name of the corresponding layer of the original model , such as :resnet The name of the last floor is fc(PyTorch in ), So what we modified resnet You can't take this name on the last floor of , It can be called fc_
Micro change basic model
PyTorch Medium torchvision There are many commonly used models in , Can be called directly :
- AlexNet
- VGG
- ResNet
- SqueezeNet
- DenseNet
import torchvision.models as models
resnet18 = models.resnet18()
alexnet = models.alexnet()
squeezenet = models.squeezenet1_0()
densenet = models.densenet_161()
But for our task, some layers are not directly usable , We need to change it a little , such as ,resnet The final full connection layer is divided into 1000 class , And we only 21 class ; And such as ,resnet The channel of convolution reception in the first layer is 3, The channel we may enter the picture is 4, Then it can be modified by the following methods :
resnet.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)
resnet.fc = nn.Linear(2048, 21)
Step by step a thaw ( stay ECCV2020 The training method proposed in )
Load one FeatureExtractor
With densenet Of dense block For example :
dense = densenet121(pretrained=True)
self.loss_network = nn.Sequential(*list(dense.features)[:5])
self.loss_network.apply(add_sn)
for param in self.loss_network.parameters():
param.requires_grad = False
# vgg=vgg16(pretrained=True)
# self.loss_network = nn.Sequential(*list(vgg.features)[:16]).eval()
# self.loss_network.apply(add_sn)
# for param in self.loss_network.parameters():
# param.requires_grad = True
Set up a list, Store frozen layers , Convenient for later thawing
self.grad = []
for name, value in self.loss_network.named_parameters():
print('name: {0},\t grad: {1}'.format(name, value.requires_grad))
if value.requires_grad == False:
self.grad.append(name)
thaw ,unfreeze1 function ( Thaw a layer from bottom to top )
def unfreeze1(self):
if len(self.grad)==0:
print("All of network have been unfreeze!")
return
self.grad.pop()
print("-------------------")
for name, value in self.loss_network.named_parameters():
if name not in self.grad:
value.requires_grad = True
for name, value in self.loss_network.named_parameters():
print('name: {0},\t grad: {1}'.format(name, value.requires_grad))
print("---------------------")
Show freeze :
def showfreeze(self):
for i in self.grad:
print(i)
print("-------------------")
Thaw during training
network.unfreeze1()
边栏推荐
- 细说共模干扰和差模干扰
- 解析树形结构 js
- [Google] solve the problem that Google browser does not pop up the account and password save box and cannot save login information
- EMC中的基石-电磁兼容滤波知识大全!
- Merge two sorted linked lists - two questions per day
- 干货|分享一个EMC实际案例及整改过程
- 4.1.4为什么要将成员变量设置为private
- 再次出现用户净流失,大失颜面的中国移动推出超低价套餐争取用户
- EMC问题的根源在哪?
- EMC中class A和class B哪个更严格?
猜你喜欢

铜铟硫CuInSe2量子点修饰DNA(脱氧核糖核酸)DNA-CuInSe2QDs(齐岳)

Summary of project experience

收藏 | 结合个人经验,我总结了这7点EMC相关知识

再次出现用户净流失,大失颜面的中国移动推出超低价套餐争取用户

Deeply analyze the implementation of singleton mode

【17】建立数据通路(上):指令+运算=CPU

SWM32系列教程5-ADC应用

【干货】32个EMC标准电路分享!

Industry standards and certification of common electronic products

CLion调试redis6源码
随机推荐
整改了七次,花了半个月时间,惨痛的EMC总结
链表中倒数第k个节点——双指
ArcGIS JS自定义Accessor,并通过watchUtils相关方法watch属性
非关系型数据库之Redis【Jedis客户端+Jedis连接集群】
Synthesis of dna-ag2sqds DNA modified silver sulfide Ag2S quantum dots
User mode vs kernel mode, process vs thread
DNA cuinseqds near infrared CuInSe quantum dots wrapped deoxyribonucleic acid DNA
磁环选型攻略及EMC整改技巧
The net loss of users occurred again, and China Mobile, which lost face, launched ultra-low price packages to win users
两个链表的第一个公共节点——每日两题
ArcGIS JS map internal and external network environment judgment
DNA脱氧核糖核酸修饰金属铂纳米颗粒PtNPS-DNA|科研试剂
The underlying principles of RDB persistence and AOF persistence of redis
Copper indium sulfide CuInSe2 quantum dots modified DNA (deoxyribonucleic acid) DNA cuinse2qds (Qiyue)
And is two numbers of S - two questions per day
Elaborate on common mode interference and differential mode interference
YOLO系列损失函数详解
细说共模干扰和差模干扰
Near infrared two region agzs quantum dots wrapped deoxyribonucleic acid dna|dna agzsqds (Qiyue)
“蔚来杯“2022牛客暑期多校训练营2补题记录(DGHJKL)