-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
227 lines (198 loc) · 9.12 KB
/
utils.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
224
225
226
227
import numpy as np
import math
import random
import os
import _pickle as cPickle
import xml.etree.ElementTree as ET
from utils import *
class DataLoader():
def __init__(self, args, logger, limit = 500):
self.data_dir = args.data_dir
self.alphabet = args.alphabet
self.batch_size = args.batch_size
self.tsteps = args.tsteps
self.data_scale = args.data_scale # scale data down by this factor
self.ascii_steps = args.tsteps/args.tsteps_per_ascii
self.logger = logger
self.limit = limit # removes large noisy gaps in the data
data_file = os.path.join(self.data_dir, "strokes_training_data.cpkl")
stroke_dir = self.data_dir + "/lineStrokes"
ascii_dir = self.data_dir + "/ascii"
if not (os.path.exists(data_file)) :
self.logger.write("\tcreating training data cpkl file from raw source")
self.preprocess(stroke_dir, ascii_dir, data_file)
self.load_preprocessed(data_file)
self.reset_batch_pointer()
def preprocess(self, stroke_dir, ascii_dir, data_file):
# create data file from raw xml files from iam handwriting source.
self.logger.write("\tparsing dataset...")
# build the list of xml files
filelist = []
# Set the directory you want to start from
rootDir = stroke_dir
for dirName, subdirList, fileList in os.walk(rootDir):
for fname in fileList:
filelist.append(dirName+"/"+fname)
# function to read each individual xml file
def getStrokes(filename):
tree = ET.parse(filename)
root = tree.getroot()
result = []
x_offset = 1e20
y_offset = 1e20
y_height = 0
for i in range(1, 4):
x_offset = min(x_offset, float(root[0][i].attrib['x']))
y_offset = min(y_offset, float(root[0][i].attrib['y']))
y_height = max(y_height, float(root[0][i].attrib['y']))
y_height -= y_offset
x_offset -= 100
y_offset -= 100
for stroke in root[1].findall('Stroke'):
points = []
for point in stroke.findall('Point'):
points.append([float(point.attrib['x'])-x_offset,float(point.attrib['y'])-y_offset])
result.append(points)
return result
# function to read each individual xml file
def getAscii(filename, line_number):
with open(filename, "r") as f:
s = f.read()
s = s[s.find("CSR"):]
if len(s.split("\n")) > line_number+2:
s = s.split("\n")[line_number+2]
return s
else:
return ""
# converts a list of arrays into a 2d numpy int16 array
def convert_stroke_to_array(stroke):
n_point = 0
for i in range(len(stroke)):
n_point += len(stroke[i])
stroke_data = np.zeros((n_point, 3), dtype=np.int16)
prev_x = 0
prev_y = 0
counter = 0
for j in range(len(stroke)):
for k in range(len(stroke[j])):
stroke_data[counter, 0] = int(stroke[j][k][0]) - prev_x
stroke_data[counter, 1] = int(stroke[j][k][1]) - prev_y
prev_x = int(stroke[j][k][0])
prev_y = int(stroke[j][k][1])
stroke_data[counter, 2] = 0
if (k == (len(stroke[j])-1)): # end of stroke
stroke_data[counter, 2] = 1
counter += 1
return stroke_data
# build stroke database of every xml file inside iam database
strokes = []
asciis = []
for i in range(len(filelist)):
if (filelist[i][-3:] == 'xml'):
stroke_file = filelist[i]
# print('processing '+stroke_file)
stroke = convert_stroke_to_array(getStrokes(stroke_file))
ascii_file = stroke_file.replace("lineStrokes","ascii")[:-7] + ".txt"
line_number = stroke_file[-6:-4]
line_number = int(line_number) - 1
ascii = getAscii(ascii_file, line_number)
if len(ascii) > 10:
strokes.append(stroke)
asciis.append(ascii)
else:
self.logger.write("\tline length was too short. line was: " + ascii)
assert(len(strokes)==len(asciis)), "There should be a 1:1 correspondence between stroke data and ascii labels."
f = open(data_file,"wb")
ccPickle.dump([strokes,asciis], f, protocol=2)
f.close()
self.logger.write("\tfinished parsing dataset. saved {} lines".format(len(strokes)))
def load_preprocessed(self, data_file):
f = open(data_file,"rb")
[self.raw_stroke_data, self.raw_ascii_data] = cPickle.load(f,encoding='bytes')
f.close()
# goes thru the list, and only keeps the text entries that have more than tsteps points
self.stroke_data = []
self.ascii_data = []
self.valid_stroke_data = []
self.valid_ascii_data = []
counter = 0
# every 1 in 20 (5%) will be used for validation data
cur_data_counter = 0
for i in range(len(self.raw_stroke_data)):
data = self.raw_stroke_data[i]
if len(data) > (self.tsteps+2):
# removes large gaps from the data
data = np.minimum(data, self.limit)
data = np.maximum(data, -self.limit)
data = np.array(data,dtype=np.float32)
data[:,0:2] /= self.data_scale
cur_data_counter = cur_data_counter + 1
if cur_data_counter % 20 == 0:
self.valid_stroke_data.append(data)
self.valid_ascii_data.append(self.raw_ascii_data[i])
else:
self.stroke_data.append(data)
self.ascii_data.append(self.raw_ascii_data[i])
# minus 1, since we want the ydata to be a shifted version of x data
self.num_batches = int(len(self.stroke_data) / self.batch_size)
self.logger.write("\tloaded dataset:")
self.logger.write("\t\t{} train individual data points".format(len(self.stroke_data)))
self.logger.write("\t\t{} valid individual data points".format(len(self.valid_stroke_data)))
self.logger.write("\t\t{} batches".format(self.num_batches))
def validation_data(self):
# returns validation data
x_batch = []
y_batch = []
ascii_list = []
for i in range(self.batch_size):
valid_ix = i%len(self.valid_stroke_data)
data = self.valid_stroke_data[valid_ix]
x_batch.append(np.copy(data[:self.tsteps]))
y_batch.append(np.copy(data[1:self.tsteps+1]))
ascii_list.append(self.valid_ascii_data[valid_ix])
one_hots = [to_one_hot(s, self.ascii_steps, self.alphabet) for s in ascii_list]
return x_batch, y_batch, ascii_list, one_hots
def next_batch(self):
# returns a randomized, tsteps-sized portion of the training data
x_batch = []
y_batch = []
ascii_list = []
for i in range(self.batch_size):
data = self.stroke_data[self.idx_perm[self.pointer]]
idx = random.randint(0, len(data)-self.tsteps-2)
x_batch.append(np.copy(data[:self.tsteps]))
y_batch.append(np.copy(data[1:self.tsteps+1]))
ascii_list.append(self.ascii_data[self.idx_perm[self.pointer]])
self.tick_batch_pointer()
one_hots = [to_one_hot(s, self.ascii_steps, self.alphabet) for s in ascii_list]
return x_batch, y_batch, ascii_list, one_hots
def tick_batch_pointer(self):
self.pointer += 1
if (self.pointer >= len(self.stroke_data)):
self.reset_batch_pointer()
def reset_batch_pointer(self):
self.idx_perm = np.random.permutation(len(self.stroke_data))
self.pointer = 0
# utility function for converting input ascii characters into vectors the network can understand.
# index position 0 means "unknown"
def to_one_hot(s, ascii_steps, alphabet):
steplimit=3e3; s = s[:3e3] if len(s) > 3e3 else s # clip super-long strings
s = s.decode('utf8')
seq = [alphabet.find(char) + 1 for char in s]
if len(seq) >= ascii_steps:
seq = seq[:int(ascii_steps)]
else:
seq = seq + [0]*(ascii_steps - len(seq))
one_hot = np.zeros((int(ascii_steps),len(alphabet)+1))
one_hot[np.arange(int(ascii_steps)),seq] = 1
return one_hot
# abstraction for logging
class Logger():
def __init__(self, args):
self.logf = '{}train_scribe.txt'.format(args.log_dir) if args.train else '{}sample_scribe.txt'.format(args.log_dir)
with open(self.logf, 'w') as f: f.write("Scribe: Realistic Handriting in Tensorflow\n by Alexandre Langeois\n\n\n")
def write(self, s, print_it=True):
if print_it:
print(s)
with open(self.logf, 'a') as f:
f.write(s + "\n")