How to use pretrained resnet-50 to get features?

I want to get 2048 features from a picture by using pretrained resnet-50,is it ok by these code?

resnet50_feature_extractor = models.resnet50(pretrained = True)
resnet50_feature_extractor.fc = nn.Linear(2048, 2048)

Thanks for your help!

I think it is mostly correct, but I think you need to zero the bias of the fc layer.
Another line of code using


I usually use

resnet50_feature_extractor = nn.Sequential(*list(models.resnet50(pretrained = True).children())[:-1])

but in this way you need to reshape the output by squeezing the spatial dimensions.