Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
-
import torch.optim as optim
|
| 4 |
import torchvision.transforms as transforms
|
| 5 |
from PIL import Image
|
| 6 |
import gradio as gr
|
|
@@ -10,16 +9,19 @@ class SimpleCNN(nn.Module):
|
|
| 10 |
def __init__(self):
|
| 11 |
super(SimpleCNN, self).__init__()
|
| 12 |
self.conv_layers = nn.Sequential(
|
| 13 |
-
nn.Conv2d(3,
|
| 14 |
nn.ReLU(),
|
| 15 |
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 16 |
-
nn.Conv2d(
|
|
|
|
|
|
|
|
|
|
| 17 |
nn.ReLU(),
|
| 18 |
nn.MaxPool2d(kernel_size=2, stride=2)
|
| 19 |
)
|
| 20 |
self.fc_layers = nn.Sequential(
|
| 21 |
nn.Flatten(),
|
| 22 |
-
nn.Linear(
|
| 23 |
nn.ReLU(),
|
| 24 |
nn.Linear(128, 6) # Output layer untuk 6 kelas
|
| 25 |
)
|
|
@@ -32,9 +34,13 @@ class SimpleCNN(nn.Module):
|
|
| 32 |
# Inisialisasi model
|
| 33 |
model = SimpleCNN()
|
| 34 |
|
| 35 |
-
# Load model
|
| 36 |
-
|
| 37 |
-
model.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Kelas mapping
|
| 40 |
class_mapping = {0: 'Bu dian', 1: 'Deri', 2: 'Putra', 3: 'Unknown', 4: 'Uqi', 5: 'Uwa'}
|
|
@@ -50,11 +56,17 @@ def predict(image):
|
|
| 50 |
image = transform(image).unsqueeze(0) # Tambah batch dimension
|
| 51 |
with torch.no_grad():
|
| 52 |
output = model(image)
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Buat UI Gradio
|
| 57 |
iface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="text")
|
| 58 |
|
| 59 |
# Jalankan aplikasi
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
|
|
|
| 3 |
import torchvision.transforms as transforms
|
| 4 |
from PIL import Image
|
| 5 |
import gradio as gr
|
|
|
|
| 9 |
def __init__(self):
|
| 10 |
super(SimpleCNN, self).__init__()
|
| 11 |
self.conv_layers = nn.Sequential(
|
| 12 |
+
nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
|
| 13 |
nn.ReLU(),
|
| 14 |
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 15 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
| 16 |
+
nn.ReLU(),
|
| 17 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 18 |
+
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
|
| 19 |
nn.ReLU(),
|
| 20 |
nn.MaxPool2d(kernel_size=2, stride=2)
|
| 21 |
)
|
| 22 |
self.fc_layers = nn.Sequential(
|
| 23 |
nn.Flatten(),
|
| 24 |
+
nn.Linear(128 * 16 * 16, 128), # Pastikan ukuran input sesuai
|
| 25 |
nn.ReLU(),
|
| 26 |
nn.Linear(128, 6) # Output layer untuk 6 kelas
|
| 27 |
)
|
|
|
|
| 34 |
# Inisialisasi model
|
| 35 |
model = SimpleCNN()
|
| 36 |
|
| 37 |
+
# Load model dengan error handling
|
| 38 |
+
try:
|
| 39 |
+
model.load_state_dict(torch.load("model_deri.pth", map_location=torch.device("cpu")), strict=False)
|
| 40 |
+
model.eval()
|
| 41 |
+
print("✅ Model berhasil dimuat!")
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"❌ Error loading model: {e}")
|
| 44 |
|
| 45 |
# Kelas mapping
|
| 46 |
class_mapping = {0: 'Bu dian', 1: 'Deri', 2: 'Putra', 3: 'Unknown', 4: 'Uqi', 5: 'Uwa'}
|
|
|
|
| 56 |
image = transform(image).unsqueeze(0) # Tambah batch dimension
|
| 57 |
with torch.no_grad():
|
| 58 |
output = model(image)
|
| 59 |
+
probabilities = torch.nn.functional.softmax(output, dim=1)
|
| 60 |
+
predicted_class = torch.argmax(probabilities, dim=1).item()
|
| 61 |
+
confidence = probabilities[0, predicted_class].item() * 100 # Konversi ke persen
|
| 62 |
+
return f"Predicted: {class_mapping[predicted_class]} (Confidence: {confidence:.2f}%)"
|
| 63 |
|
| 64 |
# Buat UI Gradio
|
| 65 |
iface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="text")
|
| 66 |
|
| 67 |
# Jalankan aplikasi
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
try:
|
| 70 |
+
iface.launch()
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"❌ Gradio error: {e}")
|