/
pipelines.py
424 lines (364 loc) · 18 KB
/
pipelines.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
from marshmallow import EXCLUDE, fields
from polyaxon_schemas.base import BaseConfig, BaseMultiSchema, BaseSchema
from polyaxon_schemas.ml.processing.feature_processors import FeatureProcessorsSchema
class BasePipelineSchema(BaseSchema):
name = fields.Str(allow_none=True)
feature_processors = fields.Nested(FeatureProcessorsSchema, allow_none=True)
shuffle = fields.Bool(allow_none=True)
num_epochs = fields.Int(allow_none=True)
batch_size = fields.Int(allow_none=True)
bucket_boundaries = fields.List(fields.Int(), allow_none=True)
allow_smaller_final_batch = fields.Bool(allow_none=True)
dynamic_pad = fields.Bool(allow_none=True)
min_after_dequeue = fields.Int(allow_none=True)
num_threads = fields.Int(allow_none=True)
capacity = fields.Int(allow_none=True)
@staticmethod
def schema_config():
return BasePipelineConfig
class BasePipelineConfig(BaseConfig):
"""Abstract InputPipeline class. All input pipelines must inherit from this.
An InputPipeline defines how data is read, parsed, and separated into
features and labels.
Args:
name: `str`, name to give for this pipeline.
feature_processors: `dict`, list of modules to call for each feature to be processed.
shuffle: `bool`, If true, shuffle the data.
num_epochs: `int`, Number of times to iterate through the dataset. If None, iterate forever.
batch_size: The new batch size pulled from the queue (all queues will have the same size).
If a list is passed in then each bucket will have a different batch_size.
(python int, int32 scalar or iterable of integers of length num_buckets).
bucket_boundaries: `list` of `int` or `None`, increasing non-negative numbers.
The edges of the buckets to use when bucketing tensors.
Two extra buckets are created, one for input_length < bucket_boundaries[0]
and one for input_length >= bucket_boundaries[-1].
allow_smaller_final_batch: `bool`, whether to allow a last small batch.
dynamic_pad: `bool`, Allow variable dimensions in input shapes.
The given dimensions are padded upon dequeue so that tensors
within a batch have the same shapes.
min_after_dequeue: `int`.
num_threads: `int`. The number of threads enqueuing tensors.
capacity: `int`, The maximum number of minibatches in the top queue,
and also the maximum number of elements within each bucket.
"""
IDENTIFIER = "BasePipeline"
SCHEMA = BasePipelineSchema
REDUCED_ATTRIBUTES = ["feature_processors"]
def __init__(
self,
name="Pipeline",
feature_processors=None,
shuffle=True,
num_epochs=1,
batch_size=64,
bucket_boundaries=None,
allow_smaller_final_batch=True,
dynamic_pad=False,
min_after_dequeue=5000,
num_threads=3,
capacity=None,
):
self.name = name
self.feature_processors = feature_processors
self.shuffle = shuffle
self.num_epochs = num_epochs
self.batch_size = batch_size
self.bucket_boundaries = bucket_boundaries
self.allow_smaller_final_batch = allow_smaller_final_batch
self.dynamic_pad = dynamic_pad
self.min_after_dequeue = min_after_dequeue
self.num_threads = num_threads
self.capacity = capacity or min_after_dequeue + num_threads * batch_size
class TFRecordImagePipelineSchema(BasePipelineSchema):
data_files = fields.List(fields.Str(), allow_none=True)
meta_data_file = fields.Str()
@staticmethod
def schema_config():
return TFRecordImagePipelineConfig
class TFRecordImagePipelineConfig(BasePipelineConfig):
"""A Pipeline to convert TF-Records to images.
Args:
name: `str`, name to give for this pipeline.
feature_processors: `dict`, list of modules to call for each feature to be processed.
shuffle: `bool`, If true, shuffle the data.
num_epochs: `int`, Number of times to iterate through the dataset. If None, iterate forever.
batch_size: The new batch size pulled from the queue (all queues will have the same size).
If a list is passed in then each bucket will have a different batch_size.
(python int, int32 scalar or iterable of integers of length num_buckets).
bucket_boundaries: `list` of `int` or `None`, increasing non-negative numbers.
The edges of the buckets to use when bucketing tensors.
Two extra buckets are created, one for input_length < bucket_boundaries[0]
and one for input_length >= bucket_boundaries[-1].
allow_smaller_final_batch: `bool`, whether to allow a last small batch.
dynamic_pad: `bool`, Allow variable dimensions in input shapes.
The given dimensions are padded upon dequeue so that tensors
within a batch have the same shapes.
min_after_dequeue: `int`.
num_threads: `int`. The number of threads enqueuing tensors.
capacity: `int`, The maximum number of minibatches in the top queue,
and also the maximum number of elements within each bucket.
data_files: `list` of `str`. List of the filenames for data.
meta_data_file: `str`. Metadata filename
Polyaxonfile usage:
```yaml
TFRecordImagePipeline:
batch_size: 64
num_epochs: 1
shuffle: true
dynamic_pad: false
data_files: ["../data/mnist/mnist_train.tfrecord"]
meta_data_file: "../data/mnist/meta_data.json"
feature_processors:
image:
input_layers: [image]
layers:
- Cast:
dtype: float32
```
"""
IDENTIFIER = "TFRecordImagePipeline"
SCHEMA = TFRecordImagePipelineSchema
def __init__(
self, data_files, meta_data_file, name="TFRecordImagePipeline", **kwargs
):
super(TFRecordImagePipelineConfig, self).__init__(name=name, **kwargs)
self.data_files = data_files
self.meta_data_file = meta_data_file
class TFRecordSequencePipelineSchema(BasePipelineSchema):
data_files = fields.List(fields.Str(), allow_none=True)
meta_data_file = fields.Str()
class Meta:
ordered = True
@staticmethod
def schema_config():
return TFRecordSequencePipelineConfig
class TFRecordSequencePipelineConfig(BasePipelineConfig):
"""A Pipeline to convert TF-Records to sequences.
At least one sequence must be `source_token`.
Args:
name: `str`, name to give for this pipeline.
feature_processors: `dict`, list of modules to call for each feature to be processed.
shuffle: `bool`, If true, shuffle the data.
num_epochs: `int`, Number of times to iterate through the dataset. If None, iterate forever.
batch_size: The new batch size pulled from the queue (all queues will have the same size).
If a list is passed in then each bucket will have a different batch_size.
(python int, int32 scalar or iterable of integers of length num_buckets).
bucket_boundaries: `list` of `int` or `None`, increasing non-negative numbers.
The edges of the buckets to use when bucketing tensors.
Two extra buckets are created, one for input_length < bucket_boundaries[0]
and one for input_length >= bucket_boundaries[-1].
allow_smaller_final_batch: `bool`, whether to allow a last small batch.
dynamic_pad: `bool`, Allow variable dimensions in input shapes.
The given dimensions are padded upon dequeue so that tensors
within a batch have the same shapes.
min_after_dequeue: `int`.
num_threads: `int`. The number of threads enqueuing tensors.
capacity: `int`, The maximum number of minibatches in the top queue,
and also the maximum number of elements within each bucket.
data_files: `list` of `str`. List of the filenames for data.
meta_data_file: `str`. Metadata filename
Polyaxonfile usage:
```yaml
TFRecordSequencePipeline:
batch_size: 64
num_epochs: 1
shuffle: true
dynamic_pad: false
data_files: ["data.tfrecord"]
meta_data_file: "meta_data.json"
feature_processors:
image:
input_layers: [sequence]
layers:
- Cast:
dtype: float32
```
"""
IDENTIFIER = "TFRecordSequencePipeline"
SCHEMA = TFRecordSequencePipelineSchema
def __init__(
self, data_files, meta_data_file, name="TFRecordSequencePipeline", **kwargs
):
super(TFRecordSequencePipelineConfig, self).__init__(name=name, **kwargs)
self.data_files = data_files
self.meta_data_file = meta_data_file
class ParallelTextPipelineSchema(BasePipelineSchema):
source_files = fields.List(fields.Str(), allow_none=True)
target_files = fields.List(fields.Str(), allow_none=True)
source_delimiter = fields.Str(allow_none=True)
target_delimiter = fields.Str(allow_none=True)
@staticmethod
def schema_config():
return ParallelTextPipelineConfig
class ParallelTextPipelineConfig(BasePipelineConfig):
"""An input pipeline that reads two parallel (line-by-line aligned) text files.
Args:
name: `str`, name to give for this pipeline.
feature_processors: `dict`, list of modules to call for each feature to be processed.
shuffle: `bool`, If true, shuffle the data.
num_epochs: `int`, Number of times to iterate through the dataset. If None, iterate forever.
batch_size: The new batch size pulled from the queue (all queues will have the same size).
If a list is passed in then each bucket will have a different batch_size.
(python int, int32 scalar or iterable of integers of length num_buckets).
bucket_boundaries: `list` of `int` or `None`, increasing non-negative numbers.
The edges of the buckets to use when bucketing tensors.
Two extra buckets are created, one for input_length < bucket_boundaries[0]
and one for input_length >= bucket_boundaries[-1].
allow_smaller_final_batch: `bool`, whether to allow a last small batch.
dynamic_pad: `bool`, Allow variable dimensions in input shapes.
The given dimensions are padded upon dequeue so that tensors
within a batch have the same shapes.
min_after_dequeue: `int`.
num_threads: `int`. The number of threads enqueuing tensors.
capacity: `int`, The maximum number of minibatches in the top queue,
and also the maximum number of elements within each bucket.
source_files: An array of file names for the source data.
target_files: An array of file names for the target data. These must
be aligned to the `source_files`.
source_delimiter: A character to split the source text on. Defaults
to " " (space). For character-level training this can be set to the
empty string.
target_delimiter: Same as `source_delimiter` but for the target text.
"""
IDENTIFIER = "ParallelTextPipeline"
SCHEMA = ParallelTextPipelineSchema
def __init__(
self,
source_files=None,
target_files=None,
source_delimiter="",
target_delimiter="",
name="ParallelTextPipeline",
**kwargs
):
super(ParallelTextPipelineConfig, self).__init__(name=name, **kwargs)
self.source_files = source_files
self.target_files = target_files
self.source_delimiter = source_delimiter
self.target_delimiter = target_delimiter
class TFRecordSourceSequencePipelineSchema(BasePipelineSchema):
files = fields.List(fields.Str(), allow_none=True)
source_field = fields.Str(allow_none=True)
target_field = fields.Str(allow_none=True)
source_delimiter = fields.Str(allow_none=True)
target_delimiter = fields.Str(allow_none=True)
@staticmethod
def schema_config():
return TFRecordSourceSequencePipelineConfig
class TFRecordSourceSequencePipelineConfig(BasePipelineConfig):
"""An input pipeline that reads a TFRecords containing both source and target sequences.
Args:
name: `str`, name to give for this pipeline.
feature_processors: `dict`, list of modules to call for each feature to be processed.
shuffle: `bool`, If true, shuffle the data.
num_epochs: `int`, Number of times to iterate through the dataset. If None, iterate forever.
batch_size: The new batch size pulled from the queue (all queues will have the same size).
If a list is passed in then each bucket will have a different batch_size.
(python int, int32 scalar or iterable of integers of length num_buckets).
bucket_boundaries: `list` of `int` or `None`, increasing non-negative numbers.
The edges of the buckets to use when bucketing tensors.
Two extra buckets are created, one for input_length < bucket_boundaries[0]
and one for input_length >= bucket_boundaries[-1].
allow_smaller_final_batch: `bool`, whether to allow a last small batch.
dynamic_pad: `bool`, Allow variable dimensions in input shapes.
The given dimensions are padded upon dequeue so that tensors
within a batch have the same shapes.
min_after_dequeue: `int`.
num_threads: `int`. The number of threads enqueuing tensors.
capacity: `int`, The maximum number of minibatches in the top queue,
and also the maximum number of elements within each bucket.
source_field: The TFRecord feature field containing the source text.
target_field: The TFRecord feature field containing the target text.
source_delimiter: A character to split the source text on. Defaults
to " " (space). For character-level training this can be set to the
empty string.
target_delimiter: Same as `source_delimiter` but for the target text.
"""
IDENTIFIER = "TFRecordSourceSequencePipeline"
SCHEMA = TFRecordSourceSequencePipelineSchema
def __init__(
self,
files=None,
source_field="source",
target_field="target",
source_delimiter="",
target_delimiter="",
name="TFRecordSourceSequencePipeline",
**kwargs
):
super(TFRecordSourceSequencePipelineConfig, self).__init__(name=name, **kwargs)
self.files = files
self.source_field = source_field
self.target_field = target_field
self.source_delimiter = source_delimiter
self.target_delimiter = target_delimiter
class ImageCaptioningPipelineSchema(BasePipelineSchema):
files = fields.List(fields.Str(), allow_none=True)
image_field = fields.Str(allow_none=True)
image_format = fields.Str(allow_none=True)
caption_ids_field = fields.Str(allow_none=True)
caption_tokens_field = fields.Str(allow_none=True)
@staticmethod
def schema_config():
return ImageCaptioningPipelineConfig
class ImageCaptioningPipelineConfig(BasePipelineConfig):
"""An input pipeline that reads a TFRecords containing both source and target sequences.
Args:
name: `str`, name to give for this pipeline.
feature_processors: `dict`, list of modules to call for each feature to be processed.
shuffle: `bool`, If true, shuffle the data.
num_epochs: `int`, Number of times to iterate through the dataset. If None, iterate forever.
batch_size: The new batch size pulled from the queue (all queues will have the same size).
If a list is passed in then each bucket will have a different batch_size.
(python int, int32 scalar or iterable of integers of length num_buckets).
bucket_boundaries: `list` of `int` or `None`, increasing non-negative numbers.
The edges of the buckets to use when bucketing tensors.
Two extra buckets are created, one for input_length < bucket_boundaries[0]
and one for input_length >= bucket_boundaries[-1].
allow_smaller_final_batch: `bool`, whether to allow a last small batch.
dynamic_pad: `bool`, Allow variable dimensions in input shapes.
The given dimensions are padded upon dequeue so that tensors
within a batch have the same shapes.
min_after_dequeue: `int`.
num_threads: `int`. The number of threads enqueuing tensors.
capacity: `int`, The maximum number of minibatches in the top queue,
and also the maximum number of elements within each bucket.
files: An array of file names to read from.
image_field: The TFRecord feature field containing the source images.
image_format: The images extensions.
caption_ids_field: The caption ids field.
caption_tokens_field: the caption tokends field.
"""
IDENTIFIER = "ImageCaptioningPipeline"
SCHEMA = ImageCaptioningPipelineSchema
def __init__(
self,
files=None,
image_field="image/data",
image_format="jpg",
caption_ids_field="image/caption_ids",
caption_tokens_field="image/caption",
name="ImageCaptioningPipeline",
**kwargs
):
super(ImageCaptioningPipelineConfig, self).__init__(name=name, **kwargs)
self.files = files
self.image_field = image_field
self.image_format = image_format
self.caption_ids_field = caption_ids_field
self.caption_tokens_field = caption_tokens_field
class PipelineSchema(BaseMultiSchema):
__multi_schema_name__ = "pipeline"
__configs__ = {
TFRecordImagePipelineConfig.IDENTIFIER: TFRecordImagePipelineConfig,
TFRecordSequencePipelineConfig.IDENTIFIER: TFRecordSequencePipelineConfig,
ParallelTextPipelineConfig.IDENTIFIER: ParallelTextPipelineConfig,
TFRecordSourceSequencePipelineConfig.IDENTIFIER: TFRecordSourceSequencePipelineConfig,
ImageCaptioningPipelineConfig.IDENTIFIER: ImageCaptioningPipelineConfig,
}
class PipelineConfig(BaseConfig):
SCHEMA = PipelineSchema
IDENTIFIER = "pipeline"
UNKNOWN_BEHAVIOUR = EXCLUDE