-
Notifications
You must be signed in to change notification settings - Fork 4
/
LSTM_Network.py
276 lines (240 loc) · 10.3 KB
/
LSTM_Network.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import matplotlib.pyplot as plt
import os
import numpy as np
from random import shuffle
import copy
import time
use_gpu = torch.cuda.is_available()
trainPath = 'ucf101_resnet18Feat/train/'
testPath = 'ucf101_resnet18Feat/test/'
classes = os.listdir(trainPath)
classes.sort()
labels = np.arange(101)
trainShuffList = []
labelShuffList = []
for c in range(101):
files = os.listdir(trainPath+classes[c])
for f in files:
trainShuffList.append(classes[c]+'/'+f)
labelShuffList.append(float(labels[c]))
# Shuffling data list and label list
trainList = list(zip(trainShuffList, labelShuffList))
shuffle(trainList)
trainShuffList, labelShuffList = zip(*trainList)
testList = []
testLabelList = []
for c in range(101):
files = os.listdir(testPath+classes[c])
for f in files:
testList.append(classes[c]+'/'+f)
testLabelList.append(float(labels[c]))
class net_LSTM(nn.Module):
def __init__(self, input_sz, hidden_sz, nLayers, nClasses):
super(net_LSTM, self).__init__()
self.lstm = nn.LSTM(input_sz, hidden_sz, nLayers, batch_first=True)
self.fc = nn.Linear(hidden_sz, nClasses)
def forward(self, x):
out, _ = self.lstm(x)
# Output from hidden state of last time step
out = self.fc(out[:, -1, :])
return out
def train(net, inputs, labels, optimizer, criterion):
net.train(True)
if use_gpu:
inputs, labels = Variable(inputs).cuda(), Variable(labels).cuda()
else:
inputs, labels = Variable(inputs), Variable(labels)
# Feed-forward
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
# Initialize gradients to zero
optimizer.zero_grad()
# Compute loss/error
loss = criterion(F.log_softmax(outputs), labels)
# Backpropagate loss and compute gradients
loss.backward()
# Update the network parameters
optimizer.step()
if use_gpu:
correct = (predicted.cpu() == labels.data.cpu()).sum()
else:
correct = (predicted == labels.data).sum()
return net, loss.data[0], correct
def test(net, inputs, labels, criterion):
net.train(False)
if use_gpu:
inputs, labels = Variable(inputs).cuda(), Variable(labels).cuda()
else:
inputs, labels = Variable(inputs), Variable(labels)
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
# Compute loss/error
loss = criterion(F.log_softmax(outputs), labels)
if use_gpu:
correct = (predicted.cpu() == labels.data.cpu()).sum()
else:
correct = (predicted == labels.data).sum()
return loss.data[0], correct
net = net_LSTM(512, 8, 2, nClasses=101) # Input feature length->512, hidden layer size->8, number of layers->2
if use_gpu:
net = net.cuda()
criterion = nn.NLLLoss() # Negative Log-likelihood
optimizer = optim.Adam(net.parameters(), lr=1e-4) # Adam
epochs = 500
bSize = 32 # Batch size
L = 32 # Number of time steps
bCount = len(trainShuffList)//bSize # Number of batches in train set
lastBatch = len(trainShuffList)%bSize # Number of samples in last batch of train set
test_bCount = len(testList)//bSize # Number of batches in test set
test_lastBatch = len(testList)%bSize # Number of samples in last batch of test set
# Lists for saving train/test loss and accuracy
trainLoss = []
trainAcc = []
testLoss = []
testAcc = []
start = time.time()
for epochNum in range(epochs):
# Shuffling train data for each epoch
trainList = list(zip(trainShuffList, labelShuffList))
shuffle(trainList)
trainShuffList, labelShuffList = zip(*trainList)
trainRunLoss = 0.0
testRunLoss = 0.0
trainRunCorr = 0
testRunCorr = 0
epochStart = time.time()
## Train
# Load data tensors batchwise
idx = 0
for bNum in range(bCount):
first = True
# Loading one batch
for dNum in range(idx,idx+bSize):
if first:
loadData = torch.load(trainPath+trainShuffList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
batchData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchLabel = torch.Tensor([labelShuffList[dNum]]).long()
first = False
else:
loadData = torch.load(trainPath+trainShuffList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
tempData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchData = torch.cat((batchData,tempData), dim=0)
batchLabel = torch.cat((batchLabel,torch.Tensor([labelShuffList[dNum]]).long()),dim=0)
# Train the network on current batch
net, tr_loss, tr_corr = train(net, batchData, batchLabel, optimizer, criterion)
trainRunLoss += tr_loss
trainRunCorr += tr_corr
idx += bSize
# Loading last batch
if lastBatch != 0:
first = True
for dNum in range(idx,idx+lastBatch):
if first:
loadData = torch.load(trainPath+trainShuffList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
batchData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchLabel = torch.Tensor([labelShuffList[dNum]]).long()
first = False
else:
loadData = torch.load(trainPath+trainShuffList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
tempData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchData = torch.cat((batchData,tempData), dim=0)
batchLabel = torch.cat((batchLabel,torch.Tensor([labelShuffList[dNum]]).long()),dim=0)
# Training network on last batch
net, tr_loss, tr_corr = train(net, batchData, batchLabel, optimizer, criterion)
trainRunLoss += tr_loss
trainRunCorr += tr_corr
# Average training loss and accuracy for each epoch
avgTrainLoss = trainRunLoss/float(len(trainShuffList))
trainLoss.append(avgTrainLoss)
avgTrainAcc = trainRunCorr/float(len(trainShuffList))
trainAcc.append(avgTrainAcc)
## Test
# Load data tensors batchwise
idx = 0
for bNum in range(test_bCount):
first = True
# Loading one batch
for dNum in range(idx,idx+bSize):
if first:
loadData = torch.load(testPath+testList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
batchData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchLabel = torch.Tensor([testLabelList[dNum]]).long()
first = False
else:
loadData = torch.load(testPath+testList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
tempData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchData = torch.cat((batchData,tempData), dim=0)
batchLabel = torch.cat((batchLabel,torch.Tensor([testLabelList[dNum]]).long()),dim=0)
# Test the network on current batch
ts_loss, ts_corr = test(net, batchData, batchLabel, criterion)
testRunLoss += ts_loss
testRunCorr += ts_corr
idx += bSize
# Loading last batch
if test_lastBatch != 0:
first = True
for dNum in range(idx,idx+test_lastBatch):
if first:
loadData = torch.load(testPath+testList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
batchData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchLabel = torch.Tensor([testLabelList[dNum]]).long()
first = False
else:
loadData = torch.load(testPath+testList[dNum])
sz = loadData.size(0)
idx1 = torch.from_numpy(np.arange(0,(sz//L)*L,sz//L))
tempData = torch.index_select(loadData,dim=0,index=idx1).unsqueeze(0)
batchData = torch.cat((batchData,tempData), dim=0)
batchLabel = torch.cat((batchLabel,torch.Tensor([testLabelList[dNum]]).long()),dim=0)
# Test network on last batch
ts_loss, ts_corr = test(net, batchData, batchLabel, criterion)
testRunLoss += ts_loss
testRunCorr += tr_corr
# Average testing loss and accuracy for each epoch
avgTestLoss = testRunLoss/float(len(testList))
testLoss.append(avgTestLoss)
avgTestAcc = testRunCorr/float(len(testList))
testAcc.append(avgTestAcc)
# Plotting training loss vs Epochs
fig1 = plt.figure(1)
plt.plot(range(epochNum+1),trainLoss,'r-',label='train')
plt.plot(range(epochNum+1),testLoss,'g-',label='test')
if epochNum==0:
plt.legend(loc='upper left')
plt.xlabel('Epochs')
plt.ylabel('Loss')
# Plotting testing accuracy vs Epochs
fig2 = plt.figure(2)
plt.plot(range(epochNum+1),trainAcc,'r-',label='train')
plt.plot(range(epochNum+1),testAcc,'g-',label='test')
if epochNum==0:
plt.legend(loc='upper left')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
epochEnd = time.time()-epochStart
print('Iteration: {:.0f} /{:.0f}; Training Loss: {:.6f} ; Training Acc: {:.3f}'\
.format(epochNum + 1,epochs, avgTrainLoss, avgTrainAcc*100))
print('Iteration: {:.0f} /{:.0f}; Testing Loss: {:.6f} ; Testing Acc: {:.3f}'\
.format(epochNum + 1,epochs, avgTestLoss, avgTestAcc*100))
print('Time consumed: {:.0f}m {:.0f}s'.format(epochEnd//60,epochEnd%60))
end = time.time()-start
print('Training completed in {:.0f}m {:.0f}s'.format(end//60,end%60))