So what im trying to do is essentially unfreeze the last portion of resnet50, densent121 and inceptionv3. I’m trying to unfreeze the last 30% of the network and I do this by unfreezing specific blocks or modules. The FC layer is already set to requires_grad = True
In another function hence this function is only called to unfreeze specific blocks. However i’m not sure if im doing it correctly
def unfreeze_blocks(model, model_name):
"""
ARGS
model- represents our current model instance
model_name - represents the name of the model we are using
we are unfreezing only the last 30% of each architecture approximately
for resnet last 30% is layer 4 which is 3 residual blocks + the last block of layer 3
for densenet 30% of the total dense layers is 17 which is inside the last dense block(dense block 4). each dense layer has 2 conv layers
for inception v3 we will unfreeze the last 3 modules
"""
if model_name == 'resnet50':
for param in model.layer4.parameters():
param.requires_grad = True
# Unfreeze the last block of layer3
if hasattr(model, 'layer3') and len(model.layer3) > 5:
for param in model.layer3[5].parameters():
param.requires_grad = True
elif model_name == 'DenseNet121':
for name, params in model.named_parameters():
if 'denseblock4' in name:
params.requires_grad = True
elif model_name == 'InceptionV3':
for name, param in model.named_parameters():
if any(module in name for module in ["Mixed_7a", "Mixed_7b", "Mixed_7c"]):
param.requires_grad = True