hi, ptrblck. Can I use forward hook function to extract the intermediate features of alexnet? But something weird happened again. This result is quite different from the other two. My code looks like this:
model = torchvision.models.alexnet(pretrained=True)
model.type(dtype)
activation = {}
def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()
return hook
model.classifier[1].register_forward_hook(get_activation(‘fc6’))
Testcomp_list =[]
Test_batchsize = 1
model.eval()
with torch.no_grad():
for i in range(TestImageData.size()[0]//Test_batchsize):
local_X = TestImageData[i*Test_batchsize:(i+1)*Test_batchsize,:,:,:].type(dtype)
out = model(local_X)
Testcomp_list.append(activation[‘fc6’].detach().cpu())
#h.remove()
print(len(Testcomp_list))
print(Testcomp_list[1].size())
Testcomp_data = torch.cat(Testcomp_list)
print(Testcomp_data.size())
Is there anything wrong again?