forked from multiscore/SMT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
200 lines (150 loc) · 6.83 KB
/
data.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
import random
import re
import cv2
import torch
import numpy as np
import cv2
import datasets
from ExperimentConfig import ExperimentConfig
from data_augmentation.data_augmentation import augment, convert_img_to_tensor
from utils import check_and_retrieveVocabulary
from rich import progress
from lightning import LightningDataModule
from torch.utils.data import Dataset
from torchvision import transforms
def load_set(dataset, split="train", reduce_ratio=1.0, fixed_size=None):
x = []
y = []
loaded_dataset = datasets.load_dataset(dataset, split=split)
for sample in progress.track(loaded_dataset):
krn_content = sample['transcription']
img = np.array(sample['image'])
if fixed_size != None:
width = fixed_size[1]
height = fixed_size[0]
elif img.shape[1] > 3056:
width = int(np.ceil(3056 * reduce_ratio))
height = int(np.ceil(max(img.shape[0], 256) * reduce_ratio))
else:
width = int(np.ceil(img.shape[1] * reduce_ratio))
height = int(np.ceil(max(img.shape[0], 256) * reduce_ratio))
img = cv2.resize(img, (width, height))
y.append([content + '\n' for content in krn_content.split("\n")])
x.append(img)
return x, y
def batch_preparation_img2seq(data):
images = [sample[0] for sample in data]
dec_in = [sample[1] for sample in data]
gt = [sample[2] for sample in data]
max_image_width = max(128, max([img.shape[2] for img in images]))
max_image_height = max(256, max([img.shape[1] for img in images]))
X_train = torch.ones(size=[len(images), 1, max_image_height, max_image_width], dtype=torch.float32)
for i, img in enumerate(images):
_, h, w = img.size()
X_train[i, :, :h, :w] = img
max_length_seq = max([len(w) for w in gt])
decoder_input = torch.zeros(size=[len(dec_in),max_length_seq])
y = torch.zeros(size=[len(gt),max_length_seq])
for i, seq in enumerate(dec_in):
decoder_input[i, 0:len(seq)-1] = torch.from_numpy(np.asarray([char for char in seq[:-1]]))
for i, seq in enumerate(gt):
y[i, 0:len(seq)-1] = torch.from_numpy(np.asarray([char for char in seq[1:]]))
return X_train, decoder_input.long(), y.long()
class OMRIMG2SEQDataset(Dataset):
def __init__(self, augment=False) -> None:
self.teacher_forcing_error_rate = 0.2
self.x = None
self.y = None
self.augment = augment
super().__init__()
def apply_teacher_forcing(self, sequence):
errored_sequence = sequence.clone()
for token in range(1, len(sequence)):
if np.random.rand() < self.teacher_forcing_error_rate and sequence[token] != self.padding_token:
errored_sequence[token] = np.random.randint(0, len(self.w2i))
return errored_sequence
def __len__(self):
return len(self.x)
def __getitem__(self, index):
if self.augment:
x = augment(self.x[index])
else:
x = convert_img_to_tensor(self.x[index])
y = torch.from_numpy(np.asarray([self.w2i[token] for token in self.y[index]]))
decoder_input = self.apply_teacher_forcing(y)
return x, decoder_input, y
def get_max_hw(self):
m_width = np.max([img.shape[1] for img in self.x])
m_height = np.max([img.shape[0] for img in self.x])
return m_height, m_width
def get_max_seqlen(self):
return np.max([len(seq) for seq in self.y])
def vocab_size(self):
return len(self.w2i)
def get_gt(self):
return self.y
def set_dictionaries(self, w2i, i2w):
self.w2i = w2i
self.i2w = i2w
self.padding_token = w2i['<pad>']
def get_dictionaries(self):
return self.w2i, self.i2w
def get_i2w(self):
return self.i2w
class GrandStaffSingleSystem(OMRIMG2SEQDataset):
def __init__(self, data_path, split, augment=False) -> None:
self.augment = augment
self.teacher_forcing_error_rate = 0.2
self.x, self.y = load_set(data_path, split)
self.y = self.preprocess_gt(self.y)
self.tensorTransform = transforms.ToTensor()
self.num_sys_gen = 1
self.fixed_systems_num = False
def erase_numbers_in_tokens_with_equal(self, tokens):
return [re.sub(r'(?<=\=)\d+', '', token) for token in tokens]
def get_width_avgs(self):
widths = [image.shape[1] for image in self.x]
return np.average(widths), np.max(widths), np.min(widths)
def __getitem__(self, index):
x = self.x[index]
y = self.y[index]
if self.augment:
x = augment(x)
else:
x = convert_img_to_tensor(x)
y = torch.from_numpy(np.asarray([self.w2i[token] for token in y]))
decoder_input = self.apply_teacher_forcing(y)
return x, decoder_input, y
def __len__(self):
return len(self.x)
def preprocess_gt(self, Y):
for idx, krn in enumerate(Y):
krnlines = []
krn = "".join(krn)
krn = krn.replace(" ", " <s> ")
krn = krn.replace("·", "")
krn = krn.replace("\t", " <t> ")
krn = krn.replace("\n", " <b> ")
krn = krn.split(" ")
Y[idx] = self.erase_numbers_in_tokens_with_equal(['<bos>'] + krn + ['<eos>'])
return Y
class GrandStaffDataset(LightningDataModule):
def __init__(self, config:ExperimentConfig) -> None:
super().__init__()
self.data_path = config.data_path
self.vocab_name = config.vocab_name
self.batch_size = config.batch_size
self.num_workers = config.num_workers
self.train_set = GrandStaffSingleSystem(data_path=self.data_path, split="train", augment=True)
self.val_set = GrandStaffSingleSystem(data_path=self.data_path, split="val",)
self.test_set = GrandStaffSingleSystem(data_path=self.data_path, split="test",)
w2i, i2w = check_and_retrieveVocabulary([self.train_set.get_gt(), self.val_set.get_gt(), self.test_set.get_gt()], "vocab/", f"{self.vocab_name}")
self.train_set.set_dictionaries(w2i, i2w)
self.val_set.set_dictionaries(w2i, i2w)
self.test_set.set_dictionaries(w2i, i2w)
def train_dataloader(self):
return torch.utils.data.DataLoader(self.train_set, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=True, collate_fn=batch_preparation_img2seq)
def val_dataloader(self):
return torch.utils.data.DataLoader(self.val_set, batch_size=self.batch_size, num_workers=self.num_workers, collate_fn=batch_preparation_img2seq)
def test_dataloader(self):
return torch.utils.data.DataLoader(self.test_set, batch_size=self.batch_size, num_workers=self.num_workers, collate_fn=batch_preparation_img2seq)