-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
82 lines (62 loc) · 2.93 KB
/
train.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
# Importing the Keras libraries and packages
# Import of keras model and hidden layers for our convolutional network
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Dense, Flatten
# Step 1 - Building the CNN
# Initializing the CNN
model = Sequential()
# First convolution layer and pooling
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(120, 120, 1)))
model.add(MaxPooling2D((2, 2)))
# Second convolution layer and pooling
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
# Third convolution layer
model.add(Conv2D(128, (3, 3), activation='relu'))
# input_shape is going to be the pooled feature maps from the previous convolution layer
model.add(MaxPooling2D((2, 2)))
# Flattening the layers
model.add(Flatten())
# Adding a fully connected layer
model.add(Dense(256, activation='relu'))
model.add(Dense(7, activation='softmax'))
# Compiling the CNN
model.compile(optimizer='adam', # Optimization routine, which tells the computer how to adjust the parameter values to minimize the loss function.
loss='categorical_crossentropy', # Loss function, which tells us how bad our predictions are.
metrics=['accuracy']) # List of metrics to be evaluated by the model during training and testing. # categorical_crossentropy for more than 2
# Step 2 - Preparing the train/test data and training the model
# Code copied from - https://keras.io/preprocessing/image/
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip= False)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('data/train',
target_size=(120, 120),
batch_size=7,
color_mode='grayscale',
class_mode='categorical')
test_set = test_datagen.flow_from_directory('data/test',
target_size=(120, 120),
batch_size=7,
color_mode='grayscale',
class_mode='categorical')
model.fit(
training_set,
steps_per_epoch=125, # No of images in training set
epochs=7,
validation_data=test_set,
validation_steps=50)# No of images in test set
test_loss, test_acc = model.evaluate(test_set)
print('Test accuracy: {:2.2f}%'.format(test_acc*100))
# Save entire model to a HDF5 file
model.save('handrecognition_model.hdf5')
model.summary()
# Saving the model
model_json = model.to_json()
with open("gesture-model.json", "w") as json_file:
json_file.write(model_json)
model.save_weights('gesture-model.h5')