Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow Dataset extremely slow compared to queues

To do same task with Dataset-API seems to be 10-100 times slower than with queues.

This is what I am trying to do with Datasets:

dataset = tf.data.TFRecordDataset(filenames).repeat()
dataset = dataset.batch(100)
dataset = dataset.map(_parse_function)
dataset = dataset.prefetch(1000)
d = dataset.make_one_shot_iterator()

%timeit -n 200 sess.run(d.get_next())

and this with queues:

filename_queue = tf.train.string_input_producer(filenames, capacity=1)

reader = tf.TFRecordReader()
_, serialized_example = reader.read_up_to(filename_queue, 100)

features = _parse_function(serialized_example)

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
tf.train.start_queue_runners()

%timeit -n 200 sess.run(features)

Observed results:

Dataset: 23.6 ms ± 8.73 ms per loop (mean ± std. dev. of 7 runs, 200 loops each)

Queue: 481 µs ± 91.7 µs per loop (mean ± std. dev. of 7 runs, 200 loops each)

Why this happens? How to make the Dataset work faster?


Using tensorflow 1.4 and python 3.5

Full code to reproduce:

import tensorflow as tf
import numpy as np
import glob
import os


def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))


def create_data(i):
    tfrecords_filename = '_temp/dstest/tt%d.tfr' % i

    writer = tf.python_io.TFRecordWriter(tfrecords_filename)

    for j in range(1000):
        f = tf.train.Features(feature={
            'x': _int64_feature([j]),
            "y": _int64_feature(np.random.randint(5, 100, size=np.random.randint(6)))
        })

        example = tf.train.Example(features=f)
        writer.write(example.SerializeToString())

    writer.close()
    return tfrecords_filename


def _parse_function(example_proto):
    features = {
        "x": tf.FixedLenFeature((), tf.int64),
        "y": tf.FixedLenSequenceFeature((), tf.int64, allow_missing=True)
    }
    parsed_features = tf.parse_example(example_proto, features)
    return parsed_features


os.makedirs("_temp/dstest", exist_ok=True)
sess = tf.InteractiveSession()

filenames = [create_data(i) for i in range(5)]

#### DATASET
dataset = tf.data.TFRecordDataset(filenames).repeat()
dataset = dataset.batch(100)
dataset = dataset.map(_parse_function)
dataset = dataset.prefetch(1000)
d = dataset.make_one_shot_iterator()

%timeit -n 200 sess.run(d.get_next())

#### QUEUE
filename_queue = tf.train.string_input_producer(filenames, capacity=1)

reader = tf.TFRecordReader()
_, serialized_example = reader.read_up_to(filename_queue, 100)

features = _parse_function(serialized_example)

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
tf.train.start_queue_runners()

%timeit -n 200 sess.run(features)

coord.request_stop()
coord.join(threads)
like image 779
Pekka Avatar asked Dec 05 '17 11:12

Pekka


1 Answers

Oh I figured it out. I am not supposed to call d.get_next() multiple times.

When I change it to be:

d = dataset.make_one_shot_iterator().get_next()
%timeit -n 200 sess.run(d)

Then the speed is similar to the queue version, even without pre-fetching.

And ineed the result of sess.run call is always different.

like image 132
Pekka Avatar answered Sep 22 '22 17:09

Pekka