1

tf.keras.utils.image_dataset_from_directory - A dataset of 20k images in two classes (directories) and creation of training and validation dataset works fine. But I can't use matplotlib.pyplot to plot the training dataset while plot of val_ds, the validation dataset, works fine. Anyone see my error?

Thanks, newbie

image_size = (180, 180)
batch_size = 128
​
train_ds, val_ds = tf.keras.utils.image_dataset_from_directory(
    "PetImages",
    labels="inferred",
    label_mode="binary",
    validation_split=0.2,
    subset="both",
    seed=1337,
    image_size=image_size,
    batch_size=batch_size,
)

Found 20791 files belonging to 2 classes. Using 16633 files for training. Using 4158 files for validation. #Visualize the data import matplotlib.pyplot as plt ​

plt.figure(figsize=(10, 10))
​
for image, label in train_ds.take(1):
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(image[i].numpy().astype("uint8"))
        plt.title(int(label[i]))
        plt.axis("off")`


InvalidArgumentError                      Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_12336\4181458072.py in <module>
      5 plt.figure(figsize=(10, 10))
      6 
----> 7 for image, label in train_ds.take(1):
      8     for i in range(9):
      9         ax = plt.subplot(3, 3, i + 1)

~\Anaconda3\lib\site-packages\tensorflow\python\data\ops\iterator_ops.py in __next__(self)
    764   def __next__(self):
    765     try:
--> 766       return self._next_internal()
    767     except errors.OutOfRangeError:
    768       raise StopIteration

~\Anaconda3\lib\site-packages\tensorflow\python\data\ops\iterator_ops.py in _next_internal(self)
    747     # to communicate that there is no more data to iterate over.
    748     with context.execution_mode(context.SYNC):
--> 749       ret = gen_dataset_ops.iterator_get_next(
    750           self._iterator_resource,
    751           output_types=self._flat_output_types,

~\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_dataset_ops.py in iterator_get_next(iterator, output_types, output_shapes, name)
   3014       return _result
   3015     except _core._NotOkStatusException as e:
-> 3016       _ops.raise_from_not_ok_status(e, name)
   3017     except _core._FallbackException:
   3018       pass

~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in raise_from_not_ok_status(e, name)
   7207 def raise_from_not_ok_status(e, name):
   7208   e.message += (" name: " + name if name is not None else "")
-> 7209   raise core._status_to_exception(e) from None  # pylint: disable=protected-access
   7210 
   7211 

InvalidArgumentError: {{function_node __wrapped__IteratorGetNext_output_types_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} Input is empty.
     [[{{node decode_image/DecodeImage}}]] [Op:IteratorGetNext]

<Figure size 1000x1000 with 0 Axes>

To plot the training and validation dataset. I see the validation dataset images in the dataset but training dataset gives an error as shown

1 Answers1

0

Please try again by providing the dataset directory in the image_dataset_from_directory() rather than giving only the folder name. Please check the image_dataset_from_directory API link for more details.

I am able to fetch the dataset and plot the train_ds and val_ds dataset successfully.

(Attaching the replicated gist here for your reference.)

image_size = (180, 180)
batch_size = 128
train_ds, val_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir,
    labels="inferred",
    label_mode="binary",
    validation_split=0.2,
    subset="both",
    seed=1337,
    image_size=image_size,
    batch_size=batch_size)

Output:

Found 2000 files belonging to 2 classes.
Using 1600 files for training.
Using 400 files for validation.

To plot the train_ds dataset:

import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
for image, label in train_ds.take(1):
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(image[i].numpy().astype("uint8"))
        plt.title(int(label[i]))
        plt.axis("off")

Output:

enter image description here