image_classifier / src /image_classifier_app.py
Sher1988's picture
Update src/image_classifier_app.py
32e35ec verified
Raw
History Blame Contribute Delete
3.14 kB
import streamlit as st
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from PIL import Image
from torchvision.models import resnet18
import os
# Get the directory where the current script (app.py) is located
# Since app.py is in /app/src/ and the model is in /app/
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MODEL_PATH = os.path.join(BASE_DIR, "resnet18_cifar10_finetuned.pth")
# Use MODEL_PATH in your load_model function
# Example: model.load_state_dict(torch.load(MODEL_PATH, map_location=device))
# ---------------- Constants ----------------
CIFAR10_CLASSES = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
# ---------------- Model Loader ----------------
@st.cache_resource
def load_model():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = resnet18(pretrained=False)
model.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
model.maxpool = nn.Identity()
in_ftrs = model.fc.in_features
model.fc = nn.Sequential(
nn.Linear(in_ftrs, in_ftrs),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(in_ftrs, 10)
)
model.load_state_dict(torch.load(MODEL_PATH, map_location=device))
model.to(device)
model.eval()
return model, device
# ---------------- Preprocessing ----------------
def preprocess_image(image):
transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465],
std=[0.2023, 0.1994, 0.2010])
])
return transform(image).unsqueeze(0)
# ---------------- UI ----------------
st.title("🎯 CIFAR-10 Image Classifier")
st.write("Upload an image to classify it.")
st.write("ResNet18 model finetuned for 3 epochs with 95.6% accuracy on CIFAR10 images.")
st.write("The model performs well on images from CIFAR10 dataset.")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file:
try:
image = Image.open(uploaded_file).convert('RGB')
st.image(image, caption="Uploaded Image", width=200)
model, device = load_model()
with st.spinner("Classifying..."):
tensor = preprocess_image(image).to(device)
with torch.no_grad():
outputs = model(tensor)
probabilities = torch.softmax(outputs, dim=1)
confidence, predicted = torch.max(probabilities, 1)
st.success(f"Predicted: {CIFAR10_CLASSES[predicted.item()]}")
st.info(f"Confidence: {confidence.item()*100:.2f}%")
except Exception as e:
import traceback
st.error("An error occurred:")
st.text(traceback.format_exc())
top5_probs, top5_indices = torch.topk(probabilities, 5)
st.subheader("Top 5 Predictions")
for i in range(5):
label = CIFAR10_CLASSES[top5_indices[0][i].item()]
prob = top5_probs[0][i].item() * 100
st.write(f"{i+1}. {label}{prob:.2f}%")
st.write("Done.")