Skip to content

Commit

Permalink
chore: create faiss package
Browse files Browse the repository at this point in the history
  • Loading branch information
m-barker committed Feb 22, 2024
1 parent 402974e commit 1678eab
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 200,161 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.0.2)
project(qualification)
project(lasr_vector_databases_faiss)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0"?>
<package format="2">
<name>qualification</name>
<name>lasr_vector_databases_faiss</name>
<version>0.0.0</version>
<description>The qualification package</description>
<description>The faiss package</description>

<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="jared.swift@kcl.ac.uk">Jared Swift</maintainer>
<maintainer email="matthew.s.barker@kcl.ac.uk">Matt Barker</maintainer>


<!-- One license tag required, multiple allowed, one license per tag -->
Expand Down Expand Up @@ -50,8 +50,6 @@
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>catkin_virtualenv</build_depend>
<depend>lasr_vision_msgs</depend>
<depend>lasr_speech_recognition_msgs</depend>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
<build_depend>actionlib_msgs</build_depend>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
faiss-cpu
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
certifi==2024.2.2 # via requests
charset-normalizer==3.3.2 # via requests
click==8.1.7 # via nltk
clip @ git+https://github.com/openai/CLIP.git # via -r requirements.in
faiss-cpu==1.7.4 # via -r requirements.in
filelock==3.13.1 # via huggingface-hub, torch, transformers, triton
fsspec==2024.2.0 # via huggingface-hub, torch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from catkin_pkg.python_setup import generate_distutils_setup

setup_args = generate_distutils_setup(
packages=["qualification"], package_dir={"": "src"}
packages=["lasr_vector_databases_faiss"], package_dir={"": "src"}
)

setup(**setup_args)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import torch
import numpy as np

# import matplotlib.pyplot as plt
from sklearn.manifold import TSNE

# import rospy
import faiss # type: ignore
from sentence_transformers import SentenceTransformer # type: ignore
Expand Down Expand Up @@ -57,11 +54,18 @@ def get_sentence_embeddings(
)


def create_vector_database(command_embeddings: np.ndarray) -> faiss.IndexFlatIP:
def create_vector_database(vectors: np.ndarray) -> faiss.IndexFlatIP:
"""Creates a vector database from an array of vectors of the same dimensionality
Args:
vectors (np.ndarray): shape (n_vectors, vector_dim)
Returns:
faiss.IndexFlatIP: Flat index containing the vectors
"""
print("Creating vector database")
index_flat = faiss.IndexFlatIP(command_embeddings.shape[1])
faiss.normalize_L2(command_embeddings)
index_flat.add(command_embeddings)
index_flat = faiss.IndexFlatIP(vectors.shape[1])
faiss.normalize_L2(vectors)
index_flat.add(vectors)
print("Finished creating vector database")
return index_flat

Expand Down Expand Up @@ -141,6 +145,7 @@ def get_similar_commands(


if __name__ == "__main__":
"""Example usage of using this to find similar commands"""
command = "find Jared and asks if he needs help"
result, distances, command_embeddings, query_embedding = get_similar_commands(
command,
Expand All @@ -149,47 +154,4 @@ def get_similar_commands(
n_similar_commands=1000,
return_embeddings=True,
)
command_embeddings = np.concatenate((command_embeddings, query_embedding))
# tsne = TSNE(
# n_components=2,
# perplexity=3,
# verbose=1,
# n_iter=1000,
# n_iter_without_progress=1000,
# )
# print("Fitting tsne")
# tsne_embed = tsne.fit_transform(command_embeddings)
# # plot the embeddings
# plt.scatter(tsne_embed[:-1, 0], tsne_embed[:-1, 1], label="Commands")
# plt.scatter(tsne_embed[-1, 0], tsne_embed[-1, 1], label="Query")
# # Annotate the query
# plt.annotate(
# command,
# (tsne_embed[-1, 0], tsne_embed[-1, 1]),
# )
# # Set title
# plt.title("t-SNE plot of 1000 closest command embeddings")
# # Label axes
# plt.xlabel("t-SNE Dimension 1")
# plt.ylabel("t-SNE Dimension 2")
# print(f"Command: {command}")
# for i in range(3):
# # decide if it should be th or st
# if i == 0:
# suffix = "st"
# elif i == 1:
# suffix = "nd"
# elif i == 2:
# suffix = "rd"
# else:
# suffix = "th"
# print(
# f"The {i+1}{suffix} closest command is {result[i]} with a similarity of {distances[i]:.2f}"
# )
# # Annotate the points
# plt.annotate(
# result[i],
# (tsne_embed[i, 0], tsne_embed[i, 1]),
# )

# plt.show()
print(result)
2 changes: 1 addition & 1 deletion common/vision/lasr_vision_clip/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from catkin_pkg.python_setup import generate_distutils_setup

setup_args = generate_distutils_setup(
packages=["qualification"], package_dir={"": "src"}
packages=["lasr_vision_clip"], package_dir={"": "src"}
)

setup(**setup_args)
Loading

0 comments on commit 1678eab

Please sign in to comment.