Extracting Features in vision models

I am running this piece of code to extract features from an intermediate layer and it’s showing the described error.
Can somebody please help me?

import torchvision
from torchvision.models.feature_extraction import create_feature_extractor
model_faster_rcnn = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)

faster_rcnn = create_feature_extractor(model_faster_rcnn, {'layer4': 'f1'})

The error given is:

TraceError: Proxy object cannot be iterated. This can be attempted when the Proxy 
is used in a loop or as a *args or **kwargs function argument. See the torch.fx docs 
on pytorch.org for a more detailed explanation of what types of control flow can 
be traced, and check out the Proxy docstring for help troubleshooting Proxy 
iteration errors.

I also tried the feature extraction code for resnet and it worked, but for this, it’s not working.

1 Like

Have you solved it?I also met the same problem :face_with_head_bandage:

You can use Pytorch hooks. Check it out.