kendrickfff
commited on
Commit
•
413219b
1
Parent(s):
e4f3534
Update app.py
Browse files
app.py
CHANGED
@@ -56,16 +56,59 @@ else:
|
|
56 |
|
57 |
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Load your model
|
62 |
def load_model():
|
63 |
model = models.resnet50(weights='DEFAULT') # Using default weights for initialization
|
64 |
num_ftrs = model.fc.in_features
|
65 |
model.fc = nn.Linear(num_ftrs, 12) # Adjust to the number of classes you have
|
66 |
|
67 |
-
# Load the state dict
|
68 |
-
model.load_state_dict(torch.load('
|
69 |
|
70 |
model.eval() # Set to evaluation mode
|
71 |
return model
|
@@ -87,18 +130,18 @@ class_names = ['battery', 'biological', 'brown-glass', 'cardboard',
|
|
87 |
|
88 |
# Define bin colors for each class
|
89 |
bin_colors = {
|
90 |
-
'battery': 'Merah (Red)',
|
91 |
-
'biological': 'Hijau (Green)',
|
92 |
-
'brown-glass': 'Kuning (Yellow)'
|
93 |
-
'cardboard': 'Biru (Blue)',
|
94 |
-
'clothes': 'Kuning (Yellow)',
|
95 |
-
'green-glass': 'Kuning (Yellow)'
|
96 |
-
'metal': 'Kuning (Yellow)',
|
97 |
-
'paper': 'Biru (Blue)',
|
98 |
-
'plastic': 'Kuning (Yellow)',
|
99 |
-
'shoes': 'Kuning (Yellow)',
|
100 |
-
'trash': '
|
101 |
-
'white-glass': 'Kuning (Yellow)'
|
102 |
}
|
103 |
|
104 |
# Define the prediction function
|
@@ -115,7 +158,7 @@ def predict(image):
|
|
115 |
bin_color = bin_colors[class_name] # Get the corresponding bin color
|
116 |
return class_name, bin_color # Return both class name and bin color
|
117 |
|
118 |
-
#
|
119 |
iface = gr.Interface(
|
120 |
fn=predict,
|
121 |
inputs=gr.Image(type="numpy", label="Unggah Gambar"),
|
@@ -124,8 +167,9 @@ iface = gr.Interface(
|
|
124 |
gr.Textbox(label="Tong Sampah yang Sesuai") # 2 output with label
|
125 |
],
|
126 |
title="Klasifikasi Sampah dengan ResNet50 v1",
|
127 |
-
description="Unggah gambar sampah, dan model akan mengklasifikasikannya ke dalam salah satu dari 12 kategori bersama dengan warna tempat sampah yang sesuai."
|
|
|
|
|
128 |
)
|
129 |
|
130 |
-
|
131 |
iface.launch(share=True)
|
|
|
56 |
|
57 |
|
58 |
|
59 |
+
import pickle
|
60 |
+
|
61 |
+
# Mengupdate hasil train dan validate terbaru
|
62 |
+
history = {
|
63 |
+
'train_loss': [
|
64 |
+
0.9568, 0.6937, 0.5917, 0.5718, 0.5109,
|
65 |
+
0.4824, 0.4697, 0.3318, 0.2785, 0.2680,
|
66 |
+
0.2371, 0.2333, 0.2198, 0.2060, 0.1962,
|
67 |
+
0.1951, 0.1880, 0.1912, 0.1811, 0.1810
|
68 |
+
],
|
69 |
+
'train_acc': [
|
70 |
+
0.7011, 0.7774, 0.8094, 0.8146, 0.8331,
|
71 |
+
0.8452, 0.8447, 0.8899, 0.9068, 0.9114,
|
72 |
+
0.9216, 0.9203, 0.9254, 0.9306, 0.9352,
|
73 |
+
0.9346, 0.9368, 0.9353, 0.9396, 0.9409
|
74 |
+
],
|
75 |
+
'val_loss': [
|
76 |
+
0.4934, 0.3939, 0.4377, 0.3412, 0.2614,
|
77 |
+
0.2966, 0.2439, 0.1065, 0.0926, 0.0797,
|
78 |
+
0.0738, 0.0639, 0.0555, 0.0560, 0.0490,
|
79 |
+
0.0479, 0.0455, 0.0454, 0.0438, 0.0427
|
80 |
+
],
|
81 |
+
'val_acc': [
|
82 |
+
0.8481, 0.8734, 0.8663, 0.8915, 0.9172,
|
83 |
+
0.9011, 0.9221, 0.9649, 0.9714, 0.9759,
|
84 |
+
0.9762, 0.9791, 0.9827, 0.9812, 0.9843,
|
85 |
+
0.9850, 0.9852, 0.9854, 0.9854, 0.9866
|
86 |
+
]
|
87 |
+
}
|
88 |
+
|
89 |
+
# Simpan history sebagai file pickle
|
90 |
+
with open('training_history.pkl', 'wb') as f:
|
91 |
+
pickle.dump(history, f)
|
92 |
+
|
93 |
+
print('Training history saved as training_history.pkl')
|
94 |
+
|
95 |
|
96 |
|
97 |
+
|
98 |
+
import torch
|
99 |
+
import torch.nn as nn
|
100 |
+
from torchvision import models, transforms
|
101 |
+
from PIL import Image
|
102 |
+
import gradio as gr
|
103 |
+
|
104 |
# Load your model
|
105 |
def load_model():
|
106 |
model = models.resnet50(weights='DEFAULT') # Using default weights for initialization
|
107 |
num_ftrs = model.fc.in_features
|
108 |
model.fc = nn.Linear(num_ftrs, 12) # Adjust to the number of classes you have
|
109 |
|
110 |
+
# Load the state dict
|
111 |
+
model.load_state_dict(torch.load('resnet50_garbage_classificationv1.2.pth', map_location=torch.device('cpu')))
|
112 |
|
113 |
model.eval() # Set to evaluation mode
|
114 |
return model
|
|
|
130 |
|
131 |
# Define bin colors for each class
|
132 |
bin_colors = {
|
133 |
+
'battery': 'Merah (Red)', # Limbah berbahaya (B3)
|
134 |
+
'biological': 'Hijau (Green)', # Limbah organik
|
135 |
+
'brown-glass': 'Kuning (Yellow or trash banks / recycling centers)', # Gelas berwarna coklat (anorganik/daur ulang)
|
136 |
+
'cardboard': 'Biru (Blue)', # Kertas (daur ulang)
|
137 |
+
'clothes': 'Kuning atau Bank Sampah (Yellow or trash banks / recycling centers)', # Pakaian (dimasukkan sebagai daur ulang)
|
138 |
+
'green-glass': 'Kuning (Yellow)', # Gelas berwarna hijau (anorganik/daur ulang)
|
139 |
+
'metal': 'Kuning (Yellow)', # Logam (anorganik/daur ulang)
|
140 |
+
'paper': 'Biru (Blue)', # Kertas (daur ulang)
|
141 |
+
'plastic': 'Kuning (Yellow)', # Plastik (anorganik/daur ulang)
|
142 |
+
'shoes': 'Kuning atau Bank Sampah (Yellow or trash banks / recycling centers)', # Sepatu (dimasukkan sebagai daur ulang)
|
143 |
+
'trash': 'Abu-abu (Gray)', # Limbah umum
|
144 |
+
'white-glass': 'Kuning (Yellow or trash banks / recycling centers)' # Gelas berwarna putih (anorganik/daur ulang)
|
145 |
}
|
146 |
|
147 |
# Define the prediction function
|
|
|
158 |
bin_color = bin_colors[class_name] # Get the corresponding bin color
|
159 |
return class_name, bin_color # Return both class name and bin color
|
160 |
|
161 |
+
# Buat antarmuka Gradio dengan deskripsi
|
162 |
iface = gr.Interface(
|
163 |
fn=predict,
|
164 |
inputs=gr.Image(type="numpy", label="Unggah Gambar"),
|
|
|
167 |
gr.Textbox(label="Tong Sampah yang Sesuai") # 2 output with label
|
168 |
],
|
169 |
title="Klasifikasi Sampah dengan ResNet50 v1",
|
170 |
+
description="Unggah gambar sampah, dan model kami akan mengklasifikasikannya ke dalam salah satu dari 12 kategori bersama dengan warna tempat sampah yang sesuai. "
|
171 |
+
"<strong>Model ini bisa memprediksi jenis sampah dari ke-12 jenis berikut:</strong> Baterai, Sampah organik, Gelas Kaca Coklat, "
|
172 |
+
"Kardus, Pakaian, Gelas Kaca Hijau, Metal, Kertas, Plastik, Sepatu/sandal, Popok/pampers, Gelas Kaca bening."
|
173 |
)
|
174 |
|
|
|
175 |
iface.launch(share=True)
|