datadabllp

integrate FAISS with a PyTorch model

Jul 19th, 2024
3,973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. import torch
  2. import faiss
  3. import numpy as np
  4.  
  5. # Assume we have a pre-trained PyTorch model for image feature extraction
  6. class FeatureExtractor(torch.nn.Module):
  7.     # ... model definition ...
  8.  
  9.     def forward(self, x):
  10.         return self.features(x)
  11.  
  12. model = FeatureExtractor()
  13. model.eval()
  14.  
  15. # Extract features from your image dataset
  16. image_features = []
  17. for image in dataset:
  18.     with torch.no_grad():
  19.         features = model(image).cpu().numpy()
  20.     image_features.append(features)
  21.  
  22. image_features = np.vstack(image_features)
  23.  
  24. # Create FAISS index
  25. index = faiss.IndexFlatL2(image_features.shape[1])
  26. index.add(image_features)
  27.  
  28. # Now you can use this index for similarity search
  29.  
Advertisement
Add Comment
Please, Sign In to add comment