1、冻结网络层参数 参考:注意是requires_grad https://zhuanlan.zhihu.com/p/357349586 https://zhuanlan.zhihu.com/p/65105409
for name, param in model.named_parameters():
if ('discriminator_G' not in name) and ('discriminator_L' not in name):
param.requires_grad = False
2.分层处理学习率 建立优化器的时候:
discriminator_L_param=list(map(id, model.backbone_2d.discriminator_L.parameters()))
discriminator_G_param=list(map(id, model.backbone_2d.discriminator_G.parameters()))
FL_param = list(map(id, model.backbone_2d.FL.parameters()))
base_params = filter(lambda p: id(p) not in discriminator_L_param + discriminator_G_param+FL_param,model.parameters())
if optim_cfg.OPTIMIZER == 'adam':
optimizer = optim.Adam([
{'params': base_params},
{'params':model.backbone_2d.discriminator_G.parameters(),'lr':optim_cfg.LR*3},
{'params':model.backbone_2d.discriminator_L.parameters(),'lr':optim_cfg.LR*3},
{'params':model.backbone_2d.FL.parameters(),'lr':optim_cfg.LR*3}],
lr=optim_cfg.LR, weight_decay=optim_cfg.WEIGHT_DECAY)
|