|
""" |
|
Thanks to Freddy Boulton (https://github.com/freddyaboulton) for helping with this. |
|
""" |
|
|
|
|
|
import pickle |
|
|
|
import gradio as gr |
|
from datasets import load_dataset |
|
from transformers import AutoModel |
|
|
|
|
|
|
|
from similarity_utils import LSH, BuildLSHTable, Table |
|
|
|
seed = 42 |
|
|
|
|
|
with open("lsh.pickle", "rb") as handle: |
|
loaded_lsh = pickle.load(handle) |
|
|
|
|
|
model_ckpt = "nateraw/vit-base-beans" |
|
model = AutoModel.from_pretrained(model_ckpt) |
|
lsh_builder = BuildLSHTable(model) |
|
lsh_builder.lsh = loaded_lsh |
|
|
|
|
|
dataset = load_dataset("beans") |
|
candidate_dataset = dataset["train"].shuffle(seed=seed) |
|
|
|
|
|
def query(image, top_k): |
|
results = lsh_builder.query(image) |
|
|
|
|
|
images = [] |
|
|
|
labels = [] |
|
|
|
candidates = [] |
|
|
|
for idx, r in enumerate(sorted(results, key=results.get, reverse=True)): |
|
if idx == top_k: |
|
break |
|
image_id, label = r.split("_")[0], r.split("_")[1] |
|
candidates.append(candidate_dataset[int(image_id)]["image"]) |
|
labels.append(f"Label: {label}") |
|
|
|
for i, candidate in enumerate(candidates): |
|
filename = f"similar_{i}.png" |
|
candidate.save(filename) |
|
images.append(filename) |
|
|
|
|
|
|
|
return list(zip(images, labels)) |
|
|
|
|
|
title = "Fetch Similar Beans 🪴" |
|
description = "This Space demos an image similarity system. You can refer to [this notebook](TODO) to know the details of the system. You can pick any image from the available samples below. On the right hand side, you'll find the similar images returned by the system. The example images have been named with their corresponding integer class labels for easier identification. The fetched images will also have their integer labels tagged so that you can validate the correctness of the results." |
|
|
|
|
|
|
|
gr.Interface( |
|
query, |
|
inputs=[gr.Image(type="pil"), gr.Slider(value=5, minimum=1, maximum=10, step=1)], |
|
outputs=gr.Gallery().style(grid=[3], height="auto"), |
|
|
|
title=title, |
|
description=description, |
|
examples=[["0.png", 5], ["1.png", 5], ["2.png", 5]], |
|
).launch() |
|
|