-
Notifications
You must be signed in to change notification settings - Fork 0
/
mjsynth_genre.py
236 lines (199 loc) · 9.19 KB
/
mjsynth_genre.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
import os
import tensorflow as tf
import matplotlib.pyplot as plt
# The list (well, string) of valid output characters
# If any example contains a character not found here, an error will result
# from the calls to .index in the decoder below
out_charset="0123456789"
def num_classes():
return len(out_charset)
def bucketed_input_pipeline(base_dir,file_patterns,
num_threads=4,
batch_size=32, # Added ,288, 320,352,384,416,448 to bound
boundaries=[32, 64, 96, 128, 160, 192, 224, 256, 288, 320,352,384,416,448,480,512,544,576,608,640,672,704,736,768,800,832,864],
input_device=None,
width_threshold=None,
length_threshold=None,
num_epochs=None):
"""Get input tensors bucketed by image width
Returns:
image : float32 image tensor [batch_size 32 ? 1] padded to batch max width
width : int32 image widths (for calculating post-CNN sequence length)
label : Sparse tensor with label sequences for the batch
length : Length of label sequence (text length)
text : Human readable string for the image
filename : Source file path
"""
queue_capacity = num_threads*batch_size*2
# Allow a smaller final batch if we are going for a fixed number of epochs
final_batch = (num_epochs!=None)
data_queue = _get_data_queue(base_dir, file_patterns,
capacity=queue_capacity,
num_epochs=num_epochs)
print(data_queue)
input('test')
with tf.device(input_device): # Create bucketing batcher
image, width, label, length, text, filename = _read_word_record(
data_queue)
image = _preprocess_image(image) # move after batch?
#tf.summary.image('images',image) # Uncomment to see images in TensorBoard
keep_input = _get_input_filter(width, width_threshold,
length, length_threshold)
data_tuple = [image, label, length, text, filename]
print(label)
#tf.summary.text('label',label.value)
'''
width,data_tuple = tf.contrib.training.bucket(tensors=data_tuple,
which_bucket=1,
batch_size=32,
num_buckets=1000,
num_threads=4,
capacity=queue_capacity,
keep_input=False,
allow_smaller_final_batch=False,#final_batch,
dynamic_pad=True)
print(width)
input('width_bucket')
'''
width,data_tuple = tf.contrib.training.bucket_by_sequence_length(
input_length=625,#width,
tensors=data_tuple,
bucket_boundaries=boundaries,
batch_size=batch_size,
capacity=queue_capacity,
keep_input=False,#keep_input,
allow_smaller_final_batch=False,#final_batch,
dynamic_pad=True)
[image, label, length, text, filename] = data_tuple
label = tf.deserialize_many_sparse(label, tf.int64) # post-batching...
#input('sec')
label = tf.cast(label, tf.int32) # for ctc_loss
#print(label)
#constant = tf.constant([1, 2, 3])
#tensor = constant * constant
#t = tf.Print(tensor,[tensor])
#ignition = tf.Variable(451, tf.int16)
#ttt = tf.convert_to_tensor(ignition)
#tf.summary.scalar('ignition',ttt)
#print(width)
#input('width')
#input('label')
#c = tf.reshape(width,[-1])
#print(c)
#input('c')
#tf.summary.scalar('asd',c)
#tf.summary.scalar([width.op.name], width)
#tf.summary.text('filename',filename)
#tf.summary.text('label',label)
#tf.summary.text('label',
#print(label)
return image, width, label, length, text, filename
def threaded_input_pipeline(base_dir,file_patterns,
num_threads=4,
batch_size=32,
batch_device=None,
preprocess_device=None,
num_epochs=None):
queue_capacity = num_threads*batch_size*2
# Allow a smaller final batch if we are going for a fixed number of epochs
final_batch = (num_epochs!=None)
data_queue = _get_data_queue(base_dir, file_patterns,
capacity=queue_capacity,
num_epochs=num_epochs)
# each thread has a subgraph with its own reader (sharing filename queue)
data_tuples = [] # list of subgraph [image, label, width, text] elements
with tf.device(preprocess_device):
for _ in range(num_threads):
image, width, label, length, text, filename = _read_word_record(
data_queue)
image = _preprocess_image(image) # move after batch?
data_tuples.append([image, width, label, length, text, filename])
with tf.device(batch_device): # Create batch queue
image, width, label, length, text, filename = tf.train.batch_join(
data_tuples,
batch_size=batch_size,
capacity=queue_capacity,
allow_smaller_final_batch=False,#final_batch,
dynamic_pad=True)
print(label)
input('label before')
label = tf.deserialize_many_sparse(label, tf.int64) # post-batching...
print(label)
input('label after')
label = tf.cast(label, tf.int32) # for ctc_loss
return image, width, label, length, text, filename
def _get_input_filter(width, width_threshold, length, length_threshold):
"""Boolean op for discarding input data based on string or image size
Input:
width : Tensor representing the image width
width_threshold : Python numerical value (or None) representing the
maximum allowable input image width
length : Tensor representing the ground truth string length
length_threshold : Python numerical value (or None) representing the
maximum allowable input string length
Returns:
keep_input : Boolean Tensor indicating whether to keep a given input
with the specified image width and string length
"""
keep_input = None
if width_threshold!=None:
keep_input = tf.less_equal(width, width_threshold)
if length_threshold!=None:
length_filter = tf.less_equal(length, length_threshold)
if keep_input==None:
keep_input = length_filter
else:
keep_input = tf.logical_and( keep_input, length_filter)
if keep_input==None:
keep_input = True
else:
keep_input = tf.reshape( keep_input, [] ) # explicitly make a scalar
return keep_input
def _get_data_queue(base_dir, file_patterns=['*.tfrecord'], capacity=2**8,
num_epochs=None):
"""Get a data queue for a list of record files"""
# List of lists ...
data_files = [tf.gfile.Glob(os.path.join(base_dir,file_pattern))
for file_pattern in file_patterns]
# flatten
data_files = [data_file for sublist in data_files for data_file in sublist]
data_queue = tf.train.string_input_producer(data_files,
capacity=capacity,
num_epochs=num_epochs)
return data_queue
def _read_word_record(data_queue):
reader = tf.TFRecordReader() # Construct a general reader
#print(data_queue)
#input('werwerwe')
key, example_serialized = reader.read(data_queue)
feature_map = {
'image/encoded': tf.FixedLenFeature( [], dtype=tf.string,
default_value='' ),
'image/labels': tf.VarLenFeature( dtype=tf.int64 ),
'image/width': tf.FixedLenFeature( [1], dtype=tf.int64,
default_value=1 ),
'image/filename': tf.FixedLenFeature([], dtype=tf.string,
default_value='' ),
'text/string': tf.FixedLenFeature([], dtype=tf.string,
default_value='' ),
'text/length': tf.FixedLenFeature( [1], dtype=tf.int64,
default_value=1 )
}
features = tf.parse_single_example( example_serialized, feature_map )
image = tf.image.decode_jpeg( features['image/encoded'], channels=1 ) #gray
width = tf.cast( features['image/width'], tf.int32) # for ctc_loss
label = tf.serialize_sparse(features['image/labels']) # for batching
length = features['text/length']
text = features['text/string']
filename = features['image/filename']
return image,width,label,length,text,filename
def _preprocess_image(image):
# Rescale from uint8([0,255]) to float([-0.5,0.5])
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.subtract(image, 0.5)
print(image)
# Pad with copy of first row to expand to 32 pixels height
#first_row = tf.slice(image, [0, 0, 0], [1, -1, -1])
#image = tf.concat([first_row, image], 0)
image = tf.squeeze(image,axis=2)
return image