Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import onnxruntime as ort
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
# ==============================================================================
|
| 9 |
+
# 1. PENGUNDUHAN & INISIALISASI MODEL ONNX FROM HUB
|
| 10 |
+
# ==============================================================================
|
| 11 |
+
REPO_ID = "ASomeoneWhoInterestedWithAI/LookThemV7_Caltech256-ONNX"
|
| 12 |
+
|
| 13 |
+
print("📡 Mengunduh sasis LookThem V7-W dari Hugging Face Hub...")
|
| 14 |
+
onnx_path = hf_hub_download(repo_id=REPO_ID, filename="LookThemV7_Caltech256.onnx")
|
| 15 |
+
onnx_data_path = hf_hub_download(repo_id=REPO_ID, filename="LookThemV7_Caltech256.onnx.data")
|
| 16 |
+
|
| 17 |
+
print("🧠 Membakar Graf ke ONNX Runtime Session...")
|
| 18 |
+
session = ort.InferenceSession(onnx_path)
|
| 19 |
+
input_name = session.get_inputs()[0].name
|
| 20 |
+
output_name = session.get_outputs()[0].name
|
| 21 |
+
|
| 22 |
+
# ==============================================================================
|
| 23 |
+
# 2. STRUKTUR MAP RESMI 257 LABEL SEMANTIK CALTECH-256
|
| 24 |
+
# ==============================================================================
|
| 25 |
+
CLASSES = [
|
| 26 |
+
# 001 - 050
|
| 27 |
+
"ak-47", "american-flag", "backpack", "baseball-bat", "baseball-glove", "basketball-hoop", "bat", "bathtub", "bear", "beer-mug",
|
| 28 |
+
"billiards", "binoculars", "birdbath", "blimp", "bonsai-101", "boom-box", "bowling-ball", "bowling-pin", "boxing-glove", "brain-101",
|
| 29 |
+
"breadmaker", "buddha-101", "bulldozer", "butterfly", "cactus", "cake", "calculator", "camel", "cannon", "canoe",
|
| 30 |
+
"car-tire", "cart", "castle", "cat", "cd", "chair", "chimp", "chopsticks", "church-101", "clapperboard",
|
| 31 |
+
"computer-monitor", "computer-mouse", "couch", "crab", "crane", "croquet-ball", "crutch", "cuff-links", "cup", "dart-board",
|
| 32 |
+
# 051 - 100
|
| 33 |
+
"dining-table", "dog", "dolphindir", "door", "donkey", "dove", "duck", "dumbbell", "electronic-calc", "electricity-meter",
|
| 34 |
+
"faces-easy", "ferry", "fire-hydrant", "fire-truck", "fireworks", "flashlight", "floppy-disk", "football-helmet", "fox", "gramophone",
|
| 35 |
+
"grill", "hand-pump", "harp", "hot-air-balloon", "hot-dog", "hot-tub", "hourglass", "ibex", "ibis-101", "icon",
|
| 36 |
+
"igloo", "ipod", "iris", "iso-motorcycle", "jaguar", "kangaroo", "ketch", "keypad", "lamp", "laptop",
|
| 37 |
+
"leotard", "light-bulb", "lighter", "lizard", "lobster", "lotus", "mandolin", "marsupial", "microphone", "minimax",
|
| 38 |
+
# 101 - 150
|
| 39 |
+
"miniskirt", "modern-chair", "motorcycle", "mountain-bike", "mouse", "mule", "mussels", "necktie", "octopus", "owl",
|
| 40 |
+
"palm-tree", "piggy-bank", "pikachu", "pipe", "pistol", "pizza", "platypus", "pool-table", "pretzel", "pumice-stone",
|
| 41 |
+
"pumpkin", "puncher", "python", "quartz-clock", "rabbit", "racoon", "ray", "remote-control", "rhinoceros", "roller-skate",
|
| 42 |
+
"rose", "rotary-phone", "roller-blades", "saddle", "sand-sculptr", "saturn", "school-bus", "schooner", "squirrel", "stapler",
|
| 43 |
+
"starfish", "stepladder", "stingray", "stocking", "stopwatch", "sumo", "sunflower", "swiss-army-knife", "sword", "syringe",
|
| 44 |
+
# 151 - 200
|
| 45 |
+
"table-lamp", "teapot", "teddy-bear", "teepee", "telephone-box", "tennis-ball", "tennis-court", "tennis-racket", "theodolite", "toaster",
|
| 46 |
+
"tomato", "tombstone", "trefoil", "triceratops", "unicycle", "usb-drive", "vase", "violin", "voltaic-pile", "walking-cane",
|
| 47 |
+
"wall-clock", "water-bottle", "watermelon", "welding-mask", "wheelbarrow", "windmill", "wine-bottle", "xlophone", "yarmulke", "yo-yo",
|
| 48 |
+
"zebra", "airplanes-101", "car-side-101", "faces-easy-101", "greyhound", "tennis-shoes", "toad", "clutter",
|
| 49 |
+
# 201 - 257 (Additional & Overlap Categories)
|
| 50 |
+
"speedboat", "stainglass", "stegosaurus", "stop-sign", "strawberry", "sun-hat", "swan", "tank", "tick", "thimble",
|
| 51 |
+
"toad-101", "tree", "trombon", "trumpet", "tv-monitor", "typewritter", "umbrella", "ventilat", "watch", "waterfall",
|
| 52 |
+
"welding-mask-101", "wheelbarrow-101", "windmill-101", "wine-bottle-101", "wrench", "xylophone-101", "yarmulke-101", "yo-yo-101", "zebra-101", "airplanes-side-101",
|
| 53 |
+
"car-side-101-dup", "ferry-101", "ketch-101", "motorbikes-101", "schooner-101", "binocular", "bonsai", "buddha", "camera", "cannon-dup",
|
| 54 |
+
"car-side", "ceiling-fan", "cellphone", "chair-dup", "chandelier", "cougar-body", "cougar-face", "crab-dup", "crayfish", "crocodile",
|
| 55 |
+
"crocodile-head", "cup-dup", "dalmatian", "dollar-bill", "dragonfly", "electric-guitar", "clutter-bg"
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
# ==============================================================================
|
| 59 |
+
# 3. FUNGSI PIPELINE INFERENCE (PREPROCESSING & RUN)
|
| 60 |
+
# ==============================================================================
|
| 61 |
+
def predict_image(img):
|
| 62 |
+
if img is None:
|
| 63 |
+
return "Silakan masukkan gambar terlebih dahulu."
|
| 64 |
+
|
| 65 |
+
# --- PREPROCESSING (Sinkron dengan Transformasi PyTorch V7-W) ---
|
| 66 |
+
if len(img.shape) == 2:
|
| 67 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
| 68 |
+
elif img.shape[2] == 4:
|
| 69 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
|
| 70 |
+
|
| 71 |
+
img_resized = cv2.resize(img, (96, 96))
|
| 72 |
+
img_normalized = img_resized.astype(np.float32) / 255.0
|
| 73 |
+
|
| 74 |
+
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
|
| 75 |
+
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
| 76 |
+
img_normalized = (img_normalized - mean) / std
|
| 77 |
+
|
| 78 |
+
img_final = img_normalized.transpose(2, 0, 1)
|
| 79 |
+
input_tensor = np.expand_dims(img_final, axis=0)
|
| 80 |
+
|
| 81 |
+
# --- TEMBAK RUNTIME INFERENCE ---
|
| 82 |
+
raw_outputs = session.run([output_name], {input_name: input_tensor})
|
| 83 |
+
logits = raw_outputs[0][0]
|
| 84 |
+
|
| 85 |
+
# --- PASCA-PROSES (SOFTMAX STABIL) ---
|
| 86 |
+
exp_logits = np.exp(logits - np.max(logits))
|
| 87 |
+
probabilities = exp_logits / np.sum(exp_logits)
|
| 88 |
+
|
| 89 |
+
# Mengambil Top 5 prediksi tertinggi untuk dashboard Gradio
|
| 90 |
+
top_5_indices = np.argsort(probabilities)[::-1][:5]
|
| 91 |
+
|
| 92 |
+
results = {}
|
| 93 |
+
for idx in top_5_indices:
|
| 94 |
+
# Penanganan darurat jika indeks melompat di luar batas list
|
| 95 |
+
if idx < len(CLASSES):
|
| 96 |
+
class_name = CLASSES[idx]
|
| 97 |
+
else:
|
| 98 |
+
class_name = f"Unknown Class Idx-{idx}"
|
| 99 |
+
|
| 100 |
+
confidence = float(probabilities[idx])
|
| 101 |
+
results[class_name] = confidence
|
| 102 |
+
|
| 103 |
+
return results
|
| 104 |
+
|
| 105 |
+
# ==============================================================================
|
| 106 |
+
# 4. ARSITEKTUR ANTARMUKA GRADIO (UI DESIGN)
|
| 107 |
+
# ==============================================================================
|
| 108 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 109 |
+
gr.Markdown(
|
| 110 |
+
"""
|
| 111 |
+
# 🏎️ LookThem V7 (JEPA-Token Engine) - Caltech-256 Demo
|
| 112 |
+
### Sasis Kustom Mini 0.17 MB (Graf) + External Data | Akurasi Pengujian Semantik 38.03% (*From Scratch*)!
|
| 113 |
+
Modul ini berjalan menggunakan **ONNX Runtime Engine** berbasis pelacakan Dynamo. Semua dropout latihan telah dilepas!
|
| 114 |
+
"""
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Row():
|
| 118 |
+
with gr.Column():
|
| 119 |
+
input_img = gr.Image(type="numpy", label="Input Gambar Caltech-256")
|
| 120 |
+
btn_submit = gr.Button("Analisis Karakteristik Gambar 🚀", variant="primary")
|
| 121 |
+
|
| 122 |
+
with gr.Column():
|
| 123 |
+
output_labels = gr.Label(num_top_classes=5, label="Top 5 Prediksi Probabilitas Semantik")
|
| 124 |
+
|
| 125 |
+
btn_submit.click(fn=predict_image, inputs=input_img, outputs=output_labels)
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
demo.launch()
|