-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image_Captioning.py
379 lines (287 loc) · 10.8 KB
/
Image_Captioning.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
## Importing Required Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import keras
import tensorflow as tf
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
from keras.layers import concatenate, BatchNormalization, Input
from keras.layers.merge import add
from keras.utils import to_categorical, plot_model
import io
import boto3
from smart_open import smart_open
import string
from keras.preprocessing.image import load_img, img_to_array
from PIL import Image
import numpy as np
from tensorflow.keras.applications.inception_v3 import InceptionV3
from numpy.testing import assert_allclose
from keras.models import load_model
from keras.callbacks import ModelCheckpoint
import pickle
from keras.applications.resnet50 import ResNet50
from keras.optimizers import Adam
from keras.layers import Dense, Flatten,Input, Convolution2D, Dropout, LSTM, TimeDistributed, Embedding, Bidirectional, Activation, RepeatVector,Concatenate
from keras.models import Sequential, Model
from keras.utils import np_utils
from keras.preprocessing import image, sequence
## Loading Text Data"""
token_path = 's3://projectdata27/data/data/captions.txt'
text = smart_open(token_path, 'r', encoding = 'utf-8').read()
## Preprocessing Text Data
descriptions = dict()
for line in text.split('\n'):
# split line by white space
tokens = line.split(',')
# take the first token as image id, the rest as description
image_id, image_desc = tokens[0], tokens[1:]
# extract filename from image id
image_id = image_id.split('.')[0]
# convert description tokens back to string
image_desc = ' '.join(image_desc)
if image_id not in descriptions.keys():
descriptions[image_id] = list()
descriptions[image_id].append(image_desc)
print(descriptions['3534548254_7bee952a0e'])
# prepare translation table for removing punctuation
table = str.maketrans('', '', string.punctuation)
for key, desc_list in descriptions.items():
for i in range(len(desc_list)):
desc = desc_list[i]
# tokenize
desc = desc.split()
# convert to lower case
desc = [word.lower() for word in desc]
# remove punctuation from each token
desc = [w.translate(table) for w in desc]
# remove hanging 's' and 'a'
desc = [word for word in desc if len(word)>1]
# remove tokens with numbers in them
desc = [word for word in desc if word.isalpha()]
# store as string
desc_list[i] = ' '.join(desc)
del descriptions['']
t=[]
token_path = 's3://projectdata27/data/data/trainimages.txt'
train = smart_open(token_path, 'r', encoding = 'utf-8').read()
for line in train.split('\n'):
t.append(line[:-4])
t.remove('')
vocabulary = set()
for key in t:
[vocabulary.update(d.split()) for d in descriptions[key]]
print('Original Vocabulary Size: %d' % len(vocabulary))
# Create a list of all the training captions
all_captions = []
for key, val in descriptions.items():
if key in t:
for cap in val:
all_captions.append(cap)
# Consider only words which occur at least 10 times in the corpus
word_count_threshold = 10
word_counts = {}
nsents = 0
for sent in all_captions:
nsents += 1
for w in sent.split(' '):
word_counts[w] = word_counts.get(w, 0) + 1
vocab = [w for w in word_counts if word_counts[w] >= word_count_threshold]
print('preprocessed words %d ' % len(vocab))
#find the maximum length of a description in a dataset
max_length = max(len(des.split()) for des in all_captions)
max_length
despc = dict()
for key, des_list in descriptions.items():
if key in t:
despc[key] = list()
for line in des_list:
desc = 'startseq ' + line + ' endseq'
despc[key].append(desc)
# word mapping to integers
ixtoword = {}
wordtoix = {}
ix = 1
for word in vocab:
wordtoix[word] = ix
ixtoword[ix] = word
ix += 1
# convert a dictionary of clean descriptions to a list of descriptions
def to_lines(descriptions):
all_desc = list()
for key in t:
[all_desc.append(d) for d in descriptions[key]]
return all_desc
# calculate the length of the description with the most words
def max_length(descriptions):
lines = to_lines(descriptions)
return max(len(d.split()) for d in lines)
# determine the maximum sequence length
max_length = max_length(despc)
print('Max Description Length: %d' % max_length)
s3 = boto3.resource('s3')
bucket = s3.Bucket('projectdata27')
temp = captions[10].split(",")
image = bucket.Object('data/data/Images/'+temp[0])
img_data = image.get().get('Body').read()
img=Image.open(io.BytesIO(img_data))
plt.imshow(img)
for ix in range(len(tokens[temp[0]])):
print(tokens[temp[0]][ix])
modelR = load_model('modelR.h5')
train_path='s3://projectdata27/data/data/trainimages.txt'
x_train = smart_open(train_path, 'r', encoding = 'utf-8').read().split("\n")
x_train.remove('')
def preprocessing(img_path):
k='data/data/Images/'+img_path
imag = bucket.Object(k)
img_data = imag.get().get('Body').read()
img=Image.open(io.BytesIO(img_data))
img=img.resize((224,224))
im = img_to_array(img)
im = np.expand_dims(im, axis=0)
return im
train_data = {}
for ix in x_train:
img = preprocessing(ix)
train_data[ix] = modelR.predict(img).reshape(2048)
train_data
# load glove vectors for embedding layer
vocab_size=1650
embeddings_index = {}
g = smart_open('s3://projectdata27/glove.6B.200d.txt', 'r', encoding = 'utf-8').read()
for line in g.split("\n"):
values = line.split(" ")
word = values[0]
indices = np.asarray(values[1: ], dtype = 'float32')
embeddings_index[word] = indices
len(embeddings_index)
emb_dim= 200
emb_matrix = np.zeros((vocab_size, emb_dim))
for word, i in wordtoix.items():
emb_vec = embeddings_index.get(word)
if emb_vec is not None:
emb_matrix[i] = emb_vec
emb_matrix.shape
X1, X2, y = list(), list(), list()
for key, des_list in despc.items():
if key in t:
pic = train_data[key + '.jpg']
for cap in des_list:
seq = [wordtoix[word] for word in cap.split(' ') if word in wordtoix]
for i in range(1, len(seq)):
in_seq, out_seq = seq[:i], seq[i]
in_seq = pad_sequences([in_seq], maxlen = max_length)[0]
out_seq = to_categorical([out_seq], num_classes = vocab_size)[0]
# store
X1.append(pic)
X2.append(in_seq)
y.append(out_seq)
X2 = np.array(X2)
X1 = np.array(X1)
y = np.array(y)
ip1 = Input(shape = (2048, ))
fe1 = Dropout(0.2)(ip1)
fe2 = Dense(256, activation = 'relu')(fe1)
ip2 = Input(shape = (max_length, ))
se1 = Embedding(vocab_size, emb_dim, mask_zero = True)(ip2)
se2 = Dropout(0.2)(se1)
se3 = LSTM(256)(se2)
decoder1 = add([fe2, se3])
decoder2 = Dense(512, activation = 'relu')(decoder1)
outputs = Dense(vocab_size, activation = 'softmax')(decoder2)
model3 = Model(inputs = [ip1, ip2], outputs = outputs)
model3.layers[2].set_weights([emb_matrix])
model3.layers[2].trainable = False
model3.compile(loss = 'categorical_crossentropy', optimizer = 'adam',metrics=['accuracy'])
# define the checkpoint
filepath = "model3.h5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
model3.fit([X1,X2], y, epochs=50, batch_size=256, callbacks=callbacks_list)
def feature_extraction(img_path):
k='data/data/Images/'+img_path
imag = bucket.Object(k)
img_data = imag.get().get('Body').read()
img=Image.open(io.BytesIO(img_data))
img=img.resize((224,224))
im = img_to_array(img)
im = np.expand_dims(im, axis=0)
im = modelR.predict(im)
return im
def final_caption(photo):
in_text = 'startseq'
for i in range(max_length):
sequence = [wordtoix[w] for w in in_text.split() if w in wordtoix]
sequence = pad_sequences([sequence], maxlen=max_length)
yhat = model3.predict([photo,sequence], verbose=0)
yhat = np.argmax(yhat)
word = ixtoword[yhat]
in_text += ' ' + word
if word == 'endseq':
break
final = in_text.split()
final = final[1:-1]
final = ' '.join(final)
return final
## ResNet50
from IPython.core.display import display, HTML
display(HTML("""<a href="http://ethereon.github.io/netscope/#/gist/db945b393d40bfa26006">ResNet50 Architecture</a>"""))
modelR = ResNet50(include_top=False,weights='imagenet',input_shape=(224,224,3),pooling='avg')
modelR.summary()
R=load_model('modelR.h5')
modelR = load_model('modelR.h5')
## Progressive Loading
def data_generator(descriptions, photos, wordtoix, max_length, num_photos_per_batch):
X1, X2, y = list(), list(), list()
n=0
# loop for ever over images
while 1:
for key, desc_list in descriptions.items():
n+=1
# retrieve the photo feature
photo = photos[key+'.jpg']
for desc in desc_list:
# encode the sequence
seq = [wordtoix[word] for word in desc.split(' ') if word in wordtoix]
# split one sequence into multiple X, y pairs
for i in range(1, len(seq)):
# split into input and output pair
in_seq, out_seq = seq[:i], seq[i]
# pad input sequence
in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
# encode output sequence
out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
# store
X1.append(photo)
X2.append(in_seq)
y.append(out_seq)
# yield the batch data
if n==num_photos_per_batch:
yield (np.array(X1), np.array(X2)), np.array(y)
X1, X2, y = list(), list(), list()
n=0
embedding_dim=200
inputs1 = Input(shape=(2048,))
fe1 = Dropout(0.5)(inputs1)
fe2 = Dense(256, activation='relu')(fe1)
inputs2 = Input(shape=(max_length,))
se1 = Embedding(vocab_size, embedding_dim, mask_zero=True)(inputs2)
se2 = Dropout(0.5)(se1)
se3 = LSTM(256)(se2)
decoder1 = add([fe2, se3])
decoder2 = Dense(512, activation='relu')(decoder1)
outputs = Dense(vocab_size, activation='softmax')(decoder2)
model2 = Model(inputs=[inputs1, inputs2], outputs=outputs)
model2.layers[2].set_weights([emb_matrix])
model2.layers[2].trainable = False
model2.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
epochs = 10
number_pics_per_bath = 3
steps = len(despc)//number_pics_per_bath
for i in range(epochs):
generator = data_generator(despc, train_data, wordtoix, max_length, number_pics_per_bath)
model2.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
model2.save('model2.h5')