Spaces:
Runtime error
Runtime error
konstantinG
commited on
Commit
•
77fd092
1
Parent(s):
2fe2683
Upload 18 files
Browse files- .gitattributes +1 -0
- data/results.csv +3 -0
- data/ten_pics/134206.jpg +0 -0
- data/ten_pics/148284.jpg +0 -0
- data/ten_pics/178045.jpg +0 -0
- data/ten_pics/205842.jpg +0 -0
- data/ten_pics/256063.jpg +0 -0
- data/ten_pics/301246.jpg +0 -0
- data/ten_pics/353913.jpg +0 -0
- data/ten_pics/36979.jpg +0 -0
- data/ten_pics/65567.jpg +0 -0
- data/ten_pics/81641.jpg +0 -0
- embeddings/emb_images_30k.npy +3 -0
- embeddings/emb_images_5000.npy +3 -0
- funcs/__pycache__/fiass_similaruty.cpython-39.pyc +0 -0
- funcs/__pycache__/get_similarity.cpython-39.pyc +0 -0
- funcs/fiass_similaruty.py +34 -0
- funcs/get_embeddings.py +34 -0
- funcs/get_similarity.py +52 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
data/results.csv filter=lfs diff=lfs merge=lfs -text
|
data/results.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7cf240f2c5d6c4e9cd0d1acef589e36225a3622ad93bb6e1fe7f828a4c9fb31d
|
3 |
+
size 13350817
|
data/ten_pics/134206.jpg
ADDED
data/ten_pics/148284.jpg
ADDED
data/ten_pics/178045.jpg
ADDED
data/ten_pics/205842.jpg
ADDED
data/ten_pics/256063.jpg
ADDED
data/ten_pics/301246.jpg
ADDED
data/ten_pics/353913.jpg
ADDED
data/ten_pics/36979.jpg
ADDED
data/ten_pics/65567.jpg
ADDED
data/ten_pics/81641.jpg
ADDED
embeddings/emb_images_30k.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:154dab0b8ad9455c4df7d819b471987f23d46c63633fc76a480bbbba9e2c20a8
|
3 |
+
size 32545920
|
embeddings/emb_images_5000.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ec835458c3f03a01564e3a2c0bbc2cc5326f0f52fae2eb6edd02ad0d0eb1734e
|
3 |
+
size 10240128
|
funcs/__pycache__/fiass_similaruty.cpython-39.pyc
ADDED
Binary file (1.43 kB). View file
|
|
funcs/__pycache__/get_similarity.cpython-39.pyc
ADDED
Binary file (2.08 kB). View file
|
|
funcs/fiass_similaruty.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
import clip
|
4 |
+
import torch.nn.functional as F
|
5 |
+
import faiss
|
6 |
+
|
7 |
+
device = 'cpu'
|
8 |
+
model_path = "weights/ViT-B-32.pt"
|
9 |
+
|
10 |
+
model, preprocess = clip.load('ViT-B/32', device)
|
11 |
+
|
12 |
+
def load_embeddings(path_to_emb_file):
|
13 |
+
features = np.load(path_to_emb_file)
|
14 |
+
features = torch.from_numpy(features)
|
15 |
+
features = features.squeeze(1)
|
16 |
+
features = F.normalize(features, p=2, dim=-1)
|
17 |
+
return features
|
18 |
+
|
19 |
+
def encode_text(query):
|
20 |
+
text = clip.tokenize([query]).to(device)
|
21 |
+
text_features = model.encode_text(text).to("cpu")
|
22 |
+
text_features= F.normalize(text_features, p=2, dim=-1)
|
23 |
+
text_features = text_features.to("cpu").detach().numpy()
|
24 |
+
return text_features
|
25 |
+
|
26 |
+
|
27 |
+
def find_matches_fiass(image_embeddings, query, image_filenames, n=5):
|
28 |
+
features = image_embeddings
|
29 |
+
index = faiss.IndexFlatL2(features.shape[1])
|
30 |
+
index.add(features)
|
31 |
+
text_features = encode_text(query)
|
32 |
+
_, I = index.search(text_features, n)
|
33 |
+
matches = [image_filenames[idx] for idx in I.squeeze(0)]
|
34 |
+
return matches
|
funcs/get_embeddings.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import clip
|
2 |
+
import os
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
|
7 |
+
device = 'cpu'
|
8 |
+
model_path = "weights/ViT-B-32.pt"
|
9 |
+
|
10 |
+
model, preprocess = clip.load('ViT-B/32', device)
|
11 |
+
|
12 |
+
|
13 |
+
def get_emb(image_folder):
|
14 |
+
image_folder = image_folder
|
15 |
+
image_paths = []
|
16 |
+
for filename in os.listdir(image_folder):
|
17 |
+
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
|
18 |
+
image_path = os.path.join(image_folder, filename)
|
19 |
+
image_paths.append(image_path)
|
20 |
+
paths = image_paths
|
21 |
+
images = [Image.open(path) for path in image_paths]
|
22 |
+
with torch.no_grad():
|
23 |
+
features = []
|
24 |
+
for image in images:
|
25 |
+
image_tensor = preprocess(image).unsqueeze(0).to(device)
|
26 |
+
feature = model.encode_image(image_tensor)
|
27 |
+
features.append(feature.detach().cpu().numpy())
|
28 |
+
features = np.array(features)
|
29 |
+
np.save("emb_images.npy", features)
|
30 |
+
return features, paths
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
funcs/get_similarity.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
import clip
|
7 |
+
import numpy as np
|
8 |
+
import torch.nn.functional as F
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
|
11 |
+
device = 'cpu'
|
12 |
+
model_path = "weights/ViT-B-32.pt"
|
13 |
+
|
14 |
+
model, preprocess = clip.load('ViT-B/32', device)
|
15 |
+
|
16 |
+
def get_similarity_score(text_query, image_features):
|
17 |
+
text_tokens = clip.tokenize([text_query]).to(device)
|
18 |
+
with torch.no_grad():
|
19 |
+
text_features = model.encode_text(text_tokens).squeeze(0)
|
20 |
+
text_features= F.normalize(text_features, p=2, dim=-1)
|
21 |
+
similarity_score = text_features @ image_features.T * 100.0
|
22 |
+
similarity_score = similarity_score.squeeze(0)
|
23 |
+
return similarity_score
|
24 |
+
|
25 |
+
def create_filelist(path_to_imagefolder):
|
26 |
+
image_folder = path_to_imagefolder
|
27 |
+
image_paths = []
|
28 |
+
for filename in os.listdir(image_folder):
|
29 |
+
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
|
30 |
+
image_path = os.path.join(image_folder, filename)
|
31 |
+
image_paths.append(image_path)
|
32 |
+
file_paths = image_paths
|
33 |
+
return file_paths
|
34 |
+
|
35 |
+
def load_embeddings(path_to_emb_file):
|
36 |
+
features = np.load(path_to_emb_file)
|
37 |
+
features = torch.from_numpy(features)
|
38 |
+
return features
|
39 |
+
|
40 |
+
|
41 |
+
def find_matches(image_embeddings, query, image_filenames, n=6):
|
42 |
+
text_query = query
|
43 |
+
features = image_embeddings
|
44 |
+
similarity_scores = []
|
45 |
+
for emb in features:
|
46 |
+
emb /= emb.norm(dim=-1, keepdim=True)
|
47 |
+
similarity_score = get_similarity_score(text_query, emb)
|
48 |
+
similarity_scores.append(similarity_score)
|
49 |
+
similarity_scores = torch.stack(similarity_scores)
|
50 |
+
values, indices = torch.topk(similarity_scores.squeeze(0), 6)
|
51 |
+
matches = [image_filenames[idx] for idx in indices]
|
52 |
+
return matches
|