Skip to content

Commit cf4f6c4

Browse files
Merge pull request Gagandeep-2003#64 from KaiAllAlone/main Add ResNet50-based drowsiness detection training notebook
- Added Colab notebook for training a ResNet50-based eye drowsiness detection model. - Includes dataset download steps, model architecture, training logs, and save/load examples. - Added README in models/ folder describing setup and usage. - No changes to existing application code.
2 parents 77305a1 + b4a27c2 commit cf4f6c4

File tree

3 files changed

+478
-0
lines changed

3 files changed

+478
-0
lines changed
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"gpuType": "T4"
8+
},
9+
"kernelspec": {
10+
"name": "python3",
11+
"display_name": "Python 3"
12+
},
13+
"language_info": {
14+
"name": "python"
15+
},
16+
"accelerator": "GPU"
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"metadata": {
23+
"colab": {
24+
"base_uri": "https://localhost:8080/"
25+
},
26+
"id": "165vZIxs7OhN",
27+
"outputId": "c1b4a906-8735-43f8-a1c1-1b2cb0e1ba3f"
28+
},
29+
"outputs": [
30+
{
31+
"output_type": "stream",
32+
"name": "stdout",
33+
"text": [
34+
"Downloading from https://www.kaggle.com/api/v1/datasets/download/akashshingha850/mrl-eye-dataset?dataset_version_number=4...\n"
35+
]
36+
},
37+
{
38+
"output_type": "stream",
39+
"name": "stderr",
40+
"text": [
41+
"100%|██████████| 329M/329M [00:02<00:00, 160MB/s]"
42+
]
43+
},
44+
{
45+
"output_type": "stream",
46+
"name": "stdout",
47+
"text": [
48+
"Extracting files...\n"
49+
]
50+
},
51+
{
52+
"output_type": "stream",
53+
"name": "stderr",
54+
"text": [
55+
"\n"
56+
]
57+
},
58+
{
59+
"output_type": "stream",
60+
"name": "stdout",
61+
"text": [
62+
"Path to dataset files: /root/.cache/kagglehub/datasets/akashshingha850/mrl-eye-dataset/versions/4\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"import kagglehub\n",
68+
"\n",
69+
"# Download latest version\n",
70+
"path = kagglehub.dataset_download(\"akashshingha850/mrl-eye-dataset\")\n",
71+
"\n",
72+
"print(\"Path to dataset files:\", path)"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"source": [
78+
"import os\n",
79+
"os.listdir(\"/root/.cache/kagglehub/datasets/akashshingha850/mrl-eye-dataset/versions/4/data/val\")"
80+
],
81+
"metadata": {
82+
"id": "ENSZig9m8QdU",
83+
"colab": {
84+
"base_uri": "https://localhost:8080/"
85+
},
86+
"outputId": "9a77fdf9-0bb1-4fc8-f3d7-2248fd4ec97b"
87+
},
88+
"execution_count": 2,
89+
"outputs": [
90+
{
91+
"output_type": "execute_result",
92+
"data": {
93+
"text/plain": [
94+
"['sleepy', 'awake']"
95+
]
96+
},
97+
"metadata": {},
98+
"execution_count": 2
99+
}
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"source": [
105+
"train_folder_path='/root/.cache/kagglehub/datasets/akashshingha850/mrl-eye-dataset/versions/4/data/train'\n",
106+
"test_folder_path='/root/.cache/kagglehub/datasets/akashshingha850/mrl-eye-dataset/versions/4/data/test'\n",
107+
"val_folder_path=\"/root/.cache/kagglehub/datasets/akashshingha850/mrl-eye-dataset/versions/4/data/val\"\n",
108+
"# Device setup"
109+
],
110+
"metadata": {
111+
"id": "dCvWRivkHYp_"
112+
},
113+
"execution_count": 3,
114+
"outputs": []
115+
},
116+
{
117+
"cell_type": "code",
118+
"source": [
119+
"import tensorflow as tf\n",
120+
"import matplotlib.pyplot as plt\n",
121+
"from tensorflow.keras import layers, models\n",
122+
"print(f\"TensorFlow version: {tf.__version__}\")\n",
123+
"\n",
124+
"# GPU Configuration for Google Colab\n",
125+
"def configure_gpu():\n",
126+
" gpus = tf.config.experimental.list_physical_devices('GPU')\n",
127+
" if gpus:\n",
128+
" try:\n",
129+
" for gpu in gpus:\n",
130+
" tf.config.experimental.set_memory_growth(gpu, True)\n",
131+
" print(f\"✅ GPU configured successfully! Available GPUs: {len(gpus)}\")\n",
132+
" print(f\"GPU details: {gpus[0]}\")\n",
133+
" return True\n",
134+
" except RuntimeError as e:\n",
135+
" print(f\"❌ GPU configuration failed: {e}\")\n",
136+
" return False\n",
137+
" else:\n",
138+
" print(\"ℹ️ No GPU available, using CPU\")\n",
139+
" return False\n",
140+
"gpu_available = configure_gpu()"
141+
],
142+
"metadata": {
143+
"colab": {
144+
"base_uri": "https://localhost:8080/"
145+
},
146+
"id": "bWsXYeSNWYs_",
147+
"outputId": "eef97b24-ae93-44ec-8fd9-a93328222fbe"
148+
},
149+
"execution_count": 5,
150+
"outputs": [
151+
{
152+
"output_type": "stream",
153+
"name": "stdout",
154+
"text": [
155+
"TensorFlow version: 2.18.0\n",
156+
"✅ GPU configured successfully! Available GPUs: 1\n",
157+
"GPU details: PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')\n"
158+
]
159+
}
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"source": [
165+
"\n",
166+
"IMG_SIZE = (224, 224)\n",
167+
"BATCH_SIZE = 16\n",
168+
"EPOCHS = 5\n",
169+
"\n",
170+
"train_ds = tf.keras.utils.image_dataset_from_directory(\n",
171+
" train_folder_path,\n",
172+
" image_size=IMG_SIZE,\n",
173+
" batch_size=BATCH_SIZE,\n",
174+
" validation_split=0.2,\n",
175+
" subset='training',\n",
176+
" label_mode='categorical',\n",
177+
" seed=42\n",
178+
")\n",
179+
"\n",
180+
"val_ds = tf.keras.utils.image_dataset_from_directory(\n",
181+
" val_folder_path,\n",
182+
" image_size=IMG_SIZE,\n",
183+
" batch_size=BATCH_SIZE,\n",
184+
" validation_split=0.2,\n",
185+
" subset='validation',\n",
186+
" label_mode='categorical',\n",
187+
" seed=42\n",
188+
")\n"
189+
],
190+
"metadata": {
191+
"colab": {
192+
"base_uri": "https://localhost:8080/"
193+
},
194+
"id": "241PAJT1MZ0v",
195+
"outputId": "bcc33ec3-db75-4049-e135-2a302d6bf87f"
196+
},
197+
"execution_count": 11,
198+
"outputs": [
199+
{
200+
"output_type": "stream",
201+
"name": "stdout",
202+
"text": [
203+
"Found 50937 files belonging to 2 classes.\n",
204+
"Using 40750 files for training.\n",
205+
"Found 16980 files belonging to 2 classes.\n",
206+
"Using 3396 files for validation.\n"
207+
]
208+
}
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"source": [
214+
"base_model = tf.keras.applications.ResNet50(\n",
215+
" input_shape=IMG_SIZE + (3,),\n",
216+
" include_top=False,\n",
217+
" weights='imagenet'\n",
218+
")\n",
219+
"base_model.trainable = False #freeze base\n",
220+
"\n",
221+
"model = models.Sequential([\n",
222+
" base_model,\n",
223+
" layers.GlobalAveragePooling2D(),\n",
224+
" layers.Dense(128, activation='relu'),\n",
225+
" layers.Dropout(0.3),\n",
226+
" layers.Dense(2, activation='softmax') # classification\n",
227+
"])\n",
228+
"\n",
229+
"model.compile(optimizer='adam',\n",
230+
" loss='binary_crossentropy',\n",
231+
" metrics=['accuracy'])"
232+
],
233+
"metadata": {
234+
"id": "tg3s90D8OIkV"
235+
},
236+
"execution_count": 15,
237+
"outputs": []
238+
},
239+
{
240+
"cell_type": "code",
241+
"source": [
242+
"history = model.fit(\n",
243+
" train_ds,\n",
244+
" validation_data=val_ds,\n",
245+
" epochs=EPOCHS,\n",
246+
" verbose=1\n",
247+
")"
248+
],
249+
"metadata": {
250+
"colab": {
251+
"base_uri": "https://localhost:8080/"
252+
},
253+
"id": "o5cxEgMDVBHE",
254+
"outputId": "7fed8812-8d9d-414a-b19c-378569cecaab"
255+
},
256+
"execution_count": 16,
257+
"outputs": [
258+
{
259+
"output_type": "stream",
260+
"name": "stdout",
261+
"text": [
262+
"Epoch 1/5\n",
263+
"\u001b[1m2547/2547\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m140s\u001b[0m 50ms/step - accuracy: 0.9445 - loss: 0.1492 - val_accuracy: 0.9753 - val_loss: 0.0677\n",
264+
"Epoch 2/5\n",
265+
"\u001b[1m2547/2547\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m118s\u001b[0m 46ms/step - accuracy: 0.9669 - loss: 0.0893 - val_accuracy: 0.9756 - val_loss: 0.0598\n",
266+
"Epoch 3/5\n",
267+
"\u001b[1m2547/2547\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m143s\u001b[0m 47ms/step - accuracy: 0.9712 - loss: 0.0774 - val_accuracy: 0.9761 - val_loss: 0.0590\n",
268+
"Epoch 4/5\n",
269+
"\u001b[1m2547/2547\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m144s\u001b[0m 47ms/step - accuracy: 0.9744 - loss: 0.0686 - val_accuracy: 0.9806 - val_loss: 0.0500\n",
270+
"Epoch 5/5\n",
271+
"\u001b[1m2547/2547\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m139s\u001b[0m 46ms/step - accuracy: 0.9776 - loss: 0.0600 - val_accuracy: 0.9850 - val_loss: 0.0415\n"
272+
]
273+
}
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"source": [
279+
"os.mkdir('/content/saved_model')"
280+
],
281+
"metadata": {
282+
"id": "YTuUHm9DbGq1"
283+
},
284+
"execution_count": 17,
285+
"outputs": []
286+
},
287+
{
288+
"cell_type": "code",
289+
"source": [
290+
"model.save('/content/saved_model/resnet.keras')"
291+
],
292+
"metadata": {
293+
"id": "MiDvSrwcXDvX"
294+
},
295+
"execution_count": 19,
296+
"outputs": []
297+
},
298+
{
299+
"cell_type": "code",
300+
"source": [],
301+
"metadata": {
302+
"id": "Q_IZYp52bQOC"
303+
},
304+
"execution_count": null,
305+
"outputs": []
306+
}
307+
]
308+
}

0 commit comments

Comments
 (0)