Feature extraction using pretrained Alexnet

Hi Guys,

I am trying to use pytorch pretrained Alexnet model for feature extraction, which I will pass to the SVM classifier (scikit). I am doing the transfer learning as my dataset is small.

I got the model as
alexnet_model = models.alexnet(pretrained=True)
Then removed the fully connected layer
alexnet_model.classifier = torch.nn.Sequential(*list(alexnet_model.classifier.children())[:-4])

I am unsure about how to pass the training image as input to the model and get the extracted features! Do I have to get the features of each input image sequentially through some loop? Is there a way I can do this all at once?


As far as I’m aware, the images have to go through the model one by one. You can speed up the process through using a GPU and batching your inputs. Generally for test_batch and val_batch sizes people choose the largest batch size that their hardware is capable of supporting.

I would recommend using a custom dataset and dataloader to assist.