-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdatasets.py
214 lines (185 loc) · 8.49 KB
/
datasets.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
import torch
import h5py
import json
import os
import numpy as np
import random
class TableDatasetEvenLength(object):
"""
Data loader for training baseline encoder-decoder model (WYGIWYS, Dent et al. 2017)
"""
def __init__(self, data_folder, data_name, batch_size, transform=None):
# Open hdf5 file where images are stored
f = os.path.join(data_folder, 'TRAIN_IMAGES_' + data_name + '.hdf5')
self.h = h5py.File(f, 'r')
self.imgs = self.h['images']
# Load encoded tables (completely into memory)
with open(os.path.join(data_folder, 'TRAIN_TABLES_' + data_name + '.json'), 'r') as j:
self.tables = json.load(j)
# Load table lengths (completely into memory)
with open(os.path.join(data_folder, 'TRAIN_TABLELENS_' + data_name + '.json'), 'r') as j:
self.tablelens = json.load(j)
# PyTorch transformation pipeline for the image (normalizing, etc.)
self.transform = transform
self.batch_size = batch_size
self.batch_id = 0
def shuffle(self):
self.batch_id = 0
self.batches = [[]]
len_dict = dict()
# Split samples into groups by table lengths
for i, l in enumerate(self.tablelens):
if l in len_dict:
len_dict[l].append(i)
else:
len_dict[l] = [i]
# Fill with long samples first, so that the samples do not need to be sorted before training
lens = sorted(list(len_dict.keys()), key=lambda x: -x)
# Shuffle each group
for l in lens:
random.shuffle(len_dict[l])
# Generate batches
for l in lens:
k = 0
# Fill previous incomplete batch
if len(self.batches[-1]) < self.batch_size:
deficit = min(len(len_dict[l]), self.batch_size - len(self.batches[-1]))
self.batches[-1] += len_dict[l][k:k + deficit]
k = deficit
# Generate complete batches
while len(len_dict[l]) - k >= self.batch_size:
self.batches.append(len_dict[l][k:k + self.batch_size])
k += self.batch_size
# Create an incomplete batch with left overs
if k < len(len_dict[l]):
self.batches.append(len_dict[l][k:])
# Shuffle the order of batches
random.shuffle(self.batches)
def __iter__(self):
return self
def __next__(self):
if self.batch_id < len(self.batches):
samples = self.batches[self.batch_id]
image_size = self.imgs[samples[0]].shape
imgs = torch.zeros(len(samples), image_size[0], image_size[1], image_size[2], dtype=torch.float)
table_size = len(self.tables[samples[0]])
tables = torch.zeros(len(samples), table_size, dtype=torch.long)
tablelens = torch.zeros(len(samples), 1, dtype=torch.long)
for i, sample in enumerate(samples):
img = torch.FloatTensor(self.imgs[sample] / 255.)
if self.transform is not None:
imgs[i] = self.transform(img)
else:
imgs[i] = img
tables[i] = torch.LongTensor(self.tables[sample])
tablelens[i] = torch.LongTensor([self.tablelens[sample]])
self.batch_id += 1
return imgs, tables, tablelens
else:
raise StopIteration()
def __len__(self):
return len(self.batches)
class TagCellDataset(object):
"""
Data loader for training encoder-dual-decoder model
"""
def __init__(self, data_folder, data_name, split, batch_size, mode='all', transform=None):
"""
:param data_folder: folder where data files are stored
:param data_name: base name of processed datasets
:param split: split, one of 'TRAIN', 'VAL', or 'TEST'
:param batch_size: batch size
:param mode: 'tag', 'tag+cell', 'tag+bbox', or 'tag+cell+bbox'
:param transform: image transform pipeline
"""
assert split in {'TRAIN', 'VAL', 'TEST'}
assert mode in {'tag', 'tag+cell', 'tag+bbox', 'tag+cell+bbox'}
self.split = split
self.mode = mode
self.batch_size = batch_size
# Open hdf5 file where images are stored
f = os.path.join(data_folder, self.split + '_IMAGES_' + data_name + '.hdf5')
self.h = h5py.File(f, 'r')
self.imgs = self.h['images']
# Load encoded tags (completely into memory)
with open(os.path.join(data_folder, self.split + '_TAGS_' + data_name + '.json'), 'r') as j:
self.tags = json.load(j)
# Load tag lengths (completely into memory)
with open(os.path.join(data_folder, self.split + '_TAGLENS_' + data_name + '.json'), 'r') as j:
self.taglens = json.load(j)
# Load cell lengths (completely into memory)
with open(os.path.join(data_folder, self.split + '_CELLLENS_' + data_name + '.json'), 'r') as j:
self.celllens = json.load(j)
if 'cell' in self.mode:
# Load encoded cell tokens (completely into memory)
with open(os.path.join(data_folder, self.split + '_CELLS_' + data_name + '.json'), 'r') as j:
self.cells = json.load(j)
if 'bbox' in self.mode:
# Load encoded tags (completely into memory)
with open(os.path.join(data_folder, self.split + '_CELLBBOXES_' + data_name + '.json'), 'r') as j:
self.cellbboxes = json.load(j)
# PyTorch transformation pipeline for the image (normalizing, etc.)
self.transform = transform
# Total number of datapoints
self.dataset_size = len(self.tags)
self.ind = np.array(range(self.dataset_size))
self.pointer = 0
def shuffle(self):
self.ind = np.random.permutation(self.dataset_size)
self.pointer = 0
def __iter__(self):
return self
def __getitem__(self, i):
img = torch.FloatTensor(self.imgs[i])
tags = self.tags[i]
taglens = self.taglens[i]
cells = self.cells[i]
celllens = self.celllens[i]
image_size = self.imgsizes[i]
return img, tags, taglens, cells, celllens, image_size
def __next__(self):
if self.pointer < self.dataset_size:
if self.dataset_size - self.pointer >= self.batch_size:
step = self.batch_size
samples = self.ind[self.pointer:self.pointer + step]
else:
step = self.dataset_size - self.pointer
lack = self.batch_size - step
samples = np.hstack((self.ind[self.pointer:self.pointer + step], np.array(range(lack))))
image_size = self.imgs[samples[0]].shape
imgs = torch.zeros(len(samples), image_size[0], image_size[1], image_size[2], dtype=torch.float)
max_tag_len = max([self.taglens[sample] for sample in samples])
tags = torch.zeros(len(samples), max_tag_len, dtype=torch.long)
taglens = torch.zeros(len(samples), 1, dtype=torch.long)
num_cells = torch.zeros(len(samples), 1, dtype=torch.long)
if 'cell' in self.mode:
cells = []
celllens = []
if 'bbox' in self.mode:
cellbboxes = []
for i, sample in enumerate(samples):
img = torch.FloatTensor(self.imgs[sample] / 255.)
if self.transform is not None:
imgs[i] = self.transform(img)
else:
imgs[i] = img
tags[i] = torch.LongTensor(self.tags[sample][:max_tag_len])
taglens[i] = torch.LongTensor([self.taglens[sample]])
num_cells[i] = len(self.celllens[sample])
if 'cell' in self.mode:
max_cell_len = max(self.celllens[sample])
cells.append(torch.LongTensor(self.cells[sample])[:, :max_cell_len])
celllens.append(torch.LongTensor(self.celllens[sample]))
if 'bbox' in self.mode:
cellbboxes.append(torch.FloatTensor(self.cellbboxes[sample]))
self.pointer += step
output = (imgs, tags, taglens, num_cells)
if 'cell' in self.mode:
output += (cells, celllens)
if 'bbox' in self.mode:
output += (cellbboxes,)
return output
else:
raise StopIteration()
def __len__(self):
return int(np.ceil(self.dataset_size / self.batch_size))