[ pytorch ] ——基本使用:(3) finetune冻结层操作 + 权重设置

1、冻结层不参与训练方法:

######### 模型定义 #########
class MyModel(nn.Module):
    def __init__(self, feat_dim):   # input the dim of output fea-map of Resnet:
        super(MyModel, self).__init__()
        
        BackBone = models.resnet50(pretrained=True)
        
        add_block = []
        add_block += [nn.Linear(2048, 512)]
        add_block += [nn.LeakyReLU(inplace=True)]
        add_block = nn.Sequential(*add_block)
        add_block.apply(weights_init_xavier)

        self.BackBone = BackBone
        self.add_block = add_block


    def forward(self, input):   # input is 2048!

        x = self.BackBone(input)
        x = self.add_block(x)

        return x
##############################

# 模型准备
model = MyModel()

# 优化、正则项、权重设置与冻结层

for param in model.parameters():
    param.requires_grad = False
for param in model.add_block.parameters():
    param.requires_grad = True

optimizer = optim.SGD(
            filter(lambda p: p.requires_grad, model.parameters()),  # 记住一定要加上filter(),不然会报错
            lr=0.01,
            weight_decay=1e-5, momentum=0.9, nesterov=True)



ignored_params = list(map(id, model.add_block.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())

2、各层采用不同学习率方法

######### 模型定义 #########
class MyModel(nn.Module):
    def __init__(self, feat_dim):   # input the dim of output fea-map of Resnet:
        super(MyModel, self).__init__()
        
        BackBone = models.resnet50(pretrained=True)
        
        add_block = []
        add_block += [nn.Linear(2048, 512)]
        add_block += [nn.LeakyReLU(inplace=True)]
        add_block = nn.Sequential(*add_block)
        add_block.apply(weights_init_xavier)

        self.BackBone = BackBone
        self.add_block = add_block


    def forward(self, input):   # input is 2048!

        x = self.BackBone(input)
        x = self.add_block(x)

        return x
##############################

# 模型准备
model = MyModel()

# 不同层学习率设置

ignored_params = list(map(id, model.add_block.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())

optimizer = optim.SGD(
             [
                {'params': base_params, 'lr': 0。01},
                {'params': model.add_block.parameters(), 'lr': 0.1},
                ]
            weight_decay=1e-5, momentum=0.9, nesterov=True)

猜你喜欢

转载自blog.csdn.net/jdzwanghao/article/details/83239111