-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
213 lines (182 loc) · 7.71 KB
/
data.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
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
import keras
import collections
import random
from tqdm import tqdm
import re
import numpy as np
import os
from nltk.translate.bleu_score import corpus_bleu
from PIL import Image
import pickle
import string
from time import time
from keras.preprocessing import sequence
from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras import Input, layers
from keras import optimizers
from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
inceptionv3 = InceptionV3(weights="imagenet", include_top=False)
modelCNN = Model(inceptionv3.input, inceptionv3.layers[-1].output)
def preprocess_img_id(img_id):
img_folder = './content/drive/My Drive/datasets/Flickr8k/Flickr8k_Dataset/Flicker8k_Dataset/'
image_path = img_folder + img_id
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img, channels = 3)
img = tf.image.resize(img, (299,299))
img = preprocess_input(img)
return img, image_path
def preprocess_img_path(image_path):
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.resize(img, (299, 299))
img = preprocess_input(img)
return img, image_path
class data_loader:
def __init__(
self, features_shape, attention_features_shape, batch_size, buffer_size, top_k
):
self.features_shape = features_shape
self.attention_features_shape = attention_features_shape
self.img_folder = "./content/drive/My Drive/datasets/Flickr8k/Flickr8k_Dataset/Flicker8k_Dataset/"
self.img_folder_np = "./content/drive/My Drive/datasets/Flickr8k/Flickr8k_Dataset/Flicker8k_numpy/"
self.glob_folder = "./content/drive/My Drive/datasets/glove/"
self.batch_size = batch_size
self.buffer_size = buffer_size
self.top_k = top_k
#
self.train_id, self.test_id = self.load_id("train"), self.load_id("test")
self.train_path, self.test_path = (
self.load_path(self.train_id),
self.load_path(self.test_id),
)
self.train_captions, self.test_captions = (
self.load_caption(self.train_id),
self.load_caption(self.test_id),
)
self.tokenizing()
self.cap_train, self.name_train = self.create_split(self.train_captions)
self.cap_test, self.name_test = self.create_split(self.test_captions)
def save_npy(self):
encode = sorted(set(self.train_id + self.test_id))
image_dataset = tf.data.Dataset.from_tensor_slices(encode)
image_dataset = image_dataset.map(preprocess_img_id, num_parallel_calls = tf.data.experimental.AUTOTUNE).batch(32)
for img, path in tqdm(image_dataset):
batch_features = modelCNN(img)
batch_features = tf.reshape(batch_features, (batch_features.shape[0],-1, batch_features.shape[3]))
for bf, p in zip(batch_features, path):
path_of_features = p.numpy().decode('utf-8')+'.npy'
path_of_features = path_of_features.split('/')
path_of_features[-2] = 'Flicker8k_numpy'
path_of_features = '/'.join(path_of_features)
print(path_of_features)
np.save(path_of_features, bf.numpy())
def save_embedding_matrix(self, embedding_dim):
# Load Glove model
embeddings_index = {}
f = open(os.path.join(self.glob_folder, 'glove.6B.200d.txt'), encoding="utf-8")
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
embedding_matrix = np.zeros((self.top_k+1, embedding_dim))
embedding_matrix[0] = 0
for word, i in self.tokenizer.word_index.items():
if i == self.top_k+1:
break
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
np.save('./content/drive/My Drive/datasets/embeddingmatrix.npy', embedding_matrix)
def load_id(self, split):
path = (
"./content/drive/My Drive/datasets/Flickr8k/Flickr8k_text/Flickr_8k."
+ split
+ "Images.txt"
)
with open(path, "r") as f:
lines = f.read().splitlines()
return lines
def load_path(self, split_id):
split_path = []
for id_ in split_id:
path = self.img_folder + id_
split_path.append(path)
return split_path
def load_caption(self, split_id):
table = str.maketrans("", "", string.punctuation)
with open(
"./content/drive/My Drive/datasets/Flickr8k/Flickr8k_text/Flickr8k.token.txt",
"r",
) as f:
lines = f.read().splitlines()
map = dict()
for line in lines:
idx, content = line.split()[0], line.split()[1:]
idx = idx[:-2]
if idx not in split_id:
continue
content = [w.lower().translate(table) for w in content if w.isalpha()]
content = "<start> " + " ".join(content) + " <end>"
if idx not in map:
map[idx] = []
map[idx].append(content)
return map
def calc_max_length(self, tensor):
return max(len(t) for t in tensor)
def tokenizing(self):
all_train_captions = []
for ele in self.train_captions.values():
all_train_captions = all_train_captions + ele
self.tokenizer = tf.keras.preprocessing.text.Tokenizer(
num_words=self.top_k,
oov_token="<unk>",
filters= '!"#$%&()*+.,-/:;=?@[\]^_`{|}~ ',
)
self.tokenizer.fit_on_texts(all_train_captions)
train_seqs = self.tokenizer.texts_to_sequences(all_train_captions)
self.tokenizer.word_index["<pad>"] = 0
self.tokenizer.index_word[0] = "<pad>"
train_seqs = self.tokenizer.texts_to_sequences(all_train_captions)
self.max_length = self.calc_max_length(train_seqs)
def create_split(self, split_captions):
img_to_cap_vector = collections.defaultdict(list)
for img_id, img_cap in split_captions.items():
img_to_cap_vector[img_id] = pad_sequences(
self.tokenizer.texts_to_sequences(img_cap),
padding="post",
maxlen=self.max_length,
)
cap_split = []
name_split = []
for img_id in img_to_cap_vector.keys():
cap_split.extend(img_to_cap_vector[img_id].tolist())
name_split.extend([img_id] * 5)
return cap_split, name_split
def load_dataset(self, split):
if split == "train":
name_split, cap_split = self.name_train, self.cap_train
else:
name_split, cap_split = self.name_test, self.cap_test
dataset = tf.data.Dataset.from_tensor_slices((name_split, cap_split))
dataset = dataset.map(
lambda item1, item2: tf.numpy_function(
self.map_func, [item1, item2], [tf.float32, tf.int32]
),
num_parallel_calls=tf.data.experimental.AUTOTUNE,
)
dataset = dataset.shuffle(self.buffer_size).batch(self.batch_size)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return dataset
def map_func(self, img_id, cap):
img_path = self.img_folder_np + img_id.decode("utf-8") + ".npy"
img_tensor = np.load(img_path)
return img_tensor, cap