File size: 2,443 Bytes
ac456a1
e99930d
 
 
 
 
 
ac456a1
e99930d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac456a1
e99930d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d86658
 
 
e99930d
 
 
 
 
 
 
 
 
 
7d86658
e99930d
7d86658
 
 
 
 
 
 
e99930d
7d86658
e99930d
 
 
 
7d86658
84c6ebc
e99930d
7d86658
e99930d
 
 
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
import gradio as gr
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from PIL import Image
import numpy as np

# Define the same model architecture
class ConvNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 32, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 3)
        self.conv3 = nn.Conv2d(64, 64, 3)
        self.fc1 = nn.Linear(64 * 4 * 4, 64)
        self.fc2 = nn.Linear(64, 10)
    
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.pool(x)
        x = F.relu(self.conv2(x))
        x = self.pool(x)
        x = F.relu(self.conv3(x))
        x = torch.flatten(x, 1)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# Initialize model and load weights
model = ConvNet()
model.load_state_dict(torch.load('cnn.pth', map_location=torch.device('cpu')))
model.eval()

# Define classes
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

# Define preprocessing
transform = transforms.Compose([
    transforms.Resize((32, 32)),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

def predict(img):
    if img is None:
        return None
    
    # Convert to PIL Image if needed
    if not isinstance(img, Image.Image):
        img = Image.fromarray(img)
    
    # Preprocess the image
    img = transform(img).unsqueeze(0)
    
    # Get predictions
    with torch.no_grad():
        outputs = model(img)
        probabilities = F.softmax(outputs, dim=1)[0]
        
    predictions = {
        classes[i]: float(probabilities[i]) * 100  # Convert to percentage
        for i in range(len(classes))
    }
    
    # Sort predictions by probability
    sorted_predictions = dict(sorted(predictions.items(), key=lambda x: x[1], reverse=True))
    
    return sorted_predictions

iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=10),  # Show all 10 classes
    examples=[["example1.jpeg"], ["example2.jpeg"]],  # Optional: Add example images
    title="CIFAR-10 Image Classifier",
    description="Upload an image to classify it into one of these categories: plane, car, bird, cat, deer, dog, frog, horse, ship, or truck. Results show prediction confidence for all classes as percentages."
)

iface.launch()