Archived
Adam optimized... better at training but worse bc overfitting worse
This commit is contained in:
1 parent
4d9bf5965b
commit
4f8c48c504
3 files changed
+611
-53
No files matched your search
File renamed without changes.
+53
-53
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 1,
|
||||
"id": "7a37220a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -20,7 +20,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 2,
|
||||
"id": "d318d1f0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -61,7 +61,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 3,
|
||||
"id": "5604ace3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -110,7 +110,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 4,
|
||||
"id": "3cedd586",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -150,7 +150,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": 5,
|
||||
"id": "8f556b22",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -184,7 +184,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 6,
|
||||
"id": "37793c77",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -222,7 +222,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 7,
|
||||
"id": "f68c1a25",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -257,7 +257,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": 8,
|
||||
"id": "e1539eaa",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -289,7 +289,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 9,
|
||||
"id": "d1b7d9ca",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -347,7 +347,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 10,
|
||||
"id": "54d11a04",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -370,7 +370,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 11,
|
||||
"id": "374d0590",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -378,36 +378,36 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 1: Loss=1.677, Accuracy=45.44%\n",
|
||||
"Epoch 2: Loss=1.340, Accuracy=56.11%\n",
|
||||
"Epoch 3: Loss=1.097, Accuracy=62.00%\n",
|
||||
"Epoch 4: Loss=0.965, Accuracy=66.50%\n",
|
||||
"Epoch 5: Loss=0.907, Accuracy=67.97%\n",
|
||||
"Epoch 6: Loss=0.841, Accuracy=70.23%\n",
|
||||
"Epoch 7: Loss=0.797, Accuracy=71.75%\n",
|
||||
"Epoch 8: Loss=0.743, Accuracy=73.90%\n",
|
||||
"Epoch 9: Loss=0.717, Accuracy=74.62%\n",
|
||||
"Epoch 10: Loss=0.672, Accuracy=76.24%\n",
|
||||
"Epoch 11: Loss=0.640, Accuracy=77.52%\n",
|
||||
"Epoch 12: Loss=0.607, Accuracy=78.61%\n",
|
||||
"Epoch 13: Loss=0.593, Accuracy=79.23%\n",
|
||||
"Epoch 14: Loss=0.560, Accuracy=80.45%\n",
|
||||
"Epoch 15: Loss=0.525, Accuracy=81.20%\n",
|
||||
"Epoch 16: Loss=0.499, Accuracy=82.29%\n",
|
||||
"Epoch 17: Loss=0.472, Accuracy=83.38%\n",
|
||||
"Epoch 18: Loss=0.451, Accuracy=84.14%\n",
|
||||
"Epoch 19: Loss=0.416, Accuracy=85.29%\n",
|
||||
"Epoch 20: Loss=0.391, Accuracy=86.13%\n",
|
||||
"Epoch 21: Loss=0.360, Accuracy=87.17%\n",
|
||||
"Epoch 22: Loss=0.307, Accuracy=89.43%\n",
|
||||
"Epoch 23: Loss=0.296, Accuracy=89.80%\n",
|
||||
"Epoch 24: Loss=0.269, Accuracy=90.47%\n",
|
||||
"Epoch 25: Loss=0.236, Accuracy=91.86%\n",
|
||||
"Epoch 26: Loss=0.206, Accuracy=92.95%\n",
|
||||
"Epoch 27: Loss=0.183, Accuracy=93.76%\n",
|
||||
"Epoch 28: Loss=0.159, Accuracy=94.71%\n",
|
||||
"Epoch 29: Loss=0.145, Accuracy=94.99%\n",
|
||||
"Epoch 30: Loss=0.115, Accuracy=96.10%\n",
|
||||
"Epoch 1: Loss=1.763, Accuracy=41.55%\n",
|
||||
"Epoch 2: Loss=1.320, Accuracy=55.89%\n",
|
||||
"Epoch 3: Loss=1.035, Accuracy=63.69%\n",
|
||||
"Epoch 4: Loss=0.915, Accuracy=68.51%\n",
|
||||
"Epoch 5: Loss=0.845, Accuracy=70.83%\n",
|
||||
"Epoch 6: Loss=0.807, Accuracy=72.21%\n",
|
||||
"Epoch 7: Loss=0.766, Accuracy=73.35%\n",
|
||||
"Epoch 8: Loss=0.752, Accuracy=73.76%\n",
|
||||
"Epoch 9: Loss=0.720, Accuracy=75.00%\n",
|
||||
"Epoch 10: Loss=0.688, Accuracy=76.09%\n",
|
||||
"Epoch 11: Loss=0.646, Accuracy=77.31%\n",
|
||||
"Epoch 12: Loss=0.623, Accuracy=78.31%\n",
|
||||
"Epoch 13: Loss=0.596, Accuracy=79.13%\n",
|
||||
"Epoch 14: Loss=0.567, Accuracy=80.19%\n",
|
||||
"Epoch 15: Loss=0.530, Accuracy=81.66%\n",
|
||||
"Epoch 16: Loss=0.502, Accuracy=82.52%\n",
|
||||
"Epoch 17: Loss=0.473, Accuracy=83.62%\n",
|
||||
"Epoch 18: Loss=0.447, Accuracy=84.63%\n",
|
||||
"Epoch 19: Loss=0.416, Accuracy=85.71%\n",
|
||||
"Epoch 20: Loss=0.401, Accuracy=86.22%\n",
|
||||
"Epoch 21: Loss=0.353, Accuracy=87.89%\n",
|
||||
"Epoch 22: Loss=0.321, Accuracy=89.02%\n",
|
||||
"Epoch 23: Loss=0.282, Accuracy=90.39%\n",
|
||||
"Epoch 24: Loss=0.260, Accuracy=91.10%\n",
|
||||
"Epoch 25: Loss=0.243, Accuracy=91.71%\n",
|
||||
"Epoch 26: Loss=0.203, Accuracy=93.18%\n",
|
||||
"Epoch 27: Loss=0.169, Accuracy=94.36%\n",
|
||||
"Epoch 28: Loss=0.139, Accuracy=95.55%\n",
|
||||
"Epoch 29: Loss=0.112, Accuracy=96.42%\n",
|
||||
"Epoch 30: Loss=0.106, Accuracy=96.63%\n",
|
||||
"Finished Training\n"
|
||||
]
|
||||
}
|
||||
@@ -445,7 +445,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 12,
|
||||
"id": "2bf2b9a2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -464,7 +464,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 13,
|
||||
"id": "bc158602",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -472,7 +472,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test Accuracy: 76.31%\n"
|
||||
"Test Accuracy: 77.52%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -494,7 +494,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"execution_count": 14,
|
||||
"id": "8cc7ed40",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -502,14 +502,14 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Accuracy for class: Bicycle is 68.2%\n",
|
||||
"Accuracy for class: Bus is 70.2%\n",
|
||||
"Accuracy for class: Car is 74.3%\n",
|
||||
"Accuracy for class: Motorcycle is 63.3%\n",
|
||||
"Accuracy for class: NonVehicles is 98.8%\n",
|
||||
"Accuracy for class: Taxi is 55.6%\n",
|
||||
"Accuracy for class: Truck is 45.5%\n",
|
||||
"Accuracy for class: Van is 28.1%\n"
|
||||
"Accuracy for class: Bicycle is 57.6%\n",
|
||||
"Accuracy for class: Bus is 76.4%\n",
|
||||
"Accuracy for class: Car is 82.3%\n",
|
||||
"Accuracy for class: Motorcycle is 72.5%\n",
|
||||
"Accuracy for class: NonVehicles is 99.1%\n",
|
||||
"Accuracy for class: Taxi is 27.8%\n",
|
||||
"Accuracy for class: Truck is 38.9%\n",
|
||||
"Accuracy for class: Van is 25.0%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -0,0 +1,558 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "7a37220a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Hello, World!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(\"Hello, World!\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "d318d1f0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Van: 1111 images\n",
|
||||
"Taxi: 748 images\n",
|
||||
"Bicycle: 1618 images\n",
|
||||
"Bus: 2133 images\n",
|
||||
"Car: 6781 images\n",
|
||||
"Motorcycle: 2986 images\n",
|
||||
"Truck: 2033 images\n",
|
||||
"NonVehicles: 8968 images\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"data_dir = '../data/raw/vehicle_classification'\n",
|
||||
"\n",
|
||||
"for class_name in os.listdir(data_dir):\n",
|
||||
" class_path = os.path.join(data_dir, class_name)\n",
|
||||
" if os.path.isdir(class_path):\n",
|
||||
" count = len(os.listdir(class_path))\n",
|
||||
" print(f\"{class_name}: {count} images\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "64122ad4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Check out sample image from dataset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "5604ace3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Size: (64, 64)\n",
|
||||
"Mode: RGB\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGzCAYAAABpdMNsAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjgsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvwVt1zgAAAAlwSFlzAAAPYQAAD2EBqD+naQAAZNhJREFUeJztvXmUXNV57v3UXNXd1VU9qQe1WmqhoTULJCGEwNigWCY2HlBinKXkch2veJkrCJOTWFkXnPiLLZbzXeM4kfFwCTjLlhXjfICxAxiEERhLgCSEhOa5W2p1t3qonms+3x+Ejlv72bYKWpymeX5r9Vrw1tauvc/Z57x1aj/1vB7HcRwIIYQQ7zJetwcghBDi/YkSkBBCCFdQAhJCCOEKSkBCCCFcQQlICCGEKygBCSGEcAUlICGEEK6gBCSEEMIVlICEEEK4ghKQEEIIV1ACEuJt8vGPfxxFRUXo7++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iEo03zODX1SARw5w+fZq2zXv5dV/XcAmN11ZEjVh380HadmpDLY0PW8RKOYv9UaLPtByyXLLWwns1k6povKqS3z8GSaG+gQEubmEFDQEgGODrMEquN5soKUD68PstN9rz0BOQEEIIV1ACEkILine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 640x480 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from PIL import Image\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"# first image in first folder\n",
|
||||
"first_class = os.listdir(data_dir)[0]\n",
|
||||
"first_image_path = os.path.join(data_dir, first_class, os.listdir(os.path.join(data_dir, first_class))[0])\n",
|
||||
"\n",
|
||||
"img = Image.open(first_image_path)\n",
|
||||
"print(f\"Size: {img.size}\")\n",
|
||||
"print(f\"Mode: {img.mode}\")\n",
|
||||
"plt.imshow(img)\n",
|
||||
"plt.title(first_class)\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c19ec00a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Ensure that all images are RGB, all of same resolution"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "3cedd586",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Unique sizes: {(64, 64)}\n",
|
||||
"Unique modes: {'RGB'}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sizes = set()\n",
|
||||
"modes = set()\n",
|
||||
"\n",
|
||||
"for class_name in os.listdir(data_dir):\n",
|
||||
" class_path = os.path.join(data_dir, class_name)\n",
|
||||
" if not os.path.isdir(class_path):\n",
|
||||
" continue\n",
|
||||
" for img_name in os.listdir(class_path):\n",
|
||||
" img = Image.open(os.path.join(class_path, img_name))\n",
|
||||
" sizes.add(img.size)\n",
|
||||
" modes.add(img.mode)\n",
|
||||
"\n",
|
||||
"print(f\"Unique sizes: {sizes}\")\n",
|
||||
"print(f\"Unique modes: {modes}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "88ac961b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Ensure that torch works with GPU (5080) [Credit: Claude]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "8f556b22",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"True\n",
|
||||
"NVIDIA GeForce RTX 5080\n",
|
||||
"GPU works! torch.Size([1000, 1000])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"print(torch.cuda.is_available()) \n",
|
||||
"print(torch.cuda.get_device_name(0)) \n",
|
||||
"\n",
|
||||
"x = torch.randn(1000, 1000).cuda()\n",
|
||||
"y = x @ x\n",
|
||||
"print(\"GPU works!\", y.shape)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "870fadbe",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Transform image dataset into tensors. Normalized with 0.5 mean and 0.5 STD (can be differente to use pretrained weights)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "37793c77",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"['Bicycle', 'Bus', 'Car', 'Motorcycle', 'NonVehicles', 'Taxi', 'Truck', 'Van']\n",
|
||||
"26378\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from torchvision import datasets, transforms\n",
|
||||
"from torch.utils.data import random_split, DataLoader\n",
|
||||
"\n",
|
||||
"transform = transforms.Compose([\n",
|
||||
" transforms.ToTensor(),\n",
|
||||
" transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n",
|
||||
"])\n",
|
||||
"\n",
|
||||
"dataset = datasets.ImageFolder(root=data_dir, transform=transform)\n",
|
||||
"\n",
|
||||
"print(dataset.classes) # should print 8 classes\n",
|
||||
"print(len(dataset)) # total image count"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ac33bc21",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Split 80-20 and save into 2 variables"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "f68c1a25",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Train: 21102, Test: 5276\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import math \n",
|
||||
"\n",
|
||||
"train_size = math.floor(len(dataset) * 0.8)\n",
|
||||
"test_size = len(dataset) - train_size\n",
|
||||
"\n",
|
||||
"train_dataset, test_dataset = random_split(dataset, [train_size, test_size])\n",
|
||||
"\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": 8,
|
||||
"id": "e1539eaa",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"8\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"train_loader = DataLoader(train_dataset, batch_size=256, shuffle=True, num_workers=16) # Increased from 4\n",
|
||||
"test_loader = DataLoader(test_dataset, batch_size=256, shuffle=False, num_workers=16) # Increased from 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": 9,
|
||||
"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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "22e71032",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Loss fn and optimizer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "54d11a04",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch.optim as optim\n",
|
||||
"\n",
|
||||
"criterion = nn.CrossEntropyLoss() # Applied softmax to convert scores --> probabilities --> penalizes model \n",
|
||||
"\n",
|
||||
"# Changed to adam optimizer (internal momentumn calculation)\n",
|
||||
"optimizer = optim.Adam(model.parameters(), lr=0.001)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "572d80e3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Tutorial training cell adapted to use 30 epochs "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "374d0590",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 1: Loss=1.379, Accuracy=54.06%\n",
|
||||
"Epoch 2: Loss=0.980, Accuracy=66.04%\n",
|
||||
"Epoch 3: Loss=0.878, Accuracy=69.10%\n",
|
||||
"Epoch 4: Loss=0.793, Accuracy=71.95%\n",
|
||||
"Epoch 5: Loss=0.734, Accuracy=74.29%\n",
|
||||
"Epoch 6: Loss=0.692, Accuracy=75.72%\n",
|
||||
"Epoch 7: Loss=0.651, Accuracy=76.99%\n",
|
||||
"Epoch 8: Loss=0.616, Accuracy=78.33%\n",
|
||||
"Epoch 9: Loss=0.583, Accuracy=79.80%\n",
|
||||
"Epoch 10: Loss=0.554, Accuracy=80.75%\n",
|
||||
"Epoch 11: Loss=0.533, Accuracy=81.49%\n",
|
||||
"Epoch 12: Loss=0.505, Accuracy=82.45%\n",
|
||||
"Epoch 13: Loss=0.480, Accuracy=83.14%\n",
|
||||
"Epoch 14: Loss=0.453, Accuracy=84.20%\n",
|
||||
"Epoch 15: Loss=0.420, Accuracy=85.17%\n",
|
||||
"Epoch 16: Loss=0.392, Accuracy=86.41%\n",
|
||||
"Epoch 17: Loss=0.377, Accuracy=86.77%\n",
|
||||
"Epoch 18: Loss=0.343, Accuracy=87.99%\n",
|
||||
"Epoch 19: Loss=0.304, Accuracy=89.47%\n",
|
||||
"Epoch 20: Loss=0.274, Accuracy=90.54%\n",
|
||||
"Epoch 21: Loss=0.248, Accuracy=91.55%\n",
|
||||
"Epoch 22: Loss=0.215, Accuracy=92.74%\n",
|
||||
"Epoch 23: Loss=0.192, Accuracy=93.65%\n",
|
||||
"Epoch 24: Loss=0.167, Accuracy=94.75%\n",
|
||||
"Epoch 25: Loss=0.149, Accuracy=95.07%\n",
|
||||
"Epoch 26: Loss=0.128, Accuracy=95.86%\n",
|
||||
"Epoch 27: Loss=0.115, Accuracy=96.20%\n",
|
||||
"Epoch 28: Loss=0.092, Accuracy=97.10%\n",
|
||||
"Epoch 29: Loss=0.073, Accuracy=97.84%\n",
|
||||
"Epoch 30: Loss=0.060, Accuracy=98.39%\n",
|
||||
"Finished Training\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for epoch in range(30): # 10 --> 30 epochs\n",
|
||||
" running_loss = 0.0\n",
|
||||
" correct = 0\n",
|
||||
" total = 0\n",
|
||||
"\n",
|
||||
" for i, data in enumerate(train_loader, 0):\n",
|
||||
" inputs, labels = data\n",
|
||||
" inputs, labels = inputs.to(device), labels.to(device) # use GPU\n",
|
||||
"\n",
|
||||
" optimizer.zero_grad()\n",
|
||||
"\n",
|
||||
" outputs = model(inputs)\n",
|
||||
" loss = criterion(outputs, labels)\n",
|
||||
" loss.backward()\n",
|
||||
" optimizer.step()\n",
|
||||
"\n",
|
||||
" # loss tracking\n",
|
||||
" running_loss += loss.item()\n",
|
||||
"\n",
|
||||
" # accuracy tracking\n",
|
||||
" _, predicted = torch.max(outputs, 1)\n",
|
||||
" total += labels.size(0)\n",
|
||||
" correct += (predicted == labels).sum().item()\n",
|
||||
"\n",
|
||||
" accuracy = 100 * correct / total\n",
|
||||
" print(f'Epoch {epoch + 1}: Loss={running_loss / len(train_loader):.3f}, Accuracy={accuracy:.2f}%') # Credit: Claude for printing accuracy in good formatting\n",
|
||||
"\n",
|
||||
"print('Finished Training')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "2bf2b9a2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PATH = '../models/adam-optimized.pth'\n",
|
||||
"torch.save(model.state_dict(), PATH)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a24dd4f0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Test on test set using Pytorch tutorial method for both total and per class (maybe spot overfitting as well)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "bc158602",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test Accuracy: 75.11%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"correct = 0\n",
|
||||
"total = 0\n",
|
||||
"\n",
|
||||
"with torch.no_grad():\n",
|
||||
" for data in test_loader:\n",
|
||||
" images, labels = data\n",
|
||||
" images, labels = images.to(device), labels.to(device)\n",
|
||||
" outputs = model(images)\n",
|
||||
" _, predicted = torch.max(outputs, 1)\n",
|
||||
" total += labels.size(0)\n",
|
||||
" correct += (predicted == labels).sum().item()\n",
|
||||
"\n",
|
||||
"print(f'Test Accuracy: {100 * correct / total:.2f}%')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "8cc7ed40",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Accuracy for class: Bicycle is 61.4%\n",
|
||||
"Accuracy for class: Bus is 61.8%\n",
|
||||
"Accuracy for class: Car is 79.5%\n",
|
||||
"Accuracy for class: Motorcycle is 72.6%\n",
|
||||
"Accuracy for class: NonVehicles is 98.9%\n",
|
||||
"Accuracy for class: Taxi is 44.0%\n",
|
||||
"Accuracy for class: Truck is 26.0%\n",
|
||||
"Accuracy for class: Van is 31.1%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"correct_pred = {classname: 0 for classname in dataset.classes}\n",
|
||||
"total_pred = {classname: 0 for classname in dataset.classes}\n",
|
||||
"\n",
|
||||
"with torch.no_grad():\n",
|
||||
" for data in test_loader:\n",
|
||||
" images, labels = data\n",
|
||||
" images, labels = images.to(device), labels.to(device)\n",
|
||||
" outputs = model(images)\n",
|
||||
" _, predictions = torch.max(outputs, 1)\n",
|
||||
" for label, prediction in zip(labels, predictions):\n",
|
||||
" if label == prediction:\n",
|
||||
" correct_pred[dataset.classes[label]] += 1\n",
|
||||
" total_pred[dataset.classes[label]] += 1\n",
|
||||
"\n",
|
||||
"for classname, correct_count in correct_pred.items():\n",
|
||||
" accuracy = 100 * float(correct_count) / total_pred[classname]\n",
|
||||
" print(f'Accuracy for class: {classname:10s} is {accuracy:.1f}%')"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv",
|
||||
"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.11.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in new issue
Block a user