-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGestureTrain.py
70 lines (57 loc) · 2.29 KB
/
GestureTrain.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
from keras.callbacks import ModelCheckpoint
from keras.layers import Dense, Flatten, Conv2D
from keras.layers import MaxPooling2D, Dropout
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
image_x, image_y = 200, 200
batch_size = 64
train_dir = "/home/ayush/Desktop/Dev/Gesture-Identification/gestures"
def keras_model(image_x, image_y):
num_of_classes = 13
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(image_x, image_y, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(5, 5), strides=(5, 5), padding='same'))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.6))
model.add(Dense(num_of_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
filepath = "GestureModel.h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
return model, callbacks_list
def main():
train_datagen = ImageDataGenerator(
rescale=1. / 255,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
rotation_range=15,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.2,
fill_mode='nearest')
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(image_x, image_y),
color_mode="grayscale",
batch_size=batch_size,
seed=42,
class_mode='categorical',
subset="training")
validation_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(image_x, image_y),
color_mode="grayscale",
batch_size=batch_size,
seed=42,
class_mode='categorical',
subset="validation")
model, callbacks_list = keras_model(image_x, image_y)
model.fit_generator(train_generator, epochs=5, validation_data=validation_generator)
scores = model.evaluate_generator(generator=validation_generator, steps=64)
print("CNN Error: %.2f%%" % (100 - scores[1] * 100))
model.save('GestureModel.h5')
main()