Spaces:
Sleeping
Sleeping
Delete vggnet (1).ipynb
Browse files- vggnet (1).ipynb +0 -465
vggnet (1).ipynb
DELETED
|
@@ -1,465 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "code",
|
| 5 |
-
"execution_count": 1,
|
| 6 |
-
"id": "6cb82750-e21c-4895-81fb-6b03a44595d0",
|
| 7 |
-
"metadata": {},
|
| 8 |
-
"outputs": [
|
| 9 |
-
{
|
| 10 |
-
"name": "stdout",
|
| 11 |
-
"output_type": "stream",
|
| 12 |
-
"text": [
|
| 13 |
-
"Requirement already satisfied: torch in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (2.5.1)\n",
|
| 14 |
-
"Requirement already satisfied: torchvision in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (0.20.1)\n",
|
| 15 |
-
"Requirement already satisfied: datasets in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (4.1.1)\n",
|
| 16 |
-
"Requirement already satisfied: tqdm in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (4.67.1)\n",
|
| 17 |
-
"Requirement already satisfied: matplotlib in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (3.9.4)\n",
|
| 18 |
-
"Requirement already satisfied: filelock in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torch) (3.19.1)\n",
|
| 19 |
-
"Requirement already satisfied: typing-extensions>=4.8.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torch) (4.14.1)\n",
|
| 20 |
-
"Requirement already satisfied: networkx in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torch) (3.2.1)\n",
|
| 21 |
-
"Requirement already satisfied: jinja2 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torch) (3.1.6)\n",
|
| 22 |
-
"Requirement already satisfied: fsspec in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torch) (2025.9.0)\n",
|
| 23 |
-
"Requirement already satisfied: sympy==1.13.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torch) (1.13.1)\n",
|
| 24 |
-
"Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from sympy==1.13.1->torch) (1.3.0)\n",
|
| 25 |
-
"Requirement already satisfied: numpy in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torchvision) (2.0.2)\n",
|
| 26 |
-
"Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from torchvision) (9.4.0)\n",
|
| 27 |
-
"Requirement already satisfied: pyarrow>=21.0.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (21.0.0)\n",
|
| 28 |
-
"Requirement already satisfied: dill<0.4.1,>=0.3.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (0.4.0)\n",
|
| 29 |
-
"Requirement already satisfied: pandas in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (2.3.1)\n",
|
| 30 |
-
"Requirement already satisfied: requests>=2.32.2 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (2.32.5)\n",
|
| 31 |
-
"Requirement already satisfied: xxhash in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (3.5.0)\n",
|
| 32 |
-
"Requirement already satisfied: multiprocess<0.70.17 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (0.70.16)\n",
|
| 33 |
-
"Requirement already satisfied: huggingface-hub>=0.24.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (0.35.0)\n",
|
| 34 |
-
"Requirement already satisfied: packaging in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (25.0)\n",
|
| 35 |
-
"Requirement already satisfied: pyyaml>=5.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from datasets) (6.0.2)\n",
|
| 36 |
-
"Requirement already satisfied: aiohttp!=4.0.0a0,!=4.0.0a1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (3.12.15)\n",
|
| 37 |
-
"Requirement already satisfied: colorama in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from tqdm) (0.4.6)\n",
|
| 38 |
-
"Requirement already satisfied: contourpy>=1.0.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from matplotlib) (1.3.0)\n",
|
| 39 |
-
"Requirement already satisfied: cycler>=0.10 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from matplotlib) (0.12.1)\n",
|
| 40 |
-
"Requirement already satisfied: fonttools>=4.22.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from matplotlib) (4.59.1)\n",
|
| 41 |
-
"Requirement already satisfied: kiwisolver>=1.3.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from matplotlib) (1.4.7)\n",
|
| 42 |
-
"Requirement already satisfied: pyparsing>=2.3.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from matplotlib) (3.2.3)\n",
|
| 43 |
-
"Requirement already satisfied: python-dateutil>=2.7 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from matplotlib) (2.9.0.post0)\n",
|
| 44 |
-
"Requirement already satisfied: importlib-resources>=3.2.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from matplotlib) (6.5.2)\n",
|
| 45 |
-
"Requirement already satisfied: aiohappyeyeballs>=2.5.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (2.6.1)\n",
|
| 46 |
-
"Requirement already satisfied: aiosignal>=1.4.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (1.4.0)\n",
|
| 47 |
-
"Requirement already satisfied: async-timeout<6.0,>=4.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (5.0.1)\n",
|
| 48 |
-
"Requirement already satisfied: attrs>=17.3.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (25.3.0)\n",
|
| 49 |
-
"Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (1.7.0)\n",
|
| 50 |
-
"Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (6.6.4)\n",
|
| 51 |
-
"Requirement already satisfied: propcache>=0.2.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (0.3.2)\n",
|
| 52 |
-
"Requirement already satisfied: yarl<2.0,>=1.17.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (1.20.1)\n",
|
| 53 |
-
"Requirement already satisfied: idna>=2.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from yarl<2.0,>=1.17.0->aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.9.0,>=2023.1.0->datasets) (3.10)\n",
|
| 54 |
-
"Requirement already satisfied: zipp>=3.1.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from importlib-resources>=3.2.0->matplotlib) (3.23.0)\n",
|
| 55 |
-
"Requirement already satisfied: six>=1.5 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0)\n",
|
| 56 |
-
"Requirement already satisfied: charset_normalizer<4,>=2 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from requests>=2.32.2->datasets) (3.4.3)\n",
|
| 57 |
-
"Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from requests>=2.32.2->datasets) (2.5.0)\n",
|
| 58 |
-
"Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from requests>=2.32.2->datasets) (2025.8.3)\n",
|
| 59 |
-
"Requirement already satisfied: MarkupSafe>=2.0 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from jinja2->torch) (3.0.2)\n",
|
| 60 |
-
"Requirement already satisfied: pytz>=2020.1 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from pandas->datasets) (2025.2)\n",
|
| 61 |
-
"Requirement already satisfied: tzdata>=2022.7 in c:\\users\\adity\\miniconda3\\envs\\dl_cpu\\lib\\site-packages (from pandas->datasets) (2025.2)\n",
|
| 62 |
-
"Note: you may need to restart the kernel to use updated packages.\n"
|
| 63 |
-
]
|
| 64 |
-
}
|
| 65 |
-
],
|
| 66 |
-
"source": [
|
| 67 |
-
"pip install torch torchvision datasets tqdm matplotlib\n"
|
| 68 |
-
]
|
| 69 |
-
},
|
| 70 |
-
{
|
| 71 |
-
"cell_type": "code",
|
| 72 |
-
"execution_count": 2,
|
| 73 |
-
"id": "eaba3244-e2d3-4d34-b44c-e94287ac08af",
|
| 74 |
-
"metadata": {},
|
| 75 |
-
"outputs": [
|
| 76 |
-
{
|
| 77 |
-
"name": "stdout",
|
| 78 |
-
"output_type": "stream",
|
| 79 |
-
"text": [
|
| 80 |
-
"Using device: cpu\n"
|
| 81 |
-
]
|
| 82 |
-
}
|
| 83 |
-
],
|
| 84 |
-
"source": [
|
| 85 |
-
"import torch\n",
|
| 86 |
-
"import torch.nn as nn\n",
|
| 87 |
-
"import torch.optim as optim\n",
|
| 88 |
-
"from torchvision import models, transforms\n",
|
| 89 |
-
"from torch.utils.data import DataLoader\n",
|
| 90 |
-
"from datasets import load_dataset\n",
|
| 91 |
-
"from tqdm import tqdm\n",
|
| 92 |
-
"import matplotlib.pyplot as plt\n",
|
| 93 |
-
"from IPython.display import clear_output\n",
|
| 94 |
-
"\n",
|
| 95 |
-
"# Set device\n",
|
| 96 |
-
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
|
| 97 |
-
"print(\"Using device:\", device)\n"
|
| 98 |
-
]
|
| 99 |
-
},
|
| 100 |
-
{
|
| 101 |
-
"cell_type": "code",
|
| 102 |
-
"execution_count": 3,
|
| 103 |
-
"id": "ded6afcd-a0f0-437d-8ad1-f816650146de",
|
| 104 |
-
"metadata": {},
|
| 105 |
-
"outputs": [],
|
| 106 |
-
"source": [
|
| 107 |
-
"from torchvision import transforms\n",
|
| 108 |
-
"\n",
|
| 109 |
-
"train_transform = transforms.Compose([\n",
|
| 110 |
-
" transforms.Resize((224, 224)),\n",
|
| 111 |
-
" transforms.RandomHorizontalFlip(),\n",
|
| 112 |
-
" transforms.ToTensor(),\n",
|
| 113 |
-
" transforms.Normalize(mean=[0.485, 0.456, 0.406],\n",
|
| 114 |
-
" std=[0.229, 0.224, 0.225])\n",
|
| 115 |
-
"])\n",
|
| 116 |
-
"\n",
|
| 117 |
-
"val_transform = transforms.Compose([\n",
|
| 118 |
-
" transforms.Resize((224, 224)),\n",
|
| 119 |
-
" transforms.ToTensor(),\n",
|
| 120 |
-
" transforms.Normalize(mean=[0.485, 0.456, 0.406],\n",
|
| 121 |
-
" std=[0.229, 0.224, 0.225])\n",
|
| 122 |
-
"])\n"
|
| 123 |
-
]
|
| 124 |
-
},
|
| 125 |
-
{
|
| 126 |
-
"cell_type": "code",
|
| 127 |
-
"execution_count": 4,
|
| 128 |
-
"id": "b8cc0c13-a42a-405c-a003-af754e657de4",
|
| 129 |
-
"metadata": {},
|
| 130 |
-
"outputs": [],
|
| 131 |
-
"source": [
|
| 132 |
-
"from torchvision.datasets import ImageFolder\n",
|
| 133 |
-
"\n",
|
| 134 |
-
"train_dataset = ImageFolder(root=\"sickle-cell-classification-dataset/train\", transform=train_transform)\n",
|
| 135 |
-
"val_dataset = ImageFolder(root=\"sickle-cell-classification-dataset/val\", transform=val_transform)\n",
|
| 136 |
-
"test_dataset = ImageFolder(root=\"sickle-cell-classification-dataset/test\", transform=val_transform)\n"
|
| 137 |
-
]
|
| 138 |
-
},
|
| 139 |
-
{
|
| 140 |
-
"cell_type": "code",
|
| 141 |
-
"execution_count": 5,
|
| 142 |
-
"id": "a16b08c8-93fb-4cf3-9f1f-900508e1d109",
|
| 143 |
-
"metadata": {},
|
| 144 |
-
"outputs": [
|
| 145 |
-
{
|
| 146 |
-
"name": "stdout",
|
| 147 |
-
"output_type": "stream",
|
| 148 |
-
"text": [
|
| 149 |
-
"{'AIN': 0, 'non_sickle': 1, 'sickle': 2}\n"
|
| 150 |
-
]
|
| 151 |
-
}
|
| 152 |
-
],
|
| 153 |
-
"source": [
|
| 154 |
-
"print(train_dataset.class_to_idx)\n"
|
| 155 |
-
]
|
| 156 |
-
},
|
| 157 |
-
{
|
| 158 |
-
"cell_type": "code",
|
| 159 |
-
"execution_count": 6,
|
| 160 |
-
"id": "a4b8be88-5cb8-449b-9287-26b6a5151e79",
|
| 161 |
-
"metadata": {},
|
| 162 |
-
"outputs": [],
|
| 163 |
-
"source": [
|
| 164 |
-
"from torch.utils.data import DataLoader\n",
|
| 165 |
-
"\n",
|
| 166 |
-
"batch_size = 32\n",
|
| 167 |
-
"num_workers = 0 # safe for Windows\n",
|
| 168 |
-
"\n",
|
| 169 |
-
"train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)\n",
|
| 170 |
-
"val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n",
|
| 171 |
-
"test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n"
|
| 172 |
-
]
|
| 173 |
-
},
|
| 174 |
-
{
|
| 175 |
-
"cell_type": "code",
|
| 176 |
-
"execution_count": 7,
|
| 177 |
-
"id": "dd1d9ad2-800c-4b87-a639-ad13158b69b5",
|
| 178 |
-
"metadata": {
|
| 179 |
-
"scrolled": true
|
| 180 |
-
},
|
| 181 |
-
"outputs": [
|
| 182 |
-
{
|
| 183 |
-
"name": "stdout",
|
| 184 |
-
"output_type": "stream",
|
| 185 |
-
"text": [
|
| 186 |
-
"VGG(\n",
|
| 187 |
-
" (features): Sequential(\n",
|
| 188 |
-
" (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 189 |
-
" (1): ReLU(inplace=True)\n",
|
| 190 |
-
" (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 191 |
-
" (3): ReLU(inplace=True)\n",
|
| 192 |
-
" (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
| 193 |
-
" (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 194 |
-
" (6): ReLU(inplace=True)\n",
|
| 195 |
-
" (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 196 |
-
" (8): ReLU(inplace=True)\n",
|
| 197 |
-
" (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
| 198 |
-
" (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 199 |
-
" (11): ReLU(inplace=True)\n",
|
| 200 |
-
" (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 201 |
-
" (13): ReLU(inplace=True)\n",
|
| 202 |
-
" (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 203 |
-
" (15): ReLU(inplace=True)\n",
|
| 204 |
-
" (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
| 205 |
-
" (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 206 |
-
" (18): ReLU(inplace=True)\n",
|
| 207 |
-
" (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 208 |
-
" (20): ReLU(inplace=True)\n",
|
| 209 |
-
" (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 210 |
-
" (22): ReLU(inplace=True)\n",
|
| 211 |
-
" (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
| 212 |
-
" (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 213 |
-
" (25): ReLU(inplace=True)\n",
|
| 214 |
-
" (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 215 |
-
" (27): ReLU(inplace=True)\n",
|
| 216 |
-
" (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
| 217 |
-
" (29): ReLU(inplace=True)\n",
|
| 218 |
-
" (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
| 219 |
-
" )\n",
|
| 220 |
-
" (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))\n",
|
| 221 |
-
" (classifier): Sequential(\n",
|
| 222 |
-
" (0): Linear(in_features=25088, out_features=4096, bias=True)\n",
|
| 223 |
-
" (1): ReLU(inplace=True)\n",
|
| 224 |
-
" (2): Dropout(p=0.5, inplace=False)\n",
|
| 225 |
-
" (3): Linear(in_features=4096, out_features=4096, bias=True)\n",
|
| 226 |
-
" (4): ReLU(inplace=True)\n",
|
| 227 |
-
" (5): Dropout(p=0.5, inplace=False)\n",
|
| 228 |
-
" (6): Linear(in_features=4096, out_features=3, bias=True)\n",
|
| 229 |
-
" )\n",
|
| 230 |
-
")\n"
|
| 231 |
-
]
|
| 232 |
-
}
|
| 233 |
-
],
|
| 234 |
-
"source": [
|
| 235 |
-
"import torch\n",
|
| 236 |
-
"import torch.nn as nn\n",
|
| 237 |
-
"from torchvision.models import vgg16, VGG16_Weights\n",
|
| 238 |
-
"\n",
|
| 239 |
-
"# Number of classes\n",
|
| 240 |
-
"num_classes = len(train_dataset.classes) # automatically from ImageFolder\n",
|
| 241 |
-
"\n",
|
| 242 |
-
"# Load pretrained VGG16\n",
|
| 243 |
-
"model = vgg16(weights=VGG16_Weights.DEFAULT)\n",
|
| 244 |
-
"\n",
|
| 245 |
-
"# Replace the last classifier layer\n",
|
| 246 |
-
"model.classifier[-1] = nn.Linear(model.classifier[-1].in_features, num_classes)\n",
|
| 247 |
-
"\n",
|
| 248 |
-
"# Move to device\n",
|
| 249 |
-
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
|
| 250 |
-
"model = model.to(device)\n",
|
| 251 |
-
"\n",
|
| 252 |
-
"print(model)\n"
|
| 253 |
-
]
|
| 254 |
-
},
|
| 255 |
-
{
|
| 256 |
-
"cell_type": "code",
|
| 257 |
-
"execution_count": 8,
|
| 258 |
-
"id": "50f8f941-36a6-4986-8221-ce21dd4d1f66",
|
| 259 |
-
"metadata": {},
|
| 260 |
-
"outputs": [
|
| 261 |
-
{
|
| 262 |
-
"name": "stdout",
|
| 263 |
-
"output_type": "stream",
|
| 264 |
-
"text": [
|
| 265 |
-
"Loss, optimizer, and scheduler set up for VGGNet.\n"
|
| 266 |
-
]
|
| 267 |
-
}
|
| 268 |
-
],
|
| 269 |
-
"source": [
|
| 270 |
-
"import torch.optim as optim\n",
|
| 271 |
-
"\n",
|
| 272 |
-
"criterion_vgg = nn.CrossEntropyLoss()\n",
|
| 273 |
-
"optimizer_vgg = optim.AdamW(model.parameters(), lr=1e-4, weight_decay=1e-4)\n",
|
| 274 |
-
"scheduler_vgg = optim.lr_scheduler.CosineAnnealingLR(optimizer_vgg, T_max=10)\n",
|
| 275 |
-
"\n",
|
| 276 |
-
"print(\"Loss, optimizer, and scheduler set up for VGGNet.\")\n"
|
| 277 |
-
]
|
| 278 |
-
},
|
| 279 |
-
{
|
| 280 |
-
"cell_type": "code",
|
| 281 |
-
"execution_count": null,
|
| 282 |
-
"id": "2cfb1d82-e0fd-4276-af78-b6497069a5d5",
|
| 283 |
-
"metadata": {},
|
| 284 |
-
"outputs": [
|
| 285 |
-
{
|
| 286 |
-
"name": "stderr",
|
| 287 |
-
"output_type": "stream",
|
| 288 |
-
"text": [
|
| 289 |
-
"Epoch 1/10 [Train]: 7%|████▎ | 6/88 [01:04<14:35, 10.67s/it]"
|
| 290 |
-
]
|
| 291 |
-
}
|
| 292 |
-
],
|
| 293 |
-
"source": [
|
| 294 |
-
"import matplotlib.pyplot as plt\n",
|
| 295 |
-
"from tqdm import tqdm\n",
|
| 296 |
-
"from IPython.display import clear_output\n",
|
| 297 |
-
"\n",
|
| 298 |
-
"num_epochs = 10\n",
|
| 299 |
-
"train_losses, val_losses = [], []\n",
|
| 300 |
-
"train_accs, val_accs = [], []\n",
|
| 301 |
-
"\n",
|
| 302 |
-
"for epoch in range(num_epochs):\n",
|
| 303 |
-
" # --- Training ---\n",
|
| 304 |
-
" model.train()\n",
|
| 305 |
-
" running_loss, correct, total = 0, 0, 0\n",
|
| 306 |
-
" for images, labels in tqdm(train_loader, desc=f\"Epoch {epoch+1}/{num_epochs} [Train]\"):\n",
|
| 307 |
-
" images, labels = images.to(device), labels.to(device)\n",
|
| 308 |
-
" optimizer_vgg.zero_grad()\n",
|
| 309 |
-
" outputs = model(images)\n",
|
| 310 |
-
" loss = criterion_vgg(outputs, labels)\n",
|
| 311 |
-
" loss.backward()\n",
|
| 312 |
-
" optimizer_vgg.step()\n",
|
| 313 |
-
"\n",
|
| 314 |
-
" running_loss += loss.item()\n",
|
| 315 |
-
" _, predicted = outputs.max(1)\n",
|
| 316 |
-
" total += labels.size(0)\n",
|
| 317 |
-
" correct += predicted.eq(labels).sum().item()\n",
|
| 318 |
-
"\n",
|
| 319 |
-
" avg_train_loss = running_loss / len(train_loader)\n",
|
| 320 |
-
" train_acc = 100. * correct / total\n",
|
| 321 |
-
"\n",
|
| 322 |
-
" # --- Validation ---\n",
|
| 323 |
-
" model.eval()\n",
|
| 324 |
-
" val_loss, val_correct, val_total = 0, 0, 0\n",
|
| 325 |
-
" with torch.no_grad():\n",
|
| 326 |
-
" for images, labels in tqdm(val_loader, desc=f\"Epoch {epoch+1}/{num_epochs} [Val]\"):\n",
|
| 327 |
-
" images, labels = images.to(device), labels.to(device)\n",
|
| 328 |
-
" outputs = model(images)\n",
|
| 329 |
-
" loss = criterion_vgg(outputs, labels)\n",
|
| 330 |
-
" val_loss += loss.item()\n",
|
| 331 |
-
" _, predicted = outputs.max(1)\n",
|
| 332 |
-
" val_total += labels.size(0)\n",
|
| 333 |
-
" val_correct += predicted.eq(labels).sum().item()\n",
|
| 334 |
-
"\n",
|
| 335 |
-
" avg_val_loss = val_loss / len(val_loader)\n",
|
| 336 |
-
" val_acc = 100. * val_correct / val_total\n",
|
| 337 |
-
"\n",
|
| 338 |
-
" # --- Save metrics ---\n",
|
| 339 |
-
" train_losses.append(avg_train_loss)\n",
|
| 340 |
-
" val_losses.append(avg_val_loss)\n",
|
| 341 |
-
" train_accs.append(train_acc)\n",
|
| 342 |
-
" val_accs.append(val_acc)\n",
|
| 343 |
-
"\n",
|
| 344 |
-
" # --- Update scheduler ---\n",
|
| 345 |
-
" scheduler_vgg.step()\n",
|
| 346 |
-
"\n",
|
| 347 |
-
" # --- Epoch summary ---\n",
|
| 348 |
-
" clear_output(wait=True)\n",
|
| 349 |
-
" print(f\"Epoch [{epoch+1}/{num_epochs}] \"\n",
|
| 350 |
-
" f\"Train Loss: {avg_train_loss:.4f}, Train Acc: {train_acc:.2f}% | \"\n",
|
| 351 |
-
" f\"Val Loss: {avg_val_loss:.4f}, Val Acc: {val_acc:.2f}%\")\n",
|
| 352 |
-
"\n",
|
| 353 |
-
" # --- Live plots ---\n",
|
| 354 |
-
" plt.figure(figsize=(10,4))\n",
|
| 355 |
-
" plt.subplot(1,2,1)\n",
|
| 356 |
-
" plt.plot(train_losses, label='Train Loss')\n",
|
| 357 |
-
" plt.plot(val_losses, label='Val Loss')\n",
|
| 358 |
-
" plt.title('Loss')\n",
|
| 359 |
-
" plt.xlabel('Epoch')\n",
|
| 360 |
-
" plt.ylabel('Loss')\n",
|
| 361 |
-
" plt.legend()\n",
|
| 362 |
-
"\n",
|
| 363 |
-
" plt.subplot(1,2,2)\n",
|
| 364 |
-
" plt.plot(train_accs, label='Train Acc')\n",
|
| 365 |
-
" plt.plot(val_accs, label='Val Acc')\n",
|
| 366 |
-
" plt.title('Accuracy')\n",
|
| 367 |
-
" plt.xlabel('Epoch')\n",
|
| 368 |
-
" plt.ylabel('Accuracy (%)')\n",
|
| 369 |
-
" plt.legend()\n",
|
| 370 |
-
"\n",
|
| 371 |
-
" plt.tight_layout()\n",
|
| 372 |
-
" plt.show()\n"
|
| 373 |
-
]
|
| 374 |
-
},
|
| 375 |
-
{
|
| 376 |
-
"cell_type": "code",
|
| 377 |
-
"execution_count": null,
|
| 378 |
-
"id": "69f51f76-a3dd-4a2b-9ec1-8ee77480f019",
|
| 379 |
-
"metadata": {},
|
| 380 |
-
"outputs": [],
|
| 381 |
-
"source": [
|
| 382 |
-
"import torch\n",
|
| 383 |
-
"import matplotlib.pyplot as plt\n",
|
| 384 |
-
"import seaborn as sns\n",
|
| 385 |
-
"from sklearn.metrics import confusion_matrix, classification_report\n",
|
| 386 |
-
"from tqdm import tqdm\n",
|
| 387 |
-
"import numpy as np\n",
|
| 388 |
-
"\n",
|
| 389 |
-
"# --- 1. Save the trained model ---\n",
|
| 390 |
-
"torch.save(model_shufflenet.state_dict(), \"vggnet_sickle.pth\")\n",
|
| 391 |
-
"print(\"✅ Model saved as vggenet_sickle.pth\")\n",
|
| 392 |
-
"\n",
|
| 393 |
-
"\n",
|
| 394 |
-
"# --- 2. Evaluation function with predictions ---\n",
|
| 395 |
-
"def evaluate_with_preds(model, data_loader, device, split_name=\"Eval\"):\n",
|
| 396 |
-
" model.eval()\n",
|
| 397 |
-
" total, correct = 0, 0\n",
|
| 398 |
-
" all_preds, all_labels = [], []\n",
|
| 399 |
-
" with torch.no_grad():\n",
|
| 400 |
-
" for images, labels in tqdm(data_loader, desc=f\"Evaluating {split_name}\"):\n",
|
| 401 |
-
" images, labels = images.to(device), labels.to(device)\n",
|
| 402 |
-
" outputs = model(images)\n",
|
| 403 |
-
" _, predicted = outputs.max(1)\n",
|
| 404 |
-
"\n",
|
| 405 |
-
" total += labels.size(0)\n",
|
| 406 |
-
" correct += predicted.eq(labels).sum().item()\n",
|
| 407 |
-
"\n",
|
| 408 |
-
" all_preds.extend(predicted.cpu().numpy())\n",
|
| 409 |
-
" all_labels.extend(labels.cpu().numpy())\n",
|
| 410 |
-
"\n",
|
| 411 |
-
" accuracy = 100. * correct / total\n",
|
| 412 |
-
" return accuracy, np.array(all_preds), np.array(all_labels)\n",
|
| 413 |
-
"\n",
|
| 414 |
-
"\n",
|
| 415 |
-
"# --- 3. Run evaluation on validation set ---\n",
|
| 416 |
-
"val_acc, preds, labels = evaluate_with_preds(model, val_loader, device, split_name=\"Val\")\n",
|
| 417 |
-
"\n",
|
| 418 |
-
"print(f\"\\n📊 Final Validation Accuracy: {val_acc:.2f}%\")\n",
|
| 419 |
-
"print(\"\\nClassification Report:\")\n",
|
| 420 |
-
"print(classification_report(labels, preds, target_names=dataset[\"train\"].features[\"label\"].names))\n",
|
| 421 |
-
"\n",
|
| 422 |
-
"\n",
|
| 423 |
-
"# --- 4. Confusion matrix heatmap ---\n",
|
| 424 |
-
"cm = confusion_matrix(labels, preds)\n",
|
| 425 |
-
"plt.figure(figsize=(6, 5))\n",
|
| 426 |
-
"sns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\",\n",
|
| 427 |
-
" xticklabels=dataset[\"train\"].features[\"label\"].names,\n",
|
| 428 |
-
" yticklabels=dataset[\"train\"].features[\"label\"].names)\n",
|
| 429 |
-
"plt.xlabel(\"Predicted\")\n",
|
| 430 |
-
"plt.ylabel(\"True\")\n",
|
| 431 |
-
"plt.title(\"ShuffleNet Confusion Matrix\")\n",
|
| 432 |
-
"plt.show()\n"
|
| 433 |
-
]
|
| 434 |
-
},
|
| 435 |
-
{
|
| 436 |
-
"cell_type": "code",
|
| 437 |
-
"execution_count": null,
|
| 438 |
-
"id": "854aedac-9b8d-4abc-843d-9b7ada1ee72f",
|
| 439 |
-
"metadata": {},
|
| 440 |
-
"outputs": [],
|
| 441 |
-
"source": []
|
| 442 |
-
}
|
| 443 |
-
],
|
| 444 |
-
"metadata": {
|
| 445 |
-
"kernelspec": {
|
| 446 |
-
"display_name": "Python 3 (ipykernel)",
|
| 447 |
-
"language": "python",
|
| 448 |
-
"name": "python3"
|
| 449 |
-
},
|
| 450 |
-
"language_info": {
|
| 451 |
-
"codemirror_mode": {
|
| 452 |
-
"name": "ipython",
|
| 453 |
-
"version": 3
|
| 454 |
-
},
|
| 455 |
-
"file_extension": ".py",
|
| 456 |
-
"mimetype": "text/x-python",
|
| 457 |
-
"name": "python",
|
| 458 |
-
"nbconvert_exporter": "python",
|
| 459 |
-
"pygments_lexer": "ipython3",
|
| 460 |
-
"version": "3.9.23"
|
| 461 |
-
}
|
| 462 |
-
},
|
| 463 |
-
"nbformat": 4,
|
| 464 |
-
"nbformat_minor": 5
|
| 465 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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