-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICBEAM.py
More file actions
280 lines (247 loc) · 9.73 KB
/
Copy pathICBEAM.py
File metadata and controls
280 lines (247 loc) · 9.73 KB
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
import glob
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import pickle
from tqdm import tqdm
import pandas as pd
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector, Merge, Activation, Flatten
from keras.optimizers import Adam, RMSprop
from keras.layers.wrappers import Bidirectional
from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
import nltk
token = 'C:/Users/pooja/Downloads/Flickr8k_text/Flickr8k.token.txt'
captions = open(token, 'r').read().strip().split('\n')
d = {}
for i, row in enumerate(captions):
row = row.split('\t')
row[0] = row[0][:len(row[0])-2]
if row[0] in d:
d[row[0]].append(row[1])
else:
d[row[0]] = [row[1]]
images = 'C:/Users/pooja/Downloads/Flickr8k_Dataset/Flicker8k_Dataset/'
img = glob.glob(images+'*.jpg')
print(img[:5])
train_images_file = 'C:/Users/pooja/Downloads/Flickr8k_text/Flickr_8k.trainImages.txt'
train_images = set(open(train_images_file, 'r').read().strip().split('\n'))
def split_data(l):
temp = []
for i in img:
if i[len(images):] in l:
temp.append(i)
return temp
train_img = split_data(train_images)
print(len(train_img))
val_images_file = 'C:/Users/pooja/Downloads/Flickr8k_text/Flickr_8k.devImages.txt'
val_images = set(open(val_images_file, 'r').read().strip().split('\n'))
# Getting the validation images from all the images
val_img = split_data(val_images)
print(len(val_img))
test_images_file = 'C:/Users/pooja/Downloads/Flickr8k_text/Flickr_8k.testImages.txt'
test_images = set(open(test_images_file, 'r').read().strip().split('\n'))
# Getting the testing images from all the images
test_img = split_data(test_images)
print(len(test_img))
def preprocess_input(x):
x /= 255.
x -= 0.5
x *= 2.
return x
def preprocess(image_path):
img = image.load_img(image_path, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return x
plt.imshow(np.squeeze(preprocess(train_img[0])))
model = InceptionV3(weights='imagenet')
from keras.models import Model
new_input = model.input
hidden_layer = model.layers[-2].output
model_new = Model(new_input, hidden_layer)
tryi = model_new.predict(preprocess(train_img[0]))
print(tryi.shape)
def encode(image):
image = preprocess(image)
temp_enc = model_new.predict(image)
temp_enc = np.reshape(temp_enc, temp_enc.shape[1])
return temp_enc
encoding_train = {}
for img in tqdm(train_img):
encoding_train[img[len(images):]] = encode(img)
with open("encoded_images_inceptionV3.p", "wb") as encoded_pickle:
pickle.dump(encoding_train, encoded_pickle)
encoding_train = pickle.load(open('encoded_images_inceptionV3.p', 'rb'))
encoding_test = {}
for img in tqdm(test_img):
encoding_test[img[len(images):]] = encode(img)
with open("encoded_images_test_inceptionV3.p", "wb") as encoded_pickle:
pickle.dump(encoding_test, encoded_pickle)
encoding_test = pickle.load(open('encoded_images_test_inceptionV3.p', 'rb'))
train_d = {}
for i in train_img:
if i[len(images):] in d:
train_d[i] = d[i[len(images):]]
val_d = {}
for i in val_img:
if i[len(images):] in d:
val_d[i] = d[i[len(images):]]
test_d = {}
for i in test_img:
if i[len(images):] in d:
test_d[i] = d[i[len(images):]]
caps = []
for key, val in train_d.items():
for i in val:
caps.append('<start> ' + i + ' <end>')
words = [i.split() for i in caps]
unique = []
for i in words:
unique.extend(i)
unique = list(set(unique))
with open("unique.p", "wb") as pickle_d:
pickle.dump(unique, pickle_d)
unique = pickle.load(open('unique.p', 'rb'))
print(len(unique))
word2idx = {val:index for index, val in enumerate(unique)}
print(word2idx['<start>'])
idx2word = {index:val for index, val in enumerate(unique)}
max_len = 0
for c in caps:
c = c.split()
if len(c) > max_len:
max_len = len(c)
vocab_size = len(unique)
print(vocab_size)
f = open('flickr8k_training_dataset.txt', 'w')
f.write("image_id\tcaptions\n")
for key, val in train_d.items():
for i in val:
f.write(key[len(images):] + "\t" + "<start> " + i +" <end>" + "\n")
f.close()
df = pd.read_csv('flickr8k_training_dataset.txt', delimiter='\t')
print(len(df))
c = [i for i in df['captions']]
print(len(c))
imgs = [i for i in df['image_id']]
a = c[-1]
print(a, imgs[-1])
for i in a.split():
print (i, "=>", word2idx[i])
samples_per_epoch = 0
for ca in caps:
samples_per_epoch += len(ca.split())-1
print(samples_per_epoch)
def data_generator(batch_size = 32):
partial_caps = []
next_words = []
images = []
df = pd.read_csv('flickr8k_training_dataset.txt', delimiter='\t')
df = df.sample(frac=1)
iter = df.iterrows()
c = []
imgs = []
for i in range(df.shape[0]):
x = next(iter)
c.append(x[1][1])
imgs.append(x[1][0])
count = 0
while True:
for j, text in enumerate(c):
current_image = encoding_train[imgs[j]]
for i in range(len(text.split())-1):
count+=1
partial = [word2idx[txt] for txt in text.split()[:i+1]]
partial_caps.append(partial)
# Initializing with zeros to create a one-hot encoding matrix
# This is what we have to predict
# Hence initializing it with vocab_size length
n = np.zeros(vocab_size)
# Setting the next word to 1 in the one-hot encoded matrix
n[word2idx[text.split()[i+1]]] = 1
next_words.append(n)
images.append(current_image)
if count>=batch_size:
next_words = np.asarray(next_words)
images = np.asarray(images)
partial_caps = sequence.pad_sequences(partial_caps, maxlen=max_len, padding='post')
yield [[images, partial_caps], next_words]
partial_caps = []
next_words = []
images = []
count = 0
embedding_size = 300
image_model = Sequential([
Dense(embedding_size, input_shape=(2048,), activation='relu'),
RepeatVector(max_len)
])
caption_model = Sequential([
Embedding(vocab_size, embedding_size, input_length=max_len),
LSTM(256, return_sequences=True),
TimeDistributed(Dense(300))
])
final_model = Sequential([
Merge([image_model, caption_model], mode='concat', concat_axis=1),
Bidirectional(LSTM(256, return_sequences=False)),
Dense(vocab_size),
Activation('softmax')
])
final_model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])
final_model.summary()
final_model.fit_generator(data_generator(batch_size=128), samples_per_epoch=samples_per_epoch, nb_epoch=1, verbose=1)
final_model.save_weights('time_inceptionV3_7_loss_3.2604.h5')
final_model.load_weights('time_inceptionV3_1.5987_loss.h5')
def predict_captions(image):
start_word = ["<start>"]
while True:
par_caps = [word2idx[i] for i in start_word]
par_caps = sequence.pad_sequences([par_caps], maxlen=max_len, padding='post')
e = encoding_test[image[len(images):]]
preds = final_model.predict([np.array([e]), np.array(par_caps)])
word_pred = idx2word[np.argmax(preds[0])]
start_word.append(word_pred)
if word_pred == "<end>" or len(start_word) > max_len:
break
return ' '.join(start_word[1:-1])
def beam_search_predictions(image, beam_index = 3):
start = [word2idx["<start>"]]
start_word = [[start, 0.0]]
while len(start_word[0][0]) < max_len:
temp = []
for s in start_word:
par_caps = sequence.pad_sequences([s[0]], maxlen=max_len, padding='post')
e = encoding_test[image[len(images):]]
preds = final_model.predict([np.array([e]), np.array(par_caps)])
word_preds = np.argsort(preds[0])[-beam_index:]
# Getting the top <beam_index>(n) predictions and creating a
# new list so as to put them via the model again
for w in word_preds:
next_cap, prob = s[0][:], s[1]
next_cap.append(w)
prob += preds[0][w]
temp.append([next_cap, prob])
start_word = temp
# Sorting according to the probabilities
start_word = sorted(start_word, reverse=False, key=lambda l: l[1])
# Getting the top words
start_word = start_word[-beam_index:]
start_word = start_word[-1][0]
intermediate_caption = [idx2word[i] for i in start_word]
final_caption = []
for i in intermediate_caption:
if i != '<end>':
final_caption.append(i)
else:
break
final_caption = ' '.join(final_caption[1:])
return final_caption
try_image = test_img[0]
print(try_image)
print ('Normal Max search:', predict_captions(try_image))
print ('Beam Search, k=3:', beam_search_predictions(try_image, beam_index=3))
print ('Beam Search, k=5:', beam_search_predictions(try_image, beam_index=5))
print ('Beam Search, k=7:', beam_search_predictions(try_image, beam_index=7))