-
Notifications
You must be signed in to change notification settings - Fork 1
/
lstm.py
160 lines (137 loc) · 4.85 KB
/
lstm.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
import random
import tensorflow as tf
import numpy as np
#############
### Utils ###
#############
# Network, input and training parameters
classes = 33 # number of action classes
batch_size = 256 # size of training/validation batch
feature_size = 4096 # size of the feature vector
n_steps = 10 # size of the temporal window
n_hidden = 2048 # size of the hidden layer
n_epochs = 3000 # duration of training
epoch_size = 1 # number of batches per epoch
# Load helper files
def get_file_list(preffix):
files = []
for i in range(classes):
files.append([])
for i in range(classes):
filename = preffix
filename += str(i)
filename += ".txt"
fp = open(filename, 'r')
for line in fp:
files[i].append(line[:-1])
fp.close()
return files
train_files = get_file_list("helper_files/train_")
val_files = get_file_list("helper_files/val_")
# Function to create a random batch of action samples
# Classes have the same probability
# Samples from a same class have the same probability
def get_new_batch(files):
features = np.array([])
labels = np.array([])
for i in range(batch_size):
c = random.randint(0, classes-1)
img = random.randint(0, len(files[c])-1)
names = files[c][img].split(";")
fc7 = np.loadtxt(names[0])
fc7 = fc7.reshape(1, fc7.shape[0])
for j in range(1, 10):
fc_cont = np.loadtxt(names[j])
fc_cont = fc_cont.reshape(1, fc_cont.shape[0])
fc7 = np.concatenate((fc7, fc_cont), axis=0)
fc7 = fc7.reshape((1, fc7.shape[0], fc7.shape[1]))
if i == 0:
features = fc7
labels = np.array([[c]])
else:
features = np.concatenate((features, fc7))
labels = np.concatenate((labels, np.array([[c]])))
return features, labels
############################
### Network architecture ###
############################
# Input
xs = tf.placeholder(tf.float32, [None, n_steps, feature_size])
ys = tf.placeholder(tf.int64, [None, 1])
ys_one_hot = tf.one_hot(ys, classes)
input_dropout = tf.placeholder(tf.float32)
inner_dropout = tf.placeholder(tf.float32)
xs_drop = tf.nn.dropout(xs, input_dropout)
# 1st LSTM layer
xs_ = tf.unstack(xs_drop, n_steps, 1)
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
lstm, _ = tf.contrib.rnn.static_rnn(lstm_cell, xs_, dtype=tf.float32)
lstm_drop = tf.nn.dropout(lstm[-1], inner_dropout)
# 1st fully connected layer
W_fc = tf.Variable(tf.truncated_normal([n_hidden, classes], stddev=0.1))
b_fc = tf.Variable(tf.constant(0.1, shape=[classes]))
fc = tf.matmul(lstm_drop, W_fc) + b_fc
# Loss function
softmax_loss = tf.nn.softmax_cross_entropy_with_logits(labels=ys_one_hot, logits=fc)
loss = tf.reduce_mean(softmax_loss)
# Optimization
train_op = tf.train.AdamOptimizer(1e-4).minimize(loss)
# Evaluation
_, top5 = tf.nn.top_k(fc, 5)
result = tf.argmax(fc, 1)
ground_truth = tf.reshape(ys, [-1])
correct_prediction = tf.equal(result, ground_truth)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#####################
### Training loop ###
#####################
init = tf.global_variables_initializer()
saver = tf.train.Saver(max_to_keep=0)
with tf.Session() as sess:
# Initialize parameters
sess.run(init)
### In case of interruption, load parameters from the last iteration (ex: 29)
#saver.restore(sess, './model_lstm_29')
### And update the loop to account for the previous iterations
#for i in range(29,n_epochs):
for i in range(n_epochs):
# Run 1 epoch
vloss = []
acc = []
for j in range(epoch_size):
x_train, y_train = get_new_batch(train_files)
ret = sess.run([train_op, loss, accuracy], feed_dict = {xs: x_train, ys: y_train, inner_dropout: 0.5, input_dropout: 0.2})
vloss.append(ret[1])
acc.append(ret[2])
print 'TRAIN '+str(i+1)+':', np.mean(vloss), np.mean(acc)
# Log training loss and accuracy for current epoch
fp = open('log_lstm.txt', 'a')
fp.write('TRAIN ' + str(i+1) + ' ' + str(np.mean(vloss)) + ' ' + str(np.mean(acc)) + '\n')
fp.close()
# Save network parameters
if (i+1)%100 == 0:
path = 'model_lstm_' + str(i+1)
save_path = saver.save(sess, path)
# Run validation
if (i+1)%1 == 0:
cont1 = 0
cont5 = 0
vloss = []
for j in range(epoch_size):
x_train, y_train = get_new_batch(val_files)
ret_all = sess.run([top5, loss], feed_dict = {xs: x_train, ys: y_train, inner_dropout: 1.0, input_dropout: 1.0})
ret = ret_all[0]
vloss.append(ret[1])
for k in range(batch_size):
c = y_train[k][0]
if c == ret[k][0]:
cont1 += 1
cont5 += 1
elif c == ret[k][1] or c == ret[k][2] or c == ret[k][3] or c == ret[k][4]:
cont5 += 1
print 'VAL '+str(i+1)+':', np.mean(vloss), (100.*cont1)/(epoch_size*batch_size), (100.*cont5)/(epoch_size*batch_size)
# Log Rank-1 and Rank-5
fp = open('log_lstm.txt', 'a')
fp.write('VAL ' + str(i+1) + ' ' + str(np.mean(vloss)) + ' ' + str((100.*cont1)/(epoch_size*batch_size)) + ' ' + str((100.*cont5)/(epoch_size*batch_size)) + '\n')
fp.close()