-
Notifications
You must be signed in to change notification settings - Fork 1
/
core_data_SRCandTGT.py
199 lines (181 loc) · 6.83 KB
/
core_data_SRCandTGT.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
# encoding=utf8
from hyper_and_conf import conf_fn as train_conf
from data import data_setentceToByte_helper
import tensorflow as tf
class DatasetManager():
def __init__(self,
source_data_path,
target_data_path,
batch_size=32,
shuffle=100,
num_sample=-1,
max_length=50,
EOS_ID=1,
PAD_ID=0,
cross_val=[0.89, 0.1, 0.01],
byte_token='@@',
word_token=' ',
split_token='\n',
tfrecord_path=None):
"""Short summary.
Args:
source_data_path (type): Description of parameter `source_data_path`.
target_data_path (type): Description of parameter `target_data_path`.
num_sample (type): Description of parameter `num_sample`.
batch_size (type): Description of parameter `batch_size`.
split_token (type): Description of parameter `split_token`.
Returns:
type: Description of returned object.
"""
self.source_data_path = source_data_path
self.target_data_path = target_data_path
self.num_sample = num_sample
self.batch_size = batch_size
self.byte_token = byte_token
self.split_token = split_token
self.word_token = word_token
self.EOS_ID = EOS_ID
self.PAD_ID = PAD_ID
self.shuffle = shuffle
self.max_length = max_length
self.cross_val = cross_val
assert isinstance(self.cross_val, list) is True
self.byter = data_setentceToByte_helper.Subtokenizer(
self.source_data_path + self.target_data_path,
PAD_ID=self.PAD_ID,
EOS_ID=self.EOS_ID)
if train_conf.get_available_gpus() > 0:
self.cpus = 12 * train_conf.get_available_gpus()
else:
self.cpus = 4
self.tfrecord_path = tfrecord_path
def corpus_length_checker(self, data=None, re=False):
self.short_20 = 0
self.median_50 = 0
self.long_100 = 0
self.super_long = 0
for k, v in enumerate(data):
v = v.split(self.word_token)
v_len = len(v)
if v_len <= 20:
self.short_20 += 1
if v_len > 20 and v_len <= 50:
self.median_50 += 1
if v_len > 50 and v_len <= 100:
self.long_100 += 1
if v_len > 100:
self.super_long += 1
if re:
print("short: %d" % self.short_20)
print("median: %d" % self.median_50)
print("long: %d" % self.long_100)
print("super long: %d" % self.super_long)
def encode(self, string, add_eos=True):
return self.byter.encode(string, add_eos=True)
def decode(self, string):
return self.byter.decode(string)
def one_file_encoder(self, file_path, num=None):
with tf.gfile.GFile(file_path, "r") as f:
raw_data = f.readlines()
re = []
if num is None:
for d in raw_data:
re.append(self.encode(d))
else:
text = raw_data[num].split(":")[1].lower().rstrip().strip()
re = self.encode(text)
f.close()
return re
def one_file_decoder(self, file_path, line_num=None):
with tf.gfile.GFile(file_path, "r") as f:
raw_data = f.readlines()
re = []
if line_num is None:
for d in raw_data:
re.append(self.decode(d))
f.close()
else:
re.append(self.decode(raw_data[line_num]))
return re
def create_dataset(self, data_path):
def _parse_example(serialized_example):
"""Return inputs and targets Tensors from a serialized tf.Example."""
data_fields = {
"text": tf.VarLenFeature(tf.int64),
"img": tf.VarLenFeature(tf.float32)
}
# import pdb;pdb.set_trace()
parsed = tf.parse_single_example(serialized_example, data_fields)
img = tf.sparse_tensor_to_dense(parsed["img"])
text = tf.sparse_tensor_to_dense(parsed["text"])
return img, text
def _filter_max_length(example, max_length=256):
return tf.logical_and(
tf.size(example[0]) <= max_length,
tf.size(example[1]) <= max_length)
# with tf.device("/cpu:0"):
dataset = tf.data.TFRecordDataset(
data_path, compression_type='GZIP')
dataset = dataset.map(_parse_example, num_parallel_calls=self.cpus)
dataset = dataset.map(
lambda img, text: (tf.reshape(img, [-1, 25088]), text),
num_parallel_calls=self.cpus)
# dataset = dataset.filter(lambda x, y: _filter_max_length((
# x, y), self.max_length))
# dataset = dataset.apply(tf.data.experimental.ignore_errors())
return dataset
def get_raw_train_dataset(self):
files = tf.data.Dataset.list_files(
self.tfrecord_path + "/train_TFRecord*", shuffle=200)
with tf.device('/cpu:0'):
return self.create_dataset(files)
# def get_raw_val_dataset(self):
# with tf.device('/cpu:0'):
# return self.create_dataset(self.val_tfr)
#
# def get_raw_test_dataset(self):
# with tf.device("/cpu:0"):
# return self.create_dataset(self.test_tfr)
#
# def get_train_size(self):
# return self.train_size
#
# def get_val_size(self):
# return self.val_size
#
# def get_test_size(self):
# return self.test_size
# tf.enable_eager_execution()
# DATA_PATH = '/Users/barid/Documents/workspace/batch_data/corpus_fr2eng'
# sentenceHelper = DatasetManager([DATA_PATH + "/europarl-v7.fr-en.en"],
# [DATA_PATH + "/europarl-v7.fr-en.en"],
# batch_size=16,
# shuffle=100)
# dataset = sentenceHelper.get_raw_train_dataset()
# for i in range(5):
# import pdb; pdb.set_trace()
# d = dataset.make_one_shot_iterator()
# d = d.get_next()
# # # # # a, b, c = sentenceHelper.prepare_data()
# # # # a, b, c = sentenceHelper.post_process()
# # for i, e in enumerate(a):
# # print(e[0])
# # print(i)
# # sentenceHelper.byter.decode(e[0].numpy())
# # break
#
#
# def dataset_prepross_fn(src, tgt):
# return (src, tgt), tgt
#
#
# dataset = dataset.map(dataset_prepross_fn, num_parallel_calls=12)
# dataset = dataset.padded_batch(
# 1,
# padded_shapes=(
# (
# tf.TensorShape([None]), # source vectors of unknown size
# tf.TensorShape([None]), # target vectors of unknown size
# ),
# tf.TensorShape([None])),
# drop_remainder=True)