Loading grayscale pngs with image_dataset_from_directory returns a 3-channel tensor

Question:

I have a set of grayscale png images split over 2 directories. I have used image_dataset_from_directory to load them as a Dataset object, as per documentation. When I use element_spec to inspect what has been loaded, it says the images have 3 channels:

from tensorflow.keras.preprocessing import image_dataset_from_directory
Dataset = image_dataset_from_directory('path/to/files')
Dataset.element_spec

Returns:

Found 14000 files belonging to 2 classes.

(TensorSpec(shape=(None, 256, 256, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None,), dtype=tf.int32, name=None))

The images were saved as grayscale pngs using MATLAB, and I have confirmed that they are grayscale using the Linux command file:

$ file path/to/files/class_1/file_1.png

path/to/files/class_1/file_1.png: PNG image data, 256 x 256, 8-bit grayscale, non-interlaced

So now I either need to tell image_dataset_from_directory to load these files as grayscale, or I need to convert the 3-channel tensor Dataset object to a 1-channel tensor. How can I do either?

Edit:

More information about the file on disk using identify (from ImageMagick):

$ identify -verbose path/to/files/class_1/file_1.png
Image: AI_Optrap/Samples/Set4/relaxed/HL60_normoxia_1_1.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 256x256+0+0
  Units: Undefined
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
  Channel statistics:
    Pixels: 65536
    Gray:
      min: 0 (0)
      max: 255 (1)
      mean: 135.92 (0.533021)
      standard deviation: 36.3709 (0.142631)
      kurtosis: 1.51412
      skewness: 0.035325
      entropy: 0.87207
  Colors: 256
Asked By: Will Hardiman

||

Answers:

By default, image_dataset_from_directory converts to 3 channels. Look at the documentation:

tf.keras.preprocessing.image_dataset_from_directory(
    directory, labels='inferred', label_mode='int', class_names=None,
    color_mode='rgb', batch_size=32, image_size=(256, 256), shuffle=True, seed=None,
    validation_split=None, subset=None, interpolation='bilinear', follow_links=False
)

Color Mode: One of "grayscale", "rgb", "rgba". Default: "rgb". Whether the images will be converted to have 1, 3, or 4 channels.

So just use this line:

Dataset = image_dataset_from_directory('path/to/files', color_mode='grayscale')

And now your images will be converted to (None, 256, 256, 1).

Answered By: Nicolas Gervais