-
Notifications
You must be signed in to change notification settings - Fork 15
/
model.py
67 lines (52 loc) · 2.35 KB
/
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
# %%
# Import necessary libraries
import numpy as np
import os
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from itertools import product
from sklearn import metrics
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# Set the path to the data directory
PATH = os.path.join('data')
# Create an array of actions (signs) labels by listing the contents of the data directory
actions = np.array(os.listdir(PATH))
# Define the number of sequences and frames
sequences = 30
frames = 10
# Create a label map to map each action label to a numeric value
label_map = {label:num for num, label in enumerate(actions)}
# Initialize empty lists to store landmarks and labels
landmarks, labels = [], []
# Iterate over actions and sequences to load landmarks and corresponding labels
for action, sequence in product(actions, range(sequences)):
temp = []
for frame in range(frames):
npy = np.load(os.path.join(PATH, action, str(sequence), str(frame) + '.npy'))
temp.append(npy)
landmarks.append(temp)
labels.append(label_map[action])
# Convert landmarks and labels to numpy arrays
X, Y = np.array(landmarks), to_categorical(labels).astype(int)
# Split the data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.10, random_state=34, stratify=Y)
# Define the model architecture
model = Sequential()
model.add(LSTM(32, return_sequences=True, activation='relu', input_shape=(10,126)))
model.add(LSTM(64, return_sequences=True, activation='relu'))
model.add(LSTM(32, return_sequences=False, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(actions.shape[0], activation='softmax'))
# Compile the model with Adam optimizer and categorical cross-entropy loss
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
# Train the model
model.fit(X_train, Y_train, epochs=100)
# Save the trained model
model.save('my_model')
# Make predictions on the test set
predictions = np.argmax(model.predict(X_test), axis=1)
# Get the true labels from the test set
test_labels = np.argmax(Y_test, axis=1)
# Calculate the accuracy of the predictions
accuracy = metrics.accuracy_score(test_labels, predictions)