-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
86 lines (71 loc) · 2.8 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Importing Project Dependencies
import numpy as np
import os
import cv2
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Setting up config for GPU training
if tf.test.is_gpu_available:
physical_devices = tf.config.list_physical_devices("GPU")
tf.config.experimental.set_memory_growth(physical_devices[0], True)
# Loading in all the images and assigning target classes
def load_images(folder):
imgs, targets = [], []
for foldername in os.listdir(folder):
loc = folder + "/" + foldername
targets.append(len(os.listdir(loc)))
for filename in os.listdir(loc):
img = cv2.imread(os.path.join(loc, filename))
img = cv2.resize(img, (86, 86))
if img is not None and img.shape == (86, 86, 3):
imgs.append(img)
print(foldername)
imgs = np.array(imgs)
y = np.zeros(imgs.shape[0]).astype(int)
j, n = 0, 0
for i in targets:
y[j:i + j] = n
n += 1
j = i + j
return imgs, y
folder = "../input/drowsiness-detection"
X, y = load_images(folder)
# Splitting the data into 2 separate training and testing sets
def train_test_split(X, y, testing_size=0.2):
no_of_rows = X.shape[0]
no_of_test_rows = int(no_of_rows * testing_size)
rand_row_num = np.random.randint(0, no_of_rows, no_of_test_rows)
X_test = np.array([X[i] for i in rand_row_num])
X_train = np.delete(X, rand_row_num, axis=0)
y_test = np.array([y[i] for i in rand_row_num])
y_train = np.delete(y, rand_row_num, axis=0)
return X_train, y_train, X_test, y_test
X_train, y_train, X_test, y_test = train_test_split(X, y, testing_size=0.2)
print(X_train[0].shape)
# Model building using sequential API
model = keras.Sequential(
[
keras.Input(shape=(86, 86, 3)),
layers.Conv2D(75, 3, padding='valid', activation='relu'),
layers.MaxPooling2D(pool_size=(5, 5)),
layers.Conv2D(64, 2, padding='valid', activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(128, 3, padding='valid', activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(2, activation='softmax'),
]
)
print(model.summary())
# Model compilation with keeping track of accuracy while training & evaluation process
model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=False),
optimizer=keras.optimizers.Adam(),
metrics=['accuracy']
)
model.fit(X_train, y_train, batch_size=32, epochs=10)
model.evaluate(X_test, y_test, batch_size=32)
# Saving the model
model.save('my_model (1).h5')