-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainingModel.py
More file actions
75 lines (58 loc) · 2.36 KB
/
TrainingModel.py
File metadata and controls
75 lines (58 loc) · 2.36 KB
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
from sklearn.model_selection import train_test_split
import numpy as np
import tensorflow as tf
import os
from keras.callbacks import TensorBoard
DATA_PATH = os.path.join('MP_Data_New_2')
DATA_PATH_2 = os.path.join('MP_Data_New_Trained')
actions = np.load('actionsArray.npy')
no_sequences = 30
sequence_length =30
label_map = {label: num for num, label in enumerate(actions)}
print(label_map)
sequences, labels = [],[]
for action in actions:
for sequence in range (no_sequences):
window =[]
for frame_num in range(sequence_length ):
res = np.load(os.path.join(DATA_PATH,action,str(sequence),"{}.npy".format(frame_num)))
window.append(res)
sequences.append(window)
labels.append(label_map[action])
window =[]
for frame_num in range(sequence_length ):
res = np.load(os.path.join(DATA_PATH_2,action,str(sequence),"{}.npy".format(frame_num)))
window.append(res)
sequences.append(window)
labels.append(label_map[action])
x=np.array(sequences)
y = tf.keras.utils.to_categorical(labels).astype(int)
print(x.shape)
print("//////////////////////////////////////////////////////////////////////")
print(y.shape)
# x_train, x_test,y_train, y_test = train_test_split(x,y, test_size=0.05)
# print(x_train.shape)
# print(y_train.shape)
# print(x_test.shape)
# print(y_test.shape)
log_dir = os.path.join("Logs")
tb_callback = TensorBoard(log_dir=log_dir)
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(64,return_sequences=True, activation='relu', input_shape =(30,126)))
model.add(tf.keras.layers.LSTM(128,return_sequences=True, activation='relu'))
model.add(tf.keras.layers.LSTM(64,return_sequences=False, activation='relu'))
model.add(tf.keras.layers.Dense(64,activation='relu'))
model.add(tf.keras.layers.Dense(32,activation='relu'))
model.add(tf.keras.layers.Dense(actions.shape[0], activation='softmax'))
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
model.fit(x, y, epochs=400, callbacks=[tb_callback])
# model.load_weights('action2.h5')
model.save('actionLarge400.h5')
model.summary()
# res = model.predict(x_test)
# print(res)
# print(actions[np.argmax(res[4])])
# print(actions[np.argmax(res[0])])
# print(actions[np.argmax(res[2])])
# print(actions[np.argmax(res[1])])
# print(actions[np.argmax(res[3])])