Amine Horseman
Amine Horseman

Reputation: 274

Creating batches from custom dataset of images in Tensorflow

I'm reading a list of .jpg images from disk, and I wanted to split it in several batches. But I got a ValueError while trying to create the first batch.

Here is my code:

import tensorflow as tf
import os

images_list = []
for i in range(6):
    image = tf.read_file("{0}.jpg".format(i))
    image_tensor = tf.image.decode_jpeg(image, channels=3)
    image_tensor = tf.image.rgb_to_grayscale(image_tensor)
    image_tensor = tf.image.resize_images(image_tensor, 28, 28)
    image_tensor = tf.expand_dims(image_tensor, 0)
    images_list.append(image_tensor)

batches, _ = tf.train.batch(images_list, batch_size=3, enqueue_many=True, capacity=6)

And this is the error message:

ValueError Traceback (most recent call last)
<ipython-input-77-a07e94cddf32> in <module>()
----> 1 batches, _ = tf.train.batch(images_list, batch_size=3, enqueue_many=True, capacity=6)

ValueError: too many values to unpack

Upvotes: 2

Views: 4012

Answers (1)

Olivier Moindrot
Olivier Moindrot

Reputation: 28198

Your error message is not linked to TensorFlow at all (you can see that the ValueError was not thrown by TensorFlow).

If you look at the doc, you can see that tf.train.batch() returns a list of tensors (one value in total), and you are trying to get two values when you write batches, _ = tf.train.batch(...).

That is why you get ValueError: too many values to unpack.

You just have to write instead:

batches = tf.train.batch(images_list, batch_size=3, enqueue_many=True, capacity=6)

Upvotes: 4

Related Questions