Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
from transformers import AutoImageProcessor, AutoModel
|
| 6 |
-
from sklearn.cluster import KMeans
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
with torch.no_grad():
|
| 19 |
-
outputs = model(**inputs)
|
| 20 |
-
embedding = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
|
| 21 |
-
features.append(embedding)
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return "Jumlah gambar harus ≥ jumlah cluster"
|
| 28 |
|
| 29 |
-
|
| 30 |
-
kmeans = KMeans(n_clusters=n_cluster, random_state=42)
|
| 31 |
-
labels = kmeans.fit_predict(X)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
return hasil
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
app = gr.Interface(
|
| 41 |
-
fn=
|
| 42 |
inputs=[
|
| 43 |
-
gr.
|
| 44 |
-
gr.
|
| 45 |
],
|
| 46 |
-
outputs=gr.Textbox(label="Hasil
|
| 47 |
-
title="🧠 Unsupervised Image
|
| 48 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from transformers import AutoImageProcessor, AutoModel
|
|
|
|
| 5 |
|
| 6 |
+
# =========================
|
| 7 |
+
# Load model pretrained
|
| 8 |
+
# =========================
|
| 9 |
+
processor = AutoImageProcessor.from_pretrained(
|
| 10 |
+
"google/vit-base-patch16-224"
|
| 11 |
+
)
|
| 12 |
+
model = AutoModel.from_pretrained(
|
| 13 |
+
"google/vit-base-patch16-224"
|
| 14 |
+
)
|
| 15 |
model.eval()
|
| 16 |
|
| 17 |
+
# =========================
|
| 18 |
+
# Fungsi ekstraksi fitur
|
| 19 |
+
# =========================
|
| 20 |
+
def get_embedding(image):
|
| 21 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
embedding = outputs.last_hidden_state.mean(dim=1)
|
| 25 |
+
return embedding.squeeze().numpy()
|
| 26 |
+
|
| 27 |
+
# =========================
|
| 28 |
+
# Cosine similarity
|
| 29 |
+
# =========================
|
| 30 |
+
def cosine_similarity(a, b):
|
| 31 |
+
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
| 32 |
+
|
| 33 |
+
# =========================
|
| 34 |
+
# Fungsi utama aplikasi
|
| 35 |
+
# =========================
|
| 36 |
+
def unsupervised_image_similarity(img1, img2):
|
| 37 |
+
if img1 is None or img2 is None:
|
| 38 |
+
return "❌ Silakan upload DUA gambar."
|
| 39 |
|
| 40 |
+
emb1 = get_embedding(img1)
|
| 41 |
+
emb2 = get_embedding(img2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
similarity = cosine_similarity(emb1, emb2)
|
| 44 |
|
| 45 |
+
hasil = f"""
|
| 46 |
+
HASIL UNSUPERVISED LEARNING
|
|
|
|
| 47 |
|
| 48 |
+
Nilai kemiripan gambar : {similarity:.2f}
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
Interpretasi:
|
| 51 |
+
- Mendekati 1 → Gambar sangat mirip
|
| 52 |
+
- Mendekati 0 → Gambar berbeda
|
| 53 |
+
- Nilai negatif → Sangat tidak mirip
|
| 54 |
|
| 55 |
+
Catatan:
|
| 56 |
+
Tidak menggunakan dataset, label, atau training ulang.
|
| 57 |
+
"""
|
| 58 |
return hasil
|
| 59 |
|
| 60 |
+
# =========================
|
| 61 |
+
# Tampilan aplikasi
|
| 62 |
+
# =========================
|
| 63 |
app = gr.Interface(
|
| 64 |
+
fn=unsupervised_image_similarity,
|
| 65 |
inputs=[
|
| 66 |
+
gr.Image(type="pil", label="Gambar 1"),
|
| 67 |
+
gr.Image(type="pil", label="Gambar 2")
|
| 68 |
],
|
| 69 |
+
outputs=gr.Textbox(label="Hasil Analisis"),
|
| 70 |
+
title="🧠 Unsupervised Image Similarity",
|
| 71 |
+
description=(
|
| 72 |
+
"Aplikasi unsupervised learning untuk mengukur kemiripan dua gambar "
|
| 73 |
+
"tanpa dataset dan tanpa label."
|
| 74 |
+
)
|
| 75 |
)
|
| 76 |
|
| 77 |
app.launch()
|