FastAI V1: Feature Extraction Callback Hooks

Hello, I would like to know more about the proper/correct way to get intermediate features/layers/embeddings on fastai_v1.

Ideally, I have a model save to learn.model (example resnet34) and will like to save all the features for my validation set. Example my validation dataset has 10k images so I would like to have a tensor of size (10k, features) where my features are an intermediate layer example learn.model[1][6] for the case of default resnet34 (which points to the BN layer). Also which will be a better layer for embedding the last BN layer or the last RELU layers just before the final BN?

Regards.

2 Likes

Any thought on this @sgugger. I apologize for the tag.

You will need to write a subclass of HookCallback for this, take a look at the source code of ActivationStats for an example. You will just need to change what happens in hook because you don’t want to take the mean and std but jsut return the output o and you may need to adjust on_batch_end.

@avn3r, sorry for tagging, I am curious, were you able to extract features from model? This would be really interesting.

1 Like