`y` argument is not supported when using `keras.utils.Sequence` as input. error

Viewed 8618
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Flatten, Dropout, Conv2D, MaxPool2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping

train_path = "D:\python_scripts\garbage/garbage/"
img_shape = (437, 694, 3)
df = pd.read_csv("mpd.csv")
scaler = MinMaxScaler()
earlyStopping = EarlyStopping(monitor="val_loss", mode="min", patience=2)

y = df[["methane", "plastic", "dsci"]].values

imgGen = ImageDataGenerator(rotation_range=(20), width_shift_range=(
    0.1), height_shift_range=(0.1), zoom_range=(0.2), shear_range=(0.1), fill_mode="nearest")
imgGen.flow_from_directory(train_path)
x = imgGen.flow_from_directory(train_path, class_mode=None,
                               color_mode="rgb", batch_size=16, target_size=(img_shape)[:0])

model = Sequential()

model.add(Conv2D(filters=128, kernel_size=(3, 3),
                 input_shape=img_shape, activation="relu"))
model.add(MaxPool2D(pool_size=(4, 4)))
model.add(Conv2D(filters=256, kernel_size=(3, 3),
                 input_shape=img_shape, activation="relu"))
model.add(MaxPool2D(pool_size=(4, 4)))
model.add(Conv2D(filters=512, kernel_size=(3, 3),
                 input_shape=img_shape, activation="relu"))
model.add(MaxPool2D(pool_size=(4, 4)))
model.add(Conv2D(filters=1024, kernel_size=(3, 3),
                 input_shape=img_shape, activation="relu"))
model.add(MaxPool2D(pool_size=(4, 4)))

model.add(Flatten())

model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(1024, activation="relu"))
model.add(Dropout(0.5))

model.add(Dense(3))

model.compile(optimizer="adam", loss="mse", metrics=["accuracy"])

model.fit(x=x, y=y, epochs=500, verbose=1, callbacks=[earlyStopping])
model.save("deep.h5")

NOTE: garbage/garbage/ contains images mpd.csv is a CSV file which corresponds to the images in garbage/garbage/

This is the output-

File "D:\python_scripts\garbage\deep.py", line 54, in <module>
    model.fit(x=x, y=y, epochs=500, verbose=1, callbacks=[earlyStopping],batch_size=16)
  File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 802, in fit
    data_handler = data_adapter.DataHandler(
  File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1100, in __init__
    self._adapter = adapter_cls(
  File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 891, in __init__
    raise ValueError("`y` argument is not supported when using "
ValueError: `y` argument is not supported when using `keras.utils.Sequence` as input.
1 Answers

The data generator you are using returns both the image and its label, the label is extracted from the directory name. if you have the following directory structure

  • data
    • methane
      • image1.jpg
    • plastic
      • image2.jpg
    • dsci
      • image3.jpg

The model will understand that image1 is of class methane and image2 is of class plastic and image3 is of class dsci, so no need to pass the labels.
If you don't have that directory structure, then you might need to define your own generator class based on tf.keras.utils.Sequence class. you can read more about that here

Related