Spaces:
Runtime error
Runtime error
File size: 2,322 Bytes
be51ac0 |
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 |
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import gradio as gr
import numpy as np
from PIL import Image
# ANN model
class ANN(nn.Module):
def __init__(self):
super(ANN, self).__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, 128)
self.fc4 = nn.Linear(128, 10)
self.relu = nn.ReLU()
def forward(self, x):
x = x.view(-1, 28 * 28)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.relu(self.fc3(x))
x = self.fc4(x)
return x
# Load dataset
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True)
# Train model
model = ANN()
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
def train_model(epochs=1):
model.train()
for epoch in range(epochs):
running_loss = 0.0
for images, labels in trainloader:
optimizer.zero_grad()
outputs = model(images)
loss = loss_fn(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch {epoch+1}, Loss: {running_loss / len(trainloader):.4f}")
model.eval()
# Call it once at the start
train_model(epochs=1)
# Inference
def predict_digit(img):
img = img.convert('L').resize((28, 28)) # grayscale and resize
img = np.array(img).astype(np.float32)
img = (img - 127.5) / 127.5 # normalize to [-1, 1]
img_tensor = torch.tensor(img).unsqueeze(0).unsqueeze(0)
with torch.no_grad():
output = model(img_tensor)
_, predicted = torch.max(output, 1)
return f"Prediction: {predicted.item()}"
gr.Interface(
fn=predict_digit,
inputs=gr.Image(image_mode="L", shape=(280, 280), invert_colors=True, source="canvas"),
outputs="text",
title="MNIST Digit Recognizer (MLP)",
description="Draw a digit and the model will try to predict it after training for 1 epoch on MNIST."
).launch()
|