Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor training loop from script to class #80

Closed
wants to merge 10 commits into from
24 changes: 12 additions & 12 deletions src/api.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
from fastapi import FastAPI, UploadFile, File
from PIL import Image
import torch
from fastapi import FastAPI, File, UploadFile
from PIL import Image
from torchvision import transforms
from main import Net # Importing Net class from main.py

# Load the model
model = Net()
model.load_state_dict(torch.load("mnist_model.pth"))
model.eval()
from main import MNISTTrainer # Importing MNISTTrainer class from main.py

trainer = MNISTTrainer()
model = trainer.load_model("mnist_model.pth")

# Transform used for preprocessing the image
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]
)

app = FastAPI()


@app.post("/predict/")
async def predict(file: UploadFile = File(...)):
"""Predict the digit in the uploaded image using the loaded model."""
image = Image.open(file.file).convert("L")
image = transform(image)
image = image.unsqueeze(0) # Add batch dimension
with torch.no_grad():
output = model(image)
output = trainer.predict(image) # Use the predict method of the trainer
_, predicted = torch.max(output.data, 1)
return {"prediction": int(predicted[0])}
92 changes: 51 additions & 41 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
from PIL import Image
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import numpy as np

# Step 1: Load MNIST Data and Preprocess
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
from torchvision import datasets, transforms

trainset = datasets.MNIST('.', download=True, train=True, transform=transform)
trainloader = DataLoader(trainset, batch_size=64, shuffle=True)

# Step 2: Define the PyTorch Model
class Net(nn.Module):
class MNISTTrainer:
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 10)

def forward(self, x):
x = x.view(-1, 28 * 28)
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = self.fc3(x)
return nn.functional.log_softmax(x, dim=1)

# Step 3: Train the Model
model = Net()
optimizer = optim.SGD(model.parameters(), lr=0.01)
criterion = nn.NLLLoss()

# Training loop
epochs = 3
for epoch in range(epochs):
for images, labels in trainloader:
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()

torch.save(model.state_dict(), "mnist_model.pth")
self.transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]
)
self.optimizer = None
self.criterion = nn.NLLLoss()

def load_data(self):
trainset = datasets.MNIST(
".", download=True, train=True, transform=self.transform
)
return DataLoader(trainset, batch_size=64, shuffle=True)

class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 10)

def forward(self, x):
x = x.view(-1, 28 * 28)
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = self.fc3(x)
return nn.functional.log_softmax(x, dim=1)

def define_model(self):
model = self.Net()
self.optimizer = optim.SGD(model.parameters(), lr=0.01)
return model

def train_model(self, model, trainloader):
epochs = 3
for _epoch in range(epochs):
for images, labels in trainloader:
self.optimizer.zero_grad()
output = model(images)
loss = self.criterion(output, labels)
loss.backward()
self.optimizer.step()

def save_model(self, model):
torch.save(model.state_dict(), "mnist_model.pth")

def load_model(self, model_path):
model = self.define_model()
model.load_state_dict(torch.load(model_path))
model.eval()
return model