votes up 7

`color_mode` must be one of ("rbg", "rgba", "grayscale"). Received: %s

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

 color_mode == 'rgb':
    num_channels = 3
  elif color_mode == 'rgba':
    num_channels = 4
  elif color_mode == 'grayscale':
    num_channels = 1
  else:
    raise ValueError(
        '`color_mode` must be one of {"rbg", "rgba", "grayscale"}. '
        'Received: %s' % (color_mode,))
  interpolation = image_preprocessing.get_interpolation(interpolation)
  dataset_utils.check_validation_split_arg(
      validation_split, subset, shuffle, seed)

  if seed is None:
    
ūüė≤ Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 2 votes down

color_mode parameter specifies whether the images will be converted to have 1, 3, or 4 channels.

Its valid values are

"grayscale", "rgb", "rgba"

Any other value causes an error:

Reproducing the error:

import tempfile
import os
import tensorflow.compat.v1 as tf
import numpy as np
from tensorflow.keras.preprocessing import image as image_preproc
from keras.preprocessing import image_dataset
with tempfile.TemporaryDirectory() as tmpdir:
  directory = tmpdir
  width = height = 24
  imgs = []
  for _ in range(30):
    img = np.random.randint(0, 256, size=(height, width, 1))
    img = image_preproc.array_to_img(img)
    imgs.append(img)
  for i, img in enumerate(imgs):
      filename = 'image_%s.jpg' % (i,)
      img.save(os.path.join(directory, filename))
  dataset = image_dataset.image_dataset_from_directory(directory, 
                                                     batch_size=5, 
                                                     image_size=(18, 18), 
                                                     labels=None,
                                                     color_mode= "rgbe" # this causes the error
                                                     )
  batch = next(iter(dataset))
  print(batch.shape)

The output error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-de858c139fe9> in <module>()
     20                                                      image_size=(18, 18),
     21                                                      labels=None,
---> 22                                                      color_mode= "rgbe"
     23                                                      )
     24   batch = next(iter(dataset))

/usr/local/lib/python3.7/dist-packages/keras/preprocessing/image_dataset.py in image_dataset_from_directory(directory, labels, label_mode, class_names, color_mode, batch_size, image_size, shuffle, seed, validation_split, subset, interpolation, follow_links, smart_resize)
    172     raise ValueError(
    173         '`color_mode` must be one of {"rbg", "rgba", "grayscale"}. '
--> 174         'Received: %s' % (color_mode,))
    175   interpolation = image_preprocessing.get_interpolation(interpolation)
    176   dataset_utils.check_validation_split_arg(

ValueError: `color_mode` must be one of {"rbg", "rgba", "grayscale"}. Received: rgbe

Fixed version of the code:

import tempfile
import os
import tensorflow.compat.v1 as tf
import numpy as np
from tensorflow.keras.preprocessing import image as image_preproc
from keras.preprocessing import image_dataset
with tempfile.TemporaryDirectory() as tmpdir:
  directory = tmpdir
  width = height = 24
  imgs = []
  for _ in range(30):
    img = np.random.randint(0, 256, size=(height, width, 1))
    img = image_preproc.array_to_img(img)
    imgs.append(img)
  for i, img in enumerate(imgs):
      filename = 'image_%s.jpg' % (i,)
      img.save(os.path.join(directory, filename))
  dataset = image_dataset.image_dataset_from_directory(directory, 
                                                     batch_size=5, 
                                                     image_size=(18, 18), 
                                                     labels=None,
                                                     color_mode= "rgb"
                                                     )
  batch = next(iter(dataset))
  print(batch.shape)

N.B. In the case of this example the only valid values are "rgb" and "greyscale" because our image is .jpg and jpeg doesn't support 4 channels. If given another error is raised.

Output:

Found 30 files belonging to 1 classes.
(5, 18, 18, 3)
Jul 08, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix