Spaces:
Runtime error
Runtime error
Commit
Β·
b04f948
0
Parent(s):
Duplicate from keras-io/metric-learning-image-similarity-search
Browse filesCo-authored-by: Vrinda Prabhu <[email protected]>
- .gitattributes +27 -0
- README.md +13 -0
- app.py +81 -0
- examples/car.jpeg +0 -0
- examples/horse.jpeg +0 -0
- examples/yatch.jpeg +0 -0
- requirements.txt +1 -0
.gitattributes
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
19 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Metric Learning Image Similarity Search
|
3 |
+
emoji: π
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.0.13
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: keras-io/metric-learning-image-similarity-search
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
from tensorflow.keras.datasets import cifar10
|
5 |
+
|
6 |
+
from huggingface_hub import from_pretrained_keras
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
|
10 |
+
def prepare_output(neighbours):
|
11 |
+
"""Function to return the image grid based on the nearest neighbours
|
12 |
+
@params neighbours: List of indices of the nearest neighbours"""
|
13 |
+
anchor_near_neighbours = reversed(neighbours)
|
14 |
+
img_grid = Image.new("RGB", (HEIGHT_WIDTH * 5, HEIGHT_WIDTH * 2))
|
15 |
+
|
16 |
+
# Image Grid of top-10 neighbours
|
17 |
+
for idx, nn_idx in enumerate(anchor_near_neighbours):
|
18 |
+
img_arr = (np.array(x_test[nn_idx]) * 255).astype(np.uint8)
|
19 |
+
img_grid.paste(
|
20 |
+
Image.fromarray(img_arr, "RGB"),
|
21 |
+
((idx % 5) * HEIGHT_WIDTH, (idx // 5) * HEIGHT_WIDTH),
|
22 |
+
)
|
23 |
+
|
24 |
+
return img_grid
|
25 |
+
|
26 |
+
|
27 |
+
def get_nearest_neighbours(img):
|
28 |
+
"""Has the inference code to get the nearest neighbours from the model
|
29 |
+
@params img: Image to be fed to the model"""
|
30 |
+
|
31 |
+
# Pre-process image
|
32 |
+
img = np.expand_dims(img / 255, axis=0)
|
33 |
+
img_x_test = np.append(x_test, img, axis=0)
|
34 |
+
|
35 |
+
# Get the embeddings and check the cosine distance
|
36 |
+
embeddings = model.predict(img_x_test)
|
37 |
+
gram_matrix = np.einsum("ae,be->ab", embeddings, embeddings)
|
38 |
+
near_neighbours = np.argsort(gram_matrix.T)[:, -(NEAR_NEIGHBOURS + 1) :]
|
39 |
+
|
40 |
+
# Make image grid output
|
41 |
+
img_grid = prepare_output(near_neighbours[-1][:-1])
|
42 |
+
return np.array(img_grid)
|
43 |
+
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
# Constants
|
47 |
+
HEIGHT_WIDTH = 32
|
48 |
+
NEAR_NEIGHBOURS = 10
|
49 |
+
|
50 |
+
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
|
51 |
+
x_test = x_test.astype("float32") / 255.0
|
52 |
+
|
53 |
+
model = from_pretrained_keras("keras-io/cifar10_metric_learning")
|
54 |
+
|
55 |
+
examples = ["examples/yatch.jpeg", "examples/horse.jpeg", "examples/car.jpeg"]
|
56 |
+
title = "Metric Learning for Image Similarity Search"
|
57 |
+
|
58 |
+
more_text = """Embeddings for the input image are computed using the model. The nearest neighbours are then calculated
|
59 |
+
using cosine distance. These are shown here as an image grid."""
|
60 |
+
|
61 |
+
description = f"This space uses model trained on CIFAR10 dataset using metric learning technique.\n{more_text}\n\n"
|
62 |
+
|
63 |
+
article = """
|
64 |
+
<p style='text-align: center'>
|
65 |
+
<a href='https://keras.io/examples/vision/metric_learning/' target='_blank'>Keras Example given by Mat Kelcey</a>
|
66 |
+
<br>
|
67 |
+
Space by Vrinda Prabhu
|
68 |
+
</p>
|
69 |
+
"""
|
70 |
+
|
71 |
+
gr.Interface(
|
72 |
+
fn=get_nearest_neighbours,
|
73 |
+
inputs=gr.Image(shape=(32, 32)), # Resize to CIFAR
|
74 |
+
outputs=gr.Image(),
|
75 |
+
examples=examples,
|
76 |
+
article=article,
|
77 |
+
allow_flagging="never",
|
78 |
+
analytics_enabled=False,
|
79 |
+
title=title,
|
80 |
+
description=description,
|
81 |
+
).launch(enable_queue=True)
|
examples/car.jpeg
ADDED
![]() |
examples/horse.jpeg
ADDED
![]() |
examples/yatch.jpeg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
tensorflow
|