-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
366 lines (296 loc) · 11.8 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import os
import pickle
import random
import xml.etree.ElementTree as ET
import numpy as np
import svgwrite
from IPython.display import SVG, display
def get_bounds(data, factor):
min_x = 0
max_x = 0
min_y = 0
max_y = 0
abs_x = 0
abs_y = 0
for i in range(len(data)):
x = float(data[i, 0]) / factor
y = float(data[i, 1]) / factor
abs_x += x
abs_y += y
min_x = min(min_x, abs_x)
min_y = min(min_y, abs_y)
max_x = max(max_x, abs_x)
max_y = max(max_y, abs_y)
return (min_x, max_x, min_y, max_y)
# old version, where each path is entire stroke (smaller svg size, but
# have to keep same color)
def draw_strokes(data, factor=10, svg_filename='sample.svg'):
min_x, max_x, min_y, max_y = get_bounds(data, factor)
dims = (50 + max_x - min_x, 50 + max_y - min_y)
dwg = svgwrite.Drawing(svg_filename, size=dims)
dwg.add(dwg.rect(insert=(0, 0), size=dims, fill='white'))
lift_pen = 1
abs_x = 25 - min_x
abs_y = 25 - min_y
p = "M%s,%s " % (abs_x, abs_y)
command = "m"
for i in range(len(data)):
if (lift_pen == 1):
command = "m"
elif (command != "l"):
command = "l"
else:
command = ""
x = float(data[i, 0]) / factor
y = float(data[i, 1]) / factor
lift_pen = data[i, 2]
p += command + str(x) + "," + str(y) + " "
the_color = "black"
stroke_width = 1
dwg.add(dwg.path(p).stroke(the_color, stroke_width).fill("none"))
dwg.save()
display(SVG(dwg.tostring()))
def draw_strokes_eos_weighted(
stroke,
param,
factor=10,
svg_filename='sample_eos.svg'):
c_data_eos = np.zeros((len(stroke), 3))
for i in range(len(param)):
# make color gray scale, darker = more likely to eos
c_data_eos[i, :] = (1 - param[i][6][0]) * 225
draw_strokes_custom_color(
stroke,
factor=factor,
svg_filename=svg_filename,
color_data=c_data_eos,
stroke_width=3)
def draw_strokes_random_color(
stroke,
factor=10,
svg_filename='sample_random_color.svg',
per_stroke_mode=True):
c_data = np.array(np.random.rand(len(stroke), 3) * 240, dtype=np.uint8)
if per_stroke_mode:
switch_color = False
for i in range(len(stroke)):
if switch_color == False and i > 0:
c_data[i] = c_data[i - 1]
if stroke[i, 2] < 1: # same strike
switch_color = False
else:
switch_color = True
draw_strokes_custom_color(
stroke,
factor=factor,
svg_filename=svg_filename,
color_data=c_data,
stroke_width=2)
def draw_strokes_custom_color(
data,
factor=10,
svg_filename='test.svg',
color_data=None,
stroke_width=1):
min_x, max_x, min_y, max_y = get_bounds(data, factor)
dims = (50 + max_x - min_x, 50 + max_y - min_y)
dwg = svgwrite.Drawing(svg_filename, size=dims)
dwg.add(dwg.rect(insert=(0, 0), size=dims, fill='white'))
lift_pen = 1
abs_x = 25 - min_x
abs_y = 25 - min_y
for i in range(len(data)):
x = float(data[i, 0]) / factor
y = float(data[i, 1]) / factor
prev_x = abs_x
prev_y = abs_y
abs_x += x
abs_y += y
if (lift_pen == 1):
p = "M " + str(abs_x) + "," + str(abs_y) + " "
else:
p = "M +" + str(prev_x) + "," + str(prev_y) + \
" L " + str(abs_x) + "," + str(abs_y) + " "
lift_pen = data[i, 2]
the_color = "black"
if (color_data is not None):
the_color = "rgb(" + str(int(color_data[i, 0])) + "," + str(
int(color_data[i, 1])) + "," + str(int(color_data[i, 2])) + ")"
dwg.add(dwg.path(p).stroke(the_color, stroke_width).fill(the_color))
dwg.save()
display(SVG(dwg.tostring()))
def draw_strokes_pdf(data, param, factor=10, svg_filename='sample_pdf.svg'):
min_x, max_x, min_y, max_y = get_bounds(data, factor)
dims = (50 + max_x - min_x, 50 + max_y - min_y)
dwg = svgwrite.Drawing(svg_filename, size=dims)
dwg.add(dwg.rect(insert=(0, 0), size=dims, fill='white'))
abs_x = 25 - min_x
abs_y = 25 - min_y
num_mixture = len(param[0][0])
for i in range(len(data)):
x = float(data[i, 0]) / factor
y = float(data[i, 1]) / factor
for k in range(num_mixture):
pi = param[i][0][k]
if pi > 0.01: # optimisation, ignore pi's less than 1% chance
mu1 = param[i][1][k]
mu2 = param[i][2][k]
s1 = param[i][3][k]
s2 = param[i][4][k]
sigma = np.sqrt(s1 * s2)
dwg.add(dwg.circle(center=(abs_x + mu1 * factor,
abs_y + mu2 * factor),
r=int(sigma * factor)).fill('red',
opacity=pi / (sigma * sigma * factor)))
prev_x = abs_x
prev_y = abs_y
abs_x += x
abs_y += y
dwg.save()
display(SVG(dwg.tostring()))
class DataLoader():
def __init__(
self,
batch_size=50,
seq_length=300,
scale_factor=10,
limit=500):
self.data_dir = "./data"
self.batch_size = batch_size
self.seq_length = seq_length
self.scale_factor = scale_factor # divide data by this factor
self.limit = limit # removes large noisy gaps in the data
data_file = os.path.join(self.data_dir, "strokes_training_data.cpkl")
raw_data_dir = self.data_dir + "/lineStrokes"
if not (os.path.exists(data_file)):
print("creating training data pkl file from raw source")
self.preprocess(raw_data_dir, data_file)
self.load_preprocessed(data_file)
self.reset_batch_pointer()
def preprocess(self, data_dir, data_file):
# create data file from raw xml files from iam handwriting source.
# build the list of xml files
filelist = []
# Set the directory you want to start from
rootDir = data_dir
for dirName, subdirList, fileList in os.walk(rootDir):
#print('Found directory: %s' % dirName)
for fname in fileList:
#print('\t%s' % fname)
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
# 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 = []
for i in range(len(filelist)):
if (filelist[i][-3:] == 'xml'):
print('processing ' + filelist[i])
strokes.append(
convert_stroke_to_array(
getStrokes(
filelist[i])))
f = open(data_file, "wb")
pickle.dump(strokes, f, protocol=2)
f.close()
def load_preprocessed(self, data_file):
f = open(data_file, "rb")
self.raw_data = pickle.load(f)
f.close()
# goes thru the list, and only keeps the text entries that have more
# than seq_length points
self.data = []
self.valid_data = []
counter = 0
# every 1 in 20 (5%) will be used for validation data
cur_data_counter = 0
for data in self.raw_data:
if len(data) > (self.seq_length + 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.scale_factor
cur_data_counter = cur_data_counter + 1
if cur_data_counter % 20 == 0:
self.valid_data.append(data)
else:
self.data.append(data)
# number of equiv batches this datapoint is worth
counter += int(len(data) / ((self.seq_length + 2)))
print("train data: {}, valid data: {}".format(
len(self.data), len(self.valid_data)))
# minus 1, since we want the ydata to be a shifted version of x data
self.num_batches = int(counter / self.batch_size)
def validation_data(self):
# returns validation data
x_batch = []
y_batch = []
for i in range(self.batch_size):
data = self.valid_data[i % len(self.valid_data)]
idx = 0
x_batch.append(np.copy(data[idx:idx + self.seq_length]))
y_batch.append(np.copy(data[idx + 1:idx + self.seq_length + 1]))
return x_batch, y_batch
def next_batch(self):
# returns a randomised, seq_length sized portion of the training data
x_batch = []
y_batch = []
for i in range(self.batch_size):
data = self.data[self.pointer]
# number of equiv batches this datapoint is worth
n_batch = int(len(data) / ((self.seq_length + 2)))
idx = random.randint(0, len(data) - self.seq_length - 2)
x_batch.append(np.copy(data[idx:idx + self.seq_length]))
y_batch.append(np.copy(data[idx + 1:idx + self.seq_length + 1]))
# adjust sampling probability.
if random.random() < (1.0 / float(n_batch)):
# if this is a long datapoint, sample this data more with
# higher probability
self.tick_batch_pointer()
return x_batch, y_batch
def tick_batch_pointer(self):
self.pointer += 1
if (self.pointer >= len(self.data)):
self.pointer = 0
def reset_batch_pointer(self):
self.pointer = 0