I am trying to save the output from the fc1 layer of the model. Below is my model formulation:
model_ft = models.resnet50(pretrained=True)
num_ftrs = model_ft.fc.out_features
model_ft.fc1 = nn.ReLU(nn.Linear(num_ftrs, 512))
model_ft.fc2 = nn.Linear(num_ftrs, num_classes)
model_ft = model_ft.to(device)
criterion = nn.CrossEntropyLoss()
optimizer_ft = optim.SGD(model_ft.parameters(), lr=learning_rate, momentum=0.9)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
The below code shows the current activation access method:
activation = {}
def get_activation(name):
def hook(model_ft, input, output):
activation[name] = output.detach()
return hook
model_ft.fc1.register_forward_hook(get_activation('fc1'))
x = torch.randn(12,3,224,224).to('cuda')
output = model_ft(x)
block4_output = activation['fc1']
The above method throws KeyError: ‘fc1’. Could anyone help me to fix this problem?
Thank You!!