finished CNN class

This commit is contained in:
KeshavAnandCode
2026-03-18 17:56:49 -05:00
parent d756b6bd1a
commit 255a9aa73c

View File

@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "7a37220a",
"metadata": {},
"outputs": [
@@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "d318d1f0",
"metadata": {},
"outputs": [
@@ -61,7 +61,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "5604ace3",
"metadata": {},
"outputs": [
@@ -110,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "3cedd586",
"metadata": {},
"outputs": [
@@ -150,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "8f556b22",
"metadata": {},
"outputs": [
@@ -184,7 +184,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "37793c77",
"metadata": {},
"outputs": [
@@ -222,7 +222,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "f68c1a25",
"metadata": {},
"outputs": [
@@ -244,6 +244,98 @@
"\n",
"print(f\"Train: {len(train_dataset)}, Test: {len(test_dataset)}\")"
]
},
{
"cell_type": "markdown",
"id": "2eede814",
"metadata": {},
"source": [
"Load the data into batches for faster loading...use 4 threads on CPU\n",
"\n",
"Shuffling train dataset so as to not overfit on one class"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e1539eaa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n"
]
}
],
"source": [
"train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4)\n",
"test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)\n",
"\n",
"classes = ('bicycle', 'bus', 'car', 'motorcycle',\n",
" 'nonvehicles', 'taxi', 'truck', 'van')\n",
"\n",
"print(len(classes))"
]
},
{
"cell_type": "markdown",
"id": "acebe98e",
"metadata": {},
"source": [
"Tutorial CNN architecture implmenetation"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d1b7d9ca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Net(\n",
" (conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))\n",
" (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))\n",
" (fc1): Linear(in_features=2704, out_features=120, bias=True)\n",
" (fc2): Linear(in_features=120, out_features=84, bias=True)\n",
" (fc3): Linear(in_features=84, out_features=8, bias=True)\n",
")\n"
]
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"\n",
"class Net(nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.conv1 = nn.Conv2d(3, 6, 5) # 3 input channels for RGB, 6 filters [arbitrailly chosen ffrom PyTorch], 5x5 Kernel Size\n",
" self.pool = nn.MaxPool2d(2, 2) # Halves spatial dimensions\n",
" self.conv2 = nn.Conv2d(6, 16, 5) # 6 matches output of first conv layer, 16 arbitrary filter (more than earlier), 5x5 kernel\n",
" self.fc1 = nn.Linear(16 * 13 * 13, 120) # 16 channels from conv layer 2; 13 *13 bc shape after 2 layers and 2 pools, 120 is arbitrary output size from tutorial\n",
" self.fc2 = nn.Linear(120, 84) # 120 output from fc1 --> arbitrary lowering to 84\n",
" self.fc3 = nn.Linear(84, len(classes)) # Bring down to number of classes \n",
"\n",
" def forward(self, x):\n",
" x = self.pool(F.relu(self.conv1(x))) #Conv layer 1 --> relu --> pool \n",
" x = self.pool(F.relu(self.conv2(x))) #Conv layer 1 --> relu --> pool \n",
" x = torch.flatten(x, 1) # Flattedn to 1d\n",
" x = F.relu(self.fc1(x)) #fully connected layer shriking 1\n",
" x = F.relu(self.fc2(x)) # Shrink again\n",
" x = self.fc3(x) # Shrink to number of classes\n",
" return x\n",
"\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"model = Net().to(device)\n",
"print(model)"
]
}
],
"metadata": {