File size: 4,557 Bytes
2504e46 392c46c 2504e46 392c46c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import numpy as np
import streamlit as st
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as T
from PIL import Image
st.set_page_config(page_title="Garbage Classification")
# CNN Model Definition
class SimpleCNN(nn.Module):
def __init__(self, num_classes, input_channels=3):
super().__init__()
# Convolutional layers
self.conv1 = nn.Conv2d(input_channels, 32, kernel_size=3, padding=0)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=0)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=0)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv4 = nn.Conv2d(128, 256, kernel_size=3, padding=0)
self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2)
self.flatten = nn.Flatten()
# Dense layers
self.fc1 = nn.Linear(256 * 12 * 12, 512)
self.dropout1 = nn.Dropout(0.5)
self.fc2 = nn.Linear(512, 512)
self.dropout2 = nn.Dropout(0.5)
self.fc3 = nn.Linear(512, num_classes)
def forward(self, x):
# Conv blocks
x = F.relu(self.conv1(x))
x = self.pool1(x)
x = F.relu(self.conv2(x))
x = self.pool2(x)
x = F.relu(self.conv3(x))
x = self.pool3(x)
x = F.relu(self.conv4(x))
x = self.pool4(x)
# Dense layers
x = self.flatten(x)
x = F.relu(self.fc1(x))
x = self.dropout1(x)
x = F.relu(self.fc2(x))
x = self.dropout2(x)
x = self.fc3(x)
return x
# Class names
CLASS_NAMES = [
"battery",
"biological",
"cardboard",
"clothes",
"glass",
"metal",
"paper",
"plastic",
"shoes",
"trash",
]
# Cache the model loading
@st.cache_resource
def load_model():
"""Load the trained model"""
device = torch.device("cpu")
model = SimpleCNN(num_classes=10)
model = nn.DataParallel(model)
try:
model.load_state_dict(torch.load("best_model.pth", map_location=device))
model.eval()
return model, device
except Exception as e:
st.error(f"Error loading model: {e}")
return None, device
def preprocess_image(image):
"""Preprocess uploaded image"""
transform = T.Compose(
[
T.Resize(224),
T.CenterCrop(224),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
image_tensor = transform(image).unsqueeze(0)
return image_tensor
def predict_image(image, model, device):
"""Make prediction on image"""
# Preprocess image
input_tensor = preprocess_image(image).to(device)
# Make prediction
with torch.no_grad():
outputs = model(input_tensor)
probabilities = F.softmax(outputs, dim=1)
confidence, predicted_idx = torch.max(probabilities, 1)
predicted_class = CLASS_NAMES[predicted_idx.item()]
confidence_score = confidence.item()
all_probabilities = probabilities.cpu().numpy().flatten()
return predicted_class, confidence_score, all_probabilities
def get_confidence_color(confidence):
"""Get color class based on confidence score"""
if confidence >= 0.7:
return "confidence-high"
elif confidence >= 0.4:
return "confidence-medium"
else:
return "confidence-low"
def main():
# Load model
model, device = load_model()
# File uploader
st.header("Garbage Classification")
uploaded_file = st.file_uploader(
"Choose an image file",
type=["jpg", "jpeg", "png"],
)
if uploaded_file is not None:
# Display uploaded image
image = Image.open(uploaded_file).convert("RGB")
col1, col2 = st.columns([1, 1])
with col1:
st.image(image, caption="Uploaded Image", use_container_width=True)
# Make prediction
with st.spinner("🔍 Analyzing image..."):
predicted_class, confidence, probabilities = predict_image(
image, model, device
)
sorted_indices = np.argsort(probabilities)[::-1]
container = col2.container(border=True)
for i, idx in enumerate(sorted_indices):
class_name = CLASS_NAMES[idx]
prob = probabilities[idx]
container.write(f"{class_name.title()}: {prob:.1%}")
if __name__ == "__main__":
main()
|