From 19f1ad287e59efb8aa00c0e27a84683ce7368020 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:57:50 +0000 Subject: [PATCH 1/5] feat: Added CNN class for MNIST classification --- src/cnn.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/cnn.py diff --git a/src/cnn.py b/src/cnn.py new file mode 100644 index 0000000..1fa75b2 --- /dev/null +++ b/src/cnn.py @@ -0,0 +1,29 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F + + +class CNN(nn.Module): + def __init__(self): + super(CNN, self).__init__() + self.conv1 = nn.Conv2d(1, 32, 3, 1) + self.conv2 = nn.Conv2d(32, 64, 3, 1) + self.dropout1 = nn.Dropout2d(0.25) + self.dropout2 = nn.Dropout2d(0.5) + self.fc1 = nn.Linear(9216, 128) + self.fc2 = nn.Linear(128, 10) + + def forward(self, x): + x = self.conv1(x) + x = F.relu(x) + x = self.conv2(x) + x = F.relu(x) + x = F.max_pool2d(x, 2) + x = self.dropout1(x) + x = torch.flatten(x, 1) + x = self.fc1(x) + x = F.relu(x) + x = self.dropout2(x) + x = self.fc2(x) + output = F.log_softmax(x, dim=1) + return output From edf06ab71dd6c650e9b94053a698826005ebf71e Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:59:46 +0000 Subject: [PATCH 2/5] feat: Updated src/main.py --- src/main.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main.py b/src/main.py index 243a31e..2e33cf5 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,11 @@ -from PIL import Image +import numpy as np import torch import torch.nn as nn import torch.optim as optim -from torchvision import datasets, transforms +from cnn import CNN +from PIL import Image from torch.utils.data import DataLoader -import numpy as np +from torchvision import datasets, transforms # Step 1: Load MNIST Data and Preprocess transform = transforms.Compose([ @@ -31,7 +32,7 @@ def forward(self, x): return nn.functional.log_softmax(x, dim=1) # Step 3: Train the Model -model = Net() +model = CNN() optimizer = optim.SGD(model.parameters(), lr=0.01) criterion = nn.NLLLoss() From d09aad01aca21d5da2fecbdc78cff73210bcf583 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:00:34 +0000 Subject: [PATCH 3/5] Sandbox run src/main.py --- src/main.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main.py b/src/main.py index 2e33cf5..774cc5d 100644 --- a/src/main.py +++ b/src/main.py @@ -2,20 +2,21 @@ import torch import torch.nn as nn import torch.optim as optim -from cnn import CNN from PIL import Image from torch.utils.data import DataLoader from torchvision import datasets, transforms +from cnn import CNN + # Step 1: Load MNIST Data and Preprocess -transform = transforms.Compose([ - transforms.ToTensor(), - transforms.Normalize((0.5,), (0.5,)) -]) +transform = transforms.Compose( + [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))] +) -trainset = datasets.MNIST('.', download=True, train=True, transform=transform) +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): def __init__(self): @@ -23,7 +24,7 @@ def __init__(self): 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)) @@ -31,6 +32,7 @@ def forward(self, x): x = self.fc3(x) return nn.functional.log_softmax(x, dim=1) + # Step 3: Train the Model model = CNN() optimizer = optim.SGD(model.parameters(), lr=0.01) @@ -46,4 +48,4 @@ def forward(self, x): loss.backward() optimizer.step() -torch.save(model.state_dict(), "mnist_model.pth") \ No newline at end of file +torch.save(model.state_dict(), "mnist_model.pth") From 600058681b644760f3583091af8d12e19cae97cd Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:01:54 +0000 Subject: [PATCH 4/5] feat: Updated src/api.py --- src/api.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/api.py b/src/api.py index 36c257a..74e0978 100644 --- a/src/api.py +++ b/src/api.py @@ -1,11 +1,12 @@ -from fastapi import FastAPI, UploadFile, File -from PIL import Image import torch -from torchvision import transforms +from cnn import CNN +from fastapi import FastAPI, File, UploadFile from main import Net # Importing Net class from main.py +from PIL import Image +from torchvision import transforms # Load the model -model = Net() +model = CNN() model.load_state_dict(torch.load("mnist_model.pth")) model.eval() @@ -25,4 +26,4 @@ async def predict(file: UploadFile = File(...)): with torch.no_grad(): output = model(image) _, predicted = torch.max(output.data, 1) - return {"prediction": int(predicted[0])} + return {"prediction": int(predicted[0])} \ No newline at end of file From 0c26d09787bade42df5a248bf6b73dd74ea8ccf7 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:02:41 +0000 Subject: [PATCH 5/5] Sandbox run src/api.py --- src/api.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/api.py b/src/api.py index 74e0978..e60d97a 100644 --- a/src/api.py +++ b/src/api.py @@ -1,23 +1,24 @@ import torch -from cnn import CNN from fastapi import FastAPI, File, UploadFile -from main import Net # Importing Net class from main.py from PIL import Image from torchvision import transforms +from cnn import CNN +from main import Net # Importing Net class from main.py + # Load the model model = CNN() model.load_state_dict(torch.load("mnist_model.pth")) model.eval() # 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(...)): image = Image.open(file.file).convert("L") @@ -26,4 +27,4 @@ async def predict(file: UploadFile = File(...)): with torch.no_grad(): output = model(image) _, predicted = torch.max(output.data, 1) - return {"prediction": int(predicted[0])} \ No newline at end of file + return {"prediction": int(predicted[0])}