-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/TileDB-Inc/TileDB-Vector-Se…
…arch into jparismorgan/ivf-pq-temp-dir-update
- Loading branch information
Showing
17 changed files
with
972 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
340 changes: 340 additions & 0 deletions
340
apis/python/examples/object_api/bioimg_similarity_search.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
apis/python/src/tiledb/vector_search/embeddings/huggingface_auto_image_embedding.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from typing import Dict, Optional, OrderedDict | ||
|
||
import numpy as np | ||
|
||
|
||
class HuggingfaceAutoImageEmbedding: | ||
def __init__( | ||
self, | ||
model_name_or_path: str, | ||
dimensions: int, | ||
device: Optional[str] = None, | ||
cache_folder: Optional[str] = None, | ||
batch_size: int = 64, | ||
): | ||
self.model_name_or_path = model_name_or_path | ||
self.dim_num = dimensions | ||
self.device = device | ||
self.cache_folder = cache_folder | ||
self.batch_size = batch_size | ||
self.processor = None | ||
self.model = None | ||
|
||
def init_kwargs(self) -> Dict: | ||
return { | ||
"model_name_or_path": self.model_name_or_path, | ||
"dimensions": self.dim_num, | ||
"device": self.device, | ||
"cache_folder": self.cache_folder, | ||
"batch_size": self.batch_size, | ||
} | ||
|
||
def dimensions(self) -> int: | ||
return self.dim_num | ||
|
||
def vector_type(self) -> np.dtype: | ||
return np.float32 | ||
|
||
def load(self) -> None: | ||
from transformers import AutoImageProcessor | ||
from transformers import AutoModel | ||
|
||
self.processor = AutoImageProcessor.from_pretrained(self.model_name_or_path) | ||
self.model = AutoModel.from_pretrained(self.model_name_or_path) | ||
|
||
def embed(self, objects: OrderedDict, metadata: OrderedDict) -> np.ndarray: | ||
from PIL import Image | ||
|
||
write_id = 0 | ||
count = 0 | ||
image_batch = [] | ||
size = len(objects["image"]) | ||
embeddings = np.zeros((size, self.dim_num), dtype=np.float32) | ||
for image_id in range(len(objects["image"])): | ||
image_batch.append( | ||
Image.fromarray( | ||
np.reshape(objects["image"][image_id], objects["shape"][image_id]) | ||
) | ||
) | ||
count += 1 | ||
if count >= self.batch_size: | ||
print(image_id) | ||
inputs = self.processor(images=image_batch, return_tensors="pt") | ||
batch_embeddings = ( | ||
self.model(**inputs).last_hidden_state[:, 0].cpu().detach().numpy() | ||
) | ||
embeddings[write_id : write_id + count] = batch_embeddings | ||
count = 0 | ||
image_batch = [] | ||
|
||
if count > 0: | ||
inputs = self.processor(images=image_batch, return_tensors="pt") | ||
batch_embeddings = ( | ||
self.model(**inputs).last_hidden_state[:, 0].cpu().detach().numpy() | ||
) | ||
embeddings[write_id : write_id + count] = batch_embeddings | ||
return embeddings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.