forked from aminullah6264/Pytorch-Action-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Training Code For LSTM.py
401 lines (310 loc) · 12.6 KB
/
Training Code For 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# -*- coding: utf-8 -*-
"""
Created on Tue May 9 15:50:43 2017
@author: AMIN
"""
# -*- coding: utf-8 -*-
"""
Created on Tue May 9 14:13:12 2017
@author: AMIN
"""
import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn
import sklearn.model_selection as sk
#X_train, X_Validation, Y_train, Y_Validation = sk.train_test_split(Features,lables,test_size=0.33,random_state = 42) #, random_state = 1
X_train=TrainData
Y_train=Tlables
X_Validation=ValData
Y_Validation=Vlables
hm_epochs = 1000
n_classes = 6
batch_size = 128
batch_size_val=1024
chunk_size =676
n_chunks =6
rnn_size = 128
#n_nodes_hl1 = 256
#n_nodes_hl2 = 128
#n_nodes_hl3 = 64
trainSamples,FeaturesLength=Y_train.shape
ValidationSamples,FeaturesLength=Y_Validation.shape
loss=[];
Val_Accuracy=[];
with tf.name_scope('Inputs'):
x = tf.placeholder('float', [None, None,chunk_size],name="Features")
y = tf.placeholder('float',name="Lables")
def recurrent_neural_network(x):
#
# W = {
# 'hidden': tf.Variable(tf.random_normal([chunk_size, rnn_size])),
# 'output': tf.Variable(tf.random_normal([rnn_size, n_classes]))
# }
# biases = {
# 'hidden': tf.Variable(tf.random_normal([rnn_size], mean=1.0)),
# 'output': tf.Variable(tf.random_normal([n_classes]))
# }
#
#
# x = tf.transpose(x, [1,0,2])
# x = tf.reshape(x, [-1,chunk_size])
# x = tf.nn.relu(tf.matmul(x, W['hidden']) + biases['hidden'])
# x = tf.split (x,n_chunks, 0)
# # new shape: n_steps * (batch_size, n_hidden)
#
# # Define two stacked LSTM cells (two recurrent layers deep) with tensorflow
# lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
# lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
# lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)
# # Get LSTM cell output
# outputs, final_states = tf.contrib.rnn.static_rnn(lstm_cells, x, dtype=tf.float32)
# # Get last time step's output feature for a "many to one" style classifier,
# # as in the image describing RNNs at the top of this page
## lstm_last_output=tf.transpose(outputs, [1,0,2])
# # Linear activation
# return tf.matmul(outputs[-1], W['output']) + biases['output']
#####################################################################
# Unstack to get a list of 'n_steps' tensors of shape (batch_size, n_input)
x = tf.unstack(x, n_chunks, 1)
lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
lstm_fw_cell = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)
lstm_cell_3 = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
lstm_cell_4 = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
lstm_bw_cell = tf.contrib.rnn.MultiRNNCell([lstm_cell_3, lstm_cell_4], state_is_tuple=True)
# Define lstm cells with tensorflow
# Forward direction cell
# lstm_fw_cell = rnn.BasicLSTMCell(rnn_size, forget_bias=1.0)
# # Backward direction cell
# lstm_bw_cell = rnn.BasicLSTMCell(rnn_size, forget_bias=1.0)
# Get lstm cell output
try:
outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
dtype=tf.float32)
except Exception: # Old TensorFlow version only returns outputs not states
outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
dtype=tf.float32)
# Hidden layer weights => 2*n_hidden because of forward + backward cells
weights = tf.Variable(tf.random_normal([2*rnn_size, n_classes]),name="weights1")
biases = tf.Variable(tf.random_normal([n_classes]),name="biases1")
# Linear activation, using rnn inner loop last output
return tf.matmul(outputs[-1], weights) + biases
#
######################################################################
# weights = {
# # Hidden layer weights => 2*n_hidden because of forward + backward cells
# 'out': tf.Variable(tf.random_normal([2*rnn_size, n_classes]))
# }
# biases = {
# 'out': tf.Variable(tf.random_normal([n_classes]))
# }
# # Unstack to get a list of 'n_steps' tensors of shape (batch_size, n_input)
# x = tf.unstack(x, n_chunks, 1)
#
# # Define lstm cells with tensorflow
# # Forward direction cell
# lstm_fw_cell = rnn.BasicLSTMCell(rnn_size, forget_bias=1.0)
# # Backward direction cell
# lstm_bw_cell = rnn.BasicLSTMCell(rnn_size, forget_bias=1.0)
#
# # Get lstm cell output
# try:
# outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
# dtype=tf.float32)
# except Exception: # Old TensorFlow version only returns outputs not states
# outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
# dtype=tf.float32)
#
# # Linear activation, using rnn inner loop last output
# return tf.matmul(outputs[-1], weights['out']) + biases['out']
#
#
#
#########################################################################
# x = tf.transpose(x, [1,0,2])
# x = tf.reshape(x, [-1,chunk_size])
# x = tf.split (x,n_chunks, 0)
# lstm_cell = rnn.BasicLSTMCell(rnn_size)
# outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
#
#
#
# weights1=tf.Variable(tf.random_normal([rnn_size, n_nodes_hl1], stddev=0.2),name="weights1") #,mean=0.2, stddev=0.2
# biases1=tf.Variable(tf.random_normal([n_nodes_hl1], stddev=0.2), name="biases1")
# l1 = tf.add(tf.matmul(outputs[-1],weights1), biases1)
# l1=tf.sigmoid(l1)
#
#
# weights2=tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2], stddev=0.2),name="weights2")
# biases2=tf.Variable(tf.random_normal([n_nodes_hl2], stddev=0.2), name="biases2")
# l2 = tf.add(tf.matmul(l1,weights2), biases2)
# l2=tf.sigmoid(l2)
#
# weights3=tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3], stddev=0.2),name="weights3")
# biases3=tf.Variable(tf.random_normal([n_nodes_hl3], stddev=0.2), name="biases3")
# l3 = tf.add(tf.matmul(l2,weights3), biases3)
# l3=tf.sigmoid(l3)
#
#
# weightsOutput=tf.Variable(tf.random_normal([n_nodes_hl3, n_classes], stddev=0.2),name="weightsOutput")
# biasesOutput=tf.Variable(tf.random_normal([n_classes], stddev=0.2), name="biasesOutput")
# output = tf.matmul(l3,weightsOutput)+ biasesOutput
# # output=tf.sigmoid(output)
# return output
def train_recurrnet_neural_network(x):
prediction= recurrent_neural_network(x)
# OLD VERSION:
#cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
# NEW:
best_accuracy = 0.0
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits
(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
with tf.Session() as sess:
# OLD:
#sess.run(tf.initialize_all_variables())
# NEW:
tf.device('/gpu:0')
sess.run(tf.global_variables_initializer())
# print(sess.run(weights))
kk=0
for epoch in range(hm_epochs):
epoch_loss = 0
valdd=[]
k=0;
for _ in range(int(trainSamples/batch_size)):
epoch_x = X_train[k:k+batch_size,:]
epoch_y = Y_train[k:k+batch_size,:]
epoch_x= epoch_x.reshape((batch_size, n_chunks, chunk_size ))
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
k=k+batch_size
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)
loss.append(epoch_loss)
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
accuracy_out = (accuracy.eval({x:X_Validation.reshape((-1,n_chunks, chunk_size)), y:Y_Validation}))
# kk=0
# for _ in range(int(ValidationSamples/batch_size_val)):
# valdd.append(accuracy.eval({x:X_Validation[kk:kk+batch_size_val,:].reshape((-1,n_chunks, chunk_size)), y:Y_Validation[kk:kk+batch_size_val,:]}))
# kk = kk+batch_size_val
# if kk > ValidationSamples:
# kk=0
# accuracy_out=np.mean(valdd)
Val_Accuracy.append(accuracy_out)
print('Validation Accuracy : ',accuracy_out,' ||| Best Accuracy :',best_accuracy)
# if accuracy_out > best_accuracy:
# best_accuracy=accuracy_out
# saver = tf.train.Saver()
# save_path = saver.save(sess, "D:\\New Experiments\\New Action Recognition\\Checkpoints\\model.chk")
# print("Model saved in file: %s" % save_path)
#Save the variables to disk.
# save_path = saver.save(sess, "D:\\Speech Project\\Dataset\\BerlinImages\\BerlinImages\\1_Singleimages\\RNN Model For 257x45 double data spects\\model.ckpt")
# print("Best Accuracy == " ,best_accuracy)
# merged = tf.summary.merge_all()
# writer=tf.summary.FileWriter("C:\\Users\\AMIN\\Anaconda2\\envs\\py35\\Lib\\site-packages\\tensorflow\\tensorboard\\otherLogs",sess.graph)
train_recurrnet_neural_network(x)
'''
import tables
file = tables.open_file('D:\\Action and Scenes\\Code\\DataHMDDATASET.mat')
lon = file.root.TotalFeatures[:]
import numpy
TotalFeaturesX=numpy.transpose(lon)
'''
''''
import h5py
import numpy as np
filepath = '/home/imlab/Desktop/RNN Codes/6SeqDataWith5FrameJump101dataSet.mat'
arrays = {}
f = h5py.File(filepath)
i=1
for k, v in f.items():
if(i==1):
Features=np.array(v)
if(i==2):
lables=np.array(v)
i=i+1
print(v)
Features=np.transpose(Features)
lables=np.transpose(lables)
'''
''''
import h5py
import numpy as np
filepath = '/home/imlab/Desktop/RNN Codes/Action DataSets/101 Dataset/ErrorFreeTestTrain101DataSet.mat'
arrays = {}
f = h5py.File(filepath)
i=1
for k, v in f.items():
if(i==1):
Tlables=np.array(v)
if(i==2):
TrainData=np.array(v)
if(i==3):
ValData=np.array(v)
if(i==4):
Vlables=np.array(v)
i=i+1
print(v)
TrainData=np.transpose(TrainData)
ValData=np.transpose(ValData)
Tlables=np.transpose(Tlables)
Vlables=np.transpose(Vlables)
'''
############################################################################
''''
import h5py
import numpy as np
filepath = '/home/imlab/Desktop/RNN Codes/Action DataSets/101 Dataset/101TrainData.mat'
arrays = {}
f = h5py.File(filepath)
i=1
for k, v in f.items():
if(i==1):
TrainData1=np.array(v)
if(i==2):
TrainData2=np.array(v)
if(i==3):
TrainData3=np.array(v)
if(i==4):
TrainData4=np.array(v)
if(i==5):
TrainData5=np.array(v)
if(i==6):
TrainData6=np.array(v)
if(i==7):
TrainData7=np.array(v)
if(i==8):
TrainData8=np.array(v)
if(i==9):
TrainData9=np.array(v)
if(i==10):
TrainData10=np.array(v)
i=i+1
print(v)
TrainData1=np.transpose(TrainData1)
TrainData2=np.transpose(TrainData2)
TrainData3=np.transpose(TrainData3)
TrainData4=np.transpose(TrainData4)
TrainData5=np.transpose(TrainData5)
TrainData6=np.transpose(TrainData6)
TrainData7=np.transpose(TrainData7)
TrainData8=np.transpose(TrainData8)
TrainData9=np.transpose(TrainData9)
TrainData10=np.transpose(TrainData10)
TrainData=np.concatenate([TrainData1,TrainData3,TrainData4,TrainData5,TrainData6,TrainData7,TrainData8,TrainData9,TrainData10,TrainData2]);
'''
''''
import h5py
import numpy as np
filepath = '/home/imlab/Desktop/RNN Codes/Action DataSets/101 Dataset/101ValData.mat'
arrays = {}
f = h5py.File(filepath)
i=1
for k, v in f.items():
if(i==1):
ValData=np.array(v)
i=i+1
print(v)
ValData=np.transpose(ValData)
'''