Extract Features using pytorch

excuse me I’m confused about questions . I need to extract the features before the classification layer as i don’t need to classify the image but need the features that help to classify only

Suppose I have a classification model called MyModel.py

After removing the output layer.

1- Should I train the class of the model on my dataset ?

2- I should get checkpoint .pth after training . Will this file be the weight ?

3- if I will train the model after remove the output layer . Should I save tensor in

def forward (self,x)

x = self.forward_features(x)

#x = self.head(x) # here I will save the tensor ??

return x

Or just the checkpoint.pth that I will get enough

4- the model comes with a .pth file ( pre-trained file) … but there is one for the classification and I shouldn’t use it . Right

There are some models pretrained on imagenet which generalise very well even for unseen classes.
You can trust that if you have no much clue about what u are doing.

If the model is not trained then yes, you should train it in your dataset.
Anyway randomly-initlised CNNs still works better than random.
The pth file is a compressed file, it contains what you save into it.
Assuming you saved the weights of your model, then yes.
You can do both, use the model on-the-fly if you have a good GPU and a small hard disk or save the tensor if you need it.
That’s up to your necessities.
Models usually have a last layer to compute class probabilities. If you train your model from scratch you can just delete it. If you use a pretrained model you need to keep it as you will have an error otherwise.