{"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":71608,"databundleVersionId":7895811,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"4e6d4974-3ec9-4ec1-ae0d-92fb7017acad","_cell_guid":"01cf9d2b-07fe-4cb8-9095-0df4d9771211","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:42:05.677138Z","iopub.status.idle":"2024-03-15T04:42:08.658138Z","shell.execute_reply.started":"2024-03-15T04:42:05.677521Z","shell.execute_reply":"2024-03-15T04:42:08.657107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"_uuid":"5f3a10f3-7ee4-46a8-8a48-b686a5c98b4e","_cell_guid":"06f166a0-3f4a-471f-b298-62eebc98e83b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:42:08.659894Z","iopub.execute_input":"2024-03-15T04:42:08.660205Z","iopub.status.idle":"2024-03-15T04:42:08.665796Z","shell.execute_reply.started":"2024-03-15T04:42:08.660178Z","shell.execute_reply":"2024-03-15T04:42:08.664803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ground_truth = pd.read_csv('/kaggle/input/aiml-general-championship/KCDH2024_Training_GroundTruth.csv')\nground_truth.head()\nground_truth.describe()","metadata":{"_uuid":"d3ea7f27-c107-4f9f-9e6f-f186adf20aee","_cell_guid":"6b440957-6759-4c98-b195-93003a4102e0","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:42:08.666830Z","iopub.execute_input":"2024-03-15T04:42:08.667105Z","iopub.status.idle":"2024-03-15T04:42:08.729501Z","shell.execute_reply.started":"2024-03-15T04:42:08.667082Z","shell.execute_reply":"2024-03-15T04:42:08.728534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LesionGrouping = pd.read_csv('/kaggle/input/aiml-general-championship/KCDH2024_Training_GroundTruth.csv')\nLesionGrouping.head()","metadata":{"_uuid":"b6b2c3a9-b12d-427b-9546-865756be1bfa","_cell_guid":"1fe46af2-28d7-4f06-b8f2-6eba5718d029","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:42:08.730714Z","iopub.execute_input":"2024-03-15T04:42:08.731006Z","iopub.status.idle":"2024-03-15T04:42:08.758420Z","shell.execute_reply.started":"2024-03-15T04:42:08.730967Z","shell.execute_reply":"2024-03-15T04:42:08.757492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport os\n\n# Specify the directory of the input training images\nimage_dir = '/kaggle/input/aiml-general-championship/KCDH2024_Training_Input_10K/KCDH2024_Training_Input_10K'\n\n# Initialize a list to store the images\nimages = []\n\n# Iterate over each image in the directory\nfor image_name in os.listdir(image_dir):\n    try:\n        # Open the image and append it to the list\n        img = Image.open(os.path.join(image_dir, image_name))\n        images.append(img)\n    except Exception as e:\n        print(f\"Error reading image file: {image_name}\")\n        print(e)","metadata":{"_uuid":"78bab52f-2e3c-4a7c-9490-b7eb8077f432","_cell_guid":"7dbd7cf2-d4cf-49c0-823e-09205ac89c45","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:42:08.760507Z","iopub.execute_input":"2024-03-15T04:42:08.760793Z","iopub.status.idle":"2024-03-15T04:42:57.395498Z","shell.execute_reply.started":"2024-03-15T04:42:08.760768Z","shell.execute_reply":"2024-03-15T04:42:57.394717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport os\nfrom PIL import Image\n\n# Define the directory where the images are stored\nimage_dir = '/kaggle/input/aiml-general-championship/KCDH2024_Training_Input_10K/KCDH2024_Training_Input_10K'\n\n# Get a list of all the image filenames\nimage_filenames = os.listdir(image_dir)\n\nprint(f\"Number of images: {len(image_filenames)}\")\nprint(f\"First 5 images: {image_filenames[:5]}\")\nimage_filenames = [i.rstrip('.jpg') for i in image_filenames]\nprint(image_filenames[:])\nprint(len(image_filenames))\nprint(len(images))","metadata":{"_uuid":"b1c50174-2d0f-440f-aa15-1ca0423d5891","_cell_guid":"6ffc588f-bbc8-49cd-b6aa-a3a8ce686d19","collapsed":true,"jupyter":{"outputs_hidden":true},"execution":{"iopub.status.busy":"2024-03-15T04:42:57.396879Z","iopub.execute_input":"2024-03-15T04:42:57.397239Z","iopub.status.idle":"2024-03-15T04:42:57.414007Z","shell.execute_reply.started":"2024-03-15T04:42:57.397207Z","shell.execute_reply":"2024-03-15T04:42:57.413165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig, axs = plt.subplots(1, 5, figsize=(20, 20))\n\nfor i, img in enumerate(images[:5]):\n    axs[i].imshow(img)\n\n\nplt.show()","metadata":{"_uuid":"76c13353-1a36-47de-a0ae-057d6a99b2ae","_cell_guid":"892fece5-3fae-4671-b2eb-604559e83256","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:42:57.415186Z","iopub.execute_input":"2024-03-15T04:42:57.415475Z","iopub.status.idle":"2024-03-15T04:42:58.640979Z","shell.execute_reply.started":"2024-03-15T04:42:57.415452Z","shell.execute_reply":"2024-03-15T04:42:58.640097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nimport tarfile\nfrom torchvision.datasets.utils import download_url\nfrom torch.utils.data import random_split\nfrom torchvision.datasets import ImageFolder\nfrom torchvision.transforms import ToTensor","metadata":{"_uuid":"273b0fd5-ea47-44d2-8eab-bb6fcf0ea876","_cell_guid":"69eee508-d7ee-4090-b692-9be7022c5dc8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:06.942214Z","iopub.execute_input":"2024-03-15T04:44:06.942906Z","iopub.status.idle":"2024-03-15T04:44:06.948174Z","shell.execute_reply.started":"2024-03-15T04:44:06.942877Z","shell.execute_reply":"2024-03-15T04:44:06.947076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Preprocessing Image\nimport torch\nimport torchvision.transforms as transforms\nfrom PIL import Image\nfrom torchvision import models\n\n# # Load pre-trained segmentation model (DeepLabV3)\n# segmentation_model = models.segmentation.deeplabv3_resnet101(pretrained=True)\n# segmentation_model.eval()\n\n# # Define transformations for preprocessing the image\n# preprocess = transforms.Compose([\n#     transforms.Resize((256, 256)),  # Resize the image to a fixed size\n#     transforms.ToTensor(),\n#     transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n# ])\n\n# # Function to perform segmentation on an image\n# def segment_image(image):\n#     input_tensor = preprocess(image)\n#     input_batch = input_tensor.unsqueeze(0)  # Add batch dimension\n#     with torch.no_grad():\n#         output = segmentation_model(input_batch)['out'][0]\n#     return output\n\n# # Function to apply mask to the original image\n# def apply_mask(image, mask):\n#     mask = mask.unsqueeze(0)\n#     mask = torch.nn.functional.interpolate(mask, size=image.size, mode='bilinear', align_corners=False)\n#     mask = mask.squeeze(0)\n#     return image * mask\n\n# # Custom transform function\n# def custom_transform(image_path):\n#     image = Image.open(image_path).convert('RGB')\n#     lesion_mask = segment_image(image)\n#     segmented_image = apply_mask(image, lesion_mask)\n    \n#     # Convert segmented image to tensor\n#     segmented_image_tensor = transforms.ToTensor()(segmented_image)\n\n#     return segmented_image_tensor\n\n\n# Custom transform function\n# def custom_transform(image_path):\n#     # Open the image\n#     image = Image.open(image_path).convert('RGB')\n    \n#     # Resize the image\n#     resized_image = transforms.Resize((256, 256))(image)\n    \n#     # Convert the resized image to a tensor\n#     resized_image_tensor = transforms.ToTensor()(resized_image)\n\n#     return resized_image_tensor\n\n# def custom_transform(image_path):\n#     image = Image.open(image_path).convert('RGB')\n    \n#     # Convert image to tensor\n#     image_tensor = transforms.ToTensor()(image)\n\n#     return image_tensor","metadata":{"_uuid":"ea7c4ae6-2d91-4bc4-9e7d-3710ca608b78","_cell_guid":"ac3ce2fc-b478-4643-b0fc-26f6309cf650","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:08.156116Z","iopub.execute_input":"2024-03-15T04:44:08.156504Z","iopub.status.idle":"2024-03-15T04:44:08.163993Z","shell.execute_reply.started":"2024-03-15T04:44:08.156473Z","shell.execute_reply":"2024-03-15T04:44:08.162658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the transformation function\ncustom_transform = custom_transform = transforms.Compose([\n    transforms.Resize((256, 256)),  # Resize the image\n    transforms.ToTensor()  # Convert the image to a tensor\n])\n\n\n# Create the dataset with the custom transformation\ndataset = ImageFolder(root='/kaggle/input/aiml-general-championship/KCDH2024_Training_Input_10K', transform=custom_transform)","metadata":{"_uuid":"d1cf690b-16b2-4938-9359-5bc9ce81fdfe","_cell_guid":"291f9fd4-f8d1-4592-bbe0-d9cac87eaf3d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:14.317577Z","iopub.execute_input":"2024-03-15T04:44:14.318327Z","iopub.status.idle":"2024-03-15T04:44:15.968017Z","shell.execute_reply.started":"2024-03-15T04:44:14.318297Z","shell.execute_reply":"2024-03-15T04:44:15.967177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = dataset[0]\nprint(img.shape, label)\nimg","metadata":{"_uuid":"75a8b9a0-c6c7-4be8-afef-fe73c65f7e3b","_cell_guid":"75704f7b-e61a-46f1-a4c7-3538fec9d6f9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:17.753164Z","iopub.execute_input":"2024-03-15T04:44:17.753505Z","iopub.status.idle":"2024-03-15T04:44:17.774702Z","shell.execute_reply.started":"2024-03-15T04:44:17.753480Z","shell.execute_reply":"2024-03-15T04:44:17.773727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nprint(dataset.classes)\n%matplotlib inline\n\nplt.rcParams['figure.facecolor'] = '#ffffff'","metadata":{"_uuid":"445845ee-99c3-4809-8289-e918cdd1a96e","_cell_guid":"be66df72-c2b5-4db6-a783-7488c3ba59b1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:20.089395Z","iopub.execute_input":"2024-03-15T04:44:20.089769Z","iopub.status.idle":"2024-03-15T04:44:20.097303Z","shell.execute_reply.started":"2024-03-15T04:44:20.089739Z","shell.execute_reply":"2024-03-15T04:44:20.096258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_example(img, label):\n    print('Label: ', dataset.classes[label], \"(\"+str(label)+\")\")\n    plt.imshow(img.permute(1, 2, 0))","metadata":{"_uuid":"1a24a1ba-40aa-47da-bc70-180130bbd577","_cell_guid":"bbfd1e78-a073-4ae6-aaad-0b32ad1a109a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:21.065828Z","iopub.execute_input":"2024-03-15T04:44:21.066211Z","iopub.status.idle":"2024-03-15T04:44:21.071534Z","shell.execute_reply.started":"2024-03-15T04:44:21.066182Z","shell.execute_reply":"2024-03-15T04:44:21.070489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_example(*dataset[0])","metadata":{"_uuid":"ea25ed21-55a3-4d8c-aed0-08d956dd40c4","_cell_guid":"89bbd3cf-dff1-440d-803b-9359362a47d5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:21.973360Z","iopub.execute_input":"2024-03-15T04:44:21.974270Z","iopub.status.idle":"2024-03-15T04:44:22.400845Z","shell.execute_reply.started":"2024-03-15T04:44:21.974234Z","shell.execute_reply":"2024-03-15T04:44:22.399741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_example(*dataset[1099])","metadata":{"_uuid":"148d5f0a-b23d-4d1e-88fe-73d6f928fe42","_cell_guid":"dd7231b3-76f7-43f7-a67d-b338176e5450","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:24.255800Z","iopub.execute_input":"2024-03-15T04:44:24.256627Z","iopub.status.idle":"2024-03-15T04:44:24.682586Z","shell.execute_reply.started":"2024-03-15T04:44:24.256592Z","shell.execute_reply":"2024-03-15T04:44:24.681575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_seed = 42\ntorch.manual_seed(random_seed);","metadata":{"_uuid":"f3d2539d-509a-4e0a-a8c7-d13c0d7daa18","_cell_guid":"ca2334b1-cea3-4dee-87d0-25d1d8eface4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:26.304953Z","iopub.execute_input":"2024-03-15T04:44:26.305757Z","iopub.status.idle":"2024-03-15T04:44:26.310890Z","shell.execute_reply.started":"2024-03-15T04:44:26.305718Z","shell.execute_reply":"2024-03-15T04:44:26.309719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_size = 5000\ntrain_size = len(dataset) - val_size\n\ntrain_ds, val_ds = random_split(dataset, [train_size, val_size])\nlen(train_ds), len(val_ds)","metadata":{"_uuid":"c5d7d08c-54eb-4ad9-8683-beaac4a74c46","_cell_guid":"ebce7c96-b860-4139-b8c0-3aeabca8fc8a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:30.838004Z","iopub.execute_input":"2024-03-15T04:44:30.838873Z","iopub.status.idle":"2024-03-15T04:44:30.846686Z","shell.execute_reply.started":"2024-03-15T04:44:30.838843Z","shell.execute_reply":"2024-03-15T04:44:30.845733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data.dataloader import DataLoader\n\nbatch_size=8","metadata":{"_uuid":"2cc64f06-bd4e-4a70-8235-46e46d91daf8","_cell_guid":"290ddf56-9cc7-42c5-9619-866c789bbdba","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:31.962913Z","iopub.execute_input":"2024-03-15T04:44:31.963748Z","iopub.status.idle":"2024-03-15T04:44:31.968142Z","shell.execute_reply.started":"2024-03-15T04:44:31.963711Z","shell.execute_reply":"2024-03-15T04:44:31.967044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = DataLoader(train_ds, batch_size, shuffle=True, num_workers=4, pin_memory=True)\nval_dl = DataLoader(val_ds, batch_size*2, num_workers=4, pin_memory=True)","metadata":{"_uuid":"284d616c-da32-4f3a-8591-df86d887414d","_cell_guid":"41819639-3ab9-44e3-ac91-38f150a21406","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:35.915383Z","iopub.execute_input":"2024-03-15T04:44:35.916338Z","iopub.status.idle":"2024-03-15T04:44:35.921800Z","shell.execute_reply.started":"2024-03-15T04:44:35.916302Z","shell.execute_reply":"2024-03-15T04:44:35.920787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.utils import make_grid\n\ndef show_batch(dl):\n    for images, labels in dl:\n        fig, ax = plt.subplots(figsize=(12, 6))\n        ax.set_xticks([]); ax.set_yticks([])\n        ax.imshow(make_grid(images, nrow=16).permute(1, 2, 0))\n        break","metadata":{"_uuid":"27074007-4f94-4772-b44f-cf5733c5e622","_cell_guid":"80d88dea-aa7b-4369-af49-70938fd25b6f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:37.974061Z","iopub.execute_input":"2024-03-15T04:44:37.974833Z","iopub.status.idle":"2024-03-15T04:44:37.980358Z","shell.execute_reply.started":"2024-03-15T04:44:37.974803Z","shell.execute_reply":"2024-03-15T04:44:37.979334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F","metadata":{"_uuid":"a5f3dc92-f235-4e1e-bdc5-0e365a0ab39b","_cell_guid":"706dc563-9a51-48b1-b0e4-f03af5e99890","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:39.009259Z","iopub.execute_input":"2024-03-15T04:44:39.010006Z","iopub.status.idle":"2024-03-15T04:44:39.014325Z","shell.execute_reply.started":"2024-03-15T04:44:39.009960Z","shell.execute_reply":"2024-03-15T04:44:39.013270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simple_model = nn.Sequential(\n    nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),\n    nn.MaxPool2d(2,2)\n)","metadata":{"_uuid":"690035e7-070b-4edd-abdc-d15a58e37df1","_cell_guid":"35d3104c-ed3c-48f6-8d03-b3bb1933ba20","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:40.825205Z","iopub.execute_input":"2024-03-15T04:44:40.826069Z","iopub.status.idle":"2024-03-15T04:44:40.831437Z","shell.execute_reply.started":"2024-03-15T04:44:40.826035Z","shell.execute_reply":"2024-03-15T04:44:40.830436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor images, labels in train_dl:\n    print('images.shape:', images.shape)\n    out = simple_model(images)\n    print('out.shape:', out.shape)\n    break","metadata":{"_uuid":"2b189aa5-beef-4516-ac8a-0bfad7f92d29","_cell_guid":"73ef4a65-3e87-490c-9846-24c10c34355c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:43.169409Z","iopub.execute_input":"2024-03-15T04:44:43.170194Z","iopub.status.idle":"2024-03-15T04:44:44.269470Z","shell.execute_reply.started":"2024-03-15T04:44:43.170163Z","shell.execute_reply":"2024-03-15T04:44:44.268171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageClassificationBase(nn.Module):\n    def training_step(self, batch):\n        images, labels = batch \n        out = self(images)                  # Generate predictions\n        loss = F.cross_entropy(out, labels) # Calculate loss\n        return loss\n    \n    def validation_step(self, batch):\n        images, labels = batch \n        out = self(images)                    # Generate predictions\n        loss = F.cross_entropy(out, labels)   # Calculate loss\n        acc = accuracy(out, labels)           # Calculate accuracy\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()   # Combine losses\n        batch_accs = [x['val_acc'] for x in outputs]\n        epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies\n        return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}\n    \n    def epoch_end(self, epoch, result):\n        print(\"Epoch [{}], train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}\".format(\n            epoch, result['train_loss'], result['val_loss'], result['val_acc']))\n        \ndef accuracy(outputs, labels):\n    _, preds = torch.max(outputs, dim=1)\n    return torch.tensor(torch.sum(preds == labels).item() / len(preds))","metadata":{"_uuid":"92428732-bbdc-4716-a535-5c635b0fa05d","_cell_guid":"368dbcda-12de-40c7-88b2-48dab04af549","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:48.738063Z","iopub.execute_input":"2024-03-15T04:44:48.738483Z","iopub.status.idle":"2024-03-15T04:44:48.750615Z","shell.execute_reply.started":"2024-03-15T04:44:48.738436Z","shell.execute_reply":"2024-03-15T04:44:48.749332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Cifar10CnnModel(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.network = nn.Sequential(\n            nn.Conv2d(3, 64, kernel_size=3, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n\n            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n\n            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n\n            nn.Flatten(),\n            nn.Linear(256 * 32 * 32, 1024),\n            nn.ReLU(),\n            nn.Linear(1024, 512),\n            nn.ReLU(),\n            nn.Linear(512, 7))\n\n    def forward(self, x):\n        return self.network(x)\n    \n   \n    def training_step(self, batch, batch_idx):\n        images, labels = batch\n        out = self(images)                  # Generate predictions\n        loss = F.cross_entropy(out, labels) # Calculate loss\n        return loss\n\n    def validation_step(self, batch, batch_idx):\n        images, labels = batch\n        out = self(images)                  # Generate predictions\n        loss = F.cross_entropy(out, labels) # Calculate loss\n        acc = accuracy(out, labels)         # Calculate accuracy\n        return {'val_loss': loss, '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()   # Combine losses\n        batch_accs = [x['val_acc'] for x in outputs]\n        epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies\n        return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}\n\n    @torch.no_grad()\n    def evaluate(model, val_loader):\n        model.eval()\n        outputs = [model.validation_step(batch, batch_idx) for batch_idx, batch in enumerate(val_loader)]\n        return model.validation_epoch_end(outputs)\n\n\n    def configure_optimizers(self):\n        return torch.optim.Adam(self.parameters(), lr=1e-3)","metadata":{"_uuid":"52c3cc80-90ca-47dd-bd13-d64c6380264f","_cell_guid":"e5b736c7-7406-4bfa-b113-d70412cd7ab8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:50.301645Z","iopub.execute_input":"2024-03-15T04:44:50.302331Z","iopub.status.idle":"2024-03-15T04:44:50.317593Z","shell.execute_reply.started":"2024-03-15T04:44:50.302296Z","shell.execute_reply":"2024-03-15T04:44:50.316628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Cifar10CnnModel()\nmodel","metadata":{"_uuid":"007fa867-a952-4b59-ad8d-50b10645dab6","_cell_guid":"92527444-53f0-4f9f-9783-f2beda549c0e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:44:51.475613Z","iopub.execute_input":"2024-03-15T04:44:51.476001Z","iopub.status.idle":"2024-03-15T04:44:54.282155Z","shell.execute_reply.started":"2024-03-15T04:44:51.475960Z","shell.execute_reply":"2024-03-15T04:44:54.281213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)\n\nfor images, labels in train_dl:\n    images, labels = images.to(device), labels.to(device)  # Move data to the same device as the model\n    print('images.shape:', images.shape)\n    out = model(images)\n    print('out.shape:', out.shape)\n    print('out[0]:', out[0])\n    break","metadata":{"_uuid":"f78c3ef0-3618-4ace-83c1-51eb6477f460","_cell_guid":"f0bbc181-ae0c-4ae7-b6a2-09b2ff4cb158","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:45:01.508477Z","iopub.execute_input":"2024-03-15T04:45:01.509143Z","iopub.status.idle":"2024-03-15T04:45:02.587317Z","shell.execute_reply.started":"2024-03-15T04:45:01.509109Z","shell.execute_reply":"2024-03-15T04:45:02.586215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_default_device():\n    \"\"\"Pick GPU if available, else CPU\"\"\"\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    \"\"\"Move tensor(s) to chosen 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    \"\"\"Wrap a dataloader to move data to a device\"\"\"\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        \"\"\"Yield a batch of data after moving it to device\"\"\"\n        for b in self.dl: \n            yield to_device(b, self.device)\n\n    def __len__(self):\n        \"\"\"Number of batches\"\"\"\n        return len(self.dl)","metadata":{"_uuid":"d615637f-75cf-4622-bda6-39c4eeb1cae4","_cell_guid":"598636e4-10e4-4a94-a393-346856400163","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:45:04.216331Z","iopub.execute_input":"2024-03-15T04:45:04.216792Z","iopub.status.idle":"2024-03-15T04:45:04.226725Z","shell.execute_reply.started":"2024-03-15T04:45:04.216742Z","shell.execute_reply":"2024-03-15T04:45:04.225453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = get_default_device()\ndevice","metadata":{"_uuid":"61ec460a-663f-40ba-9164-01ebc1fb10b9","_cell_guid":"0d2834f4-968f-4512-a793-1dd85372d252","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:45:05.284780Z","iopub.execute_input":"2024-03-15T04:45:05.285530Z","iopub.status.idle":"2024-03-15T04:45:05.292193Z","shell.execute_reply.started":"2024-03-15T04:45:05.285499Z","shell.execute_reply":"2024-03-15T04:45:05.291067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = DeviceDataLoader(train_dl, device)\nval_dl = DeviceDataLoader(val_dl, device)\nto_device(model, device);","metadata":{"_uuid":"6b6d20eb-333c-4fdd-96ba-a42baca02d63","_cell_guid":"408f8382-7314-429c-9ef5-eb8b70886ca5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:45:06.422198Z","iopub.execute_input":"2024-03-15T04:45:06.422560Z","iopub.status.idle":"2024-03-15T04:45:06.428943Z","shell.execute_reply.started":"2024-03-15T04:45:06.422532Z","shell.execute_reply":"2024-03-15T04:45:06.427865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):\n    history = []\n    optimizer = opt_func(model.parameters(), lr)\n    for epoch in range(epochs):\n        # Training Phase \n        model.train()\n        train_losses = []\n        for batch in train_loader:\n            loss = model.training_step(batch)\n            train_losses.append(loss)\n            loss.backward()\n            optimizer.step()\n            optimizer.zero_grad()\n        # Validation phase\n        result = evaluate(model, val_loader)\n        result['train_loss'] = torch.stack(train_losses).mean().item()\n        model.epoch_end(epoch, result)\n        history.append(result)\n    return history","metadata":{"_uuid":"96c2851c-2024-488d-86c5-9368da8d50fb","_cell_guid":"28aee2d0-010c-49c6-a57d-f9394674a062","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:45:07.557699Z","iopub.execute_input":"2024-03-15T04:45:07.558429Z","iopub.status.idle":"2024-03-15T04:45:07.567109Z","shell.execute_reply.started":"2024-03-15T04:45:07.558399Z","shell.execute_reply":"2024-03-15T04:45:07.566159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = to_device(Cifar10CnnModel(), device)","metadata":{"_uuid":"3f34403f-21bb-4e71-ab99-aac2fe3f2542","_cell_guid":"9a010554-5866-41fb-8178-3884d7e3684d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:45:08.946797Z","iopub.execute_input":"2024-03-15T04:45:08.947777Z","iopub.status.idle":"2024-03-15T04:45:11.971115Z","shell.execute_reply.started":"2024-03-15T04:45:08.947739Z","shell.execute_reply":"2024-03-15T04:45:11.970125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache()\nevaluate(model, val_dl)","metadata":{"_uuid":"8157ae57-fcb2-4b93-9b7d-c491cc941d3e","_cell_guid":"eb7f9ed5-aa85-44d8-b5fc-61fdfa4077f1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-15T04:45:13.683594Z","iopub.execute_input":"2024-03-15T04:45:13.683987Z","iopub.status.idle":"2024-03-15T04:45:14.634015Z","shell.execute_reply.started":"2024-03-15T04:45:13.683943Z","shell.execute_reply":"2024-03-15T04:45:14.630489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 10\nopt_func = torch.optim.Adam\nlr = 0.001","metadata":{"_uuid":"f85ba1ef-e369-4ec8-9875-209e6ab80421","_cell_guid":"25c4e67e-c101-46eb-b315-b46133aefc01","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = fit(num_epochs, lr, model, train_dl, val_dl, opt_func)","metadata":{"_uuid":"cc277d4b-61a6-42a5-baaf-6604525b56ce","_cell_guid":"6270dd3f-858f-439d-b81e-d73c8a6a2861","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"_uuid":"d298ba34-2d12-4391-b778-eb6dfa000d70","_cell_guid":"e573c174-d737-4088-8f70-f8c2eebec212","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_accuracies(history)","metadata":{"_uuid":"d3275ecd-2941-422d-bdae-108ca7420029","_cell_guid":"a0958826-7d14-4b6e-b90e-b93482a25ca8","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_image(img, model):\n    # Convert to a batch of 1\n    xb = to_device(img.unsqueeze(0), device)\n    # Get predictions from model\n    yb = model(xb)\n    # Pick index with highest probability\n    _, preds  = torch.max(yb, dim=1)\n    # Retrieve the class label\n    return dataset.classes[preds[0].item()]","metadata":{"_uuid":"36e6f42c-f1a9-4b70-9c1b-8299b6b660ef","_cell_guid":"f8755019-a69f-48e0-ba80-bbc7efee6668","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[0]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label:', dataset.classes[label], ', Predicted:', predict_image(img, model))","metadata":{"_uuid":"6bd7e233-864d-4174-ab8a-cf1eb079dd25","_cell_guid":"d94a1b2e-8308-4c31-bfa7-a4811b684eb2","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[1002]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label:', dataset.classes[label], ', Predicted:', predict_image(img, model))","metadata":{"_uuid":"e3c828a9-1af3-46ba-a643-b52918400514","_cell_guid":"230deec2-bdad-4071-a964-e1d9756edfbb","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[6153]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label:', dataset.classes[label], ', Predicted:', predict_image(img, model))","metadata":{"_uuid":"9c15c6b9-7793-4b12-b0ae-16fadd76cdf1","_cell_guid":"13d85ab0-650e-41cd-a0dd-fea82fbe2df8","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DeviceDataLoader(DataLoader(test_dataset, batch_size*2), device)\nresult = evaluate(model, test_loader)\nresult","metadata":{"_uuid":"30b9e00e-a537-4ac0-a65d-47111b5c3163","_cell_guid":"778b893f-d7ac-4828-992d-460c3f0a365e","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}