How can i complete the task with .pth files?

i used per-trained model for my datasets and got files with .pth (epochs) … i need to use the result for image captioning … how can i extract the features from the files i got ?

Does epochs with .pth that come from training the model are the weights ?

Yes, you can use the .pth files to load model weights and from there you can use the intermediate activations of the model as features. The ImageNet example: examples/main.py at cbb760d5e50a03df667cdc32a61f75ac28e11cbf · pytorch/examples · GitHub
shows an example of loading from checkpoints.

1 Like

excuse me what do you mean by using the intermediate activation of the model as features . is there any example for this ?
and the way you shared it is supposed for any model right ?
Appreciating your help

Yes, the model loading should be generic across models.
I think extracting model features has been thoroughly discussed in the past; example thread:
https://discuss.pytorch.org/t/how-to-extract-features-of-an-image-from-a-trained-model

Thanks for sharing the link i will try it but excuse me . what is the difference between save tensor and extract features as i tried

    def forward(self, x):
        x = self.forward_features(x)
        y=x[-1].cpu().numpy()
        with open('res.pkl', 'wb') as df_file:
             pickle.dump(y, file = df_file)
    return x

but i got the tensor for one image . Does the code i wrote right ? and if it is. how can i got it for all images.

I haven’t tried an implementation like that before but if you indeed are seeing values saved to storage then I suspect it’s working fine. You can compute the features for all images simply by passing all of them through the model (you can do this in a batched way with something like a dataloader). The ImageNet example also shows how to iterate through a dataset with a dataloader.

1 Like

thanks but what is the difference between save tensor and extract features

It depends on what you actually want to do. If you want to use features computed on the images later, one way to do this is to extract them and save them for another model to use later. You can also just extract them (e.g., by returning them from the forward function) and pass them directly to another model or function.