From c1d82d7aca880ddc70cbc17084bc1e7c3eed1b82 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:15:12 +0000 Subject: [PATCH 01/10] feat: Updated src/main.py --- src/main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main.py b/src/main.py index 243a31e..8d4fe12 100644 --- a/src/main.py +++ b/src/main.py @@ -6,11 +6,14 @@ 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,)) -]) +class MNISTTrainer: + def __init__(self): + self.transform = transforms.Compose([ + transforms.ToTensor(), + transforms.Normalize((0.5,), (0.5,)) + ]) + self.testset = datasets.MNIST('.', download=True, train=False, transform=self.transform) + self.testloader = DataLoader(self.testset, batch_size=64, shuffle=True) trainset = datasets.MNIST('.', download=True, train=True, transform=transform) trainloader = DataLoader(trainset, batch_size=64, shuffle=True) From 572182f07c3754459068a5ac0993c0bbed96b097 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:15:26 +0000 Subject: [PATCH 02/10] feat: Updated src/main.py --- src/main.py | 63 ++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/main.py b/src/main.py index 8d4fe12..114bf16 100644 --- a/src/main.py +++ b/src/main.py @@ -7,45 +7,48 @@ import numpy as np class MNISTTrainer: - def __init__(self): + def __init__(self, epochs=3): self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ]) + self.trainset = datasets.MNIST('.', download=True, train=True, transform=self.transform) + self.trainloader = DataLoader(self.trainset, batch_size=64, shuffle=True) self.testset = datasets.MNIST('.', download=True, train=False, transform=self.transform) self.testloader = DataLoader(self.testset, batch_size=64, shuffle=True) + self.model = self._define_model() + self.optimizer = optim.SGD(self.model.parameters(), lr=0.01) + self.criterion = nn.NLLLoss() + self.epochs = epochs -trainset = datasets.MNIST('.', download=True, train=True, transform=transform) -trainloader = DataLoader(trainset, batch_size=64, shuffle=True) + def _define_model(self): + 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) -# Step 2: Define the PyTorch Model -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 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) + return Net() -# Step 3: Train the Model -model = Net() -optimizer = optim.SGD(model.parameters(), lr=0.01) -criterion = nn.NLLLoss() + def train(self): + for epoch in range(self.epochs): + for images, labels in self.trainloader: + self.optimizer.zero_grad() + output = self.model(images) + loss = self.criterion(output, labels) + loss.backward() + self.optimizer.step() -# 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(self.model.state_dict(), "mnist_model.pth") + +trainer = MNISTTrainer() +trainer.train() torch.save(model.state_dict(), "mnist_model.pth") \ No newline at end of file From 97f968c32be71b925b3e9ae0696cf7b4fed291cf Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:16:37 +0000 Subject: [PATCH 03/10] feat: Updated src/api.py --- src/api.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/api.py b/src/api.py index 36c257a..b392398 100644 --- a/src/api.py +++ b/src/api.py @@ -2,18 +2,15 @@ from PIL import Image import torch from torchvision import transforms -from main import Net # Importing Net class from main.py +from main import MNISTTrainer # Importing MNISTTrainer class from main.py -# Load the model -model = Net() -model.load_state_dict(torch.load("mnist_model.pth")) -model.eval() +# Create an instance of MNISTTrainer and load the model +trainer = MNISTTrainer() +trainer.model.load_state_dict(torch.load("mnist_model.pth")) +trainer.model.eval() # Transform used for preprocessing the image -transform = transforms.Compose([ - transforms.ToTensor(), - transforms.Normalize((0.5,), (0.5,)) -]) +transform = trainer.transform app = FastAPI() From 6f693c01953344da374e08cd1ea78f12ab83c7af Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:18:15 +0000 Subject: [PATCH 04/10] feat: Updated src/api.py --- src/api.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/api.py b/src/api.py index b392398..09037b3 100644 --- a/src/api.py +++ b/src/api.py @@ -1,3 +1,12 @@ +import os +import subprocess + +# Check if poetry is installed, if not, install it +try: + subprocess.run(["poetry", "--version"], check=True) +except subprocess.CalledProcessError: + subprocess.run(["pip", "install", "poetry"], check=True) + from fastapi import FastAPI, UploadFile, File from PIL import Image import torch @@ -20,6 +29,6 @@ async def predict(file: UploadFile = File(...)): image = transform(image) image = image.unsqueeze(0) # Add batch dimension with torch.no_grad(): - output = model(image) + output = trainer.model(image) _, predicted = torch.max(output.data, 1) return {"prediction": int(predicted[0])} From 3ecbea18ce27e1c7c8a5f6748a1bdca494b67663 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:19:19 +0000 Subject: [PATCH 05/10] feat: Updated src/api.py --- src/api.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/api.py b/src/api.py index 09037b3..d4cb55f 100644 --- a/src/api.py +++ b/src/api.py @@ -1,5 +1,13 @@ import os import subprocess +import sys + +# Check if pip is installed +try: + subprocess.run(["pip", "--version"], check=True) +except subprocess.CalledProcessError: + print("Error: pip is not installed. Please install pip and try again.") + sys.exit(1) # Check if poetry is installed, if not, install it try: From b6d23fd9739af48f0c5fd149fb8a43728e68725c Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:20:23 +0000 Subject: [PATCH 06/10] feat: Updated src/api.py --- src/api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/api.py b/src/api.py index d4cb55f..f435a48 100644 --- a/src/api.py +++ b/src/api.py @@ -13,7 +13,10 @@ try: subprocess.run(["poetry", "--version"], check=True) except subprocess.CalledProcessError: - subprocess.run(["pip", "install", "poetry"], check=True) + # Download the get-poetry.py script + subprocess.run(["curl", "-sSL", "https://install.python-poetry.org", "-o", "get-poetry.py"], check=True) + # Execute the get-poetry.py script to install poetry + subprocess.run(["python", "get-poetry.py", "--yes"], check=True) from fastapi import FastAPI, UploadFile, File from PIL import Image From 0fb69abb685b830f71951233647fbe88710bccc2 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:21:19 +0000 Subject: [PATCH 07/10] feat: Updated README.md --- README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea3afcc..611321b 100644 --- a/README.md +++ b/README.md @@ -1 +1,57 @@ -# evals \ No newline at end of file +# evals + +This project provides a Python implementation for training and evaluating a simple neural network on the MNIST dataset using PyTorch. + +## MNISTTrainer class + +The `MNISTTrainer` class is used to train and evaluate the model. It is defined in `src/main.py`. + +### Usage + +First, create an instance of the `MNISTTrainer` class: + +```python +trainer = MNISTTrainer() +``` + +You can then train the model using the `train` method: + +```python +trainer.train() +``` + +The trained model's parameters are automatically saved to a file named "mnist_model.pth". + +To load the model parameters from this file, use the following code: + +```python +trainer.model.load_state_dict(torch.load("mnist_model.pth")) +``` + +To evaluate the model, you can use the `predict` method in `src/api.py`. This method takes an image file as input and returns the model's prediction. + +## Dependencies + +This project requires the following Python libraries: + +- PyTorch +- torchvision +- numpy +- PIL +- FastAPI + +You can install these dependencies using pip: + +```bash +pip install torch torchvision numpy pillow fastapi +``` + +## Running the project + +To run the project, first start the FastAPI server: + +```bash +uvicorn src.api:app --reload +``` + +You can then send a POST request to the `/predict` endpoint with an image file to get the model's prediction. \ No newline at end of file From 06517a9a64b73a3ab08457db1fbaded97ea6a960 Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:22:57 +0000 Subject: [PATCH 08/10] feat: Updated README.md --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 611321b..23336db 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,29 @@ This project requires the following Python libraries: - PIL - FastAPI -You can install these dependencies using pip: +Before installing these dependencies, ensure that `pip` is installed and working correctly. You can verify this by running the following command: + +```bash +pip --version +``` + +If `pip` is not installed or not working correctly, you may need to troubleshoot your Python installation or install `pip` separately. + +Once `pip` is working correctly, you can install the `poetry` package manager using `pip`: + +```bash +pip install poetry +``` + +If you encounter issues with the `poetry` installation, you may need to troubleshoot your `poetry` installation or seek help from the `poetry` community. + +Once `poetry` is installed, you can install the project dependencies using `poetry`: + +```bash +poetry install +``` + +If you are unable to install `poetry`, you can install the project dependencies directly using `pip`: ```bash pip install torch torchvision numpy pillow fastapi From 78b733da9dbbbc90ab6d39b8897350f84e1ca73a Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:25:09 +0000 Subject: [PATCH 09/10] feat: Updated README.md --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 23336db..8898a12 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,19 @@ Once `pip` is working correctly, you can install the `poetry` package manager us pip install poetry ``` -If you encounter issues with the `poetry` installation, you may need to troubleshoot your `poetry` installation or seek help from the `poetry` community. +If you encounter issues with the `poetry` installation, you may need to troubleshoot your `poetry` installation. Here are some steps you can take: + +1. Check if the `poetry` executable is in your system's PATH. You can do this by running the following command: + +```bash +which poetry +``` + +If the `poetry` executable is in your PATH, this command will print its location. If it's not, it won't print anything. + +2. If the `poetry` executable is not in your PATH, you need to add it. The process for this varies depending on your operating system and shell, but generally involves adding a line to a shell startup file like `~/.bashrc` or `~/.bash_profile` that exports the `poetry` executable's location to the PATH. You can find more detailed instructions in the `poetry` documentation or by searching online for "add to PATH". + +3. If you're still having trouble, you can seek help from the `poetry` community or check the `poetry` documentation for more troubleshooting tips. Here's the link to the `poetry` documentation: https://python-poetry.org/docs/ Once `poetry` is installed, you can install the project dependencies using `poetry`: From d355989ae26e82499e6d362370d6c84d52b94eef Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:27:39 +0000 Subject: [PATCH 10/10] feat: Updated README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 8898a12..4ee8847 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,14 @@ Once `pip` is working correctly, you can install the `poetry` package manager us pip install poetry ``` +After installing `poetry`, verify that it was installed correctly by checking its version: + +```bash +poetry --version +``` + +If the command does not return a version number, `poetry` was not installed correctly. + If you encounter issues with the `poetry` installation, you may need to troubleshoot your `poetry` installation. Here are some steps you can take: 1. Check if the `poetry` executable is in your system's PATH. You can do this by running the following command: