Best way to store feature vectors from pretrained model to hard disk?

Hi all,
Suppose I extracted the image features from a pretrained CNN and wants to save these features to disk for later use. Which of the following will be a good method in terms of memory and speed.

  1. Store the feature dictionary in a pickle file
  2. Convert the feature tensor to numpy arrays and save using np.save()
  3. Save it in a json file ( TypeError: Object of type ‘FloatTensor’ is not JSON serializable is coming)

Thanks!!

I would probably save them as torch.tensors with torch.save.
This should be fast and as long as you load them again in PyTorch there shouldn’t be a problem.

2 Likes

would be possible to extract features from the saved model and save them as a .np file?
I have saved the model using

     if epoch % 10 == 9:
             
            torch.save({
                    'epoch': epoch + 1,
                    'state_dict': model.state_dict(),
                    'optimizer': optimizer.state_dict(),
            }, 'NAS_exp_epoch{}.pth'.format(epoch+1))
             

I wonder how can I extract features from the model for each image patch?

I assume “features” refers to an intermediate forward activation. If so, you could use forward hooks as described here to grab them and that you could store them as numpy arrays afterwards.

1 Like