Risma22 commited on
Commit
c07af7a
·
verified ·
1 Parent(s): 158ce08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -30
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
- # Load model pretrained (tanpa training ulang)
9
- processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
10
- model = AutoModel.from_pretrained("google/vit-base-patch16-224")
 
 
 
 
 
 
11
  model.eval()
12
 
13
- def extract_features(images):
14
- features = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- for img in images:
17
- inputs = processor(images=img, return_tensors="pt")
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
- return np.array(features)
24
 
25
- def cluster_images(images, n_cluster):
26
- if len(images) < n_cluster:
27
- return "Jumlah gambar harus ≥ jumlah cluster"
28
 
29
- X = extract_features(images)
30
- kmeans = KMeans(n_clusters=n_cluster, random_state=42)
31
- labels = kmeans.fit_predict(X)
32
 
33
- hasil = ""
34
- for i in range(n_cluster):
35
- idx = np.where(labels == i)[0]
36
- hasil += f"\n📁 Cluster {i+1}: {len(idx)} gambar\n"
37
 
 
 
 
38
  return hasil
39
 
 
 
 
40
  app = gr.Interface(
41
- fn=cluster_images,
42
  inputs=[
43
- gr.Gallery(label="Upload beberapa gambar"),
44
- gr.Slider(2, 5, step=1, label="Jumlah Cluster")
45
  ],
46
- outputs=gr.Textbox(label="Hasil Clustering"),
47
- title="🧠 Unsupervised Image Clustering",
48
- description="Aplikasi unsupervised learning: mengelompokkan gambar TANPA dataset & TANPA label."
 
 
 
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()