-
Notifications
You must be signed in to change notification settings - Fork 3
/
LSTM.py
331 lines (226 loc) · 7.67 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.autograd as autograd
import pickle
import numpy as np
import time
import matplotlib.pyplot as plt
import cv2
torch.manual_seed(1)
word_to_ix = {}
label_to_ix = {}
instructions = []
labels = []
with open('instructions.txt','r') as f:
f = f.readlines()
for line in f:
line = line.strip().split(',')
label = [line[1]]
sentence = list(map(lambda x : x.lower(),line[0].strip().split(' ')))
instructions.append((sentence,label))
#print (instructions)
for sent,label in instructions:
for word in sent:
if word not in word_to_ix:
word_to_ix[word] = len(word_to_ix)
for lab in label:
if lab not in label_to_ix:
label_to_ix[lab] = len(label_to_ix)
print(word_to_ix)
print(label_to_ix)
def prepare_sentence(sent, to_ix):
sent = sent.lower().strip().split(' ')
idxs = [to_ix[w] for w in sent]
return torch.tensor(idxs, dtype=torch.long)
class LSTMClassifier(nn.Module):
def __init__(self):
super(LSTMClassifier, self).__init__()
self.embeddings = nn.Embedding(VOCAB_SIZE, EMBEDDING_DIM)
self.lstm = nn.LSTM(EMBEDDING_DIM, HIDDEN_DIM_LSTM)
self.fullyconnected = nn.Linear(HIDDEN_DIM_LSTM, 10)
self.hidden = self.init_hidden()
def init_hidden(self):
# the first is the hidden h
# the second is the cell c
return (autograd.Variable(torch.zeros(1, 1, HIDDEN_DIM_LSTM)),
autograd.Variable(torch.zeros(1, 1, HIDDEN_DIM_LSTM)))
def forward(self, sentence):
embeds = self.embeddings(sentence)
x = embeds.view(len(sentence), 1, -1)
lstm_out, self.hidden = self.lstm(x, self.hidden)
#print (lstm_out)
y = self.fullyconnected(lstm_out[-1])
# log_probs = F.log_softmax(y)
#print (y)
return y
class ConvNetClassifier(nn.Module):
def __init__(self):
super(ConvNetClassifier, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(6, 32, kernel_size = 5, stride = 1, padding = 2),
nn.ReLU(),
nn.MaxPool2d(kernel_size = 2, stride = 2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size = 5, stride = 1, padding = 2),
nn.ReLU(),
nn.MaxPool2d(kernel_size = 2, stride = 2)
)
self.layer3 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size = 4, stride = 1, padding = 2),
nn.ReLU(),
nn.MaxPool2d(kernel_size = 2, stride = 2)
)
self.layer4 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size = 3, stride = 1, padding = 1)
)
self.layer5 = nn.Linear(26*20*64 , 10)
self.layer6 = nn.PReLU()
self.layer7 = nn.Linear(10, 10)
def forward(self, x):
x = np.swapaxes(x,0,2)
x = np.swapaxes(x,1,2)
x = autograd.Variable(torch.from_numpy(x).unsqueeze(0).float())
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = out.view(out.size(0), -1)
out = self.layer5(out)
out = self.layer6(out)
out = self.layer7(out)
#print (out)
return out
EMBEDDING_DIM = 20
HIDDEN_DIM_LSTM = 10
VOCAB_SIZE = len(word_to_ix)
LABEL_SIZE = len(label_to_ix)
text_model = LSTMClassifier()
image_model = ConvNetClassifier()
loss_function = nn.CosineEmbeddingLoss()
optimizer1 = optim.SGD(text_model.parameters(), lr = 0.001)
optimizer2 = optim.SGD(image_model.parameters(), lr = 0.001)
def train():
with open('dataset/dataset_true.pickle','rb') as f:
dataset = pickle.load(f)
with open('dataset/dataset_false.pickle','rb') as g:
dataset_false = pickle.load(g)
for epoch in range(100):
t1 = time.time()
total_loss = 0.0
for (frame1,frame2), sentence in dataset[:300]:
text_model.hidden = text_model.init_hidden()
text_model.zero_grad()
image_model.zero_grad()
enc_sentence = prepare_sentence(sentence, word_to_ix)
text_embed = text_model(enc_sentence)
stack = np.dstack((frame1,frame2))
frame_embed = image_model(stack)
loss = loss_function(text_embed, frame_embed,torch.tensor([1]).float())
total_loss += loss.item()
loss.backward()
torch.nn.utils.clip_grad_norm(text_model.parameters(),1)
torch.nn.utils.clip_grad_norm(image_model.parameters(),1)
optimizer1.step()
optimizer2.step()
ind = np.random.randint(0,10000,5)
for j in ind:
text_model.hidden = text_model.init_hidden()
text_model.zero_grad()
image_model.zero_grad()
enc_sentence = prepare_sentence(dataset_false[j][1], word_to_ix)
text_embed = text_model(enc_sentence)
stack = np.dstack(dataset_false[j][0])
frame_embed = image_model(stack)
loss = loss_function(text_embed, frame_embed,torch.tensor([-1]).float())
total_loss += loss.item()
loss.backward()
torch.nn.utils.clip_grad_norm(text_model.parameters(),1)
torch.nn.utils.clip_grad_norm(image_model.parameters(),1)
optimizer1.step()
optimizer2.step()
t2 = time.time()
print("epoch %d loss %f time %f"%(epoch,total_loss,t2-t1))
if (epoch+1) % 20 == 0:
torch.save(text_model, 'models/sentence/text_model_' + str(epoch+1))
torch.save(image_model, 'models/image/image_model_' + str(epoch+1))
def false_dataset():
#text_model = torch.load('models/text_model_50')
#image_model = torch.load('models/image_model_50')
with open('dataset/dataset_true.pickle','rb') as f:
dataset = pickle.load(f)
dataset_false = []
for i in range(300):
for j in range(300):
if dataset[i][1] != dataset[j][1]:
dataset_false.append((dataset[i][0],dataset[j][1]))
dataset_false.append((dataset[j][0],dataset[i][1]))
print (len(dataset_false))
with open('dataset/dataset_false.pickle','wb') as f:
pickle.dump(dataset_false,f)
test_dataset_false = []
for i in range(301, 347):
for j in range(301, 347):
if dataset[i][1] != dataset[j][1]:
test_dataset_false.append((dataset[i][0],dataset[j][1]))
test_dataset_false.append((dataset[j][0],dataset[i][1]))
print (len(test_dataset_false))
'''
with open('dataset/dataset_false.pickle','wb') as f:
pickle.dump(test_dataset_false,f)
'''
def test():
text_model = torch.load('models/text_model_60')
image_model = torch.load('models/image_model_60')
# True labels
with open('dataset.pickle','rb') as f:
true_dataset = pickle.load(f)
items = np.random.randint(301, 347, 15)
iter = 1
for i in items:
(img1, img2), text = true_dataset[i]
#img1 = img1[:,:,::-1]
#img2 = img2[:,:,::-1]
'''
enc_sentence = prepare_sentence(text, word_to_ix)
text_embed = text_model(enc_sentence)
stack = np.dstack((img1, img2))
frame_embed = image_model(stack)
dp = torch.dot(text_embed[0], frame_embed[0]) / (torch.norm(text_embed[0]) * torch.norm(frame_embed[0]))
print(dp)
'''
#both = np.hstack((img1, img2))
c1 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=[255,255,255])
c2 = cv2.copyMakeBorder(img2,10,10,10,10,cv2.BORDER_CONSTANT,value=[255,255,255])
both = np.hstack((c1,c2))
print (text)
cv2.imshow('sample', both)
cv2.waitKey(0)
'''
with open('test_dataset_false.pickle','rb') as f:
false_dataset = pickle.load(f)
items = np.random.randint(0, 3384, 15)
iter = 1
for i in items:
(img1, img2), text = false_dataset[i]
# img1 = img1[:,:,::-1]
# img2 = img2[:,:,::-1]
enc_sentence = prepare_sentence(text, word_to_ix)
text_embed = text_model(enc_sentence)
stack = np.dstack((img1, img2))
frame_embed = image_model(stack)
dp = torch.dot(text_embed[0], frame_embed[0]) / (torch.norm(text_embed[0]) * torch.norm(frame_embed[0]))
print(dp)
# both = np.hstack((img1, img2))
c1 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=[255,255,255])
c2 = cv2.copyMakeBorder(img2,10,10,10,10,cv2.BORDER_CONSTANT,value=[255,255,255])
both = np.hstack((c1,c2))
cv2.imwrite('images/'+text+str(iter)+'_false' + str(dp.data)+ '.jpg', both)
iter += 1
'''
# False labels
# train()
# false_dataset()
test()