Reputation: 65
I want to train a model on several GPUs using tensorflow 2.0. In the tensorflow tutorial for distributed training (https://www.tensorflow.org/guide/distributed_training), the tf.data
datagenerator is converted into a distributed dataset as follows:
dist_dataset = mirrored_strategy.experimental_distribute_dataset(dataset)
However, I want to use my own custom data generator instead (for example, the keras.utils.Sequence
datagenerator, along with keras.utils.data_utils.OrderedEnqueuer
for asynchronous batch generation). But the mirrored_strategy.experimental_distribute_dataset
method supports only tf.data
datagenerator. How do I use the keras datagenerator instead?
Thank you!
Upvotes: 6
Views: 5829
Reputation: 428
Without using an Enqueuer, here is another way, assuming you have a generator dg which yields samples in the form (feature, label) when called:
import tensorflow as tf
import numpy as np
def get_tf_data_Dataset(data_generator_settings_dict):
length_req = data_generator_settings_dict["length"]
x_d1 = data_generator_settings_dict['x_d1']
x_d2 = data_generator_settings_dict['x_d2']
x_d3 = data_generator_settings_dict['x_d3']
y_d1 = data_generator_settings_dict['y_d1']
x_d2 = data_generator_settings_dict['x_d2']
y_d3 = data_generator_settings_dict['y_d3']
list_of_x_arrays = [np.zeros((x_d1, x_d2, x_d3)) for _ in range(length_req)]
list_of_y_arrays = [np.zeros((y_d1, y_d2, y_d3)) for _ in range(length_req)]
list_of_tuple_samples = [(x, y) for (x, y) in dg()]
list_of_x_samples = [x for (x, y) in list_of_tuple_samples]
list_of_y_samples = [y for (x, y) in list_of_tuple_samples]
for sample_index in range(length_req):
list_of_x[sample_index][:] = list_of_x_samples[sample_index]
list_of_y[sample_index][:] = list_of_y_samples[sample_index]
return tf.data.Dataset.from_tensor_slices((list_of_x, list_of_y))
It is convoluted but guaranteed to work. This also implies that the __call__
method of dg is a for loop like (after the __init__
of course):
def __call__(self):
for _ in self.length:
# generate x (single sample of feature)
# generate y (single matching sample of label)
yield x, y
Upvotes: 0
Reputation: 106
I used tf.data.Dataset.from_generator
with my keras.utils.sequence
in the same situation, and it solved my issues!
train_generator = SegmentationMultiGenerator(datasets, folder) # My keras.utils.sequence object
def generator():
multi_enqueuer = OrderedEnqueuer(train_generator, use_multiprocessing=True)
multi_enqueuer.start(workers=10, max_queue_size=10)
while True:
batch_xs, batch_ys, dset_index = next(multi_enqueuer.get()) # I have three outputs
yield batch_xs, batch_ys, dset_index
dataset = tf.data.Dataset.from_generator(generator,
output_types=(tf.float64, tf.float64, tf.int64),
output_shapes=(tf.TensorShape([None, None, None, None]),
tf.TensorShape([None, None, None, None]),
tf.TensorShape([None, None])))
strategy = tf.distribute.MirroredStrategy()
train_dist_dataset = strategy.experimental_distribute_dataset(dataset)
Note that this is my first working solution - at the moment I have found it most convenient to just put 'None' in the place of the real output shapes, which I have found to work.
Upvotes: 2