Hi back @ptrblck,
Thank you for you help/ But l get stuck once again and lâm confused. Here is my network architecture (variable net
)
net
ConvNet_LeNet5(
(cl1): Linear(in_features=25, out_features=32, bias=True)
(cl2): Linear(in_features=25, out_features=64, bias=True)
(fc1): Linear(in_features=51200, out_features=100, bias=True)
(fc2): Linear(in_features=100, out_features=4, bias=True)
)
my best network is saved as follow :
save_checkpoint({
'epoch': int(epoch) + 1,
'state_dict': net.state_dict(),
'best_prec1': best_prec1,
'optimizer': optimizer.state_dict(),
}, is_best)
Before loading the best model letâs try forward hook as you suggested
net.fc2.register_forward_hook(get_activation('fc2'))
works well.
But we need to load the best model as a feature extractor. I did that as follow :
my_best_model=net.load_state_dict(torch.load('model_best.pth.tar'))
l get the following error :
*** KeyError: 'unexpected key "epoch" in state_dict'
Then l loaded the best model without specifying load_state_dict
as follow :
my_best_model = torch.load('model_best.pth.tar')
my_best_model.keys()
dict_keys(['epoch', 'state_dict', 'best_prec1', 'optimizer'])
my_best_model=my_model['state_dict']
my_best_model.keys()
odict_keys(['cl1.weight', 'cl1.bias', 'cl2.weight', 'cl2.bias', 'fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias'])
my_best_model=my_model['fc2.weight']
Iâm not sure if itâs the correct way to load fc2 as a feature extractor (since l didnât succeed to do that with hook). Please correct me.
x is a test example
output=my_best_model(x)
It returns *** TypeError: âtorch.cuda.FloatTensorâ object is not callable
All what is needed is to fix
my_best_model=net.load_state_dict(torch.load(âmodel_best.pth.tarâ)) # error *** KeyError: âunexpected key âepochâ in state_dictâ
and
my_best_model.fc2.register_forward_hook(get_activation('fc2'))
Thank you for your help @ptrblck