ann-unsplash-25k / main.py
Ashot Vardanian
Fix: Vectorizing images and indexing on-demand
e01653e
raw
history blame
2.11 kB
#!/usr/bin/env python3
from os import listdir, path, PathLike, remove
from os.path import isfile, join, exists
import pandas as pd
import numpy as np
from PIL import Image
from PIL import ImageFile
from tqdm import tqdm
from uform import get_model
from usearch.index import Index, MetricKind
from usearch.io import save_matrix, load_matrix
ImageFile.LOAD_TRUNCATED_IMAGES = True
def is_image(path: PathLike) -> bool:
if not isfile(path):
return False
try:
Image.open(path)
return True
except Exception:
return False
def trim_extension(filename: str) -> str:
return filename.rsplit('.', 1)[0]
names = sorted(f for f in listdir('images') if is_image(join('images', f)))
names = [trim_extension(f) for f in names]
table = pd.read_table('images.tsv') if path.exists(
'images.tsv') else pd.read_table('images.csv')
table = table[table['photo_id'].isin(names)]
table = table.sort_values('photo_id')
table.reset_index()
table.to_csv('images.csv', index=False)
names = list(set(table['photo_id']).intersection(names))
names_to_delete = [f for f in listdir(
'images') if trim_extension(f) not in names]
if len(names_to_delete) > 0:
print(f'Plans to delete: {len(names_to_delete)} images without metadata')
for name in names_to_delete:
remove(join('images', name))
if not exists('images.fbin'):
model = get_model('unum-cloud/uform-vl-english')
vectors = []
for name in tqdm(names, desc='Vectorizing images'):
image = Image.open(join('images', name + '.jpg'))
image_data = model.preprocess_image(image)
image_embedding = model.encode_image(image_data).detach().numpy()
vectors.append(image_embedding)
image_mat = np.concatenate(vectors)
save_matrix(image_mat, 'images.fbin')
if not exists('images.usearch'):
index = Index(ndim=256, metric=MetricKind.Cos)
image_mat = load_matrix('images.fbin')
count = image_mat.shape[0]
for idx in tqdm(range(count), desc='Indexing vectors'):
index.add(idx, image_mat[idx, :].flatten())
index.save('images.usearch')