forked from LBH1024/CAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset new.py
195 lines (159 loc) · 6.57 KB
/
dataset new.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
import torch
from torch.utils.data import DataLoader, RandomSampler
import os
from dataclasses import dataclass
from typing import List, Tuple
from zipfile import ZipFile
from PIL import Image
from torch import FloatTensor, LongTensor
from torchvision.transforms import transforms
from vocab import CROHMEVocab
vocab = CROHMEVocab()
Data = List[Tuple[str, Image.Image, List[str]]]
MAX_SIZE = 35e4 # change here accroading to your GPU memory
# load data
def data_iterator(
data: Data,
batch_size: int,
batch_Imagesize: int = MAX_SIZE,
maxlen: int = 200,
maxImagesize: int = MAX_SIZE,
):
fname_batch = []
feature_batch = []
label_batch = []
feature_total = []
label_total = []
fname_total = []
biggest_image_size = 0
data.sort(key=lambda x: x[1].size[0] * x[1].size[1])
i = 0
for fname, fea, lab in data:
size = fea.size[0] * fea.size[1]
fea = transforms.ToTensor()(fea)
if size > biggest_image_size:
biggest_image_size = size
batch_image_size = biggest_image_size * (i + 1)
if len(lab) > maxlen:
print("sentence", i, "length bigger than", maxlen, "ignore")
elif size > maxImagesize:
print(
f"image: {fname} size: {fea.shape[1]} x {fea.shape[2]} = bigger than {maxImagesize}, ignore"
)
else:
if batch_image_size > batch_Imagesize or i == batch_size: # a batch is full
fname_total.append(fname_batch)
feature_total.append(feature_batch)
label_total.append(label_batch)
i = 0
biggest_image_size = size
fname_batch = []
feature_batch = []
label_batch = []
fname_batch.append(fname)
feature_batch.append(fea)
label_batch.append(lab)
i += 1
else:
fname_batch.append(fname)
feature_batch.append(fea)
label_batch.append(lab)
i += 1
# last batch
fname_total.append(fname_batch)
feature_total.append(feature_batch)
label_total.append(label_batch)
print("total ", len(feature_total), "batch data loaded")
return list(zip(fname_total, feature_total, label_total))
def extract_data(archive: ZipFile, dir_name: str) -> Data:
"""Extract all data need for a dataset from zip archive
Args:
archive (ZipFile):
dir_name (str): dir name in archive zip (eg: train, test_2014......)
Returns:
Data: list of tuple of image and formula
"""
with archive.open(f"{dir_name}/caption.txt", "r") as f:
captions = f.readlines()
data = []
for line in captions:
tmp = line.decode().strip().split()
img_name = tmp[0]
formula = tmp[1:]
with archive.open(f"{dir_name}/{img_name}.bmp", "r") as f:
# move image to memory immediately, avoid lazy loading, which will lead to None pointer error in loading
img = Image.open(f).copy()
data.append((img_name, img, formula))
print(f"Extract data from: {dir_name}, with data size: {len(data)}")
return data
@dataclass
class Batch:
img_bases: List[str] # [b,]
imgs: FloatTensor # [b, 1, H, W]
mask: LongTensor # [b, H, W]
indices: List[List[int]] # [b, l]
def __len__(self) -> int:
return len(self.img_bases)
def to(self, device) -> "Batch":
return Batch(
img_bases=self.img_bases,
imgs=self.imgs.to(device),
mask=self.mask.to(device),
indices=self.indices,
)
def build_dataset(archive, folder: str, batch_size: int):
data = extract_data(archive, folder)
return data_iterator(data, batch_size)
def get_crohme_dataset(params):
params['word_num'] = vocab.__len__()
with ZipFile("data.zip") as archive:
train_dataset = build_dataset(archive, "train", params['batch_size'])
eval_dataset = build_dataset(archive, params['eval_year'], 1)
train_sampler = RandomSampler(train_dataset)
eval_sampler = RandomSampler(eval_dataset)
train_loader = DataLoader(train_dataset, sampler=train_sampler,
num_workers=params['workers'], collate_fn=collate_fn, pin_memory=True)
eval_loader = DataLoader(eval_dataset, sampler=eval_sampler,
num_workers=params['workers'], collate_fn=collate_fn, pin_memory=True)
print(f'train dataset: {len(train_dataset)} train steps: {len(train_loader)} '
f'eval dataset: {len(eval_dataset)} eval steps: {len(eval_loader)} ')
return train_loader, eval_loader
def collate_fn(batch_images):
max_width, max_height, max_length = 0, 0, 0
batch, channel = len(batch_images), batch_images[0][0].shape[0]
proper_items = []
for item in batch_images:
if item[0].shape[1] * max_width > 1600 * 320 or item[0].shape[2] * max_height > 1600 * 320:
continue
max_height = item[0].shape[1] if item[0].shape[1] > max_height else max_height
max_width = item[0].shape[2] if item[0].shape[2] > max_width else max_width
max_length = item[1].shape[0] if item[1].shape[0] > max_length else max_length
proper_items.append(item)
images, image_masks = torch.zeros((len(proper_items), channel, max_height, max_width)), torch.zeros((len(proper_items), 1, max_height, max_width))
labels, labels_masks = torch.zeros((len(proper_items), max_length)).long(), torch.zeros((len(proper_items), max_length))
for i in range(len(proper_items)):
_, h, w = proper_items[i][0].shape
images[i][:, :h, :w] = proper_items[i][0]
image_masks[i][:, :h, :w] = 1
l = proper_items[i][1].shape[0]
labels[i][:l] = proper_items[i][1]
labels_masks[i][:l] = 1
return images, image_masks, labels, labels_masks
def collate_fn_bttr(batch):
assert len(batch) == 1
batch = batch[0]
fnames = batch[0]
images_x = batch[1]
seqs_y = [vocab.words2indices(x) for x in batch[2]]
heights_x = [s.size(1) for s in images_x]
widths_x = [s.size(2) for s in images_x]
n_samples = len(heights_x)
max_height_x = max(heights_x)
max_width_x = max(widths_x)
x = torch.zeros(n_samples, 1, max_height_x, max_width_x)
x_mask = torch.ones(n_samples, max_height_x, max_width_x, dtype=torch.bool)
for idx, s_x in enumerate(images_x):
x[idx, :, : heights_x[idx], : widths_x[idx]] = s_x
x_mask[idx, : heights_x[idx], : widths_x[idx]] = 0
# return fnames, x, x_mask, seqs_y
return Batch(fnames, x, x_mask, seqs_y)