-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
36 lines (29 loc) · 1.45 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
import os
from keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
from keras.utils.np_utils import to_categorical
import random,shutil
from keras.models import Sequential
from keras.layers import Dropout,Conv2D,Flatten,Dense, MaxPooling2D, BatchNormalization
from keras.models import load_model
def generator(dir, gen=image.ImageDataGenerator(rescale=1./255), shuffle=True,batch_size=1,target_size=(24,24),class_mode='categorical' ):
return gen.flow_from_directory(dir,batch_size=batch_size,shuffle=shuffle,color_mode='grayscale',class_mode=class_mode,target_size=target_size)
train_batch= generator('data/train',shuffle=True, batch_size=3,target_size=(24,24))
valid_batch= generator('data/test',shuffle=True, batch_size=3,target_size=(24,24))
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(24,24,1)),
MaxPooling2D(pool_size=(1,1)),
Conv2D(32,(3,3),activation='relu'),
MaxPooling2D(pool_size=(1,1)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(1,1)),
Dropout(0.25),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(2, activation='softmax')
])
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit_generator(train_batch, validation_data=valid_batch,epochs=15,steps_per_epoch=100 ,validation_steps=50,verbose=1)
model.save('models/eyes.h5', overwrite=True)