forked from rakeshvar/rnn_ctc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscribe_rows.py
executable file
·136 lines (110 loc) · 4.1 KB
/
scribe_rows.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
#!/usr/bin/env python3
'''
This file is used to 'scribe' a random piece of 'text' on to a 'slab'.
'text' - A sequence based on an alphabet [0, 1, 2 ...n_chars)
'slab' - An numpy matrix
Has as many rows as the size of the alphabet i.e. n_chars
A character 'i' in the text is of length i+2 by default and is written in
the i-th line! i.e. 2 is written in the 2nd line with a length of 4 'pixels'.
Characters are read from left to right.
Example:
The text [2, 1, 3, 0] is written as
0¦ ██ ¦
1¦ ███ ¦
2¦ ████ ¦
3¦ █████¦
'''
import numpy as np
class RowScribe():
def __init__(self, n_chars, avg_seq_len, buffer_len=3, char_lens=None):
self.nChars = n_chars
self.nDims = n_chars
self.len = avg_seq_len
self.buffer = buffer_len
if char_lens is None:
self.char_lens = np.arange(n_chars) + 2
else:
self.char_lens = None
def get_data(self, complexx, vary):
if complexx:
return self.get_complex(vary)
else:
return self.get_simple(vary)
def get_sample_length(self, vary):
return self.len + \
vary * (np.random.randint(self.len // 2) - self.len // 4)
def get_complex(self, vary):
length = self.get_sample_length(vary)
ret_x = np.zeros((self.nDims, length), dtype=int)
ret_y = np.zeros((self.nDims, length), dtype=int) - 1
for char in range(self.nChars):
ix = np.random.randint(2 * self.nChars) + self.buffer
while ix < length - self.buffer - self.char_lens[char]:
ret_x[char, ix:ix + self.char_lens[char]] = 1
ret_y[char, ix] = char
ix += self.char_lens[char] + \
np.random.exponential(self.nChars**2.) + 1
ret_y2 = [char for column in ret_y.T for char in column if char > -1]
return ret_x, ret_y2
def get_simple(self, vary):
"""
simple - Implies a character is written only after the previous
one is done printing.
:param vary: Make the slab of variable length
:return: The Scribe
"""
length = self.get_sample_length(vary)
ret_x = np.zeros((self.nDims, length), dtype=int)
ret_y = []
ix = np.random.exponential(self.buffer) + self.buffer
while ix < length - self.buffer - self.nChars:
char = np.random.randint(self.nChars)
ret_x[char, ix:ix + self.char_lens[char]] = 1
ret_y += [char]
ix += self.char_lens[char] + \
np.random.exponential(self.buffer // 2) + 1
return ret_x, ret_y
if __name__ == "__main__":
import pickle
import sys
from print_utils import slab_print
if len(sys.argv) < 2:
print('Usage \n'
'{} <out_file_name> [num_chars=4] [avg_sequence_len=30] '
'[complex=True] [variable_length=True]'.format(sys.argv[0]))
sys.exit()
out_file_name = sys.argv[1]
out_file_name += '.pkl' if not out_file_name.endswith('.pkl') else ''
try:
nChars = int(sys.argv[2])
except IndexError:
nChars = 4
try:
avg_seq_len = int(sys.argv[3])
except IndexError:
avg_seq_len = 30
try:
complx = sys.argv[4].lower() in ("yes", "true", "t", "1")
except IndexError:
complx = True
try:
variable_len = sys.argv[5].lower() in ("yes", "true", "t", "1")
except IndexError:
variable_len = True
scribe = RowScribe(nChars, avg_seq_len, buffer_len=avg_seq_len // 10)
xs = []
ys = []
for i in range(1000):
x, y = scribe.get_data(complx, variable_len)
xs.append(x)
ys.append(y)
print(y)
slab_print(x)
print('Output: {}\n'
'Char set size: {}\n'
'(Avg.) Len: {}\n'
'Varying Length: {}\n'
'Complex Scribe: {}\n'.format(
out_file_name, nChars, avg_seq_len, variable_len, complx, ))
with open(out_file_name, 'wb') as f:
pickle.dump({'x': xs, 'y': ys, 'nChars': nChars}, f, -1)