DimasMP3 commited on
Commit ·
8c7f090
1
Parent(s): 772fbec
fix: inference.py and app.py
Browse files- app.py +8 -68
- inference.py +11 -7
app.py
CHANGED
|
@@ -1,73 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import tensorflow as tf
|
| 5 |
-
from typing import List, Dict
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
LABELS: List[str] = ["Heart", "Oblong", "Oval", "Round", "Square"]
|
| 9 |
-
IMG_SIZE = 244
|
| 10 |
-
|
| 11 |
-
# Fungsi ini harus meniru persis apa yang dilakukan `preprocess_input` dari EfficientNet
|
| 12 |
-
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 13 |
-
"""
|
| 14 |
-
Mengubah ukuran gambar dan melakukan preprocessing yang sesuai untuk EfficientNet.
|
| 15 |
-
"""
|
| 16 |
-
# Pastikan gambar dalam format RGB
|
| 17 |
-
if image.mode != "RGB":
|
| 18 |
-
image = image.convert("RGB")
|
| 19 |
-
|
| 20 |
-
# Ubah ukuran gambar
|
| 21 |
-
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 22 |
-
|
| 23 |
-
# Konversi ke array numpy
|
| 24 |
-
image_array = np.asarray(image)
|
| 25 |
-
|
| 26 |
-
# Tambahkan dimensi batch
|
| 27 |
-
image_array = np.expand_dims(image_array, axis=0)
|
| 28 |
-
|
| 29 |
-
# Gunakan fungsi preprocessing bawaan dari EfficientNet
|
| 30 |
-
# Ini akan menormalisasi piksel ke rentang [-1, 1]
|
| 31 |
-
processed_array = tf.keras.applications.efficientnet.preprocess_input(image_array)
|
| 32 |
-
|
| 33 |
-
return processed_array
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# --- 3. Muat Model Anda ---
|
| 37 |
-
class FaceShapeModel:
|
| 38 |
-
def __init__(self, model_path: str = "best_model_antioverfit.keras") -> None:
|
| 39 |
-
"""Memuat model Keras saat kelas diinisialisasi."""
|
| 40 |
-
try:
|
| 41 |
-
# Kita tidak perlu meng-compile model untuk inferensi
|
| 42 |
-
self.model = tf.keras.models.load_model(model_path, compile=False)
|
| 43 |
-
print("Model berhasil dimuat.")
|
| 44 |
-
except Exception as e:
|
| 45 |
-
print(f"Error saat memuat model: {e}")
|
| 46 |
-
self.model = None
|
| 47 |
-
|
| 48 |
-
def predict_image(self, image: Image.Image) -> Dict[str, float]:
|
| 49 |
-
"""Melakukan prediksi pada satu gambar."""
|
| 50 |
-
if not self.model:
|
| 51 |
-
return {"Error": "Model tidak dapat dimuat."}
|
| 52 |
-
|
| 53 |
-
processed_input = preprocess_image(image)
|
| 54 |
-
|
| 55 |
-
preds = self.model.predict(processed_input)[0]
|
| 56 |
-
|
| 57 |
-
# Buat dictionary hasil {label: skor}
|
| 58 |
-
confidences = {label: float(score) for label, score in zip(LABELS, preds)}
|
| 59 |
-
|
| 60 |
-
return confidences
|
| 61 |
-
|
| 62 |
-
# Buat instance dari model kita
|
| 63 |
-
model = FaceShapeModel()
|
| 64 |
-
|
| 65 |
-
def predict(image_pil: Image.Image) -> Dict[str, float]:
|
| 66 |
-
"""Fungsi wrapper yang akan dipanggil oleh Gradio."""
|
| 67 |
-
return model.predict_image(image_pil)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# --- 4. Buat Antarmuka Gradio ---
|
| 71 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 72 |
gr.Markdown(
|
| 73 |
"""
|
|
@@ -83,8 +17,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 83 |
with gr.Column(scale=1):
|
| 84 |
label_output = gr.Label(num_top_classes=3, label="Hasil Prediksi")
|
| 85 |
|
|
|
|
| 86 |
gr.Examples(
|
| 87 |
examples=[
|
|
|
|
|
|
|
|
|
|
| 88 |
],
|
| 89 |
inputs=image_input,
|
| 90 |
outputs=label_output,
|
|
@@ -92,8 +30,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 92 |
cache_examples=True
|
| 93 |
)
|
| 94 |
|
|
|
|
| 95 |
submit_btn.click(fn=predict, inputs=image_input, outputs=label_output)
|
| 96 |
|
|
|
|
| 97 |
if __name__ == "__main__":
|
| 98 |
iface.launch()
|
| 99 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from inference import predict
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Tema dan tata letak diatur di sini
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 6 |
gr.Markdown(
|
| 7 |
"""
|
|
|
|
| 17 |
with gr.Column(scale=1):
|
| 18 |
label_output = gr.Label(num_top_classes=3, label="Hasil Prediksi")
|
| 19 |
|
| 20 |
+
# Bagian untuk menampilkan contoh gambar (opsional)
|
| 21 |
gr.Examples(
|
| 22 |
examples=[
|
| 23 |
+
"path/to/example1.jpg",
|
| 24 |
+
"path/to/example2.jpg",
|
| 25 |
+
"path/to/example3.jpg"
|
| 26 |
],
|
| 27 |
inputs=image_input,
|
| 28 |
outputs=label_output,
|
|
|
|
| 30 |
cache_examples=True
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# Hubungkan tombol "Prediksi" dengan fungsi predict yang sudah diimpor
|
| 34 |
submit_btn.click(fn=predict, inputs=image_input, outputs=label_output)
|
| 35 |
|
| 36 |
+
# Luncurkan aplikasi jika file ini dijalankan secara langsung
|
| 37 |
if __name__ == "__main__":
|
| 38 |
iface.launch()
|
| 39 |
|
inference.py
CHANGED
|
@@ -2,11 +2,13 @@ import numpy as np
|
|
| 2 |
from PIL import Image
|
| 3 |
import tensorflow as tf
|
| 4 |
from typing import List, Dict
|
|
|
|
| 5 |
|
|
|
|
| 6 |
LABELS: List[str] = ["Heart", "Oblong", "Oval", "Round", "Square"]
|
| 7 |
IMG_SIZE = 244
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 11 |
"""
|
| 12 |
Mengubah ukuran gambar dan melakukan preprocessing yang sesuai untuk EfficientNet.
|
|
@@ -23,11 +25,13 @@ def preprocess_image(image: Image.Image) -> np.ndarray:
|
|
| 23 |
|
| 24 |
return processed_array
|
| 25 |
|
| 26 |
-
#
|
| 27 |
class FaceShapeModel:
|
| 28 |
-
|
| 29 |
-
def __init__(self, model_path: str = "models/best_model_antioverfit.keras") -> None:
|
| 30 |
"""Memuat model Keras saat kelas diinisialisasi."""
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
self.model = tf.keras.models.load_model(model_path, compile=False)
|
| 33 |
print("Model berhasil dimuat dari inference.py.")
|
|
@@ -47,12 +51,12 @@ class FaceShapeModel:
|
|
| 47 |
confidences = {label: float(score) for label, score in zip(LABELS, preds)}
|
| 48 |
|
| 49 |
return confidences
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
model_instance = FaceShapeModel()
|
| 53 |
|
| 54 |
def predict(image_pil: Image.Image) -> Dict[str, float]:
|
| 55 |
-
"""Fungsi wrapper yang akan dipanggil oleh Gradio
|
| 56 |
if model_instance:
|
| 57 |
return model_instance.predict_image(image_pil)
|
| 58 |
return {"Error": "Instance model tidak tersedia."}
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import tensorflow as tf
|
| 4 |
from typing import List, Dict
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# --- 1. Konfigurasi Model ---
|
| 8 |
LABELS: List[str] = ["Heart", "Oblong", "Oval", "Round", "Square"]
|
| 9 |
IMG_SIZE = 244
|
| 10 |
|
| 11 |
+
# --- 2. Fungsi Preprocessing ---
|
| 12 |
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 13 |
"""
|
| 14 |
Mengubah ukuran gambar dan melakukan preprocessing yang sesuai untuk EfficientNet.
|
|
|
|
| 25 |
|
| 26 |
return processed_array
|
| 27 |
|
| 28 |
+
# --- 3. Kelas untuk Mengelola Model ---
|
| 29 |
class FaceShapeModel:
|
| 30 |
+
def __init__(self) -> None:
|
|
|
|
| 31 |
"""Memuat model Keras saat kelas diinisialisasi."""
|
| 32 |
+
model_path = os.path.join(os.getcwd(), "models/best_model_antioverfit.keras")
|
| 33 |
+
print(f"Mencoba memuat model dari path: {model_path}")
|
| 34 |
+
|
| 35 |
try:
|
| 36 |
self.model = tf.keras.models.load_model(model_path, compile=False)
|
| 37 |
print("Model berhasil dimuat dari inference.py.")
|
|
|
|
| 51 |
confidences = {label: float(score) for label, score in zip(LABELS, preds)}
|
| 52 |
|
| 53 |
return confidences
|
| 54 |
+
|
| 55 |
+
# Buat satu instance dari model kita
|
| 56 |
model_instance = FaceShapeModel()
|
| 57 |
|
| 58 |
def predict(image_pil: Image.Image) -> Dict[str, float]:
|
| 59 |
+
"""Fungsi wrapper yang akan dipanggil oleh Gradio."""
|
| 60 |
if model_instance:
|
| 61 |
return model_instance.predict_image(image_pil)
|
| 62 |
return {"Error": "Instance model tidak tersedia."}
|