-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathdataset_iter.py
224 lines (205 loc) · 11.7 KB
/
dataset_iter.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
'''
Filename: dataset_iter.py
Project: image2katex
File Created: Wednesday, 5th December 2018 3:24:14 pm
Author: xiaofeng ([email protected])
--------------------------
Last Modified: Sunday, 9th December 2018 4:38:25 pm
Modified By: xiaofeng ([email protected])
---------------------------
: 2018.06 - 2018 .
'''
import os
import random
from math import ceil
import cv2
from utils.process_image import img_aug
import numpy as np
from PIL import Image
class DataIteratorSeq2SeqAtt(object):
def __init__(self, _config, _logging, data_set, **kwargs):
self._config = _config
self._logging = _logging
self._image_folder = self._config.dataset.get('image_folder')
self.pad_idx = self._config.dataset.get("id_pad")
self.end_idx = self._config.dataset.get("id_end")
self._prepared_dir = self._config.dataset.get('prepared_folder')
self._set = data_set
assert isinstance(self._set, list), 'Input dataset mus be list'
assert isinstance(self._prepared_dir, list) and isinstance(
self._image_folder, list), 'Input dataset details must be list format'
def generate(self, _seed=100):
""" Generate iters """
for _dataset in self._set:
if _dataset in ['train', 'validate']:
batch_size = self._config.model.batch_size
else:
assert _dataset in ['test'], '_dataset must be [train,validate,test]'
batch_size = self._config.model.test_batch_size
self._logging.info('Generate the [{}] dataset'.format(_dataset))
if len(self._prepared_dir) > 1:
# because the seed can be used for once, so set seed for each list
random.seed(_seed)
random.shuffle(self._prepared_dir)
random.seed(_seed)
random.shuffle(self._image_folder)
for idx in range(len(self._prepared_dir)):
_dataset_dir = os.path.join(self._prepared_dir[idx], _dataset + '_buckets.npy')
_image_folder = self._image_folder[idx]
self._logging.info('Load dataset is [{:s}]'.format(_image_folder))
dataset_details = np.load(_dataset_dir).tolist()
bucket_size = [x for x in dataset_details if x[0]*x[1] < 48000]
# bucket_size = [x for x in dataset_details]
# bucket_size=[x for x in dataset_details]
random.shuffle(bucket_size)
total_nums = sum((len(dataset_details[x]) for x in dataset_details))
self._logging.info('Total num is [{:d}]'.format(total_nums))
for bucket in bucket_size:
bucket_details = dataset_details[bucket]
set_list = [(image_name, label) for image_name, label in bucket_details
if os.path.exists(os.path.join(_image_folder, image_name + '.png'))]
random.shuffle(set_list)
dataset_num = len(set_list)
if _dataset in ['train']:
if dataset_num < 100:
continue
_iters = int(ceil(dataset_num / batch_size))
self._logging.info('Total iter of the bucket size [{}] is {}'.format(
bucket, _iters))
for i in range(_iters):
start_idx = i * batch_size
end_idx = min((i + 1) * batch_size, dataset_num-1)
_samples_nums = end_idx - start_idx
if _samples_nums != batch_size:
continue
_sublist = set_list[start_idx:end_idx]
batch_imgs = []
_batch_forms = []
batch_names = []
for image_name, label in _sublist:
image_path = os.path.join(_image_folder, image_name + '.png')
rand = random.random()
# process image
if np.random.rand() < 0.5:
img = cv2.imread(image_path)
im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
image_data = cv2.adaptiveThreshold(
im_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 10)
else:
image_data = np.asarray(Image.open(image_path).convert('L'))
# image augment
if self._config.datatype == 'Augment':
if np.random.rand() > 0.5:
image_data = img_aug(img_path=image_path)
else:
image_data = np.asarray(Image.open(image_path).convert('L'))
if image_data.ndim == 2:
image_data = image_data[:, :, np.newaxis]
batch_imgs.append(image_data)
_batch_forms.append(label)
batch_names.append(image_path)
if _dataset in ['test']:
assert len(batch_imgs) == len(
_batch_forms) == 1, 'In "test" model batch must be one'
yield batch_imgs[0], _batch_forms[0]
else:
max_len = max(map(lambda x: len(x), _batch_forms))
batch_formulas = self.pad_idx * np.ones([len(_batch_forms),
max_len + 1],
dtype=np.int32)
batch_formula_length = np.zeros(len(_batch_forms), dtype=np.int32)
for idx, formula in enumerate(_batch_forms):
# now the formulua sequence is [start,...,end]
batch_formulas[idx, : len(formula)] = formula
# and the input is [start ,....]
# the target is [....,end]
# padding sequence is [start,...,end,pad,...]
batch_formula_length[idx] = len(formula) - 1
yield batch_imgs, batch_formulas, batch_formula_length, batch_names
class DataIteratorErrorChecker(object):
""" Dataloade for the ErrorChecker Model """
def __init__(self, _config, _logging, data_set, **kwargs):
self._config = _config
self._logging = _logging
self._bucket_size = self._config.dataset.get("bucket_size")
self.pad_idx = self._config.dataset.get("id_pad")
self._prepared_dir = self._config.dataset.get('prepared_folder')
self._set = data_set
assert isinstance(self._set, list), 'Input dataset mus be list'
assert isinstance(self._prepared_dir, list), 'Input dataset details must be list format'
def generate(self, _seed=100):
""" Generate iters """
for _dataset in self._set:
if _dataset in ['train', 'validate']:
batch_size = self._config.model.batch_size
else:
assert _dataset in ['test'], '_dataset must be [train,validate,test]'
batch_size = self._config.model.test_batch_size
self._logging.info('Generate the [{}] dataset'.format(_dataset))
if len(self._prepared_dir) > 1:
# because the seed can be used for once, so set seed for each list
random.seed(_seed)
random.shuffle(self._prepared_dir)
for idx in range(len(self._prepared_dir)):
_dataset_dir = os.path.join(self._prepared_dir[idx], _dataset + '_buckets.npy')
self._logging.info('Load the dataset: {:s}'.format(_dataset_dir))
dataset_details = np.load(_dataset_dir).tolist()
bucket_size = self._bucket_size
random.shuffle(bucket_size)
total_nums = sum((len(dataset_details[x]) for x in dataset_details))
self._logging.info('Total num is [{:d}]'.format(total_nums))
self._logging.info('bucket size is: {}'.format(bucket_size))
for bucket in bucket_size:
bucket_details = dataset_details[bucket]
random.shuffle(bucket_details)
dataset_num = len(bucket_details)
_iters = int(ceil(dataset_num / batch_size))
self._logging.info('Total iter of the bucket size [{}]is {}'.format(
bucket, _iters))
source_size, traget_size = bucket
for i in range(_iters):
start_idx = i * batch_size
end_idx = min((i + 1) * batch_size, dataset_num-1)
_samples_nums = end_idx - start_idx
if _samples_nums != batch_size:
continue
_sublist = bucket_details[start_idx:end_idx]
batch_source = []
batch_traget = []
for sources_seq, target_seq in _sublist:
batch_source.append(sources_seq)
batch_traget.append(target_seq)
if _dataset in ['test']:
assert len(batch_source) == len(
batch_traget) == 1, 'In "test" model batch must be one'
yield batch_source[0], batch_traget[0]
else:
batch_source_pad = self.pad_idx * np.ones([len(batch_source),
source_size],
dtype=np.int32)
batch_traget_pad = self.pad_idx * np.ones([len(batch_source),
traget_size],
dtype=np.int32)
batch_target_length = np.zeros(len(batch_traget), dtype=np.int32)
for idx, tar_seq in enumerate(batch_traget):
# now the target sequence is [start,...,end]
batch_traget_pad[idx, :len(tar_seq)] = tar_seq
# the input is [start ,....]
# the label is [....,end]
# padding sequence is [start,...,end,pad,...]
batch_target_length[idx] = len(tar_seq) - 1
for idx, source_seq in enumerate(batch_source):
batch_source_pad[idx, :len(source_seq)] = source_seq
yield batch_source_pad, batch_traget_pad, batch_target_length
class DataIteratorDis(object):
""" Define the iterator for the Discriminator """
def __init__(self, _config, _logging, data_set, **kwargs):
self._config = _config
self._logging = _logging
self._bucket_size = self._config.dataset.get("bucket_size")
self.pad_idx = self._config.dataset.get("id_pad")
self._prepared_dir = self._config.dataset.get('prepared_folder')
self._set = data_set
assert isinstance(self._set, list), 'Input dataset mus be list'
assert isinstance(self._prepared_dir, list), 'Input dataset details must be list format'
def generate(self,):