Spaces:
Runtime error
Runtime error
File size: 1,475 Bytes
bfd8285 358b6e7 bfd8285 e5a7396 bfd8285 f4b23ea bfd8285 f4b23ea bfd8285 |
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 |
import torch
import numpy as np
import gradio as gr
from faiss import read_index
from PIL import Image, ImageOps
from datasets import load_dataset
import torchvision.transforms as T
from torchvision.models import resnet50
from model import DINO
transforms = T.Compose(
[T.ToTensor(), T.Resize(244), T.CenterCrop(224), T.Normalize([0.5], [0.5])]
)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dataset = load_dataset("ethz/food101")
model = DINO(batch_size_per_device=32, num_classes=1000).to(device)
model.load_state_dict(torch.load("./bin/model.ckpt", map_location=device)["state_dict"])
def augment(img, transforms=transforms) -> torch.Tensor:
img = Image.fromarray(img)
if img.mode == "L":
# Convert grayscale image to RGB by duplicating the single channel three times
img = ImageOps.colorize(img, black="black", white="white")
return transforms(img).unsqueeze(0)
def search_index(input_image, k = 1):
with torch.no_grad():
embedding = model(augment(input_image))
index = read_index("./bin/dino.index")
_, I = index.search(np.array(embedding[0].reshape(1, -1)), k)
indices = I[0]
answer = ""
for i, index in enumerate(indices[:1]):
retrieved_img = dataset["train"][int(index)]["image"]
return retrieved_img
app = gr.Interface(
search_index,
inputs=gr.Image(),
outputs="image",
)
if __name__ == "__main__":
app.launch()
|