How to extract data/labels back from TensorFlow dataset

Valentin picture Valentin · May 20, 2019 · Viewed 16.3k times · Source

there are plenty of examples how to create and use TensorFlow datasets, e.g.

dataset = tf.data.Dataset.from_tensor_slices((images, labels))

My question is how to get back the data/labels from the TF dataset in numpy form? In other words want would be reverse operation of the line above, i.e. I have a TF dataset and want to get back images and labels from it.

Answer

kawingkelvin picture kawingkelvin · Jul 9, 2020

In case your tf.data.Dataset is batched, the following code will retrieve all the y labels:

y = np.concatenate([y for x, y in ds], axis=0)