How to retrieve vector of 512 features for each image during inference

Hi everyone,

After training a resnet (18, 34 or 50) I do inference on new images.
I do prediction on img like this:
predicted_class = model.predict(img)[0] # index 0 to have only the class name

What I would like to have now is the vector of 512 features for this image.

I see that it is localized at the end, in the third group of layer of the resnet.
It is the output of the first “linear” (index 5 of group 3)
I can see this with model.summary

I don’t know how to retrieve these values.
Can you help me on this please :slight_smile:

The idea is to collect kind of “fingerprint” for all the images and work later on similarity with these features…

Best regards

You will need to use hooks for that, there is HookOutput class that will help. You should watch the lesson with CAM (can’t remember which one it is) to see how they can be used.

Hi Sylvain,
Thanks for the answer. I’ll try to find it :slight_smile:

I found it: lesson6-pets-more.ipynb
heatmap part of the notebook

1 Like