-
Notifications
You must be signed in to change notification settings - Fork 1
/
L1_LSTM_Model.py
95 lines (85 loc) · 3.14 KB
/
L1_LSTM_Model.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
# lstm model
import numpy as np
import csv
import tensorflow as tf
# load a single file as a numpy array
def load_file(filepath):
data = []
with open(filepath) as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
data.append(row)
return np.array(data)
# load a list of files and return as a 3d numpy array
def load_group(filenames, prefix=''):
loaded = list()
for name in filenames:
data = load_file(prefix + name)
loaded.append(data)
# stack group so that features are the 3rd dimension
loaded = np.dstack(loaded)
return loaded
# load a dataset group, such as train or test
def load_dataset_group(group, prefix=''):
filepath = prefix + group
# load all 9 files as a single array
filenames = list()
# total acceleration
filenames = ['01_acc_x.csv', '02_acc_y.csv', '03_acc_z.csv',
'04_gyro_x.csv', '05_gyro_y.csv', '06_gyro_z.csv',
'07_euler_x.csv', '08_euler_y.csv', '09_euler_z.csv']
# load input data
X = load_group(filenames, filepath).astype(np.float64)
# load class output
y = load_file(prefix + group + '10_label.csv').astype(np.int)
return X, y
# load the dataset, returns train and test X and y elements
def load_dataset(prefix=''):
# load all train
trainX, trainy = load_dataset_group('train/', prefix + 'data/Gestures/Groups/')
# load all test
testX, testy = load_dataset_group('test/', prefix + 'data/Gestures/Groups/')
# zero-offset class values
trainy = trainy - 1
testy = testy - 1
# one hot encode y
trainy = tf.keras.utils.to_categorical(trainy)
testy = tf.keras.utils.to_categorical(testy)
return trainX, trainy, testX, testy
# fit and evaluate a model
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 1, 25, 8
print(trainX.shape, testX.shape)
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
model = tf.keras.models.Sequential([
tf.keras.layers.LSTM(100, input_shape=(n_timesteps,n_features)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(100, activation='relu'),
tf.keras.layers.Dense(n_outputs, activation='softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
# summarize scores
def summarize_results(scores):
print(scores)
m, s = np.mean(scores), np.std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
# run an experiment
def run_experiment(repeats=10):
# load data
trainX, trainy, testX, testy = load_dataset()
# repeat experiment
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
# summarize results
summarize_results(scores)
# run the experiment
run_experiment()