-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
260 lines (196 loc) · 7.98 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# import the necessary packages
import itertools
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
from tensorflow.keras.models import load_model
# CONSTANTS
classes = ["rock(0)", "paper(1)", "scissors(2)", "OK(3)"]
# Read .csv datas
path = "Resources/" # Path to .csv datas
rock_dataset = pd.read_csv(path + "0.csv", header=None) # class = 0 Rock
scissors_dataset = pd.read_csv(path + "1.csv", header=None) # class = 1 Paper
paper_dataset = pd.read_csv(path + "2.csv", header=None) # class = 2 Scissors
ok_dataset = pd.read_csv(path + "3.csv", header=None) # class = 3 OK
# Concatenate all the data
frames = [rock_dataset, scissors_dataset, paper_dataset, ok_dataset]
dataset = pd.concat(frames)
def plot_8sensors_data(data,
title,
interval=40,
no_of_sensors=8,
n_steps=8):
"""Plotting 8 sensors 8 sequnetial data in one figure"""
xTime = np.linspace(interval, interval * n_steps, n_steps)
yInterval = np.linspace(-128, 128, 1)
n = 1
fig = plt.figure()
for i in range(0, len(data)-1, n_steps):
plt.subplot(int(no_of_sensors/2), 2, n)
plt.plot(xTime, data[i: i + n_steps])
plt.title("{}. sensor".format(n))
plt.xlabel("time(ms)")
plt.ylabel("Samples")
plt.xticks(xTime)
plt.yticks(yInterval)
n += 1
plt.suptitle(title)
plt.tight_layout()
# fig.savefig("Resources/" + title + ".png", dpi=100)
plt.show()
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Spectral):
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
def check_train_test_distribution(y_train,
y_test,
classes,
sequential=False):
"""Observe the distribution of the train and test sets"""
if sequential:
y_test = decoder(y_test)
y_train = decoder(y_train)
else:
pass
train_class_counts = [(y_train == 0).sum(),
(y_train == 1).sum(),
(y_train == 2).sum(),
(y_train == 3).sum()]
test_class_counts = [(y_test == 0).sum(),
(y_test == 1).sum(),
(y_test == 2).sum(),
(y_test == 3).sum()]
width_x = max(len(x) for x in classes)
res = "\n".join(
"{:>{}} : {} {}".format(x, width_x, y, z) for x, y, z in zip(classes, train_class_counts, test_class_counts))
print(res + "\n")
def random_forest_classification(dataset):
"""Random Forest Classification"""
# Split data into training and test set
X = dataset.iloc[:, 0:64]
y = dataset.iloc[:, 64]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
check_train_test_distribution(y_train, y_test, classes)
# Create a Gaussian Classifier
RFclf = RandomForestClassifier(n_estimators=100)
# Train the model using the training sets
print("Randomforest Training Session has begun..\n")
RFclf.fit(X_train, y_train)
# Predict from Test set
y_pred = RFclf.predict(X_test)
# Compare prediction and actual class with accuracy score
print("Accuracy : {accuracy:.3f}\n".format(accuracy=metrics.accuracy_score(y_pred, y_test)))
cm = metrics.confusion_matrix(y_test, y_pred)
fig = plt.figure()
plot_confusion_matrix(cm, classes=classes, title="Confusion Matrix: RandomForest Model")
# fig.savefig("Resources/cmRF.png", dpi=100)
plt.show()
def decoder(y_list):
"""One-hot Decoder Specified for LSTM Classification"""
y_classes = []
for el in y_list:
y_classes.append(np.argmax(el))
return np.array(y_classes)
def lstm_model(n_steps, n_features):
"""LSTM Model Definition"""
model = Sequential()
model.add(
LSTM(50, return_sequences=True, input_shape=(n_steps, n_features)))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=64))
model.add(Dense(units=128))
model.add(Dense(units=4, activation="softmax"))
model.compile(optimizer='adam', loss='mse')
return model
def lstm_classification(dataset, saved_model=False):
"""LSTM classification"""
model = None
history = None
# sc = MinMaxScaler(feature_range=(0, 1))
ssc = StandardScaler()
# Reshape the input data for LSTM net
X = np.array(dataset.iloc[:, 0:64])
y = np.array(dataset.iloc[:, 64])
# Reshape to one flatten vector
X = X.reshape(X.shape[0] * X.shape[1], 1)
X = ssc.fit_transform(X)
X = X.reshape((-1, 8, 8))
for i in range(len(X)):
X[i] = X[i].reshape((8, 8)).T
# Convert to one hot
y = np.eye(np.max(y) + 1)[y]
# Split data into training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
check_train_test_distribution(y_train, y_test, classes, sequential=True)
if saved_model:
# Load Model
print("Trained LSTM Model is loading..\n")
model = load_model('Resources/model_cross_splited_data_SC_250epochs.h5')
model.summary()
elif not saved_model:
# Define the Model
model = lstm_model(X_train.shape[1], X_train.shape[2]) # n_steps = 8, n_features = 8
print("LSTM Training Session has begun..\n")
history = model.fit(X_train, y_train, epochs=250, batch_size=32, verbose=2)
# list all data in history
print(history.history.keys())
# summarize history for loss
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# Save
model.save("Resources/model_cross_splited_data_SC_250epochsv2.h5")
print("Saved model to disk\n")
y_pred = model.predict(X_test)
y_pred_classes = decoder(y_pred)
y_test_classes = decoder(y_test)
print("Accuracy : {accuracy:.3f}\n".format(accuracy=metrics.accuracy_score(y_pred_classes, y_test_classes)))
cm = metrics.confusion_matrix(y_test_classes, y_pred_classes)
fig = plt.figure()
plot_confusion_matrix(cm, classes=classes, title="Confusion Matrix: LSTM model")
# fig.savefig("Resources/nonSCcmLSTM.png", dpi=100)
plt.show()
if __name__ == '__main__':
print("Main Function is running..\n")
plot_8sensors_data(rock_dataset.iloc[1], classes[0])
plot_8sensors_data(paper_dataset.iloc[1], classes[1])
plot_8sensors_data(scissors_dataset.iloc[1], classes[2])
plot_8sensors_data(ok_dataset.iloc[1], classes[3])
random_forest_classification(dataset)
lstm_classification(dataset, saved_model=True)