File size: 1,880 Bytes
4ee77f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# coding: utf-8

# In[2]:


# In[3]:


import gradio as gr
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        # Modify fc1 to match the size in the saved checkpoint
        self.fc1 = nn.Linear(400, 120)
        # Modify fc2 to match the size in the saved checkpoint
        self.fc2 = nn.Linear(120, 84)
        # Modify fc3 to match the size in the saved checkpoint
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(torch.relu(self.conv1(x)))
        x = self.pool(torch.relu(self.conv2(x)))
        x = x.view(x.shape[0], -1)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x
        
# Load the trained model
model = Net()
model.load_state_dict(torch.load("cifar_net.pth"))
model.eval()

# Define the transformation to be applied to input images
preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Resize((32, 32)),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# Define the CIFAR-10 class names
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

# Define a function to make predictions on input images
def classify_image(image):
    img_tensor = preprocess(image)
    img_tensor = img_tensor.unsqueeze(0)
    output = model(img_tensor)
    _, predicted = torch.max(output, dim=1)
    return classes[predicted[0]]  # Return as a list

# Create Gradio interface
iface = gr.Interface(fn=classify_image, inputs="image", outputs="text")

# Launch the interface
iface.launch()


# In[ ]: