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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import subprocess
|
4 |
+
import zipfile
|
5 |
+
import time
|
6 |
+
import torch
|
7 |
+
import torch.nn as nn
|
8 |
+
import torch.optim as optim
|
9 |
+
from torchvision import datasets, transforms, models
|
10 |
+
from torch.optim import lr_scheduler
|
11 |
+
import subprocess
|
12 |
+
import zipfile
|
13 |
+
from PIL import Image
|
14 |
+
import gradio as gr
|
15 |
+
|
16 |
+
# Step 1: Setup Kaggle API
|
17 |
+
# Ensure the .kaggle directory exists
|
18 |
+
kaggle_dir = os.path.expanduser("~/.kaggle")
|
19 |
+
if not os.path.exists(kaggle_dir):
|
20 |
+
os.makedirs(kaggle_dir)
|
21 |
+
|
22 |
+
# Step 2: Copy the kaggle.json file to the ~/.kaggle directory
|
23 |
+
kaggle_json_path = "kaggle.json"
|
24 |
+
kaggle_dest_path = os.path.join(kaggle_dir, "kaggle.json")
|
25 |
+
|
26 |
+
if not os.path.exists(kaggle_dest_path):
|
27 |
+
shutil.copy(kaggle_json_path, kaggle_dest_path)
|
28 |
+
os.chmod(kaggle_dest_path, 0o600)
|
29 |
+
print("Kaggle API key copied and permissions set.")
|
30 |
+
else:
|
31 |
+
print("Kaggle API key already exists.")
|
32 |
+
|
33 |
+
# Step 3: Download the dataset from Kaggle using Kaggle CLI
|
34 |
+
dataset_name = "mostafaabla/garbage-classification"
|
35 |
+
print(f"Downloading the dataset: {dataset_name}")
|
36 |
+
download_command = f"kaggle datasets download -d {dataset_name}"
|
37 |
+
|
38 |
+
# Run the download command
|
39 |
+
subprocess.run(download_command, shell=True)
|
40 |
+
|
41 |
+
# Step 4: Unzip the downloaded dataset
|
42 |
+
dataset_zip = "garbage-classification.zip"
|
43 |
+
extracted_folder = "./garbage-classification"
|
44 |
+
|
45 |
+
# Check if the zip file exists
|
46 |
+
if os.path.exists(dataset_zip):
|
47 |
+
if not os.path.exists(extracted_folder):
|
48 |
+
with zipfile.ZipFile(dataset_zip, 'r') as zip_ref:
|
49 |
+
zip_ref.extractall(extracted_folder)
|
50 |
+
print("Dataset unzipped successfully!")
|
51 |
+
else:
|
52 |
+
print("Dataset already unzipped.")
|
53 |
+
else:
|
54 |
+
print(f"Dataset zip file '{dataset_zip}' not found.")
|
55 |
+
|
56 |
+
|
57 |
+
# Path to the data directory
|
58 |
+
data_dir = '/home/user/app/data'
|
59 |
+
|
60 |
+
# Define data transformations
|
61 |
+
data_transforms = {
|
62 |
+
'train': transforms.Compose([
|
63 |
+
transforms.RandomResizedCrop(224),
|
64 |
+
transforms.RandomRotation(15),
|
65 |
+
transforms.RandomHorizontalFlip(),
|
66 |
+
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
|
67 |
+
transforms.ToTensor(),
|
68 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
69 |
+
]),
|
70 |
+
'valid': transforms.Compose([
|
71 |
+
transforms.Resize(256),
|
72 |
+
transforms.CenterCrop(224),
|
73 |
+
transforms.ToTensor(),
|
74 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
75 |
+
]),
|
76 |
+
}
|
77 |
+
|
78 |
+
# Create the datasets from the image folder
|
79 |
+
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
|
80 |
+
for x in ['train', 'valid']}
|
81 |
+
|
82 |
+
# Create the dataloaders
|
83 |
+
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=32, shuffle=True, num_workers=4)
|
84 |
+
for x in ['train', 'valid']}
|
85 |
+
|
86 |
+
# Class names
|
87 |
+
class_names = image_datasets['train'].classes
|
88 |
+
print(f"Classes: {class_names}")
|
89 |
+
|
90 |
+
# Check if a GPU is available
|
91 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
92 |
+
|
93 |
+
# Load pre-trained ResNet50 model
|
94 |
+
model = models.resnet50(weights='ResNet50_Weights.DEFAULT') # Use weights instead of pretrained
|
95 |
+
|
96 |
+
# Modify the final layer to match the number of classes
|
97 |
+
num_ftrs = model.fc.in_features
|
98 |
+
model.fc = nn.Linear(num_ftrs, len(class_names)) # Output classes match
|
99 |
+
|
100 |
+
# Move the model to the GPU if available
|
101 |
+
model = model.to(device)
|
102 |
+
|
103 |
+
# Loss function and optimizer
|
104 |
+
criterion = nn.CrossEntropyLoss()
|
105 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
106 |
+
|
107 |
+
# Learning rate scheduler
|
108 |
+
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
|
109 |
+
|
110 |
+
# Number of epochs
|
111 |
+
num_epochs = 20
|
112 |
+
|
113 |
+
# Training function with detailed output for each epoch
|
114 |
+
def train_model(model, criterion, optimizer, scheduler, num_epochs=10):
|
115 |
+
since = time.time()
|
116 |
+
|
117 |
+
best_model_wts = model.state_dict()
|
118 |
+
best_acc = 0.0
|
119 |
+
|
120 |
+
for epoch in range(num_epochs):
|
121 |
+
epoch_start = time.time() # Start time for this epoch
|
122 |
+
print(f'Epoch {epoch + 1}/{num_epochs}')
|
123 |
+
print('-' * 10)
|
124 |
+
|
125 |
+
# Each epoch has a training and validation phase
|
126 |
+
for phase in ['train', 'valid']:
|
127 |
+
if phase == 'train':
|
128 |
+
model.train() # Set model to training mode
|
129 |
+
else:
|
130 |
+
model.eval() # Set model to evaluate mode
|
131 |
+
|
132 |
+
running_loss = 0.0
|
133 |
+
running_corrects = 0
|
134 |
+
|
135 |
+
# Iterate over data
|
136 |
+
for inputs, labels in dataloaders[phase]:
|
137 |
+
inputs = inputs.to(device)
|
138 |
+
labels = labels.to(device)
|
139 |
+
|
140 |
+
# Zero the parameter gradients
|
141 |
+
optimizer.zero_grad()
|
142 |
+
|
143 |
+
# Forward
|
144 |
+
with torch.set_grad_enabled(phase == 'train'):
|
145 |
+
outputs = model(inputs)
|
146 |
+
_, preds = torch.max(outputs, 1)
|
147 |
+
loss = criterion(outputs, labels)
|
148 |
+
|
149 |
+
# Backward + optimize only if in training phase
|
150 |
+
if phase == 'train':
|
151 |
+
loss.backward()
|
152 |
+
optimizer.step()
|
153 |
+
|
154 |
+
# Statistics
|
155 |
+
running_loss += loss.item() * inputs.size(0)
|
156 |
+
running_corrects += torch.sum(preds == labels.data)
|
157 |
+
|
158 |
+
if phase == 'train':
|
159 |
+
scheduler.step()
|
160 |
+
|
161 |
+
# Calculate epoch loss and accuracy
|
162 |
+
epoch_loss = running_loss / len(image_datasets[phase])
|
163 |
+
epoch_acc = running_corrects.double() / len(image_datasets[phase])
|
164 |
+
|
165 |
+
# Print loss and accuracy for each phase
|
166 |
+
print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
|
167 |
+
|
168 |
+
# Deep copy the model if it's the best accuracy
|
169 |
+
if phase == 'valid' and epoch_acc > best_acc:
|
170 |
+
best_acc = epoch_acc
|
171 |
+
best_model_wts = model.state_dict()
|
172 |
+
|
173 |
+
epoch_end = time.time() # End time for this epoch
|
174 |
+
print(f'Epoch {epoch + 1} completed in {epoch_end - epoch_start:.2f} seconds.')
|
175 |
+
|
176 |
+
time_elapsed = time.time() - since
|
177 |
+
print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')
|
178 |
+
print(f'Best val Acc: {best_acc:.4f}')
|
179 |
+
|
180 |
+
# Load best model weights
|
181 |
+
model.load_state_dict(best_model_wts)
|
182 |
+
return model
|
183 |
+
|
184 |
+
# Train the model
|
185 |
+
best_model = train_model(model, criterion, optimizer, scheduler, num_epochs=num_epochs)
|
186 |
+
|
187 |
+
# Save the model
|
188 |
+
torch.save(model.state_dict(), 'resnet50_garbage_classification.pth')
|
189 |
+
|
190 |
+
import pickle
|
191 |
+
|
192 |
+
# Mengupdate hasil train dan validate terbaru
|
193 |
+
history = {
|
194 |
+
'train_loss': [
|
195 |
+
0.9568, 0.6937, 0.5917, 0.5718, 0.5109,
|
196 |
+
0.4824, 0.4697, 0.3318, 0.2785, 0.2680,
|
197 |
+
0.2371, 0.2333, 0.2198, 0.2060, 0.1962,
|
198 |
+
0.1951, 0.1880, 0.1912, 0.1811, 0.1810
|
199 |
+
],
|
200 |
+
'train_acc': [
|
201 |
+
0.7011, 0.7774, 0.8094, 0.8146, 0.8331,
|
202 |
+
0.8452, 0.8447, 0.8899, 0.9068, 0.9114,
|
203 |
+
0.9216, 0.9203, 0.9254, 0.9306, 0.9352,
|
204 |
+
0.9346, 0.9368, 0.9353, 0.9396, 0.9409
|
205 |
+
],
|
206 |
+
'val_loss': [
|
207 |
+
0.4934, 0.3939, 0.4377, 0.3412, 0.2614,
|
208 |
+
0.2966, 0.2439, 0.1065, 0.0926, 0.0797,
|
209 |
+
0.0738, 0.0639, 0.0555, 0.0560, 0.0490,
|
210 |
+
0.0479, 0.0455, 0.0454, 0.0438, 0.0427
|
211 |
+
],
|
212 |
+
'val_acc': [
|
213 |
+
0.8481, 0.8734, 0.8663, 0.8915, 0.9172,
|
214 |
+
0.9011, 0.9221, 0.9649, 0.9714, 0.9759,
|
215 |
+
0.9762, 0.9791, 0.9827, 0.9812, 0.9843,
|
216 |
+
0.9850, 0.9852, 0.9854, 0.9854, 0.9866
|
217 |
+
]
|
218 |
+
}
|
219 |
+
|
220 |
+
# Simpan history sebagai file pickle
|
221 |
+
with open('training_history.pkl', 'wb') as f:
|
222 |
+
pickle.dump(history, f)
|
223 |
+
|
224 |
+
print('Training history saved as training_history.pkl')
|
225 |
+
|
226 |
+
|
227 |
+
|
228 |
+
|
229 |
+
import torch
|
230 |
+
import torch.nn as nn
|
231 |
+
from torchvision import models, transforms
|
232 |
+
from PIL import Image
|
233 |
+
import gradio as gr
|
234 |
+
|
235 |
+
# Load your model
|
236 |
+
def load_model():
|
237 |
+
model = models.resnet50(weights='DEFAULT') # Using default weights for initialization
|
238 |
+
num_ftrs = model.fc.in_features
|
239 |
+
model.fc = nn.Linear(num_ftrs, 12) # Adjust to the number of classes you have
|
240 |
+
|
241 |
+
# Load the state dict
|
242 |
+
model.load_state_dict(torch.load('resnet50_garbage_classificationv1.2.pth', map_location=torch.device('cpu')))
|
243 |
+
|
244 |
+
model.eval() # Set to evaluation mode
|
245 |
+
return model
|
246 |
+
|
247 |
+
model = load_model()
|
248 |
+
|
249 |
+
# Define image transformations
|
250 |
+
transform = transforms.Compose([
|
251 |
+
transforms.Resize(256),
|
252 |
+
transforms.CenterCrop(224),
|
253 |
+
transforms.ToTensor(),
|
254 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
255 |
+
])
|
256 |
+
|
257 |
+
# Class names
|
258 |
+
class_names = ['battery', 'biological', 'brown-glass', 'cardboard',
|
259 |
+
'clothes', 'green-glass', 'metal', 'paper',
|
260 |
+
'plastic', 'shoes', 'trash', 'white-glass']
|
261 |
+
|
262 |
+
# Define bin colors for each class
|
263 |
+
bin_colors = {
|
264 |
+
'battery': 'Merah (Red)', # Limbah berbahaya (B3)
|
265 |
+
'biological': 'Hijau (Green)', # Limbah organik
|
266 |
+
'brown-glass': 'Kuning (Yellow or trash banks / recycling centers)', # Gelas berwarna coklat (anorganik/daur ulang)
|
267 |
+
'cardboard': 'Biru (Blue)', # Kertas (daur ulang)
|
268 |
+
'clothes': 'Kuning atau Bank Sampah (Yellow or trash banks / recycling centers)', # Pakaian (dimasukkan sebagai daur ulang)
|
269 |
+
'green-glass': 'Kuning (Yellow)', # Gelas berwarna hijau (anorganik/daur ulang)
|
270 |
+
'metal': 'Kuning (Yellow)', # Logam (anorganik/daur ulang)
|
271 |
+
'paper': 'Biru (Blue)', # Kertas (daur ulang)
|
272 |
+
'plastic': 'Kuning (Yellow)', # Plastik (anorganik/daur ulang)
|
273 |
+
'shoes': 'Kuning atau Bank Sampah (Yellow or trash banks / recycling centers)', # Sepatu (dimasukkan sebagai daur ulang)
|
274 |
+
'trash': 'Abu-abu (Gray)', # Limbah umum
|
275 |
+
'white-glass': 'Kuning (Yellow or trash banks / recycling centers)' # Gelas berwarna putih (anorganik/daur ulang)
|
276 |
+
}
|
277 |
+
|
278 |
+
# Define the prediction function
|
279 |
+
def predict(image):
|
280 |
+
image = Image.fromarray(image) # Convert numpy array to PIL Image
|
281 |
+
image = transform(image) # Apply transformations
|
282 |
+
image = image.unsqueeze(0) # Add batch dimension
|
283 |
+
|
284 |
+
with torch.no_grad():
|
285 |
+
outputs = model(image)
|
286 |
+
_, predicted = torch.max(outputs, 1)
|
287 |
+
|
288 |
+
class_name = class_names[predicted.item()] # Return predicted class name
|
289 |
+
bin_color = bin_colors[class_name] # Get the corresponding bin color
|
290 |
+
return class_name, bin_color # Return both class name and bin color
|
291 |
+
|
292 |
+
# Buat antarmuka Gradio dengan deskripsi
|
293 |
+
iface = gr.Interface(
|
294 |
+
fn=predict,
|
295 |
+
inputs=gr.Image(type="numpy", label="Unggah Gambar"),
|
296 |
+
outputs=[
|
297 |
+
gr.Textbox(label="Jenis Sampah"),
|
298 |
+
gr.Textbox(label="Tong Sampah yang Sesuai") # 2 output with label
|
299 |
+
],
|
300 |
+
title="Klasifikasi Sampah dengan ResNet50 v1.2",
|
301 |
+
description="Unggah gambar sampah, dan model kami akan mengklasifikasikannya ke dalam salah satu dari 12 kategori bersama dengan warna tempat sampah yang sesuai. "
|
302 |
+
"<strong>Model ini bisa memprediksi jenis sampah dari ke-12 jenis berikut:</strong> Baterai, Sampah organik, Gelas Kaca Coklat, "
|
303 |
+
"Kardus, Pakaian, Gelas Kaca Hijau, Metal, Kertas, Plastik, Sepatu/sandal, Popok/pampers, Gelas Kaca bening."
|
304 |
+
)
|
305 |
+
|
306 |
+
iface.launch(share=True)
|
307 |
+
|
308 |
+
token = os.getenv("HUGGINGFACE_TOKEN")
|