-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
205 lines (169 loc) · 6.21 KB
/
data_loader.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
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torch.utils.data as data
from torch.utils.data import Dataset, DataLoader
import os
import pickle
import numpy as np
import nltk
from PIL import Image
from build_vocab import Vocabulary
import os, glob
import numpy as np
# from dataset import TwitterDataset
from preprocess.japanese_tokenizer import JapaneseTokenizer
from pycocotools2.coco import COCO
class CocoDataset(data.Dataset):
"""COCO Custom Dataset compatible with torch.utils.data.DataLoader."""
def __init__(self, root, json, vocab, transform=None, text_tokenizer=None):
"""Set the path for images, captions and vocabulary wrapper.
Args:
root: image directory.
json: coco annotation file path.
vocab: vocabulary wrapper.
transform: image transformer.
"""
self.root = root
self.coco = COCO(json)
self.ids = list(self.coco.anns.keys())
self.vocab = vocab
self.transform = transform
self.text_tokenizer = text_tokenizer
def __getitem__(self, index):
"""Returns one data pair (image and caption)."""
coco = self.coco
vocab = self.vocab
ann_id = self.ids[index]
caption = coco.anns[ann_id]["caption"]
# caption = coco.anns[ann_id]["tokenized_caption"]
img_id = coco.anns[ann_id]["image_id"]
path = coco.loadImgs(img_id)[0]["file_name"]
image = Image.open(os.path.join(self.root, path)).convert("RGB")
if self.transform is not None:
image = self.transform(image)
# Convert caption (string) to word ids.
# tokens = nltk.tokenize.word_tokenize(str(caption).lower())
# tokens = str(caption).split()
tokens = self.text_tokenizer.tokenize(caption, return_str=True).split()
caption = []
caption.append(vocab("<start>"))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab("<end>"))
target = torch.Tensor(caption)
return image, target
def __len__(self):
return len(self.ids)
def loadPickle(fileName):
with open(fileName, mode="rb") as f:
return pickle.load(f)
class TwitterDataset(Dataset):
def __init__(
self,
split,
use_account,
vocab,
image_transform=None,
text_tokenizer=None,
data_dir="data",
):
self.split = split
self.image_transform = image_transform
self.text_tokenizer = text_tokenizer
self.vocab = vocab
self.data_dir = data_dir
self.imgs = glob.glob(os.path.join(self.data_dir, "images/*.png"))
# 使うtwitterアカウントのアノテーションだけ読み込む
self.annos = []
for user in use_account:
ann_path = os.path.join(self.data_dir, f"annos/{user}.pickle")
ann = loadPickle(ann_path)
if self.split == 'train':
self.annos += ann[:-20]
elif self.split == 'val':
self.annos += ann[-20:]
print(f"Created {self.split} Dataset of Len: {len(self.annos)}")
def __getitem__(self, idx):
ann = self.annos[idx]
image_file = os.path.join(self.data_dir, f'images/{ann["filename"]}')
orig_text = ann["text"]
orig_img = Image.open(image_file).convert("RGB")
img = self.image_transform[self.split](orig_img)
tokens = self.text_tokenizer.tokenize(orig_text, return_str=True).split()
caption = []
caption.append(self.vocab("<start>"))
caption.extend([self.vocab(token) for token in tokens])
caption.append(self.vocab("<end>"))
target = torch.Tensor(caption)
# data = {'image': img, 'text': text, 'orig_img': orig_img, 'orig_text': orig_text ,'screen_name': ann['screen_name']}
return img, target
def __len__(self):
return len(self.annos)
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 = zip(*data)
# Merge images (from tuple of 3D tensor to 4D tensor).
images = torch.stack(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, targets, lengths
def get_loader(
root,
json,
vocab,
transform,
batch_size,
shuffle,
num_workers,
mode,
mecab_dict_path,
):
"""Returns torch.utils.data.DataLoader for dataset."""
text_tokenizer = JapaneseTokenizer(splitter="MeCab", model=mecab_dict_path)
# COCO caption dataset
if mode == "coco":
dataset = CocoDataset(
root=root,
json=json,
vocab=vocab,
transform=transform,
text_tokenizer=text_tokenizer,
)
if mode == "twitter":
# TODO 読み込み方
from collect_twitter_data.data_info import data_info
use_account = data_info["animal"]
dataset = TwitterDataset(
"train",
use_account,
vocab,
image_transform=transform,
text_tokenizer=text_tokenizer,
data_dir="data",
)
data_loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
collate_fn=collate_fn,
)
return data_loader