简体   繁体   中英

Changing label_mode of tf.keras.preprocessing.image_dataset_from_directory after setting it once

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM