-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
331 lines (259 loc) · 12.6 KB
/
main.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
from __future__ import unicode_literals, print_function, division
from io import open
import unicodedata
import string
import re
import random
import glob
import time
import math
from Lang import Lang
import torch
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
from AttDecoder import AttnDecoderRNN
from DecoderRnn import DecoderRNN
from EncoderRnn import EncoderRNN
import json
import numpy as np
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import sklearn.model_selection._split
from generator import getGenerator
MAX_LENGTH = 50
SOS_token = torch.zeros(1947)
SOS_token[0] = 1
EOS_token = torch.zeros(1947)
EOS_token[1] = 1
teacher_forcing_ratio = 0.5
plt.switch_backend('agg')
capLang = None
def showPlot(points):
plt.figure()
fig, ax = plt.subplots()
# this locator puts ticks at regular intervals
loc = ticker.MultipleLocator(base=0.2)
ax.yaxis.set_major_locator(loc)
plt.plot(points)
plt.savefig("experiment.png")
def asMinutes(s):
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)
#this function takes two arguments .
#"since" is the time when this training started.
#percent is currentEpoch/total epoch yan kitna percent hogyahai
def timeSince(since, percent):
now = time.time()
s = now - since
es = s / (percent)
rs = es - s
return '%s (- %s)' % (asMinutes(s), asMinutes(rs))
#receive a sentence and return array of indexes of words in sentence (hotkey)
def indexesFromSentence(lang, sentence):
return [lang.word2index[word] for word in sentence.split(' ')]
#append EOS and return Tensors
def tensorFromSentence(lang, sentence):
indexes = indexesFromSentence(lang, sentence)
indexes.append(EOS_token)
#indexes = torch.tensor(indexes, dtype=torch.long)
final = torch.tensor([])
for i in indexes:
if(len(final) == 0):
final = torch.zeros(lang.n_words)
final[torch.tensor(i)] = 1
else:
temp = torch.zeros(lang.n_words)
temp[torch.tensor(i ,dtype=torch.long)] = 1
final = torch.cat((final , temp) , dim=0)
final = torch.cat((final , EOS_token))
final = final.view(-1 , lang.n_words)
return final
def tensorsFromPair(input_lang,pair):
target_tensor = tensorFromSentence(input_lang, pair[1])
input_tensor = torch.tensor(pair[0] , dtype=torch.float)
return (input_tensor, target_tensor)
#remove non letter charecters
def normalizeString(s):
s = re.sub(r"([.!?])", r" \1", s)
s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
return s
def getCaption(info , id):
for v in info["sentences"]:
if(v["video_id"] == "video" + str(id)):
return normalizeString(v["caption"])
#Returns list of tuples
def GetfeatureVocabPair(file):
print("reading ...")
with open("info.json", 'r' , encoding='utf8') as f:
info = json.load(f)
arr = []
list = glob.glob(file +"/*.npy")
for l in list:
feature = np.load(l.split("/")[-1])
id = l.split("/")[-1].split(".npy")[0].split("video")[-1]
caption = getCaption(info ,id)
arr.append((feature , caption , id))
print(id)
return arr
#get pairs (videoFeature, caption) and build vocab
def PrepareData(file):
pairs = GetfeatureVocabPair(file)
capLang = Lang(name="EnglishCaptions")
for pair in pairs:
capLang.addSentence(pair[1])
return capLang , pairs
#This function is single step in training
def train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, max_length=MAX_LENGTH):
encoder_hidden = encoder.initHidden("lstm")
encoder_optimizer.zero_grad()
decoder_optimizer.zero_grad()
#first dimention that is
#input and output sequence lengths
input_length = input_tensor.size(0)
target_length = target_tensor.size(0)
encoder_outputs = torch.zeros(max_length, encoder.hidden_size)
loss = 0
#input mein pehla frame dia sath hi hidden state di. phr us say output
#or next hiden state ai wo phr say daidi. input sequence lenght tak yehi kia
for ei in range(input_length):
encoder_output, encoder_hidden = encoder (input_tensor[ei], encoder_hidden)
encoder_outputs[ei] = encoder_output[0, 0]
#deoder input mein abhi sirf SOS Token dala hai
#pechay say jo hidden state aithi wo.
decoder_input = torch.tensor(SOS_token)
decoder_hidden = ((encoder_hidden[0][0] + encoder_hidden[0][1]).unsqueeze(0) , (encoder_hidden[1][0] + encoder_hidden[1][1]).unsqueeze(0))
#use_teacher_forcing = True if random.random() < teacher_forcing_ratio else False
use_teacher_forcing = False
#print("train------------------------------------------------")
if use_teacher_forcing:
# Teacher forcing: Feed the target as the next input
for di in range(target_length):
decoder_output, decoder_hidden , decoder_att = decoder(decoder_input, decoder_hidden , encoder_outputs)
target = torch.argmax(target_tensor[di]).view(1)
predicted = torch.argmax(decoder_output.view(-1))
#print("= " + str(capLang.index2word[int(target)]))
#print("-> " + str(capLang.index2word[int(predicted)]))
#print("loss = {}".format(loss))
loss += criterion(torch.tensor(decoder_output.view(1 , -1) , dtype=torch.float) , target)
decoder_input = target_tensor[di] # Teacher forcing
if (torch.argmax(decoder_input) == 1):
break
else:
# Without teacher forcing: use its own predictions as the next input
for di in range(target_length):
decoder_output, decoder_hidden, decoder_att = decoder(decoder_input, decoder_hidden , encoder_outputs)
decoder_input = decoder_output.view(-1).detach()
target = torch.argmax(target_tensor[di]).view(1)
predicted = predicted = torch.argmax(decoder_output.view(-1))
#print("= " + str(capLang.index2word[int(target)]))
#print("-> " + str(capLang.index2word[int(predicted)]))
#print("loss = {}".format(loss))
loss += criterion(torch.tensor(decoder_output.view(1 , -1) , dtype=torch.float), target)
if (torch.argmax(decoder_input) == 1):
break
loss.backward()
encoder_optimizer.step()
decoder_optimizer.step()
return loss.item() / target_length
def trainIters(language, pairs, encoder, decoder, epochs, batch, print_every=1000, plot_every=50, learning_rate=0.01, cont=False):
if(cont):
encoder.load_state_dict(torch.load("encoder.pt"))
decoder.load_state_dict(torch.load("decoder.pt"))
decoder = decoder.eval()
start = time.time()
plot_losses = []
print_loss_total = 0 # Reset every print_every
plot_loss_total = 0 # Reset every plot_every
encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate)
decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate)
training_pairs = [tensorsFromPair(language, i) for i in pairs]
criterion = nn.NLLLoss()
#ye hai wo loop jis mien training ho rahi hai
for i in range(1 , epochs+1):
loss_total = np.array([])
plot_loss_total = np.array([])
for iter in range(len(training_pairs)):
training_pair = training_pairs[iter]
input_tensor = training_pair[0]
target_tensor = training_pair[1]
#for one example
loss = train(input_tensor, target_tensor, encoder,decoder, encoder_optimizer, decoder_optimizer, criterion)
loss_total = np.append(loss_total, loss)
plot_loss_total = np.append(plot_loss_total,loss)
if i % print_every == 0:
print("Epoch: {}\t loss = {}\nEst: {}\n".format(i, np.average(loss_total), timeSince(start , i/epochs)))
if i % plot_every == 0:
plot_loss_avg = np.average(plot_loss_total)
plot_losses.append(plot_loss_avg)
torch.save(encoder.state_dict() , "encoder.pt")
torch.save(decoder.state_dict() , "decoder.pt")
showPlot(plot_losses)
def evaluate(encoder, decoder, video, inputlang, max_length=MAX_LENGTH):
encoder.load_state_dict(torch.load("encoder.pt"))
decoder.load_state_dict(torch.load("decoder.pt"))
decoder = decoder.eval()
encoder_outputs = torch.zeros(max_length, encoder.hidden_size)
encoder_hidden = encoder.initHidden("lstm")
video = torch.tensor(video, dtype=torch.float)
for ei in range(max_length):
encoder_output, encoder_hidden = encoder (video[ei], encoder_hidden)
encoder_outputs[ei] = encoder_output[0, 0]
decoder_hidden = ((encoder_hidden[0][0] + encoder_hidden[0][1]).unsqueeze(0),
(encoder_hidden[1][0] + encoder_hidden[1][1]).unsqueeze(0))
decoder_input = torch.tensor(SOS_token)
Sentence = ""
for di in range(max_length):
word = inputlang.index2word[int(torch.argmax(decoder_input))]
Sentence = Sentence + " " + word
decoder_output, decoder_hidden , decoder_att = decoder(decoder_input, decoder_hidden , encoder_outputs)
decoder_input = decoder_output.view(-1).detach()
if (torch.argmax(decoder_input) == 1):
break
return Sentence
def GetCartoonPairs(arr, Pairs):
newArr = []
for i in arr:
idvid = i.split("video")[-1]
for p in Pairs:
if(idvid == p[-1]):
newArr.append(p)
print(p[1])
return newArr
#returns train , test
def trainTestSplit(arr , percentTrain):
mid = int((percentTrain / 100) * len(arr))
np.random.shuffle(arr)
return arr[:mid] , arr[mid:]
def main():
global capLang
hidden_size = 128
features_size = 128
encoder1 = EncoderRNN(features_size ,hidden_size , layers=1 )
attn_decoder1 = AttnDecoderRNN(15, hidden_size,features_size, dropout_p=0.2)
#decoder1 = DecoderRNN(capLang.n_words , hidden_size , layers=1)
'''
Here we will plug the code to load the data in batches using the generator that we created
'''
trainGenerator = getGenerator('featuresBig', 5, 999)
#testGenerator = getGenerator('featuresBig', 5, 999, train=False)
for language, pair in trainGenerator:
trainIters(language, pair, encoder1, attn_decoder1, epochs=10 , print_every=1, batch=batch, cont=False)
cartoonID = open("cartoonID.txt", "r")
arr = cartoonID.read().split(" ")
Pairs = TestPairs
#Pairs = GetCartoonPairs(arr , Pairs)
#capLang, Pairs = PrepareData("featuresBig")
results = open("results.txt","w")
for i in range(len(Pairs)):
pair = random.choice(Pairs)
results.write("video" + str(pair[-1]) + str("\n"))
results.write("Real:| " + str(pair[1])+str("\n"))
print("video" + str(pair[-1]))
print("Real:| " + str(pair[1]))
sent = evaluate(encoder1, attn_decoder1, pair[0], capLang)
results.write("predicted:| " + str(sent) + str("\n"))
print("predicted:| " + str(sent) + str("\n\n"))
results.close()
main()