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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 = 'C:\\Users\\kendr\\Downloads\\data' # Adjust this if necessary
|
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 = 10
|
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 |
+
# Manually creating the history dictionary based on the logs you provided
|
193 |
+
history = {
|
194 |
+
'train_loss': [1.0083, 0.7347, 0.6510, 0.5762, 0.5478, 0.5223, 0.4974, 0.3464, 0.2896, 0.2604],
|
195 |
+
'train_acc': [0.6850, 0.7687, 0.7913, 0.8126, 0.8210, 0.8272, 0.8355, 0.8870, 0.9049, 0.9136],
|
196 |
+
'val_loss': [0.6304, 0.8616, 0.5594, 0.4006, 0.3968, 0.4051, 0.3223, 0.2221, 0.2125, 0.2076],
|
197 |
+
'val_acc': [0.7985, 0.7307, 0.8260, 0.8655, 0.8793, 0.8729, 0.9094, 0.9338, 0.9338, 0.9326]
|
198 |
+
}
|
199 |
+
|
200 |
+
# Save the history as a pickle file
|
201 |
+
with open('training_history.pkl', 'wb') as f:
|
202 |
+
pickle.dump(history, f)
|
203 |
+
|
204 |
+
print('Training history saved as training_history.pkl')
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
+
# Load your model
|
209 |
+
def load_model():
|
210 |
+
model = models.resnet50(weights='DEFAULT') # Using default weights for initialization
|
211 |
+
num_ftrs = model.fc.in_features
|
212 |
+
model.fc = nn.Linear(num_ftrs, 12) # Adjust to the number of classes you have
|
213 |
+
|
214 |
+
# Load the state dict without the weights_only argument
|
215 |
+
model.load_state_dict(torch.load('resnet50_garbage_classification.pth', map_location=torch.device('cpu')))
|
216 |
+
|
217 |
+
model.eval() # Set to evaluation mode
|
218 |
+
return model
|
219 |
+
|
220 |
+
model = load_model()
|
221 |
+
|
222 |
+
# Define image transformations
|
223 |
+
transform = transforms.Compose([
|
224 |
+
transforms.Resize(256),
|
225 |
+
transforms.CenterCrop(224),
|
226 |
+
transforms.ToTensor(),
|
227 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
228 |
+
])
|
229 |
+
|
230 |
+
# Class names
|
231 |
+
class_names = ['battery', 'biological', 'brown-glass', 'cardboard',
|
232 |
+
'clothes', 'green-glass', 'metal', 'paper',
|
233 |
+
'plastic', 'shoes', 'trash', 'white-glass']
|
234 |
+
|
235 |
+
# Define bin colors for each class
|
236 |
+
bin_colors = {
|
237 |
+
'battery': 'Merah (Red)', # Limbah berbahaya
|
238 |
+
'biological': 'Cokelat (Brown)', # Limbah organik
|
239 |
+
'brown-glass': 'Hijau (Green)', # Gelas berwarna coklat
|
240 |
+
'cardboard': 'Kuning (Yellow)', # Limbah daur ulang
|
241 |
+
'clothes': 'Biru (Blue)', # Pakaian dan tekstil
|
242 |
+
'green-glass': 'Hijau (Green)', # Gelas berwarna hijau
|
243 |
+
'metal': 'Kuning (Yellow)', # Limbah daur ulang
|
244 |
+
'paper': 'Kuning (Yellow)', # Limbah daur ulang
|
245 |
+
'plastic': 'Kuning (Yellow)', # Limbah daur ulang
|
246 |
+
'shoes': 'Biru (Blue)', # Pakaian dan tekstil
|
247 |
+
'trash': 'Hitam (Black)', # Limbah umum
|
248 |
+
'white-glass': 'Putih (White)' # Gelas berwarna putih
|
249 |
+
}
|
250 |
+
|
251 |
+
# Define the prediction function
|
252 |
+
def predict(image):
|
253 |
+
image = Image.fromarray(image) # Convert numpy array to PIL Image
|
254 |
+
image = transform(image) # Apply transformations
|
255 |
+
image = image.unsqueeze(0) # Add batch dimension
|
256 |
+
|
257 |
+
with torch.no_grad():
|
258 |
+
outputs = model(image)
|
259 |
+
_, predicted = torch.max(outputs, 1)
|
260 |
+
|
261 |
+
class_name = class_names[predicted.item()] # Return predicted class name
|
262 |
+
bin_color = bin_colors[class_name] # Get the corresponding bin color
|
263 |
+
return class_name, bin_color # Return both class name and bin color
|
264 |
+
|
265 |
+
# Make Gradio Interface
|
266 |
+
iface = gr.Interface(
|
267 |
+
fn=predict,
|
268 |
+
inputs=gr.Image(type="numpy", label="Unggah Gambar"),
|
269 |
+
outputs=[
|
270 |
+
gr.Textbox(label="Jenis Sampah"),
|
271 |
+
gr.Textbox(label="Tong Sampah yang Sesuai") # 2 output with label
|
272 |
+
],
|
273 |
+
title="Klasifikasi Sampah dengan ResNet50",
|
274 |
+
description="Unggah gambar sampah, dan model akan mengklasifikasikannya ke dalam salah satu dari 12 kategori bersama dengan warna tempat sampah yang sesuai."
|
275 |
+
)
|
276 |
+
|
277 |
+
|
278 |
+
iface.launch(share=True)
|