forked from ariesiitr/Online-Class-Note-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaption.py
190 lines (150 loc) · 5.18 KB
/
caption.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
# -*- coding: utf-8 -*-
import os
import string
import glob
from tensorflow.keras.applications import MobileNet
import tensorflow.keras.applications.mobilenet
from tensorflow.keras.applications.inception_v3 import InceptionV3
import tensorflow.keras.applications.inception_v3
from tqdm import tqdm
import tensorflow.keras.preprocessing.image
import pickle
from time import time
import numpy as np
from PIL import Image
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (LSTM, Embedding,
TimeDistributed, Dense, RepeatVector,
Activation, Flatten, Reshape, concatenate,
Dropout, BatchNormalization)
from tensorflow.keras.optimizers import Adam, RMSprop
from tensorflow.keras import Input, layers
from tensorflow.keras import optimizers
from tensorflow.keras.models import Model
from tensorflow.keras.layers import add
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
START = "startseq"
STOP = "endseq"
EPOCHS = 10
USE_INCEPTION = True
image_folder="/home/shreyansh/repo/Image-Caption/data/Images/*.jpg"
trainImages_txt='/home/shreyansh/repo/Image-Caption/data/Flickr_8k.trainImages.txt'
testImages_txt='/home/shreyansh/repo/Image-Caption/data/Flickr_8k.testImages.txt'
token_txt='/home/shreyansh/repo/Image-Caption/data/Flickr8k.token.txt'
model_file="/home/shreyansh/repo/Image-Caption/data/caption-model.hdf5"
if USE_INCEPTION:
encode_model = InceptionV3(weights='imagenet')
encode_model = Model(encode_model.input, encode_model.layers[-2].output)
WIDTH = 299
HEIGHT = 299
OUTPUT_DIM = 2048
preprocess_input = \
tensorflow.keras.applications.inception_v3.preprocess_input
else:
encode_model = MobileNet(weights='imagenet',include_top=False)
WIDTH = 224
HEIGHT = 224
OUTPUT_DIM = 50176
preprocess_input = tensorflow.keras.applications.mobilenet.preprocess_input
def encodeImage(img):
img = img.resize((WIDTH, HEIGHT), Image.ANTIALIAS)
x = tensorflow.keras.preprocessing.image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
x = encode_model.predict(x)
x = np.reshape(x, OUTPUT_DIM )
return x
"""generateCaption
"""
img= glob.glob(image_folder)
train_images_path = os.path.join('',\
trainImages_txt)
train_images = set(open(train_images_path, 'r').read().strip().split('\n'))
test_images_path = os.path.join('',
testImages_txt)
test_images = set(open(test_images_path, 'r').read().strip().split('\n'))
train_img = []
test_img = []
for i in img:
f = os.path.split(i)[-1]
if f in train_images:
train_img.append(f)
elif f in test_images:
test_img.append(f)
null_punct = str.maketrans('', '', string.punctuation)
lookup = dict()
with open(token_txt, 'r') as fp:
max_length = 0
for line in fp.read().split('\n'):
tok = line.split()
if len(line) >= 2:
id = tok[0].split('.')[0]
desc = tok[1:]
# Cleanup description
desc = [word.lower() for word in desc]
desc = [w.translate(null_punct) for w in desc]
desc = [word for word in desc if len(word)>1]
desc = [word for word in desc if word.isalpha()]
max_length = max(max_length,len(desc))
if id not in lookup:
lookup[id] = list()
lookup[id].append(' '.join(desc))
lex = set()
for key in lookup:
[lex.update(d.split()) for d in lookup[key]]
train_descriptions = {k:v for k,v in lookup.items() if f'{k}.jpg' \
in train_images}
for n,v in train_descriptions.items():
for d in range(len(v)):
v[d] = f'{START} {v[d]} {STOP}'
all_train_captions = []
for key, val in train_descriptions.items():
for cap in val:
all_train_captions.append(cap)
word_count_threshold = 10
word_counts = {}
nsents = 0
for sent in all_train_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]
idxtoword = {}
wordtoidx = {}
ix = 1
for w in vocab:
wordtoidx[w] = ix
idxtoword[ix] = w
ix += 1
vocab_size = len(idxtoword) + 1
vocab_size
def generateCaption(photo):
in_text = START
for i in range(max_length):
sequence = [wordtoidx[w] for w in in_text.split() if w in wordtoidx]
sequence = pad_sequences([sequence], maxlen=max_length)
yhat = caption_model.predict([photo,sequence], verbose=0)
yhat = np.argmax(yhat)
word = idxtoword[yhat]
in_text += ' ' + word
if word == STOP:
break
final = in_text.split()
final = final[1:-1]
final = ' '.join(final)
return final
inputs1 = Input(shape=(OUTPUT_DIM,))
fe1 = Dropout(0.5)(inputs1)
fe2 = Dense(256, activation='relu')(fe1)
inputs2 = Input(shape=(max_length,))
se1 = Embedding(vocab_size, 200, mask_zero=True)(inputs2)
se2 = Dropout(0.5)(se1)
se3 = LSTM(256)(se2)
decoder1 = add([fe2, se3])
decoder2 = Dense(256, activation='relu')(decoder1)
outputs = Dense(vocab_size, activation='softmax')(decoder2)
caption_model = Model(inputs=[inputs1, inputs2], outputs=outputs)
caption_model.compile(loss='categorical_crossentropy', optimizer='adam')
caption_model.load_weights(model_file)