-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataset.py
323 lines (283 loc) · 10 KB
/
dataset.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import csv
import os
import random
import torch
from PIL import Image, ImageOps
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
START = "<SOS>"
END = "<EOS>"
PAD = "<PAD>"
SPECIAL_TOKENS = [START, END, PAD]
# Rather ignorant way to encode the truth, but at least it works.
def encode_truth(truth, token_to_id):
"""Encoding string truth to id
Args:
truth (string) : ground truth
token_to_id (dict) : token dictionary
Returns:
truth(list) : List that replace truth with id
"""
truth_tokens = truth.split()
for token in truth_tokens:
if token not in token_to_id:
raise Exception("Truth contains unknown token")
truth_tokens = [token_to_id[x] for x in truth_tokens]
if '' in truth_tokens: truth_tokens.remove('')
return truth_tokens
def load_vocab(tokens_paths):
"""Load vocab from token.txt
Args:
tokens_paths(list) : list of token_paths
Returns:
token_to_id(dict) : dictionary with token in key and id in value
id_to_token(dict) : dictionary with id in key and token in value
"""
tokens = []
tokens.extend(SPECIAL_TOKENS)
for tokens_file in tokens_paths:
with open(tokens_file, "r") as fd:
reader = fd.read()
for token in reader.split("\n"):
if token not in tokens:
tokens.append(token)
token_to_id = {tok: i for i, tok in enumerate(tokens)}
id_to_token = {i: tok for i, tok in enumerate(tokens)}
return token_to_id, id_to_token
def split_gt(groundtruth, proportion=1.0, test_percent=None):
"""Split dataset in train and test
Args:
groundtruth (string) : Path to ground truth text file
proportion (float, optional): represent the proportion of the dataset to include in the train split. Defaults to 1.0.
test_percent (float, optional): represent the proportion of the dataset to include in the test split. Defaults to None.
"""
root = os.path.join(os.path.dirname(groundtruth), "images")
with open(groundtruth, "r") as fd:
data=[]
for line in fd:
data.append(line.strip().split("\t"))
random.shuffle(data)
dataset_len = round(len(data) * proportion)
data = data[:dataset_len]
data = [[os.path.join(root, x[0]), x[1]] for x in data]
if test_percent:
test_len = round(len(data) * test_percent)
return data[test_len:], data[:test_len]
else:
return data
def collate_batch(data):
"""Dataloader function
Collect batch data with padding.
Args:
data (list) : train dataset
Returns:
data(list) : train dataset
"""
max_len = max([len(d["truth"]["encoded"]) for d in data])
# Padding with -1, will later be replaced with the PAD token
padded_encoded = [
d["truth"]["encoded"] + (max_len - len(d["truth"]["encoded"])) * [-1]
for d in data
]
return {
"path": [d["path"] for d in data],
"image": torch.stack([d["image"] for d in data], dim=0),
"truth": {
"text": [d["truth"]["text"] for d in data],
"encoded": torch.tensor(padded_encoded)
},
}
def collate_eval_batch(data):
"""Dataloader function
Collect batch data with padding.
Args:
data (list) : eval dataset
Returns:
data(list) : eval dataset
"""
max_len = max([len(d["truth"]["encoded"]) for d in data])
# Padding with -1, will later be replaced with the PAD token
padded_encoded = [
d["truth"]["encoded"] + (max_len - len(d["truth"]["encoded"])) * [-1]
for d in data
]
return {
"path": [d["path"] for d in data],
"file_path":[d["file_path"] for d in data],
"image": torch.stack([d["image"] for d in data], dim=0),
"truth": {
"text": [d["truth"]["text"] for d in data],
"encoded": torch.tensor(padded_encoded)
},
}
class LoadDataset(Dataset):
"""Load Dataset"""
def __init__(
self,
groundtruth,
tokens_file,
crop=False,
transform=None,
rgb=3,
):
"""
Args:
groundtruth (string): Path to ground truth TXT/TSV file
tokens_file (string): Path to tokens TXT file
ext (string): Extension of the input files
crop (bool, optional): Crop images to their bounding boxes [Default: False]
transform (callable, optional): Optional transform to be applied
on a sample.
"""
super(LoadDataset, self).__init__()
self.crop = crop
self.transform = transform
self.rgb = rgb
self.token_to_id, self.id_to_token = load_vocab(tokens_file)
self.data = [
{
"path": p,
"truth": {
"text": truth,
"encoded": [
self.token_to_id[START],
*encode_truth(truth, self.token_to_id),
self.token_to_id[END],
],
},
}
for p, truth in groundtruth
]
def __len__(self):
return len(self.data)
def __getitem__(self, i):
item = self.data[i]
image = Image.open(item["path"])
if self.rgb == 3:
image = image.convert("RGB")
elif self.rgb == 1:
image = image.convert("L")
else:
raise NotImplementedError
if self.crop:
# Image needs to be inverted because the bounding box cuts off black pixels,
# not white ones.
bounding_box = ImageOps.invert(image).getbbox()
image = image.crop(bounding_box)
if self.transform:
image = self.transform(image)
return {"path": item["path"], "truth": item["truth"], "image": image}
class LoadEvalDataset(Dataset):
"""Load Dataset"""
def __init__(
self,
groundtruth,
token_to_id,
id_to_token,
crop=False,
transform=None,
rgb=3,
):
"""
Args:
groundtruth (string): Path to ground truth TXT/TSV file
tokens_file (string): Path to tokens TXT file
ext (string): Extension of the input files
crop (bool, optional): Crop images to their bounding boxes [Default: False]
transform (callable, optional): Optional transform to be applied
on a sample.
"""
super(LoadEvalDataset, self).__init__()
self.crop = crop
self.rgb = rgb
self.token_to_id = token_to_id
self.id_to_token = id_to_token
self.transform = transform
self.data = [
{
"path": p,
"file_path":p1,
"truth": {
"text": truth,
"encoded": [
self.token_to_id[START],
*encode_truth(truth, self.token_to_id),
self.token_to_id[END],
],
},
}
for p, p1,truth in groundtruth
]
def __len__(self):
return len(self.data)
def __getitem__(self, i):
item = self.data[i]
image = Image.open(item["path"])
if self.rgb == 3:
image = image.convert("RGB")
elif self.rgb == 1:
image = image.convert("L")
else:
raise NotImplementedError
if self.crop:
# Image needs to be inverted because the bounding box cuts off black pixels,
# not white ones.
bounding_box = ImageOps.invert(image).getbbox()
image = image.crop(bounding_box)
if self.transform:
image = self.transform(image)
return {"path": item["path"], "file_path":item["file_path"],"truth": item["truth"], "image": image}
def dataset_loader(options, train_transformed, valid_transformed):
"""load dataset
split and return dataset into train/test
Args:
options (Flags) : model's option
train_transformed (Compose) : transform to be applied on a sample.
valid_transformed (Compose) : transform to be applied on a sample.
Returns:
train_data_loader(DataLoader) : train dataloader
valid_data_loader(DataLoader) : valid dataloader
train_dataset(list) : train dataset
valid_dataset(list) : valid dataset
"""
# Read data
train_data, valid_data = [], []
if options.data.random_split:
for i, path in enumerate(options.data.train):
prop = 1.0
if len(options.data.dataset_proportions) > i:
prop = options.data.dataset_proportions[i]
train, valid = split_gt(path, prop, options.data.test_proportions)
train_data += train
valid_data += valid
else:
for i, path in enumerate(options.data.train):
prop = 1.0
if len(options.data.dataset_proportions) > i:
prop = options.data.dataset_proportions[i]
train_data += split_gt(path, prop)
for i, path in enumerate(options.data.test):
valid = split_gt(path)
valid_data += valid
# Load data
train_dataset = LoadDataset(
train_data, options.data.token_paths, crop=options.data.crop, transform=train_transformed, rgb=options.data.rgb
)
train_data_loader = DataLoader(
train_dataset,
batch_size=options.batch_size,
shuffle=True,
num_workers=options.num_workers,
collate_fn=collate_batch,
)
valid_dataset = LoadDataset(
valid_data, options.data.token_paths, crop=options.data.crop, transform=valid_transformed, rgb=options.data.rgb
)
valid_data_loader = DataLoader(
valid_dataset,
batch_size=options.batch_size,
shuffle=False,
num_workers=options.num_workers,
collate_fn=collate_batch,
)
return train_data_loader, valid_data_loader, train_dataset, valid_dataset