Hi, I am trying to return the names of the parameters of a finetuned pretrained resnet18 model and I get an empty list. Basically what i want to do is for
model = resnet18(pretrained=True)
params = []
params += [n for n, p in model.named_parameters() if 'layer' in n]
print(params)
I get the parameter names printed out as [‘layer1.0.conv1.weight’, layer1.0.bn1.weight’…]
but after finetuning and saving my model and loading, I get an empty list when i try to return a similar list as above.
model = torch.load(‘finetuned_resnet’)
params = []
params += [n for n, p in model.named_parameters() if ‘layer’ in n]
print(params)
Can anyone help explain why this is so?
Thanks
I think I found my problem. Given that i redefined my model as:
class ModifiedResNet18Model(torch.nn.Module):
def init(self):
super(ModifiedResNet18Model, self).init()
model = models.resnet18(pretrained=True)#squeezenet1_1
modules = list(model.children())[:-1] # delete the last fc layer.
model = nn.Sequential(*modules)
self.features = model
print("start pruning:")
for param in self.features.parameters():
param.requires_grad = False
self.fc = nn.Sequential(
nn.Dropout(),
nn.Linear(512,400),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(400,256),
nn.ReLU(inplace=True),
nn.Linear(256, 256))
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.fc(x)#self.classifier(x)
return x
I needed to add features when i return it as:
model = torch.load(‘finetuned_resnet’)
params = []
params += [n for n, p in model.features.named_parameters() if ‘layer’ in n]
print(params)