-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_loader_f8k.py
140 lines (117 loc) · 5.38 KB
/
data_loader_f8k.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
import torch
import torchvision.transforms as transforms
import torch.utils.data as data
import cv2
import os
import pickle
import numpy as np
import random
import nltk
from PIL import Image
from build_vocab import Vocabulary
from pycocotools.coco import COCO
class F8kDataset(data.Dataset):
"""COCO Custom Dataset compatible with torch.utils.data.DataLoader."""
def __init__(self, root_txt, image_path, vocab, transform=None):
"""Set the path for images, captions and vocabulary wrapper.
Args:
root_txt: image directory.
vocab: vocabulary wrapper.
transform: image transformer.
"""
self.image_list = []
self.caption_list = []
self.neg_image_list = []
self.image_path = image_path
self.root_txt = root_txt
self.vocab = vocab
self.transform = transform
with open(self.root_txt, 'r') as f:
lines = f.readlines()
for line in lines:
line_list = line.split(',')
self.image_list.append(line_list[0])
self.caption_list.append(line_list[1].replace('\n', ''))
self.neg_image_list = self.image_list.copy()
for i in range(10 % len(self.neg_image_list)):
self.neg_image_list.insert(0,self.neg_image_list.pop())
def __getitem__(self, index):
"""Returns one data pair (image and caption)."""
# print(os.path.join(self.image_path,"//"+self.image_list[index]))
# # image = cv2.imread(self.image_path+self.image_list[index])
# print(image.shape)
# cv2.imshow("index", image)
# cv2.WaitKey(0)
vocab = self.vocab
l = len(self.image_list)
neg_index = random.randint(0,l-1)
while neg_index > index - 5 and neg_index < index + 5:
neg_index = random.randint(0,l-1)
image = Image.open(os.path.join(self.image_path, self.image_list[index])).convert('RGB')
neg_image = Image.open(os.path.join(self.image_path, self.image_list[neg_index])).convert('RGB')
caption = self.caption_list[index]
if self.transform is not None:
image = self.transform(image)
neg_image = self.transform(neg_image)
# Convert caption (string) to word ids.
tokens = nltk.tokenize.word_tokenize(str(caption).lower())
caption = []
neg_caption = []
caption.append(vocab('<start>'))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab('<end>'))
target = torch.Tensor(caption)
return image, target, neg_image
def __len__(self):
return len(self.image_list)
def collate_fn(data):
"""Creates mini-batch tensors from the list of tuples (image, caption).
We should build custom collate_fn rather than using default collate_fn,
because merging caption (including padding) is not supported in default.
Args:
data: list of tuple (image, caption).
- image: torch tensor of shape (3, 256, 256).
- caption: torch tensor of shape (?); variable length.
Returns:
images: torch tensor of shape (batch_size, 3, 256, 256).
targets: torch tensor of shape (batch_size, padded_length).
lengths: list; valid length for each padded caption.
"""
# Sort a data list by caption length (descending order).
data.sort(key=lambda x: len(x[1]), reverse=True)
images, captions, neg_images = zip(*data)
# Merge images (from tuple of 3D tensor to 4D tensor).
images = torch.stack(images, 0)
neg_images = torch.stack(neg_images, 0)
# Merge captions (from tuple of 1D tensor to 2D tensor).
lengths = [len(cap) for cap in captions]
targets = torch.zeros(len(captions), max(lengths)).long()
for i, cap in enumerate(captions):
end = lengths[i]
targets[i, :end] = cap[:end]
return images, neg_images, targets, lengths
def get_loader(root_txt, image_path, vocab, transform, batch_size, shuffle, num_workers):
"""Returns torch.utils.data.DataLoader for custom coco dataset."""
# self, root_txt,image_path, vocab, transform = None
F8K = F8kDataset(root_txt=root_txt,
image_path=image_path,
vocab=vocab,
transform=transform)
# Data loader for COCO dataset
# This will return (images, captions, lengths) for each iteration.
# images: a tensor of shape (batch_size, 3, 224, 224).
# captions: a tensor of shape (batch_size, padded_length).
# lengths: a list indicating valid length for each caption. length is (batch_size).
data_loader = torch.utils.data.DataLoader(dataset=F8K,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
collate_fn=collate_fn)
return data_loader
# if __name__ == '__main__':
# f8k = F8kDataset(
# root_txt=r'/Users/a.w/Desktop/pythonProject/attack-on-multimodal-models/attack-on-multimodal-models/image_captioning/Flickr8k/captions.txt',
# image_path=r'/Users/a.w/Desktop/pythonProject/attack-on-multimodal-models/attack-on-multimodal-models/image_captioning/Flickr8k/Flickr8k_Dataset/Flicker8k_Dataset/')
# img, txt = f8k.__getitem__(1)
# print(txt)
# img.show()