Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# In[ ]:
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
import torch.optim as optim
|
| 11 |
+
import torchvision
|
| 12 |
+
import torchvision.transforms as transforms
|
| 13 |
+
|
| 14 |
+
class Net(nn.Module):
|
| 15 |
+
def __init__(self):
|
| 16 |
+
super(Net, self).__init__()
|
| 17 |
+
self.conv1 = nn.Conv2d(3, 7, 10)
|
| 18 |
+
self.pool = nn.MaxPool2d(2, 2)
|
| 19 |
+
self.conv2 = nn.Conv2d(6, 16, 5)
|
| 20 |
+
# Modify fc1 to match the size in the saved checkpoint
|
| 21 |
+
self.fc1 = nn.Linear(400, 120)
|
| 22 |
+
# Modify fc2 to match the size in the saved checkpoint
|
| 23 |
+
self.fc2 = nn.Linear(120, 84)
|
| 24 |
+
# Modify fc3 to match the size in the saved checkpoint
|
| 25 |
+
self.fc3 = nn.Linear(84, 10)
|
| 26 |
+
|
| 27 |
+
def forward(self, x):
|
| 28 |
+
x = self.pool(torch.relu(self.conv1(x)))
|
| 29 |
+
x = self.pool(torch.relu(self.conv2(x)))
|
| 30 |
+
x = x.view(x.shape[0], -1)
|
| 31 |
+
print(x.shape, "x-shape")
|
| 32 |
+
x = torch.relu(self.fc1(x))
|
| 33 |
+
x = torch.relu(self.fc2(x))
|
| 34 |
+
x = self.fc3(x)
|
| 35 |
+
return x
|
| 36 |
+
|
| 37 |
+
# Load the trained model
|
| 38 |
+
model = Net()
|
| 39 |
+
model.load_state_dict(torch.load("/Users/godwinswinton/Documents/Projects/deep/cifar_net.pth"))
|
| 40 |
+
model.eval()
|
| 41 |
+
|
| 42 |
+
# Define the transformation to be applied to input images
|
| 43 |
+
preprocess = transforms.Compose([
|
| 44 |
+
transforms.ToTensor(),
|
| 45 |
+
transforms.Resize((32, 32)),
|
| 46 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
| 47 |
+
])
|
| 48 |
+
|
| 49 |
+
# Define the CIFAR-10 class names
|
| 50 |
+
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
|
| 51 |
+
|
| 52 |
+
# Define a function to make predictions on input images
|
| 53 |
+
def classify_image(image):
|
| 54 |
+
img_tensor = preprocess(image)
|
| 55 |
+
print(img_tensor.shape)
|
| 56 |
+
img_tensor = img_tensor.unsqueeze(0)
|
| 57 |
+
output = model(img_tensor)
|
| 58 |
+
print(output)
|
| 59 |
+
_, predicted = torch.max(output, dim=1)
|
| 60 |
+
print(predicted)
|
| 61 |
+
return classes[predicted[0]] # Return as a list
|
| 62 |
+
|
| 63 |
+
# Create input and output interfaces
|
| 64 |
+
image_input = gr.inputs.Image()
|
| 65 |
+
label_output = gr.outputs.Label()
|
| 66 |
+
|
| 67 |
+
# Create a Gradio interface
|
| 68 |
+
iface = gr.Interface(
|
| 69 |
+
fn=classify_image,
|
| 70 |
+
inputs=image_input,
|
| 71 |
+
outputs=label_output,
|
| 72 |
+
title="CIFAR-10 Image Classification",
|
| 73 |
+
description="Upload an image from the CIFAR-10 dataset and let the model classify it."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Launch the interface
|
| 77 |
+
iface.launch()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# In[ ]:
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|