Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,103 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
from transformers import AutoImageProcessor, AutoModel
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def
|
| 17 |
inputs = processor(images=image, return_tensors="pt")
|
| 18 |
with torch.no_grad():
|
| 19 |
outputs = model(**inputs)
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return hasil
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
-
|
| 64 |
-
with gr.Tab("1οΈβ£ Tambah Data Sampling"):
|
| 65 |
-
img1 = gr.Image(type="pil", label="Gambar Sampling")
|
| 66 |
-
lbl = gr.Textbox(label="Label")
|
| 67 |
-
out1 = gr.Textbox(label="Status")
|
| 68 |
-
btn1 = gr.Button("Tambah")
|
| 69 |
-
|
| 70 |
-
btn1.click(tambah_sampling, [img1, lbl], out1)
|
| 71 |
-
|
| 72 |
-
with gr.Tab("2οΈβ£ Tebak Gambar"):
|
| 73 |
-
img2 = gr.Image(type="pil", label="Gambar Uji")
|
| 74 |
-
out2 = gr.Textbox(label="Hasil")
|
| 75 |
-
btn2 = gr.Button("Tebak")
|
| 76 |
-
|
| 77 |
-
btn2.click(tebak_gambar, img2, out2)
|
| 78 |
|
| 79 |
app.launch()
|
|
|
|
| 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 |
+
import os
|
| 7 |
+
|
| 8 |
+
# =========================
|
| 9 |
+
# Load model pretrained
|
| 10 |
+
# =========================
|
| 11 |
+
processor = AutoImageProcessor.from_pretrained(
|
| 12 |
+
"google/vit-base-patch16-224"
|
| 13 |
+
)
|
| 14 |
+
model = AutoModel.from_pretrained(
|
| 15 |
+
"google/vit-base-patch16-224"
|
| 16 |
+
)
|
| 17 |
model.eval()
|
| 18 |
|
| 19 |
+
# =========================
|
| 20 |
+
# Ekstraksi fitur
|
| 21 |
+
# =========================
|
| 22 |
+
def get_embedding(image):
|
| 23 |
inputs = processor(images=image, return_tensors="pt")
|
| 24 |
with torch.no_grad():
|
| 25 |
outputs = model(**inputs)
|
| 26 |
+
embedding = outputs.last_hidden_state.mean(dim=1)
|
| 27 |
+
return embedding.squeeze().numpy()
|
| 28 |
+
|
| 29 |
+
# =========================
|
| 30 |
+
# Cosine similarity
|
| 31 |
+
# =========================
|
| 32 |
+
def cosine_similarity(a, b):
|
| 33 |
+
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
| 34 |
+
|
| 35 |
+
# =========================
|
| 36 |
+
# Fungsi utama
|
| 37 |
+
# =========================
|
| 38 |
+
def unsupervised_image_similarity(sample_path, test_path):
|
| 39 |
+
if sample_path is None or test_path is None:
|
| 40 |
+
return "β Silakan upload gambar sampling dan gambar uji."
|
| 41 |
+
|
| 42 |
+
# Ambil nama file sampling
|
| 43 |
+
sample_name = os.path.basename(sample_path)
|
| 44 |
+
|
| 45 |
+
# Buka gambar
|
| 46 |
+
img_sample = Image.open(sample_path).convert("RGB")
|
| 47 |
+
img_test = Image.open(test_path).convert("RGB")
|
| 48 |
+
|
| 49 |
+
# Ekstraksi embedding
|
| 50 |
+
emb1 = get_embedding(img_sample)
|
| 51 |
+
emb2 = get_embedding(img_test)
|
| 52 |
+
|
| 53 |
+
similarity = cosine_similarity(emb1, emb2)
|
| 54 |
+
|
| 55 |
+
# Threshold
|
| 56 |
+
threshold = 0.75
|
| 57 |
+
|
| 58 |
+
if similarity >= threshold:
|
| 59 |
+
status = (
|
| 60 |
+
f"β
GAMBAR UJI MEMILIKI KEMIRIPAN\n"
|
| 61 |
+
f"dengan SAMPLING: **{sample_name}**"
|
| 62 |
+
)
|
| 63 |
+
else:
|
| 64 |
+
status = (
|
| 65 |
+
"β GAMBAR UJI TIDAK MEMILIKI KEMIRIPAN\n"
|
| 66 |
+
"dengan sampling yang diinput"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
hasil = f"""
|
| 70 |
+
HASIL UNSUPERVISED LEARNING
|
| 71 |
+
|
| 72 |
+
Nama sampling : {sample_name}
|
| 73 |
+
Nilai kemiripan: {similarity:.2f}
|
| 74 |
+
|
| 75 |
+
Keputusan:
|
| 76 |
+
{status}
|
| 77 |
+
|
| 78 |
+
Catatan:
|
| 79 |
+
- Tidak menggunakan dataset
|
| 80 |
+
- Tidak menggunakan label
|
| 81 |
+
- Tidak melakukan training ulang
|
| 82 |
+
- Pendekatan unsupervised berbasis similarity
|
| 83 |
+
"""
|
| 84 |
return hasil
|
| 85 |
|
| 86 |
+
# =========================
|
| 87 |
+
# UI Gradio
|
| 88 |
+
# =========================
|
| 89 |
+
app = gr.Interface(
|
| 90 |
+
fn=unsupervised_image_similarity,
|
| 91 |
+
inputs=[
|
| 92 |
+
gr.Image(type="filepath", label="Gambar Sampling"),
|
| 93 |
+
gr.Image(type="filepath", label="Gambar Uji")
|
| 94 |
+
],
|
| 95 |
+
outputs=gr.Textbox(label="Hasil Analisis"),
|
| 96 |
+
title="π§ Unsupervised Image Similarity dengan Nama Sampling",
|
| 97 |
+
description=(
|
| 98 |
+
"Aplikasi unsupervised learning untuk mengecek apakah gambar uji "
|
| 99 |
+
"memiliki kemiripan dengan gambar sampling tertentu."
|
| 100 |
)
|
| 101 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
app.launch()
|