-
Notifications
You must be signed in to change notification settings - Fork 1
/
beamsearch.py
443 lines (349 loc) · 17.7 KB
/
beamsearch.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import torch
import torchvision.transforms as transforms
from models import *
import torchfile as tf
from scipy.misc import imread, imresize
from PIL import Image
import torch.nn.functional as F
import sys
start=0
end=1
def beam_search(encoder, decoder, image_path, beam_size):
k = beam_size
vocab_size = 5725+1 ###
# Read image and process
img = imread(image_path)
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
img = np.concatenate([img, img, img], axis=2)
img = imresize(img, (256, 256))
img = img.transpose(2, 0, 1)
img = img / 255.
img = torch.FloatTensor(img).to(device)
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
transform = transforms.Compose([normalize])
image = transform(img) # (3, 256, 256)
# Encode
image = image.unsqueeze(0) # (1, 3, 256, 256)
encoder_out = encoder(image) # (1, enc_image_size, enc_image_size, encoder_dim)
enc_image_size = encoder_out.size(1)
encoder_dim = encoder_out.size(3)
# Flatten encoding
encoder_out = encoder_out.view(1, -1, encoder_dim) # (1, num_pixels, encoder_dim)
num_pixels = encoder_out.size(1)
# We'll treat the problem as having a batch size of k
encoder_out = encoder_out.expand(k, num_pixels, encoder_dim) # (k, num_pixels, encoder_dim)
# Tensor to store top k previous words at each step; now they're just <start>
k_prev_words = torch.LongTensor([[start]] * k).to(device) # (k, 1)
# Tensor to store top k sequences; now they're just <start>
seqs = k_prev_words # (k, 1)
# Tensor to store top k sequences' scores; now they're just 0
top_k_scores = torch.zeros(k, 1).to(device) # (k, 1)
# Tensor to store top k sequences' alphas; now they're just 1s
seqs_alpha = torch.ones(k, 1, enc_image_size, enc_image_size).to(device) # (k, 1, enc_image_size, enc_image_size)
# Lists to store completed sequences, their alphas and scores
complete_seqs = list()
# complete_seqs_alpha = list()
complete_seqs_scores = list()
# Start decoding
step = 1
h, c = decoder.init_hidden_state(encoder_out)
# s is a number less than or equal to k, because sequences are removed from this process once they hit <end>
while True:
embeddings = decoder.embedding(k_prev_words).squeeze(1) # (s, embed_dim)
awe = decoder.attention(encoder_out, h) # (s, encoder_dim), (s, num_pixels)
gate = decoder.sigmoid(decoder.f_beta(h)) # gating scalar, (s, encoder_dim)
awe = gate * awe
h, c = decoder.decode_step(torch.cat([embeddings, awe], dim=1), (h, c)) # (s, decoder_dim)
scores = decoder.fc(h) # (s, vocab_size)
scores = F.log_softmax(scores, dim=1)
# Add
scores = top_k_scores.expand_as(scores) + scores # (s, vocab_size)
# For the first step, all k points will have the same scores (since same k previous words, h, c)
if step == 1:
top_k_scores, top_k_words = scores[0].topk(k, 0, True, True) # (s)
else:
# Unroll and find top scores, and their unrolled indices
top_k_scores, top_k_words = scores.view(-1).topk(k, 0, True, True) # (s)
# Convert unrolled indices to actual indices of scores
prev_word_inds = top_k_words / vocab_size # (s)
next_word_inds = top_k_words % vocab_size # (s)
# Add new words to sequences, alphas
seqs = torch.cat([seqs[prev_word_inds], next_word_inds.unsqueeze(1)], dim=1) # (s, step+1)
# Which sequences are incomplete (didn't reach <end>)?
incomplete_inds = [ind for ind, next_word in enumerate(next_word_inds) if
next_word != end]
complete_inds = list(set(range(len(next_word_inds))) - set(incomplete_inds))
# Set aside complete sequences
if len(complete_inds) > 0:
complete_seqs.extend(seqs[complete_inds].tolist())
complete_seqs_scores.extend(top_k_scores[complete_inds])
k -= len(complete_inds) # reduce beam length accordingly
# Proceed with incomplete sequences
if k == 0:
break
seqs = seqs[incomplete_inds]
h = h[prev_word_inds[incomplete_inds]]
c = c[prev_word_inds[incomplete_inds]]
encoder_out = encoder_out[prev_word_inds[incomplete_inds]]
top_k_scores = top_k_scores[incomplete_inds].unsqueeze(1)
k_prev_words = next_word_inds[incomplete_inds].unsqueeze(1)
# Break if things have been going on too long
if step > 50:
break
step += 1
i = complete_seqs_scores.index(max(complete_seqs_scores))
seq = complete_seqs[i]
# alphas = complete_seqs_alpha[i]
return seq
def beam_search_justify_main(encoder, decoder, image_path ,class_t ,class_d,lambda_, beam_size):
k = beam_size
vocab_size = 5725+1 ###
# Read image and process
img = imread(image_path)
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
img = np.concatenate([img, img, img], axis=2)
img = imresize(img, (256, 256))
img = img.transpose(2, 0, 1)
img = img / 255.
img = torch.FloatTensor(img).to(device)
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
transform = transforms.Compose([normalize])
image = transform(img) # (3, 256, 256)
class_embedding_t=decoder.class_embedding(torch.LongTensor([[class_t]]).to(device))
class_embedding_d=decoder.class_embedding(torch.LongTensor([[class_d]]).to(device))
# Encode
image = image.unsqueeze(0) # (1, 3, 256, 256)
encoder_out = encoder(image) # (1, enc_image_size, enc_image_size, encoder_dim)
enc_image_size = encoder_out.size(1)
encoder_dim = encoder_out.size(3)
# Flatten encoding
encoder_out = encoder_out.view(1, -1, encoder_dim) # (1, num_pixels, encoder_dim)
num_pixels = encoder_out.size(1)
# We'll treat the problem as having a batch size of k
encoder_out = encoder_out.expand(k, num_pixels, encoder_dim) # (k, num_pixels, encoder_dim)
class_embedding_t=class_embedding_t.expand(k,1,512)
class_embedding_d=class_embedding_d.expand(k,1,512)
# Tensor to store top k previous words at each step; now they're just <start>
k_prev_words = torch.LongTensor([[start]] * k).to(device) # (k, 1)
# Tensor to store top k sequences; now they're just <start>
seqs = k_prev_words # (k, 1)
# Tensor to store top k sequences' scores; now they're just 0
top_k_scores = torch.zeros(k, 1).to(device) # (k, 1)
# Tensor to store top k sequences' alphas; now they're just 1s
# Lists to store completed sequences, their alphas and scores
complete_seqs = list()
# complete_seqs_alpha = list()
complete_seqs_scores = list()
# Start decoding
step = 1
h_t, c_t = decoder.init_hidden_state(encoder_out,class_embedding_t)
h_d, c_d = decoder.init_hidden_state(encoder_out,class_embedding_d)
# s is a number less than or equal to k, because sequences are removed from this process once they hit <end>
while True:
embeddings = decoder.embedding(k_prev_words).squeeze(1) # (s, embed_dim)
awe_t = decoder.attention(encoder_out, h_t) # (s, encoder_dim), (s, num_pixels)
awe_d = decoder.attention(encoder_out, h_d) # (s, encoder_dim), (s, num_pixels)
gate_t = decoder.sigmoid(decoder.f_beta(h_t)) # gating scalar, (s, encoder_dim)
awe_t = gate_t * awe_t
gate_d = decoder.sigmoid(decoder.f_beta(h_d)) # gating scalar, (s, encoder_dim)
awe_d = gate_d * awe_d
h_t, c_t = decoder.decode_step(torch.cat([embeddings, awe_t,class_embedding_t[:,0,:]], dim=1), (h_t, c_t)) # (s, decoder_dim)
h_d, c_d = decoder.decode_step(torch.cat([embeddings, awe_d,class_embedding_d[:,0,:]], dim=1), (h_d, c_d)) # (s, decoder_dim)
scores_t = decoder.fc(h_t) # (s, vocab_size)
scores_d = decoder.fc(h_d)
scores_t = F.log_softmax(scores_t, dim=1)
scores_d = F.log_softmax(scores_d, dim=1)
scores = scores_t-(1-lambda_)*scores_d
scores = top_k_scores.expand_as(scores) + scores # (s, vocab_size)
# For the first step, all k points will have the same scores (since same k previous words, h, c)
if step == 1:
top_k_scores, top_k_words = scores[0].topk(k, 0, True, True) # (s)
else:
# Unroll and find top scores, and their unrolled indices
top_k_scores, top_k_words = scores.view(-1).topk(k, 0, True, True) # (s)
# Convert unrolled indices to actual indices of scores
prev_word_inds = top_k_words / vocab_size # (s)
next_word_inds = top_k_words % vocab_size # (s)
# Add new words to sequences, alphas
seqs = torch.cat([seqs[prev_word_inds], next_word_inds.unsqueeze(1)], dim=1) # (s, step+1)
# Which sequences are incomplete (didn't reach <end>)?
incomplete_inds = [ind for ind, next_word in enumerate(next_word_inds) if
next_word != end]
complete_inds = list(set(range(len(next_word_inds))) - set(incomplete_inds))
# Set aside complete sequences
if len(complete_inds) > 0:
complete_seqs.extend(seqs[complete_inds].tolist())
complete_seqs_scores.extend(top_k_scores[complete_inds])
k -= len(complete_inds) # reduce beam length accordingly
# Proceed with incomplete sequences
if k == 0:
break
seqs = seqs[incomplete_inds]
h_t = h_t[prev_word_inds[incomplete_inds]]
c_t = c_t[prev_word_inds[incomplete_inds]]
h_d = h_d[prev_word_inds[incomplete_inds]]
c_d = c_d[prev_word_inds[incomplete_inds]]
encoder_out = encoder_out[prev_word_inds[incomplete_inds]]
top_k_scores = top_k_scores[incomplete_inds].unsqueeze(1)
k_prev_words = next_word_inds[incomplete_inds].unsqueeze(1)
class_embedding_t=class_embedding_t[incomplete_inds,:,:]
class_embedding_d=class_embedding_d[incomplete_inds,:,:]
# Break if things have been going on too long
if step > 50:
break
step += 1
i = complete_seqs_scores.index(max(complete_seqs_scores))
seq = complete_seqs[i]
# alphas = complete_seqs_alpha[i]
return seq
def beam_search_discriminative(encoder, decoder, image_path_t,image_path_d,lambda_, beam_size=3):
k = beam_size
vocab_size = 5725+1 ###
# Read image and process
img = imread(image_path_t)
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
img = np.concatenate([img, img, img], axis=2)
img = imresize(img, (256, 256))
img = img.transpose(2, 0, 1)
img = img / 255.
img = torch.FloatTensor(img).to(device)
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
transform = transforms.Compose([normalize])
image = transform(img) # (3, 256, 256)
# Encode
image = image.unsqueeze(0) # (1, 3, 256, 256)
encoder_out_t = encoder(image) # (1, enc_image_size, enc_image_size, encoder_dim)
img = imread(image_path_d)
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
img = np.concatenate([img, img, img], axis=2)
img = imresize(img, (256, 256))
img = img.transpose(2, 0, 1)
img = img / 255.
img = torch.FloatTensor(img).to(device)
image = transform(img) # (3, 256, 256)
# Encode
image = image.unsqueeze(0) # (1, 3, 256, 256)
encoder_out_d = encoder(image) # (1, enc_image_size, enc_image_size, encoder_dim)
enc_image_size = encoder_out_t.size(1)
encoder_dim = encoder_out_t.size(3)
# Flatten encoding
encoder_out_t = encoder_out_t.view(1, -1, encoder_dim) # (1, num_pixels, encoder_dim)
encoder_out_d = encoder_out_d.view(1, -1, encoder_dim) # (1, num_pixels, encoder_dim)
num_pixels = encoder_out_t.size(1)
# We'll treat the problem as having a batch size of k
encoder_out_t = encoder_out_t.expand(k, num_pixels, encoder_dim) # (k, num_pixels, encoder_dim)
encoder_out_d = encoder_out_d.expand(k, num_pixels, encoder_dim) # (k, num_pixels, encoder_dim)
# Tensor to store top k previous words at each step; now they're just <start>
k_prev_words = torch.LongTensor([[start]] * k).to(device) # (k, 1)
# Tensor to store top k sequences; now they're just <start>
seqs = k_prev_words # (k, 1)
# Tensor to store top k sequences' scores; now they're just 0
top_k_scores = torch.zeros(k, 1).to(device) # (k, 1)
# Lists to store completed sequences, their alphas and scores
complete_seqs = list()
complete_seqs_alpha = list()
complete_seqs_scores = list()
# Start decoding
step = 1
h_t, c_t = decoder.init_hidden_state(encoder_out_t)
h_d, c_d = decoder.init_hidden_state(encoder_out_d)
# s is a number less than or equal to k, because sequences are removed from this process once they hit <end>
while True:
embeddings = decoder.embedding(k_prev_words).squeeze(1) # (s, embed_dim)
awe_t= decoder.attention(encoder_out_t, h_t) # (s, encoder_dim), (s, num_pixels)
awe_d= decoder.attention(encoder_out_d, h_d) # (s, encoder_dim), (s, num_pixels)
gate_t = decoder.sigmoid(decoder.f_beta(h_t)) # gating scalar, (s, encoder_dim)
awe_t = gate_t * awe_t
gate_d = decoder.sigmoid(decoder.f_beta(h_d)) # gating scalar, (s, encoder_dim)
awe_d = gate_d * awe_d
h_t, c_t = decoder.decode_step(torch.cat([embeddings, awe_t], dim=1), (h_t, c_t)) # (s, decoder_dim)
h_d, c_d = decoder.decode_step(torch.cat([embeddings, awe_d], dim=1), (h_d, c_d)) # (s, decoder_dim)
# scores = decoder.fc(h_t)- (1-lambda_)*decoder.fc(h_d) # (s, vocab_size)
scores = F.log_softmax(decoder.fc(h_t), dim=1) -(1-lambda_)*F.log_softmax(decoder.fc(h_d), dim=1)
# Add
scores = top_k_scores.expand_as(scores) + scores # (s, vocab_size)
# For the first step, all k points will have the same scores (since same k previous words, h, c)
if step == 1:
top_k_scores, top_k_words = scores[0].topk(k, 0, True, True) # (s)
else:
# Unroll and find top scores, and their unrolled indices
top_k_scores, top_k_words = scores.view(-1).topk(k, 0, True, True) # (s)
# Convert unrolled indices to actual indices of scores
prev_word_inds = top_k_words / vocab_size # (s)
next_word_inds = top_k_words % vocab_size # (s)
# Add new words to sequences, alphas
seqs = torch.cat([seqs[prev_word_inds], next_word_inds.unsqueeze(1)], dim=1) # (s, step+1)
# Which sequences are incomplete (didn't reach <end>)?
incomplete_inds = [ind for ind, next_word in enumerate(next_word_inds) if
next_word != end]
complete_inds = list(set(range(len(next_word_inds))) - set(incomplete_inds))
# Set aside complete sequences
if len(complete_inds) > 0:
complete_seqs.extend(seqs[complete_inds].tolist())
# complete_seqs_alpha.extend(seqs_alpha[complete_inds].tolist())
complete_seqs_scores.extend(top_k_scores[complete_inds])
k -= len(complete_inds) # reduce beam length accordingly
# Proceed with incomplete sequences
if k == 0:
break
seqs = seqs[incomplete_inds]
h_t = h_t[prev_word_inds[incomplete_inds]]
c_t = c_t[prev_word_inds[incomplete_inds]]
encoder_out_t = encoder_out_t[prev_word_inds[incomplete_inds]]
h_d = h_d[prev_word_inds[incomplete_inds]]
c_d = c_d[prev_word_inds[incomplete_inds]]
encoder_out_d = encoder_out_d[prev_word_inds[incomplete_inds]]
top_k_scores = top_k_scores[incomplete_inds].unsqueeze(1)
k_prev_words = next_word_inds[incomplete_inds].unsqueeze(1)
# Break if things have been going on too long
if step > 50:
break
step += 1
i = complete_seqs_scores.index(max(complete_seqs_scores))
seq = complete_seqs[i]
return seq
if __name__ == "__main__":
if sys.argv[1]=="c": ## Caption image_path
checkpoints=torch.load('checkpoint_d')
encoder=checkpoints['encoder']
decoder=checkpoints['decoder']
encoder.eval()
decoder.eval()
image_path=sys.argv[2]
word_map=tf.load('C:/Users/hello/Desktop/Accads/cvpr2016_cub/vocab_c10.t7',force_8bytes_long=True)
word_map={word_map[i]:i for i in word_map}
seq=beam_search(encoder,decoder,image_path,1)
for i in seq[1:]:
print(word_map[i].decode("utf-8") ,end=" ")
print("")
elif sys.argv[1]=="cj": ## cj Image_path target_class distractor class
checkpoints=torch.load('checkpoint_j')
encoder=checkpoints['encoder']
decoder=checkpoints['decoder']
encoder.eval()
decoder.eval()
image_path=sys.argv[2]
word_map=tf.load('C:/Users/hello/Desktop/Accads/cvpr2016_cub/vocab_c10.t7',force_8bytes_long=True)
word_map={word_map[i]:i for i in word_map}
seq=beam_search_justify_main(encoder,decoder,image_path,int(sys.argv[3]),int(sys.argv[4]),0.5,1)
for i in seq[1:]:
print(word_map[i].decode("utf-8") ,end=" ")
print("")
elif sys.argv[1]=="cd": ## cd Image_path_t Image_path_d
checkpoints=torch.load('checkpoint_j')
encoder=checkpoints['encoder']
decoder=checkpoints['decoder']
encoder.eval()
decoder.eval()
image_path_t=sys.argv[2]
image_path_d=sys.argv[3]
word_map=tf.load('C:/Users/hello/Desktop/Accads/cvpr2016_cub/vocab_c10.t7',force_8bytes_long=True)
word_map={word_map[i]:i for i in word_map}
seq=beam_search_discriminative(encoder,decoder,image_path_t,image_path_d,0.5,3)
for i in seq[1:]:
print(word_map[i].decode("utf-8") ,end=" ")
print("")