20
votes

I'm changing my TensorFlow code from the old queue interface to the new Dataset API. With the old interface I could specify the num_threads argument to the tf.train.shuffle_batch queue. However, the only way to control the amount of threads in the Dataset API seems to be in the map function using the num_parallel_calls argument. However, I'm using the flat_map function instead, which doesn't have such an argument.

Question: Is there a way to control the number of threads/processes for the flat_map function? Or is there are way to use map in combination with flat_map and still specify the number of parallel calls?

Note that it is of crucial importance to run multiple threads in parallel, as I intend to run heavy pre-processing on the CPU before data enters the queue.

There are two (here and here) related posts on GitHub, but I don't think they answer this question.

Here is a minimal code example of my use-case for illustration:

with tf.Graph().as_default():
    data = tf.ones(shape=(10, 512), dtype=tf.float32, name="data")
    input_tensors = (data,)

    def pre_processing_func(data_):
        # normally I would do data-augmentation here
        results = (tf.expand_dims(data_, axis=0),)
        return tf.data.Dataset.from_tensor_slices(results)

    dataset_source = tf.data.Dataset.from_tensor_slices(input_tensors)
    dataset = dataset_source.flat_map(pre_processing_func)
    # do something with 'dataset'
1
Kind of a clunky workaround, but how about using map() with your preprocessing function, then prefetch() to buffer items and finally flat_map() with a lambda x : x function that takes care of flattening? - GPhilo
@GPhilo That idea seems to work indeed, not sure if there is any overhead anywhere. Do you want to give this as an answer, i.e.: pre_processing_func no longer returning a dataset and using a regular map on it, followed by a dataset.flat_map(lambda x: tf.data.Dataset.from_tensor_slices(x)) - CNugteren
Good to hear that it helped. I'll write a small full-code example as an answer. - GPhilo
I have a question though, is it your intended use-case to have one Dataset instance for each sample? It looks extremely confusing to me, as typically you'd have only one Dataset instance that produces batches. - GPhilo
No, one Dataset in total. In the example posted here I would have 10 input vectors with 512 elements each. Then, in pre_processing_func I would apply data-augmentation, generating a random amount of vectors of 512 for each input. In the example there is no randomness, but always returns 1 sample for 1 input, simulated by the tf.expand_dims. - CNugteren

1 Answers

14
votes

To the best of my knowledge, at the moment flat_map does not offer parallelism options. Given that the bulk of the computation is done in pre_processing_func, what you might use as a workaround is a parallel map call followed by some buffering, and then using a flat_map call with an identity lambda function that takes care of flattening the output.

In code:

NUM_THREADS = 5
BUFFER_SIZE = 1000

def pre_processing_func(data_):
    # data-augmentation here
    # generate new samples starting from the sample `data_`
    artificial_samples = generate_from_sample(data_)
    return atificial_samples

dataset_source = (tf.data.Dataset.from_tensor_slices(input_tensors).
                  map(pre_processing_func, num_parallel_calls=NUM_THREADS).
                  prefetch(BUFFER_SIZE).
                  flat_map(lambda *x : tf.data.Dataset.from_tensor_slices(x)).
                  shuffle(BUFFER_SIZE)) # my addition, probably necessary though

Note (to myself and whoever will try to understand the pipeline):

Since pre_processing_func generates an arbitrary number of new samples starting from the initial sample (organised in matrices of shape (?, 512)), the flat_map call is necessary to turn all the generated matrices into Datasets containing single samples (hence the tf.data.Dataset.from_tensor_slices(x) in the lambda) and then flatten all these datasets into one big Dataset containing individual samples.

It's probably a good idea to .shuffle() that dataset, or generated samples will be packed together.