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 ?
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
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.
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.