{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\"> Project:- Data Augmentation using CIFAR10</p>","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\"> DataSet Discription</p>\n\n**The CIFAR-10 dataset comprises 60,000 32x32 color images categorized into 10 classes, with each class having 6,000 images. Among these, 50,000 images are for training and 10,000 for testing, maintaining the original split. The dataset includes:**\n* * train.7z: A folder with training images in PNG format.\n* * test.7z: A folder with test images in PNG format.\n* * trainLabels.csv: File containing training labels.\n\n**To prevent cheating, 290,000 \"junk\" images were added to the test set, disregarded during scoring. Also, slight modifications were made to the original 10,000 test images to prevent identification via file hash. These alterations should not significantly affect scoring. Predictions are required for all 300,000 images. The classes include:**\n\n* Airplane\n* Automobile\n* Bird\n* Cat\n* Deer\n* Dog\n* Frog\n* Horse\n* Ship\n* Truck\n\n**Classes are mutually exclusive with no overlap; for instance, \"automobile\" encompasses sedans and SUVs, while \"truck\" refers to large trucks excluding pickups**","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">About me </p>\n\n### ***👋 Greetings, all! I'm <strong>Mohsin Saleem<strong>, a data scientist that is interested about mastering <strong>machine learning<strong> techniques and continually extending his expertise. I believe in the mindset of continuous learning and support because I am dedicated to the continued development and support of others in the profession***\n##  <span style=\"color: #EEBB6D;\"></span> <span style=\"color: orange; font-weight: bold;\"> Connect with me :</span>\n[![GitHub](https://img.shields.io/badge/GitHub-Profile-blue?style=for-the-badge&logo=github)](https://github.com/Mohsin-Sial099) \n\n[![Kaggle](https://img.shields.io/badge/Kaggle-Profile-blue?style=for-the-badge&logo=kaggle)](https://www.kaggle.com/mohsinsial) \n\n[![LinkedIn](https://img.shields.io/badge/LinkedIn-Profile-blue?style=for-the-badge&logo=linkedin)](https://www.linkedin.com/in/mohsinsial/)","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\"> Loading Neccessary Libraries</p>","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torchvision\nimport tarfile\nimport torch.nn as nn\nimport numpy as np\nimport torch.nn.functional as F\nfrom torchvision.datasets.utils import download_url\nfrom torchvision.datasets import ImageFolder\nfrom torch.utils.data import DataLoader\nimport torchvision.transforms as tt\nfrom torch.utils.data import random_split\nfrom torchvision.utils import make_grid\nimport matplotlib\nimport matplotlib.pyplot as plt\n%matplotlib inline\nmatplotlib.rcParams['figure.facecolor'] = '#ffffff'","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:30:46.099668Z","iopub.execute_input":"2024-05-18T03:30:46.100038Z","iopub.status.idle":"2024-05-18T03:30:52.747855Z","shell.execute_reply.started":"2024-05-18T03:30:46.099987Z","shell.execute_reply":"2024-05-18T03:30:52.747059Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Downloading Dataset</p>","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-18T03:31:18.915963Z","iopub.execute_input":"2024-05-18T03:31:18.916969Z","iopub.status.idle":"2024-05-18T03:31:19.236376Z","shell.execute_reply.started":"2024-05-18T03:31:18.916935Z","shell.execute_reply":"2024-05-18T03:31:19.235417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Given Project Name</p>","metadata":{}},{"cell_type":"code","source":"project_name='05b-cifar10-resnet'","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:31:20.171313Z","iopub.execute_input":"2024-05-18T03:31:20.171826Z","iopub.status.idle":"2024-05-18T03:31:20.176198Z","shell.execute_reply.started":"2024-05-18T03:31:20.171794Z","shell.execute_reply":"2024-05-18T03:31:20.175235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:31:52.667492Z","iopub.execute_input":"2024-05-18T03:31:52.667850Z","iopub.status.idle":"2024-05-18T03:32:18.523101Z","shell.execute_reply.started":"2024-05-18T03:31:52.667823Z","shell.execute_reply":"2024-05-18T03:32:18.521884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\"> Loading DataSet</p>","metadata":{}},{"cell_type":"code","source":"import py7zr\nimport os\ntrain_archive_path = '/kaggle/input/cifar-10/train.7z'\ntest_archive_path = '/kaggle/input/cifar-10/test.7z'\nextracted_train_path = '/kaggle/working/cifar-10/train'\nextracted_test_path = '/kaggle/working/cifar-10/test'\n\nwith py7zr.SevenZipFile(train_archive_path, mode='r') as z:\n    z.extractall(path=extracted_train_path)\n\nwith py7zr.SevenZipFile(test_archive_path, mode='r') as z:\n    z.extractall(path=extracted_test_path)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:32:18.524882Z","iopub.execute_input":"2024-05-18T03:32:18.525175Z","iopub.status.idle":"2024-05-18T03:35:53.344596Z","shell.execute_reply.started":"2024-05-18T03:32:18.525148Z","shell.execute_reply":"2024-05-18T03:35:53.343765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Dataset Transformation</p>\n\n**I did Applied random cropping, horizontal flipping, tensor conversion, and normalization with provided statistics to prepare training and validation sets for CIFAR10**","metadata":{}},{"cell_type":"code","source":"stats = ((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))\ntrain_tfm = tt.Compose([tt.RandomCrop(32, padding=4, padding_mode='reflect'),\n                        tt.RandomHorizontalFlip(),\n                        tt.ToTensor(),\n                        tt.Normalize(*stats, inplace=True)])\nval_tfm = tt.Compose([tt.ToTensor(), tt.Normalize(*stats)])","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:35:53.346489Z","iopub.execute_input":"2024-05-18T03:35:53.346841Z","iopub.status.idle":"2024-05-18T03:35:53.353597Z","shell.execute_reply.started":"2024-05-18T03:35:53.346808Z","shell.execute_reply":"2024-05-18T03:35:53.352479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">ImageFolder for training and validation</p>","metadata":{}},{"cell_type":"code","source":"train_ds = ImageFolder(extracted_train_path, transform=train_tfm)\nval_ds = ImageFolder(extracted_test_path, transform=val_tfm)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:35:53.354663Z","iopub.execute_input":"2024-05-18T03:35:53.354918Z","iopub.status.idle":"2024-05-18T03:35:55.100795Z","shell.execute_reply.started":"2024-05-18T03:35:53.354895Z","shell.execute_reply":"2024-05-18T03:35:55.100032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 400","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:35:55.102465Z","iopub.execute_input":"2024-05-18T03:35:55.102765Z","iopub.status.idle":"2024-05-18T03:35:55.106866Z","shell.execute_reply.started":"2024-05-18T03:35:55.102739Z","shell.execute_reply":"2024-05-18T03:35:55.105880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Training PyTorch DataLoader\"</p>\n\n**In order to improve performance, I have used the DataLoader from PyTorch for a more efficient handling of training and validation data, as well as movement and concurrent loading.**","metadata":{}},{"cell_type":"code","source":"train_dl = DataLoader(train_ds, batch_size, shuffle=True, num_workers=3, pin_memory=True)\nval_dl = DataLoader(val_ds, batch_size*2, num_workers=3,pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:11.497954Z","iopub.execute_input":"2024-05-18T03:36:11.498679Z","iopub.status.idle":"2024-05-18T03:36:11.503662Z","shell.execute_reply.started":"2024-05-18T03:36:11.498641Z","shell.execute_reply":"2024-05-18T03:36:11.502599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Visualation Batches of Images </p>","metadata":{}},{"cell_type":"code","source":"def show_batch(dl):\n  for images, labels in dl:\n    fig, ax = plt.subplots(figsize=(7,5))\n    ax.set_xticks([]) ; ax.set_yticks([])\n    ax.imshow(make_grid(images[:64], nrow=8).permute(1,2,0))\n    break","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:11.996131Z","iopub.execute_input":"2024-05-18T03:36:11.996795Z","iopub.status.idle":"2024-05-18T03:36:12.002554Z","shell.execute_reply.started":"2024-05-18T03:36:11.996765Z","shell.execute_reply":"2024-05-18T03:36:12.001578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_batch(train_dl)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:12.227054Z","iopub.execute_input":"2024-05-18T03:36:12.227811Z","iopub.status.idle":"2024-05-18T03:36:13.577928Z","shell.execute_reply.started":"2024-05-18T03:36:12.227783Z","shell.execute_reply":"2024-05-18T03:36:13.576893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Device Assignment </p>\n\n**In order to ensure that PyTorch runs efficiently on available hardware, I've defined functions for checking and assigning the correct deviceCPU or GPU To run CiFAR10 dataset**","metadata":{}},{"cell_type":"code","source":"def get_default_device():\n    if torch.cuda.is_available():\n        return torch.device('cuda')\n    else:\n        return torch.device('cpu')\n    \ndef to_device(data, device):\n    if isinstance(data, (list,tuple)):\n        return [to_device(x, device) for x in data]\n    return data.to(device, non_blocking=True)\n\nclass DeviceDataLoader():\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        for b in self.dl: \n            yield to_device(b, self.device)\n\n    def __len__(self):\n        return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:13.579662Z","iopub.execute_input":"2024-05-18T03:36:13.579961Z","iopub.status.idle":"2024-05-18T03:36:13.590058Z","shell.execute_reply.started":"2024-05-18T03:36:13.579932Z","shell.execute_reply":"2024-05-18T03:36:13.589128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = get_default_device()\ndevice","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:13.964151Z","iopub.execute_input":"2024-05-18T03:36:13.964774Z","iopub.status.idle":"2024-05-18T03:36:13.970868Z","shell.execute_reply.started":"2024-05-18T03:36:13.964746Z","shell.execute_reply":"2024-05-18T03:36:13.969880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Integration of DeviceDataLoader</p>","metadata":{}},{"cell_type":"code","source":"train_dl = DeviceDataLoader(train_dl, device)\nval_dl = DeviceDataLoader(val_dl, device)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:14.268431Z","iopub.execute_input":"2024-05-18T03:36:14.269106Z","iopub.status.idle":"2024-05-18T03:36:14.273569Z","shell.execute_reply.started":"2024-05-18T03:36:14.269073Z","shell.execute_reply":"2024-05-18T03:36:14.272509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Implementation of Simple Residual Block</p>","metadata":{}},{"cell_type":"code","source":"class SimpleResidualBlock(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3, stride=1, padding=1)\n        self.relu1 = nn.ReLU()\n        self.conv2 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3, stride=1, padding=1)\n        self.relu2 = nn.ReLU()\n        \n    def forward(self, x):\n        out = self.conv1(x)\n        out = self.relu1(out)\n        out = self.conv2(out)\n        return self.relu2(out) + x ","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:14.542536Z","iopub.execute_input":"2024-05-18T03:36:14.543262Z","iopub.status.idle":"2024-05-18T03:36:14.550104Z","shell.execute_reply.started":"2024-05-18T03:36:14.543231Z","shell.execute_reply":"2024-05-18T03:36:14.549052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Applying Simple ResNet</p>\n\n**I did Utilized a custom SimpleResidualBlock model on the CIFAR-10 dataset, verifying output shapes for a single batch, optimizing memory usage by releasing variables and clearing CUDA cache**","metadata":{}},{"cell_type":"code","source":"simple_resnet = to_device(SimpleResidualBlock(), device)\n\nfor images, labels in train_dl:\n    out = simple_resnet(images)\n    print(out.shape)\n    break\n    \ndel simple_resnet, images, labels\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:14.858667Z","iopub.execute_input":"2024-05-18T03:36:14.859060Z","iopub.status.idle":"2024-05-18T03:36:16.189488Z","shell.execute_reply.started":"2024-05-18T03:36:14.859023Z","shell.execute_reply":"2024-05-18T03:36:16.188082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">training and validation of Image Classification</p>","metadata":{}},{"cell_type":"code","source":"def accuracy(outputs, labels):\n    _, preds = torch.max(outputs, dim=1)\n    return torch.tensor(torch.sum(preds == labels).item() / len(preds))\n\nclass ImageClassificationBase(nn.Module):\n    def training_step(self, batch):\n        images, labels = batch \n        out = self(images)\n        loss = F.cross_entropy(out, labels)\n        return loss\n    \n    def validation_step(self, batch):\n        images, labels = batch \n        out = self(images)                   \n        loss = F.cross_entropy(out, labels)\n        acc = accuracy(out, labels) \n        return {'val_loss': loss.detach(), 'val_acc': acc}\n        \n    def validation_epoch_end(self, outputs):\n        batch_losses = [x['val_loss'] for x in outputs]\n        epoch_loss = torch.stack(batch_losses).mean()  \n        batch_accs = [x['val_acc'] for x in outputs]\n        epoch_acc = torch.stack(batch_accs).mean()\n        return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}\n    \n    def epoch_end(self, epoch, result):\n        print(\"Epoch [{}], last_lr: {:.5f}, train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}\".format(\n            epoch, result['lrs'][-1], result['train_loss'], result['val_loss'], result['val_acc']))","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:16.191837Z","iopub.execute_input":"2024-05-18T03:36:16.192165Z","iopub.status.idle":"2024-05-18T03:36:16.202789Z","shell.execute_reply.started":"2024-05-18T03:36:16.192139Z","shell.execute_reply":"2024-05-18T03:36:16.201725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\"> Applying ResNet9 Architecture</p>","metadata":{}},{"cell_type":"code","source":"def conv_block(in_channels, out_channels, pool=False):\n    layers = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), \n              nn.BatchNorm2d(out_channels), \n              nn.ReLU(inplace=True)]\n    if pool: layers.append(nn.MaxPool2d(2))\n    return nn.Sequential(*layers)\n\nclass ResNet9(ImageClassificationBase):\n    def __init__(self, in_channels, num_classes):\n        super().__init__()\n        \n        self.conv1 = conv_block(in_channels, 64)\n        self.conv2 = conv_block(64, 128, pool=True)\n        self.res1 = nn.Sequential(conv_block(128, 128), conv_block(128, 128))\n        \n        self.conv3 = conv_block(128, 256, pool=True)\n        self.conv4 = conv_block(256, 512, pool=True)\n        self.res2 = nn.Sequential(conv_block(512, 512), conv_block(512, 512))\n        \n        self.classifier = nn.Sequential(nn.MaxPool2d(4), \n                                        nn.Flatten(), \n                                        nn.Dropout(0.2),\n                                        nn.Linear(512, num_classes))\n        \n    def forward(self, xb):\n        out = self.conv1(xb)\n        out = self.conv2(out)\n        out = self.res1(out) + out\n        out = self.conv3(out)\n        out = self.conv4(out)\n        out = self.res2(out) + out\n        out = self.classifier(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:16.203826Z","iopub.execute_input":"2024-05-18T03:36:16.204107Z","iopub.status.idle":"2024-05-18T03:36:16.215381Z","shell.execute_reply.started":"2024-05-18T03:36:16.204084Z","shell.execute_reply":"2024-05-18T03:36:16.214434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = to_device(ResNet9(3, 10), device)\nmodel","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:16.375995Z","iopub.execute_input":"2024-05-18T03:36:16.376897Z","iopub.status.idle":"2024-05-18T03:36:16.465095Z","shell.execute_reply.started":"2024-05-18T03:36:16.376856Z","shell.execute_reply":"2024-05-18T03:36:16.463956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">Training with One-Cycle Learning Rate</p>","metadata":{}},{"cell_type":"code","source":"@torch.no_grad()\ndef evaluate(model, val_loader):\n    model.eval()\n    outputs = [model.validation_step(batch) for batch in val_loader]\n    return model.validation_epoch_end(outputs)\n\ndef get_lr(optimizer):\n    for param_group in optimizer.param_groups:\n        return param_group['lr']\n\ndef fit_one_cycle(epochs, max_lr, model, train_loader, val_loader, \n                  weight_decay=0, grad_clip=None, opt_func=torch.optim.SGD):\n    torch.cuda.empty_cache()\n    history = []\n    \n    optimizer = opt_func(model.parameters(), max_lr, weight_decay=weight_decay)\n    sched = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr, epochs=epochs, \n                                                steps_per_epoch=len(train_loader))\n    \n    for epoch in range(epochs):\n        model.train()\n        train_losses = []\n        lrs = []\n        for batch in train_loader:\n            loss = model.training_step(batch)\n            train_losses.append(loss)\n            loss.backward()\n            \n            if grad_clip: \n                nn.utils.clip_grad_value_(model.parameters(), grad_clip)\n            \n            optimizer.step()\n            optimizer.zero_grad()\n            \n            lrs.append(get_lr(optimizer))\n            sched.step()\n        \n        result = evaluate(model, val_loader)\n        result['train_loss'] = torch.stack(train_losses).mean().item()\n        result['lrs'] = lrs\n        model.epoch_end(epoch, result)\n        history.append(result)\n    return history","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:16.705213Z","iopub.execute_input":"2024-05-18T03:36:16.706099Z","iopub.status.idle":"2024-05-18T03:36:16.717078Z","shell.execute_reply.started":"2024-05-18T03:36:16.706060Z","shell.execute_reply":"2024-05-18T03:36:16.716068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = [evaluate(model, val_dl)]\nhistory","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:36:17.153160Z","iopub.execute_input":"2024-05-18T03:36:17.153818Z","iopub.status.idle":"2024-05-18T03:37:19.925179Z","shell.execute_reply.started":"2024-05-18T03:36:17.153791Z","shell.execute_reply":"2024-05-18T03:37:19.924076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 8\nmax_lr = 0.01\ngrad_clip = 0.1\nweight_decay = 1e-4\nopt_func = torch.optim.Adam","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:37:19.927124Z","iopub.execute_input":"2024-05-18T03:37:19.927411Z","iopub.status.idle":"2024-05-18T03:37:19.933731Z","shell.execute_reply.started":"2024-05-18T03:37:19.927384Z","shell.execute_reply":"2024-05-18T03:37:19.933025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nhistory+= fit_one_cycle(epochs, max_lr, model, train_dl, val_dl, \n                        grad_clip=grad_clip, weight_decay=weight_decay,\n                        opt_func=opt_func)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:37:19.935056Z","iopub.execute_input":"2024-05-18T03:37:19.935385Z","iopub.status.idle":"2024-05-18T03:48:06.904093Z","shell.execute_reply.started":"2024-05-18T03:37:19.935357Z","shell.execute_reply":"2024-05-18T03:48:06.902949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\">visualization of Accuracy Plotter</p>","metadata":{}},{"cell_type":"code","source":"def plot_accuracies(history):\n  accuracies = [x['val_acc'] for x in history]\n  plt.plot(accuracies, '-x')\n  plt.xlabel('epoch')\n  plt.ylabel('accuracy')\n  plt.title('Accuracy vs No. of Epochs')","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:55:18.464122Z","iopub.execute_input":"2024-05-18T03:55:18.464972Z","iopub.status.idle":"2024-05-18T03:55:18.470608Z","shell.execute_reply.started":"2024-05-18T03:55:18.464936Z","shell.execute_reply":"2024-05-18T03:55:18.469566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_accuracies(history)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:55:31.960895Z","iopub.execute_input":"2024-05-18T03:55:31.961286Z","iopub.status.idle":"2024-05-18T03:55:32.275547Z","shell.execute_reply.started":"2024-05-18T03:55:31.961258Z","shell.execute_reply":"2024-05-18T03:55:32.274635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_losses(history):\n    train_losses = [x.get('train_loss') for x in history]\n    val_losses = [x['val_loss'] for x in history]\n    plt.plot(train_losses, '-bx')\n    plt.plot(val_losses, '-rx')\n    plt.xlabel('epoch')\n    plt.ylabel('loss')\n    plt.legend(['Training', 'Validation'])\n    plt.title('Loss vs. No. of epochs');","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:55:38.294727Z","iopub.execute_input":"2024-05-18T03:55:38.295440Z","iopub.status.idle":"2024-05-18T03:55:38.301477Z","shell.execute_reply.started":"2024-05-18T03:55:38.295411Z","shell.execute_reply":"2024-05-18T03:55:38.300493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_losses(history)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:55:43.591049Z","iopub.execute_input":"2024-05-18T03:55:43.591443Z","iopub.status.idle":"2024-05-18T03:55:43.873773Z","shell.execute_reply.started":"2024-05-18T03:55:43.591414Z","shell.execute_reply":"2024-05-18T03:55:43.872940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_lrs(history):\n  lrs = np.concatenate([x.get('lrs', []) for x in history])\n  plt.plot(lrs)\n  plt.xlabel('Batch No')\n  plt.ylabel('Learning Rate')\n  plt.title('Learning Rate vs Batch Size')","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:55:50.943640Z","iopub.execute_input":"2024-05-18T03:55:50.944666Z","iopub.status.idle":"2024-05-18T03:55:50.950117Z","shell.execute_reply.started":"2024-05-18T03:55:50.944627Z","shell.execute_reply":"2024-05-18T03:55:50.949194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_lrs(history)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:56:05.557680Z","iopub.execute_input":"2024-05-18T03:56:05.558575Z","iopub.status.idle":"2024-05-18T03:56:05.848868Z","shell.execute_reply.started":"2024-05-18T03:56:05.558542Z","shell.execute_reply":"2024-05-18T03:56:05.847902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:Algerian;font-size:150%;text-align:center;color:#0F18BA;\"> Conclusion</p>","metadata":{}},{"cell_type":"code","source":"def pred_images(img, model):\n  xb = to_device(img.unsqueeze(0), device)\n  yb = model(xb)\n  _, preds = torch.max(yb, dim=1)\n  return train_ds.classes[preds[0].item()]","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:56:15.502220Z","iopub.execute_input":"2024-05-18T03:56:15.502559Z","iopub.status.idle":"2024-05-18T03:56:15.507984Z","shell.execute_reply.started":"2024-05-18T03:56:15.502535Z","shell.execute_reply":"2024-05-18T03:56:15.507028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = val_ds[2476]\nplt.imshow(img.permute(1,2,0).clamp(0,1))\nprint('Label:', train_ds.classes[label], ', Predicted:', pred_images(img, model))","metadata":{"execution":{"iopub.status.busy":"2024-05-18T03:56:21.637001Z","iopub.execute_input":"2024-05-18T03:56:21.637426Z","iopub.status.idle":"2024-05-18T03:56:21.946187Z","shell.execute_reply.started":"2024-05-18T03:56:21.637397Z","shell.execute_reply":"2024-05-18T03:56:21.945274Z"},"trusted":true},"execution_count":null,"outputs":[]}]}