-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
61 lines (49 loc) · 1.96 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
import keras
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, Activation, MaxPooling2D, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
def buildModel():
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu',
padding='same', input_shape=(24, 24, 1)))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(64, (2, 2), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(128, (2, 2), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(64, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
return model
def preprocessData(dir):
datagen = ImageDataGenerator(rescale=1./255)
data_generator = datagen.flow_from_directory(dir,
target_size=(24, 24),
batch_size=32,
color_mode='grayscale',
class_mode='binary')
return data_generator
def trainModel(model):
datagen = preprocessData('./assets/dataset_EyeImages')
model.fit_generator(datagen, steps_per_epoch=len(datagen)/32, epochs=50)
model.save('eyeblink.hdf5')
# manipulate the image so as to have the same format as at training
def cnnPreprocess(img):
img = img.astype('float32')
img /= 255
img = np.expand_dims(img, axis=2)
img = np.expand_dims(img, axis=0)
return img
def main():
model = buildModel()
trainModel(model)
if __name__ == "__main__":
main()