Why are my images loaded into a dataset all white?

Viewed 284

I created a dataset using my images:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

dataset = tf.keras.preprocessing.image_dataset_from_directory(
    <directory>,
    label_mode=None,
    seed=1,
    subset='training',
    validation_split=0.1,
    image_size=(900, 900))

images = next(iter(dataset))
print(tf.shape(images))

I get the output: Found 209 files belonging to 1 classes. Using 189 files for training. tf.Tensor([ 32 900 900 3], shape=(4,), dtype=int32)

Now I want to look at one images with:

plt.imshow(images[19])
plt.show()

As output I get: Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

And also a plain white image as an output.

I am sure that the images loaded into the dataset are not plain white. Can somebody help me?

4 Answers

I've tried to use the methods mentioned above but none of them worked the way i wanted. What you can do is change the interpolation to "nearest" default is "bilinear":

Try this:

ds = tf.keras.preprocessing.image_dataset_from_directory(
    directory = directory, # path to images
    ....
    color_mode="rgb",
    interpolation="nearest",
    follow_links=False,
)

Then to display an image:

for image, label in ds:
    pass

plt.imshow(image[0])

your dataset is a tf.data.Dataset so you can use this visualization function. https://www.tensorflow.org/datasets/api_docs/python/tfds/visualization/show_examples

tfds.visualization.show_examples(
    ds: tf.data.Dataset,
    ds_info: tfds.core.DatasetInfo,
    **options_kwargs
)

implemented this way:

import tensorflow_datasets as tfds

ds, info = tfds.load(<your dataset name>, split='train', with_info=True)
fig = tfds.show_examples(ds, info)

Not a direct answer to your question but franky I prefer to use the ImageDataGenerator.flow_from_directory, It allows you to rescale images, augment the images and the output of the generator is easy to use. Documentation is here. For your application, code would be :

data_gen=tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255,
    validation_split=.01)
train_gen=data_gen.flow_from_directory(
    directory= r'c:\your_directory',
    target_size=(900,900),
    class_mode="categorical",
    batch_size=32,
    shuffle=True,
    seed=123,    
    subset='training')
valid_gen=data_gen.flow_from_directory(
    directory= r'c:\your_directory',
    target_size=(900,900),
    class_mode="categorical",
    batch_size=32,
    shuffle=False,
    seed=123,    
    subset='validation')
tr_images, tr_labels=next (train_gen) # generate a batch of 32 training images, and labels
val_images, val_labels=next(valid_gen)
print('tr_images.shape = ', tr_images.shape)
# result will be tr_images.shape =  (32, 900, 900, 3)
image1=tr_images[0]
plt.imshow(image1)
plt.show()
# result will be showing the image
# other useful things available
class_dict=train_gen.class_indices # a dictionary where key is the text class name and value is integer label of the class
print (class_dict)
labels= train_gen.labels # a sequential list of all the generator labels
file_names= test_gen.filenames  # a sequential list of all the generator file names
# hope this helps

Transfer them to numpy and cast to uint8. Here is the function I use to check inputs and outputs, I use batch size of 32 but print only 8 of them.

def generate_images(test_input):
  prediction = decoder(encoder(test_input), training=True)
  plt.figure(figsize=(15, 15))
  for i in range(8):
    plt.subplot(4, 8, i*2+1)
    plt.imshow(test_input[i].numpy().astype("uint8") )
    plt.subplot(4, 8, i*2+2)
    plt.imshow(prediction[i] )
  plt.axis('off')
  plt.show() 

to call this from dataset

for imgs, labels in train_ds.take(1):
  generate_images( imgs)
Related