当前位置:网站首页>Pytorch: sub model parameter freezing + BN freezing
Pytorch: sub model parameter freezing + BN freezing
2022-06-10 04:46:00 【CV research Capriccio】
Use scenarios : Need to completely freeze a part of weight And BN layer
When loading the pre training model , If only para.requires_grad = False , The parameters of the model cannot be completely frozen , Because of the BN Layer does not follow loss.backward() And optimizer.step() To update , But in the model forward Is based on momentum , So you need every forward Before freezing BN layer :
The complete freezing method is as follows :
''' A pile of code '''
# frozen BN
def freeze_bn(m):
classname = ly.__class__.__name__
if classname.find('BatchNorm') != -1:
m.eval()
''' A pile of code '''
freeze_state_dict = torch.load(opt.loadckpt_freeze)
frozen_list = [k for k, _ in freeze_state_dict['state_dict'].items() if k in model_dict]
# Freeze first except BN Parameters other than
for param in model.named_parameters():
if param[0] in frozen_list: # Parameter list to be frozen
param[1].requires_grad = False
# The parameters optimized by the optimizer only contain the parameters that need gradient update
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=opt.lr, betas=(0.9,0.999))
''' A pile of code '''
for epoch in range(opt.epoch):
model.train()
optimizer.zero_grad()
# frozen BN
model.apply(freeze_bn)
# Forward propagation
output = model(input)
loss = loss_F(gt, output)
loss.backward()
optimizer.step()
边栏推荐
- Webcodecs解析GIF图
- Jenkinsclient | easy to use Jenkins client
- 使用MindSpore在GPU-PYNATIVE/ CPU-GRAPH_MODE 与 GPU-GRAPH_MODE 执行不一致
- S系列·删除文件夹的几种姿势
- Softing为艾默生提供AMS设备管理系统的连接解决方案
- S系列·向文本文件新增数据并且不添加重复值
- 2022山东省安全员C证考试题库及答案
- These programming languages are old and almost dead. Young people can't touch them
- 信息学奥赛一本通 1288:三角形最佳路径问题 | OpenJudge NOI 2.6 7625:三角形最佳路径问题
- Examination questions and online simulation examination of the third batch of Guangdong Provincial Safety Officer a certificate (principal) in 2022
猜你喜欢

Email: analysis of wrong arrangement

Execution strategy of application software efficiency test

Fastapi-16-page beautification-1

Kubernetes distributed performance test using locust

S系列·删除文件夹的几种姿势

2022 mobile crane driver examination questions and online simulation examination

24. browser object model BOM

Pysimplegui classic practice: how to read this Chinese character?

2022g1 industrial boiler stoker examination questions and answers

midway的使用教程
随机推荐
Who ate IO?
2022.5.24-----leetcode. nine hundred and sixty-five
When the fcos model is implemented using mindscore, the gradient is concentrated at 0
Add of pytorch_ How can module (name, module) be represented by mindspore
What are the advantages of multi merchant mall applet source code?
Fastapi-14-file upload-2
Webcodecs解析GIF图
MindSpore【数据集功能】无法查看数据集
Softing provides Emerson with connection solutions for AMS device management system
2022.5.25-----leetcode. four hundred and sixty-seven
S series · add data to the text file without adding duplicate values
Metersphere | a super easy-to-use open source testing platform
Network principle TCP
使用MindSpore在GPU-PYNATIVE/ CPU-GRAPH_MODE 与 GPU-GRAPH_MODE 执行不一致
2022山东省安全员C证考试题库及答案
How to use API interface of national weather forecast for rapid development
2022年危险化学品生产单位安全生产管理人员操作证考试题库及答案
大事件回顾 | Eolink 5月重要动态速览!
Apispace minute precipitation forecast API interface is free and easy to use
2022 mobile crane driver examination questions and online simulation examination