Autoencoder Custom Dataset tensorflow 2.3 ValueError: `y` argument is not supported when using dataset as input

951 views Asked by At

I'm trying to implement an Autoencoder in Tensorflow 2.3. I am taking my own Image dataset stored on disk as input.can someone explain to me how this can be done in a correct way?

I tried loading the data in directory using tf.keras.preprocessing.image_dataset_from_directory() but when I use start training with the data taken from above method I am getting following error.

"ValueError: y argument is not supported when using dataset as input."

PFB the code that I am running

'''

import tensorflow as tf
from convautoencoder import ConvAutoencoder
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
import numpy as np

EPOCHS = 25
batch_size = 1
img_height = 180
img_width = 180
data_dir = "/media/aniruddha/FE47-91B8/Laptop_Backup/Auto-Encoders/Basic/data"

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

(encoder, decoder, autoencoder) = ConvAutoencoder.build(224, 224, 3)
opt = Adam(lr=1e-3)
autoencoder.compile(loss="mse", optimizer=opt)

H = autoencoder.fit(    train_ds, train_ds, validation_data=(val_ds, val_ds),   epochs=EPOCHS, batch_size=batch_size)

'''

3

There are 3 answers

1
Goan_Pal On

I resolved this. I was not feeding the input dataset as a tuple to the model for training. Once I corrected that the training started.

0
Skippy le Grand Gourou On

fit is expecting data and labels, but it only accepts a single tf.data.Dataset. To use data as labels for the autoencoder you should provide it twice to the dataset constructor, e.g. :

dataset = tf.data.Dataset.from_tensor_slices((images, images))
0
Goan_Pal On

I used generators to feed the input data as tuple to the autoencoder. Please find my code below.

# initialize the training training data augmentation object
trainAug = ImageDataGenerator(rescale=1. / 255)

valAug = ImageDataGenerator(rescale=1. / 255)

# initialize the training generator
trainGen = trainAug.flow_from_directory(
    config.TRAIN_PATH,
    class_mode="input",
    classes=None,
    target_size=(64, 64),
    color_mode="grayscale",
    shuffle=True,
    batch_size=BS)
# initialize the validation generator
valGen = valAug.flow_from_directory(
    config.TRAIN_PATH,
    class_mode="input",
    classes=None,
    target_size=(64, 64),
    color_mode="grayscale",
    shuffle=False,
    batch_size=BS)
# initialize the testing generator
testGen = valAug.flow_from_directory(
    config.TRAIN_PATH,
    class_mode="input",
    classes=None,
    target_size=(64, 64),
    color_mode="grayscale",
    shuffle=False,
    batch_size=BS)

early_stop = EarlyStopping(monitor='val_loss', patience=20)
mc = ModelCheckpoint('best_model_1.h5', monitor='val_loss', mode='min', save_best_only=True)

# construct our convolutional autoencoder
print("[INFO] building autoencoder...")
(encoder, decoder, autoencoder) = ConvAutoencoder.build(64, 64, 1)
opt = Adam(learning_rate= 0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-04, amsgrad=False)
autoencoder.compile(loss="mse", optimizer=opt)

# train the convolutional autoencoder
H = autoencoder.fit(    trainGen,   validation_data=valGen, epochs=EPOCHS, batch_size=BS ,callbacks=[ mc , early_stop])