-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrainMultiModel.py
186 lines (159 loc) · 5.28 KB
/
trainMultiModel.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, Conv1D, Flatten
import os
from keras.utils import np_utils, plot_model
import matplotlib.pyplot as plt
saveModel = True
sampleLength = 32
def createCNNLSTM(cnn1, cnn2, lstm):
model = Sequential()
if(cnn1>0):
model.add(Conv1D(cnn1, kernel_size=3, activation='relu', input_shape = (sampleLength, 8)))
model.add(Dropout(rate=0.5))
if(cnn2>0):
model.add(Conv1D(cnn2, kernel_size=3, activation='relu'))
model.add(Dropout(rate=0.5))
if(lstm>0):
model.add(LSTM(lstm, return_sequences=True))
model.add(Flatten())
model.add(Dense(num_gestures, activation='softmax'))
return model
def shuffleData(in_x, in_y):
newTrainX = []
newTrainY = []
dataLen = len(in_x)
k = np.arange(dataLen)
np.random.shuffle(k)
for i in k:
newTrainX.append(in_x[i])
newTrainY.append(in_y[i])
newTrainX = np.stack(newTrainX, axis = 0)
newTrainY = np.stack(newTrainY, axis = 0)
return newTrainX, newTrainY
#get training data directories by pre-determined gestures
gesture_names = ['Rest', 'Fist', 'Hold_Left', 'Hold_Right', 'Flower', 'Finger_Spread','Metal','Thumbs_Up','Peace']
#get number of outputs
num_gestures = len(gesture_names)
#Input data
trainX = []
#Output Class
trainY = []
#load data
classification = 0
for gesture in gesture_names:
for r, d, f in os.walk('Gestures/'+gesture):
for file in f:
if '.csv' in file:
# data = pd.read_csv('Gestures/'+gesture+'/'+file)
data = np.genfromtxt('Gestures/'+gesture+'/'+file, delimiter=',')
fileX = []
length = 0
for x in data:
if(length>=sampleLength):
trainX.append(fileX)
trainY.append(classification)
fileX = []
length=0
fileX.append(np.asarray(x))
length = length + 1
# Stack the list of numpy arrays
classification = classification + 1
trainX = np.stack(trainX, axis=0)
# Normalize data to 0 1 analog
trainY = np_utils.to_categorical(trainY)
# Shuffle data to remove overfitting
trainX, trainY = shuffleData(trainX, trainY)
model_nodes = ['32_length_Sample_32cnn', '32_length_sample_64cnn', '32_length_sample_128cnn','32_length_sample_32_32cnn', '32_length_sample_16lstm','32_length_sample_32lstm','32_length_sample_64lstm', '32_Length_Sample_32_32_16', '32_Length_Sample_64_32_16', '32_Length_Sample_64_64_32', '32_Length_Sample_128_64_32']
# Empty variable to contain array of models
models = []
# Single Layer CNN Models
models.append(createCNNLSTM(32,0,0))
models.append(createCNNLSTM(64,0,0))
models.append(createCNNLSTM(128,0,0))
# Double Layer CNN Model
models.append(createCNNLSTM(32,32,0))
# Single Layer RNN LSTM Model
models.append(createCNNLSTM(0,0,16))
models.append(createCNNLSTM(0,0,32))
models.append(createCNNLSTM(0,0,64))
# Multi Layer CNN-RNN Model
models.append(createCNNLSTM(32,32,16))
models.append(createCNNLSTM(64,32,16))
models.append(createCNNLSTM(64,64,32))
models.append(createCNNLSTM(128,64,32))
histories = []
for model, model_name in zip(models, model_nodes):
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(trainX, trainY,validation_split=0.33, epochs=225, batch_size=32, shuffle=True)
histories.append(history)
if(saveModel):
model.save(model_name+'.h5')
for history, savefile in zip(histories, model_nodes):
# Plot training & validation accuracy values
# Create 2 subplots
plt.subplot(2, 1, 1)
# Subplot for model accuracy
plt.title('Model Accuracy')
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
# Subplot for model loss
plt.subplot(2,1,2)
plt.title('Model Loss')
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.tight_layout()
plt.savefig(savefile+'.png')
plt.clf()
plt.cla()
plt.close()
# Add train per gesture here
# Singular Classification for each model
gestureX = []
classification = 0
classificationRange = 1000
for gesture in gesture_names:
for r, d, f in os.walk('Gestures/'+gesture):
gesture_x = []
gesture_y = []
for file in f:
if '.csv' in file:
# data = pd.read_csv('Gestures/'+gesture+'/'+file)
data = np.genfromtxt('Gestures/'+gesture+'/'+file, delimiter=',')
fileX = []
length = 0
for x in data:
if(len(gesture_x)<classificationRange):
if(length>=sampleLength):
gesture_x.append(fileX)
fileX = []
length=0
fileX.append(np.asarray(x))
length = length + 1
# Stack the list of numpy arrays
gestureX.append(gesture_x)
classification = classification + 1
model_acc = []
for model, model_profile in zip(models,model_nodes):
# Get classification Predictions
print('Classification Data for model_profile: ' + model_profile)
acc_gestures = []
for gesture, gestureName in zip(gestureX, gesture_names):
count = 0
for datas in gesture:
data = np.array(datas)
data = data.reshape(1, data.shape[0], data.shape[1])
prediction = model.predict_classes(data)
if(gesture_names[prediction[0]]==gestureName):
count=count+1
acc = count/len(gesture) * 100
print('Gesture (' + gestureName + '): ' + str(acc)+'%')
acc_gestures.append(acc)
model_acc.append(acc_gestures)
#write the model accuracy