File size: 3,139 Bytes
90c960d
421b6e5
 
 
 
 
965e1b1
90c960d
965e1b1
 
 
 
 
 
 
67d0f13
421b6e5
965e1b1
67d0f13
421b6e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32e35ec
 
421b6e5
 
67d0f13
90c960d
421b6e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")