mobadara commited on
Commit
e276ccc
·
1 Parent(s): 226efba

Created using Colab

Browse files
notebooks/01_model_training_and_fine_tuning.ipynb ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4",
8
+ "authorship_tag": "ABX9TyM5Fah77XuALjsYEAXaCFqE",
9
+ "include_colab_link": true
10
+ },
11
+ "kernelspec": {
12
+ "name": "python3",
13
+ "display_name": "Python 3"
14
+ },
15
+ "language_info": {
16
+ "name": "python"
17
+ },
18
+ "accelerator": "GPU"
19
+ },
20
+ "cells": [
21
+ {
22
+ "cell_type": "markdown",
23
+ "metadata": {
24
+ "id": "view-in-github",
25
+ "colab_type": "text"
26
+ },
27
+ "source": [
28
+ "<a href=\"https://colab.research.google.com/github/mobadara/precision-diagnostics-xai/blob/main/notebooks/01_model_training_and_fine_tuning.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "markdown",
33
+ "source": [
34
+ "# **Model Training and Fine-Tuning**\n",
35
+ "\n",
36
+ "**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",
37
+ "\n",
38
+ "## **Environment Setup**\n",
39
+ "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)."
40
+ ],
41
+ "metadata": {
42
+ "id": "24IiOoFPN2oN"
43
+ }
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": 2,
48
+ "metadata": {
49
+ "colab": {
50
+ "base_uri": "https://localhost:8080/"
51
+ },
52
+ "id": "IGGkAlCRNlro",
53
+ "outputId": "919ad755-854c-49b1-a812-43d413128bb3"
54
+ },
55
+ "outputs": [
56
+ {
57
+ "output_type": "stream",
58
+ "name": "stdout",
59
+ "text": [
60
+ "Compute Device: cuda\n",
61
+ "GPU Model: Tesla T4\n"
62
+ ]
63
+ }
64
+ ],
65
+ "source": [
66
+ "import torch\n",
67
+ "import torch.nn as nn\n",
68
+ "import torch.optim as optim\n",
69
+ "from torchvision import datasets, transforms, models\n",
70
+ "from torch.utils.data import DataLoader\n",
71
+ "import os\n",
72
+ "import numpy as np\n",
73
+ "\n",
74
+ "# Configure the device\n",
75
+ "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
76
+ "print(f'Compute Device: {device}')\n",
77
+ "if device.type == 'cuda':\n",
78
+ " print(f'GPU Model: {torch.cuda.get_device_name(0)}')"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "markdown",
83
+ "source": [
84
+ "## **Data Preprocessing and Augmentation Pipeline**\n",
85
+ "As determined in our EDA, we must standardize all input resolutions to 224x224.\n",
86
+ "\n",
87
+ "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."
88
+ ],
89
+ "metadata": {
90
+ "id": "2SGyO1PxOmDX"
91
+ }
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "source": [
96
+ "# Define the dataset paths\n",
97
+ "data_dir = '../dataset/chest_xray'\n",
98
+ "train_dir = os.path.join(data_dir, 'train')\n",
99
+ "val_dir = os.path.join(data_dir, 'val')\n",
100
+ "test_dir = os.path.join(data_dir, 'test')\n",
101
+ "\n",
102
+ "# 1. Training Transforms (with Augmentation)\n",
103
+ "train_transforms = transforms.Compose([\n",
104
+ " transforms.Resize((224, 224)),\n",
105
+ " transforms.RandomRotation(10), # Slight rotation to simulate patient misalignment\n",
106
+ " transforms.ColorJitter(brightness=0.2, contrast=0.2), # Account for different X-ray exposures\n",
107
+ " transforms.ToTensor(),\n",
108
+ " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Standard ImageNet normalization\n",
109
+ "])\n",
110
+ "\n",
111
+ "# 2. Validation/Testing Transforms (Strictly unaltered)\n",
112
+ "eval_transforms = transforms.Compose([\n",
113
+ " transforms.Resize((224, 224)),\n",
114
+ " transforms.ToTensor(),\n",
115
+ " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n",
116
+ "])\n",
117
+ "\n",
118
+ "print(\"Transforms configured successfully.\")"
119
+ ],
120
+ "metadata": {
121
+ "colab": {
122
+ "base_uri": "https://localhost:8080/"
123
+ },
124
+ "id": "ikceEotXOU_1",
125
+ "outputId": "367466cb-bd4f-4de8-b8c6-1142abda4b49"
126
+ },
127
+ "execution_count": 3,
128
+ "outputs": [
129
+ {
130
+ "output_type": "stream",
131
+ "name": "stdout",
132
+ "text": [
133
+ "Transforms configured successfully.\n"
134
+ ]
135
+ }
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "source": [],
141
+ "metadata": {
142
+ "id": "I6obJOa1QYMy"
143
+ },
144
+ "execution_count": null,
145
+ "outputs": []
146
+ }
147
+ ]
148
+ }