This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
rnn_utils.py
105 lines (84 loc) · 3.4 KB
/
rnn_utils.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
"""
Utilities used by our other RNN scripts.
"""
from collections import deque
from sklearn.model_selection import train_test_split
from tflearn.data_utils import to_categorical
import tflearn
import numpy as np
import pickle
def get_data(input_data_dump, num_frames_per_video, labels, ifTrain):
"""Get the data from our saved predictions or pooled features."""
# Local vars.
X = []
y = []
temp_list = deque()
# Open and get the features.
with open(input_data_dump, 'rb') as fin:
frames = pickle.load(fin)
for i, frame in enumerate(frames):
features = frame[0]
actual = frame[1].lower()
# frameCount = frame[2]
# Convert our labels into binary.
actual = labels[actual]
# Add to the queue.
if len(temp_list) == num_frames_per_video - 1:
temp_list.append(features)
flat = list(temp_list)
X.append(np.array(flat))
y.append(actual)
temp_list.clear()
else:
temp_list.append(features)
continue
print("Class Name\tNumeric Label")
for key in labels:
print("%s\t\t%d" % (key, labels[key]))
# Numpy.
X = np.array(X)
y = np.array(y)
print("Dataset shape: ", X.shape)
# One-hot encoded categoricals.
y = to_categorical(y, len(labels))
# Split into train and test.
if ifTrain:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
return X_train, X_test, y_train, y_test
else:
return X, y
def get_network(frames, input_size, num_classes):
"""Create our LSTM"""
net = tflearn.input_data(shape=[None, frames, input_size])
net = tflearn.lstm(net, 128, dropout=0.8, return_seq=True)
net = tflearn.lstm(net, 128)
net = tflearn.fully_connected(net, num_classes, activation='softmax')
net = tflearn.regression(net, optimizer='adam',
loss='categorical_crossentropy', name="output1")
return net
def get_network_deep(frames, input_size, num_classes):
"""Create a deeper LSTM"""
net = tflearn.input_data(shape=[None, frames, input_size])
net = tflearn.lstm(net, 64, dropout=0.2, return_seq=True)
net = tflearn.lstm(net, 64, dropout=0.2, return_seq=True)
net = tflearn.lstm(net, 64, dropout=0.2)
net = tflearn.fully_connected(net, num_classes, activation='softmax')
net = tflearn.regression(net, optimizer='adam',
loss='categorical_crossentropy', name="output1")
return net
def get_network_wide(frames, input_size, num_classes):
"""Create a wider LSTM"""
net = tflearn.input_data(shape=[None, frames, input_size])
net = tflearn.lstm(net, 256, dropout=0.2)
net = tflearn.fully_connected(net, num_classes, activation='softmax')
net = tflearn.regression(net, optimizer='adam',
loss='categorical_crossentropy', name='output1')
return net
def get_network_wider(frames, input_size, num_classes):
"""Create a wider LSTM"""
net = tflearn.input_data(shape=[None, frames, input_size])
net = tflearn.lstm(net, 512, dropout=0.2)
net = tflearn.fully_connected(net, num_classes, activation='softmax')
net = tflearn.regression(net, optimizer='adam',
loss='categorical_crossentropy', name='output1')
return net