forked from TangxinKevin/RGB_action_recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
86 lines (74 loc) · 2.58 KB
/
train.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
from data import DataSet
from lstm_model import MultiLstm
import tensorflow as tf
import numpy as np
from sklearn.model_selection import StratifiedKFold
import math
from itertools import chain
def train(seq_length):
# Set variables.
nb_epoch = 10000
batch_size = 32
regularization_value = 0.004
learning_rate = 0.001
nb_feature = 2048
database = 'HMDB'
data = DataSet(database, seq_length)
skf = StratifiedKFold(n_splits=5)
nb_class = len(data.classes)
# Set model
num_hiddens = [30, 30, 30, 30, 30]
seq_images = tf.placeholder(dtype=tf.float32,
shape=[None,
seq_length,
nb_feature])
input_labels = tf.placeholder(dtype=tf.float32,
shape=[None, nb_class])
drop_out = tf.placeholder(dtype=tf.float32)
rnn_model = MultiLstm(seq_images,
input_labels,
drop_out,
num_hiddens,
regularization_value,
learning_rate,
5)
# training
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
all_samples_prediction, all_samples_true = [], []
for train, test in skf.split(data.data,data.label):
genaretor = data.frame_generator_train(batch_size, train)
for epoch in range(nb_epoch):
batch_seq_images, batch_labels = next(genaretor)
sess.run(rnn_model.optimize,
feed_dict={seq_images: batch_seq_images,
input_labels: batch_labels,
drop_out: 0.5})
accuracy = sess.run(rnn_model.accuracy,
feed_dict={seq_images: batch_seq_images,
input_labels: batch_labels,
drop_out: 1.})
print("Epoch {:2d}, training accuracy {:4.2f}".format(epoch,
accuracy))
test_data, test_label = data.get_set_from_data(test)
all_samples_true.append(test_label)
for test_epoch in range(1, math.ceil(len(test) / batch_size) + 1):
test_batch_images = data.frame_generator_test(
test_data, batch_size, test_epoch)
test_predict_labels = sess.run(rnn_model.test_prediction,
feed_dict={seq_images: test_batch_images,
drop_out: 1.})
all_samples_prediction.append(list(test_predict_labels))
all_samples_prediction = np.array(list(chain.from_iterable(
all_samples_prediction)), dtype=float)
all_samples_true = np.array(list(chain.from_iterable(
all_samples_true)), dtype=float)
test_accuracy_cv = np.mean(np.equal(all_samples_prediction,
all_samples_true))
print("CV test accuracy {:4.2f}".format(test_accuracy_cv))
def main():
time_steps = 50
train(time_steps)
if __name__ == '__main__':
main()