1

I'm using this code to load images that I have to pass to a Convolutional variational autoenocder:

import tensorflow as tf

train = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir + 'Train/', label_mode=None,
  image_size=(img_height, img_width),
  batch_size=batch_size)

To be able to pass this to the autoencoder, I have to set label_mode = None. Also, the images received at the decoder are further to be passed to a CNN for classification where I need the labels.

How can I make train also return the labels later for the CNN when initially its label_mode=None.

Imanpal Singh
  • 1,105
  • 1
  • 12
  • 22

1 Answers1

1

You can load the images with the labels and then create another dataset without label.

import tensorflow as tf

train = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir + 'Train/', label_mode='categorical',
  image_size=(img_height, img_width),
  batch_size=batch_size)

X = np.array([])
for x, y in testData:
  if X.size == 0:
    X = x.numpy()
    continue
  X = np.concatenate([X, x.numpy()])
dataset = tf.data.Dataset.from_tensor_slices(X)