-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgesture_cnn.py
61 lines (51 loc) · 1.9 KB
/
gesture_cnn.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
### Gesture Detection using CNN
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras import models, layers
num_classes = 5 # number of gestures
target_size = (96,96)
# Dataset
train_dir = 'datasets/gesture'
# Data Generator
rescale = 1./255
train_datagen = ImageDataGenerator(
rescale=rescale,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=target_size,
class_mode='categorical',
batch_size=10,
color_mode="grayscale",
shuffle=True)
# Build Model
model = models.Sequential()
# conv-layer 1
model.add(layers.Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', input_shape=(96,96,1)))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
# conv-layer 2
model.add(layers.Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
# conv-layer 3
model.add(layers.Conv2D(96, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
# conv-layer 4
model.add(layers.Conv2D(128, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
# fully-connected layers
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(num_classes , activation='softmax'))
model.summary()
# Compile Model
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
# Train Model
num_epochs=100
STEP_SIZE_TRAIN = train_generator.n // train_generator.batch_size
model.fit(train_generator, steps_per_epoch=STEP_SIZE_TRAIN, epochs=num_epochs)
# Save Model
models.save_model(model, 'models/gesture_cnn.h5')