-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtrain_model.py
61 lines (49 loc) · 1.79 KB
/
train_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
from typing import Sequence
import numpy as np
import pandas as pd
from keras.layers import LSTM, Dense, Dropout
from keras.models import Sequential
from sklearn.model_selection import train_test_split
neutral_df = pd.read_csv("neutral.txt")
resting_df = pd.read_csv("resting.txt")
holding_df = pd.read_csv("holding.txt")
gripping_df = pd.read_csv("gripping.txt")
X = []
y = []
no_of_timesteps = 20
datasets = neutral_df.iloc[:, 1:].values
n_samples = len(datasets)
for i in range(no_of_timesteps, n_samples):
X.append(datasets[i-no_of_timesteps:i, :])
y.append(0)
datasets = resting_df.iloc[:, 1:].values
n_samples = len(datasets)
for i in range(no_of_timesteps, n_samples):
X.append(datasets[i-no_of_timesteps:i, :])
y.append(1)
datasets = holding_df.iloc[:, 1:].values
n_samples = len(datasets)
for i in range(no_of_timesteps, n_samples):
X.append(datasets[i-no_of_timesteps:i, :])
y.append(2)
datasets = gripping_df.iloc[:, 1:].values
n_samples = len(datasets)
for i in range(no_of_timesteps, n_samples):
X.append(datasets[i-no_of_timesteps:i, :])
y.append(3)
X, y = np.array(X), np.array(y)
print(X.shape, y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X.shape[1], X.shape[2])))
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=4, activation="softmax"))
model.compile(optimizer="adam", metrics=["accuracy"], loss="sparse_categorical_crossentropy")
model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))
model.save("lstm-hand-grasping.h5")