-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathdata_processing.py
More file actions
208 lines (161 loc) · 6.98 KB
/
data_processing.py
File metadata and controls
208 lines (161 loc) · 6.98 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
import os # when loading file paths
import pandas as pd # for lookup in annotation file
import spacy # for tokenizer
import torch
from torch.nn.utils.rnn import pad_sequence # pad batch
from torch.utils.data import DataLoader, Dataset
from PIL import Image # Load img
import torchvision.transforms as transforms
from sklearn.model_selection import train_test_split
# download spacy first with pip install spacy
# This is the version for the english language
spacy_eng = spacy.load("en_core_web_sm")
class Vocabulary :
def __init__(self, freq_threshold):
# itos will contain the mapping of indices to the corresponding words
self.itos = {0: "<PAD>", 1: "<SOS>", 2: "<EOS>", 3: "<UNK>"}
# stoi is just the inverse mapping of itos
self.stoi = {"<PAD>": 0, "<SOS>": 1, "<EOS>": 2, "<UNK>": 3}
# the number of times a word has to occur in the text for it to be eligible to be
# part of our vocabulary
self.freq_threshold = freq_threshold
# will return us the length of our vocabulary
def __len__(self):
return len(self.itos)
@staticmethod
def tokenizer_eng(text):
return [tok.text.lower() for tok in spacy_eng.tokenizer(text)]
# building our vocabulary from the set of sentences given to us
def build_vocabulary(self, sentence_list):
# to keep track of the frequency of a word appearing in our sentences
frequencies = {}
# starting from 4 as the first 4 are already taken up by the key words
idx = 4
for sentence in sentence_list:
for word in self.tokenizer_eng(sentence):
if word not in frequencies:
frequencies[word] = 1
else:
frequencies[word] += 1
if frequencies[word] == self.freq_threshold:
self.stoi[word] = idx
self.itos[idx] = word
idx += 1
def numericalize(self, text):
tokenized_text = self.tokenizer_eng(text)
return [
self.stoi[token] if token in self.stoi else self.stoi["<UNK>"]
for token in tokenized_text
]
class Flickr8k(Dataset):
def __init__(self,images_path,captions_file,transform = None,freq_threshold=4):
self.images_path = images_path
self.captions_file = captions_file
self.dataframe = pd.read_csv(captions_file)
self.transform = transform
# Get img, caption columns
self.imgs = self.dataframe["image"]
self.captions = self.dataframe["caption"]
# Initialize vocabulary and build vocab
self.vocab = Vocabulary(freq_threshold)
self.vocab.build_vocabulary(self.captions.tolist())
# Splitting it into train and test datasets
self.train_imgs , self.test_imgs , self.train_captions , self.test_captions = train_test_split(self.imgs,self.captions,test_size=0.2,train_size=0.8,random_state=1,shuffle=True)
self.train_imgs.reset_index(drop=True, inplace=True)
self.train_captions.reset_index(drop=True, inplace=True)
self.test_imgs.reset_index(drop=True, inplace=True)
self.test_captions.reset_index(drop=True, inplace=True)
def get_training_data(self):
return self.train_imgs, self.train_captions
def get_testing_data(self):
return self.test_imgs, self.test_captions
class Flickr8k_Training(Dataset):
def __init__(self, images_path , train_imgs, train_captions, vocab, transform=None):
self.images_path = images_path
self.transform = transform
self.imgs = train_imgs
self.captions = train_captions
# Initialize vocabulary and build vocab
self.vocab = vocab
def __len__(self):
return len(self.captions)
def __getitem__(self, index):
caption = self.captions[index]
img_id = self.imgs[index]
img = Image.open(os.path.join(self.images_path, img_id)).convert("RGB")
if self.transform is not None:
img = self.transform(img)
numericalized_caption = [self.vocab.stoi["<SOS>"]]
numericalized_caption += self.vocab.numericalize(caption)
numericalized_caption.append(self.vocab.stoi["<EOS>"])
return img, torch.tensor(numericalized_caption)
class Flickr8k_Testing(Dataset):
def __init__(self, images_path, test_imgs, test_captions, vocab, transform=None):
self.images_path = images_path
self.transform = transform
self.imgs = test_imgs
self.captions = test_captions
# Initialize vocabulary and build vocab
self.vocab = vocab
def __len__(self):
return len(self.captions)
def __getitem__(self, index):
caption = self.captions[index]
img_id = self.imgs[index]
img = Image.open(os.path.join(self.images_path, img_id)).convert("RGB")
if self.transform is not None:
img = self.transform(img)
numericalized_caption = [self.vocab.stoi["<SOS>"]]
numericalized_caption += self.vocab.numericalize(caption)
numericalized_caption.append(self.vocab.stoi["<EOS>"])
return img, torch.tensor(numericalized_caption)
class MyCollate:
def __init__(self, pad_idx):
self.pad_idx = pad_idx
def __call__(self, batch):
imgs = [item[0].unsqueeze(0) for item in batch]
imgs = torch.cat(imgs, dim=0)
targets = [item[1] for item in batch]
targets = pad_sequence(targets, batch_first=False, padding_value=self.pad_idx)
return imgs, targets
def get_loader(root_folder,
annotation_file,
transform,
batch_size=32,
num_workers=2,
shuffle=True,
pin_memory=True,
):
dataset = Flickr8k(root_folder,annotation_file,transform)
train_imgs , train_captions = dataset.get_training_data()
test_imgs , test_captions = dataset.get_testing_data()
train_dataset = Flickr8k_Training(root_folder,train_imgs,train_captions,dataset.vocab,transform)
test_dataset = Flickr8k_Testing(root_folder,test_imgs,test_captions,dataset.vocab,transform)
pad_idx_train = train_dataset.vocab.stoi["<PAD>"]
pad_idx_test = test_dataset.vocab.stoi["<PAD>"]
train_loader = DataLoader(
dataset=train_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=shuffle,
pin_memory=pin_memory,
collate_fn=MyCollate(pad_idx=pad_idx_train),
)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=shuffle,
pin_memory=pin_memory,
collate_fn=MyCollate(pad_idx=pad_idx_test),
)
return train_loader, test_loader, train_dataset, test_dataset
if __name__ == "__main__":
transform = transforms.Compose(
[transforms.Resize((224, 224)), transforms.ToTensor(), ]
)
train_loader, test_loader, train_dataset, test_dataset = get_loader(
"flickr8k/Images/", "flickr8k/captions.txt", transform=transform
)
for idx, (data, image) in enumerate(test_loader):
print(idx)