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

pull #1

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Binary file added WEEK 4/2024-07-31 21-20-04.mkv
Binary file not shown.
666 changes: 666 additions & 0 deletions WEEK 4/Facial_Recognition.ipynb

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

357 changes: 357 additions & 0 deletions Week 1/Assignment/Python Modules Assigment/Python_Modules.ipynb

Large diffs are not rendered by default.

230 changes: 230 additions & 0 deletions Week 2/Assignment/Neural Network Assignment/submission.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"#imports\n",
"\n",
"import torch\n",

"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"import torch.optim as optim\n",
"import torchvision.transforms as transforms\n",
"import torchvision\n",
"from torch.utils.data import DataLoader\n",
"import os\n",
"import pandas as pd\n",
"import torch\n",
"from torch.utils.data import Dataset\n",
"from skimage import io\n"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"class SmpDataset(Dataset):\n",
" def __init__(self, csv_file, root_dir, transform=None):\n",
" self.annotations = pd.read_csv(csv_file)\n",
" self.root_dir = root_dir\n",
" self.transform = transform\n",
"\n",
" def __len__(self):\n",
" return len(self.annotations)\n",
"\n",
" def __getitem__(self, index):\n",
" img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])\n",
" image = io.imread(img_path)\n",
" y_label = torch.tensor(int(self.annotations.iloc[index, 1]))\n",
"\n",
" if self.transform:\n",
" image = self.transform(image)\n",
" return (image, y_label)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"# Transform to resize images to 64x64 and convert to tensor\n",
"transform = transforms.Compose([\n",
" transforms.ToPILImage(),\n",
" transforms.Resize((64, 64)),\n",
" transforms.ToTensor()\n",
"])\n",
"\n",
"\n",
"class NN(nn.Module):\n",
" def __init__(self, input_size, num_classes):\n",
" super(NN, self).__init__()\n",
" self.fc1 = nn.Linear(input_size, 1024)\n",
" self.fc2 = nn.Linear(1024, 512)\n",
" self.fc3 = nn.Linear( 512, num_classes)\n",
"\n",
" def forward(self, x):\n",
" x = F.relu(self.fc1(x))\n",
" x = F.relu(self.fc2(x))\n",
" x = F.softmax(self.fc3(x), dim=1)\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"#device\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"\n",
"#hyperparameter\n",
"input_size = 3 * 64 * 64\n",
"num_classes = 2\n",
"learning_rate = 0.00005\n",
"batch_size = 32\n",
"num_epochs = 100\n",
"#load data\n",
"\n",
"dataset = SmpDataset(csv_file='dataset.csv', root_dir='imgs',\n",
" transform=transform)\n",
"\n",
"train_set, test_set = torch.utils.data.random_split(dataset , [int(len(dataset) * 0.9) , len(dataset) - int(len(dataset) * 0.9)] )\n",
"\n",
"train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)\n",
"test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=True)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"#initialize the network\n",
"model= NN(input_size=input_size,num_classes=num_classes,).to(device=device)\n",
"\n",
"#loss and optimizer\n",
"criterion = nn.CrossEntropyLoss()\n",
"optimizer = optim.Adam(model.parameters() , lr =learning_rate)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"current epoch 0 with loss 0.6752\n",
"current epoch 10 with loss 0.5226\n",
"current epoch 20 with loss 0.5462\n",
"current epoch 30 with loss 0.4593\n",
"current epoch 40 with loss 0.3766\n",
"current epoch 50 with loss 0.3583\n",
"current epoch 60 with loss 0.3317\n",
"current epoch 70 with loss 0.3247\n",
"current epoch 80 with loss 0.3203\n",
"current epoch 90 with loss 0.3201\n"
]
}
],
"source": [
"#train network\n",
"\n",
"for epochs in range(num_epochs):\n",
" curr_loss = 0\n",
" for batch_idx , (data , targets) in enumerate(train_loader):\n",
" #get to cuda\n",
" data = data.to(device=device)\n",
" targets = targets.to(device=device)\n",
"\n",
" #reshape the data\n",
" data = data.reshape(data.shape[0] , -1)\n",
"\n",
" #forward\n",
" scores = model(data)\n",
" loss = criterion(scores, targets)\n",
" curr_loss = loss\n",
"\n",
" #backwards\n",
" optimizer.zero_grad()\n",
" loss.backward()\n",
"\n",
" #gradient descent step\n",
" optimizer.step()\n",
" if ( epochs % 10 == 0):\n",
" print(f\"current epoch {epochs} with loss {curr_loss.item():.4f}\")"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Got 241 / 242 with accuracy 99.59\n",
"Got 22 / 27 with accuracy 81.48\n"
]
}
],
"source": [
"#check accuray now\n",
"def check_accuracy(loader , model):\n",
" num_correct = 0\n",
" num_samples = 0\n",
" model.eval()\n",
" with torch.no_grad():\n",
" for x , y in loader:\n",
" x = x.to(device=device)\n",
" y = y.to(device=device)\n",
" x = x.reshape(x.shape[0] , -1 )\n",
"\n",
" scores = model(x)\n",
" _,predictions = scores.max(1)\n",
" num_correct += (predictions == y ).sum()\n",
" num_samples += predictions.size(0)\n",
"\n",
" print(f\"Got {num_correct} / {num_samples} with accuracy {(float(num_correct)*100/float(num_samples)):.2f}\")\n",
" model.train()\n",
"\n",
"check_accuracy(train_loader , model)\n",
"check_accuracy(test_loader, model)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "dl",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
132 changes: 132 additions & 0 deletions Week 2/Assignment/Neural Network Assignment/train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#imports

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision
from torch.utils.data import DataLoader
import os
import pandas as pd
import torch
from torch.utils.data import Dataset
from skimage import io


class SmpDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform

def __len__(self):
return len(self.annotations)

def __getitem__(self, index):
img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
image = io.imread(img_path)
y_label = torch.tensor(int(self.annotations.iloc[index, 1]))

if self.transform:
image = self.transform(image)
return (image, y_label)


# Transform to resize images to 64x64 and convert to tensor
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((64, 64)),
transforms.ToTensor()
])


class NN(nn.Module):
def __init__(self, input_size, num_classes):
super(NN, self).__init__()
self.fc1 = nn.Linear(input_size, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, num_classes)

def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.softmax(self.fc3(x) ,dim=1)
return x


#device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

#hyperparameter
input_size = 3 * 64 * 64
num_classes = 2
learning_rate = 0.0001
batch_size = 32
num_epochs = 200

#load data
dataset = SmpDataset(csv_file='dataset.csv', root_dir='imgs',
transform=transform)

train_set, test_set = torch.utils.data.random_split(dataset , [int(len(dataset) * 0.9) , len(dataset) - int(len(dataset) * 0.9)] )

train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=True)

#initialize the network
model= NN(input_size=input_size,num_classes=num_classes,).to(device=device)

#loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() , lr =learning_rate)

#train network

for epochs in range(num_epochs):
curr_loss = 0
for batch_idx , (data , targets) in enumerate(train_loader):
#get to cuda
data = data.to(device=device)
targets = targets.to(device=device)

#reshape the data
data = data.reshape(data.shape[0] , -1)

#forward
scores = model(data)
loss = criterion(scores, targets)
curr_loss = loss

#backwards
optimizer.zero_grad()
loss.backward()

#gradient descent step
optimizer.step()
if ( epochs % 10 == 0):
print(f"current epoch {epochs} with loss {curr_loss.item():.4f}")

#check accuray now
def check_accuracy(loader , model):
num_correct = 0
num_samples = 0
model.eval()
with torch.no_grad():
for x , y in loader:
x = x.to(device=device)
y = y.to(device=device)
x = x.reshape(x.shape[0] , -1 )

scores = model(x)
_,predictions = scores.max(1)
num_correct += (predictions == y ).sum()
num_samples += predictions.size(0)

print(f"Got {num_correct} / {num_samples} with accuracy {(float(num_correct)*100/float(num_samples)):.2f}")
model.train()

check_accuracy(train_loader , model)
check_accuracy(test_loader, model)


Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://drive.google.com/drive/folders/1bJyDRDZ_XWitt3z7hzH2SQnkwLXU7A6G?usp=drive_link
This drive contains the yolo_submission.ipynb the colab file , the confusion matrix is in the runs/detect/val/ and the predicted outputs in the runs/detect/predict/ .
Loading