{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4", "authorship_tag": "ABX9TyM5Fah77XuALjsYEAXaCFqE", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# **Model Training and Fine-Tuning**\n", "\n", "**Objective:** Ingest the Kaggle Chest X-Ray dataset, apply strategic data augmentation, handle severe class imbalance via loss weighting, and fine-tune a pre-trained DenseNet121 architecture to detect pneumonia.\n", "\n", "## **Environment Setup**\n", "First, we import the necessary PyTorch libraries and configure our hardware accelerator. Because image processing is computationally expensive, we must ensure PyTorch detects and utilizes the Colab GPU (CUDA)." ], "metadata": { "id": "24IiOoFPN2oN" } }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IGGkAlCRNlro", "outputId": "919ad755-854c-49b1-a812-43d413128bb3" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Compute Device: cuda\n", "GPU Model: Tesla T4\n" ] } ], "source": [ "import torch\n", "import torch.nn as nn\n", "import torch.optim as optim\n", "from torchvision import datasets, transforms, models\n", "from torch.utils.data import DataLoader\n", "import os\n", "import numpy as np\n", "\n", "# Configure the device\n", "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "print(f'Compute Device: {device}')\n", "if device.type == 'cuda':\n", " print(f'GPU Model: {torch.cuda.get_device_name(0)}')" ] }, { "cell_type": "markdown", "source": [ "## **Data Preprocessing and Augmentation Pipeline**\n", "As determined in our EDA, we must standardize all input resolutions to 224x224.\n", "\n", "To prevent overfitting without introducing artificial artifacts, we apply light data augmentation **only to the training set** (rotation and color jitter). The validation and test sets remain strictly unaltered to provide an honest evaluation metric." ], "metadata": { "id": "2SGyO1PxOmDX" } }, { "cell_type": "code", "source": [ "# Define the dataset paths\n", "data_dir = '../dataset/chest_xray'\n", "train_dir = os.path.join(data_dir, 'train')\n", "val_dir = os.path.join(data_dir, 'val')\n", "test_dir = os.path.join(data_dir, 'test')\n", "\n", "# 1. Training Transforms (with Augmentation)\n", "train_transforms = transforms.Compose([\n", " transforms.Resize((224, 224)),\n", " transforms.RandomRotation(10), # Slight rotation to simulate patient misalignment\n", " transforms.ColorJitter(brightness=0.2, contrast=0.2), # Account for different X-ray exposures\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Standard ImageNet normalization\n", "])\n", "\n", "# 2. Validation/Testing Transforms (Strictly unaltered)\n", "eval_transforms = transforms.Compose([\n", " transforms.Resize((224, 224)),\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", "])\n", "\n", "print(\"Transforms configured successfully.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ikceEotXOU_1", "outputId": "367466cb-bd4f-4de8-b8c6-1142abda4b49" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Transforms configured successfully.\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "I6obJOa1QYMy" }, "execution_count": null, "outputs": [] } ] }