-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite_faiss_index.py
More file actions
121 lines (90 loc) · 3.92 KB
/
write_faiss_index.py
File metadata and controls
121 lines (90 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import argparse
import glob
import os
from pathlib import Path
import faiss
import numpy as np
import torch
from PIL import Image
from tqdm import tqdm
from models.configs import get_model_config
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, required=True, help='Path to dataset with images')
parser.add_argument('--output', type=str, required=True, help='Path to output FAISS index')
parser.add_argument('--batch_size', type=int, default=32, help='Batch size')
parser.add_argument('--model_family', type=str, required=True, help='VLM model family')
parser.add_argument('--model_id', type=str, required=True, help='HF model id')
parser.add_argument('--index_type', type=str, default='flat_ip', help='Index type')
parser.add_argument(
'--m',
type=int,
default=32,
help='Number of connections per layer only for `hnsw` index type.'
)
parser.add_argument('--device', type=str, default='cuda' if torch.cuda.is_available() else 'cpu',
help='Device cuda/cpu for generating embeddings')
return parser.parse_args()
def get_image_paths(data_dir):
extensions = ['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG']
image_paths = []
for ext in extensions:
image_paths.extend(glob.glob(os.path.join(data_dir, f'**/*.{ext}'), recursive=True))
return image_paths
def encode_images(vlm_wrapper, image_paths, batch_size):
features = []
paths = []
for i in tqdm(range(0, len(image_paths), batch_size), desc="Encoding images"):
batch_paths = image_paths[i:i+batch_size]
batch_images = []
valid_indices = []
for j, path in tqdm(enumerate(batch_paths), desc="Encoding images"):
try:
img = Image.open(path).convert('RGB')
# Use the processor directly on the image
batch_images.append(img)
valid_indices.append(j)
except Exception as e:
print(f"Error processing {path}: {e}")
processed_images = vlm_wrapper.process_inputs(images=batch_images)
with torch.no_grad():
outputs = vlm_wrapper.get_image_embeddings(processed_images)
features.append(outputs.cpu().numpy())
paths.extend(batch_paths)
return np.vstack(features), paths
def create_faiss_index(features, feature_dim, index_type='flat_ip', m=32):
faiss.normalize_L2(features)
if index_type == 'flat_ip':
index = faiss.IndexFlatIP(feature_dim)
elif index_type == 'hnsw':
index = faiss.IndexHNSWFlat(feature_dim, m)
else:
raise ValueError(f"Invalid index type: {index_type}")
index.add(features)
return index
def main():
args = parse_args()
# Get all image paths
image_paths = get_image_paths(args.data)
print(f"Found {len(image_paths)} images")
model_config = get_model_config(args.model_family, args.model_id)
processor = model_config["processor_class"].from_pretrained(model_config["model_id"])
model = model_config["model_class"].from_pretrained(model_config["model_id"])
wrapper = model_config["wrapper_class"](model=model, processor=processor)
model.to(args.device)
model.eval()
features, paths = encode_images(wrapper, image_paths, args.batch_size)
feature_dim = features.shape[1]
# Create FAISS index
index = create_faiss_index(features, feature_dim)
# Save index and paths
output_dir = os.path.join(args.output, args.model_id)
os.makedirs(output_dir, exist_ok=True)
faiss.write_index(index, os.path.join(output_dir, "image_index.faiss"))
# Save paths to a text file
with open(os.path.join(output_dir, "image_paths.txt"), "w") as f:
for path in paths:
f.write(f"{path}\n")
print(f"Index created with {len(paths)} images and saved to {output_dir}")
if __name__ == "__main__":
main()