使用keras.utils.Sequence作为输入时,不支持y参数。错误

时间:2020-05-31 13:01:25

标签: python tensorflow machine-learning keras deep-learning

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")

注意:垃圾/垃圾/包含图像 mpd.csv是CSV文件,与垃圾/垃圾/

中的图像相对应

这是输出-

文件“ D:\ python_scripts \ garbage \ deep.py”,第54行,在 model.fit(x = x,y = y,epochs = 500,verbose = 1,回调= [earlyStopping],batch_size = 16) _method_wrapper中的文件“ C:\ Python38 \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ training.py”,行66 返回方法(自身,* args,** kwargs) 文件“ C:\ Python38 \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ training.py”,行802,适合 data_handler = data_adapter.DataHandler( init 中的文件“ C:\ Python38 \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ data_adapter.py”,第1100行 self._adapter = adapter_cls( init 中的文件“ C:\ Python38 \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ data_adapter.py”,第891行 使用“时不支持引发ValueError(“ y参数 ValueError:使用y作为输入时,不支持keras.utils.Sequence参数。

1 个答案:

答案 0 :(得分:0)

您正在使用的数据生成器将返回图像及其标签,该标签是从目录名称中提取的。如果您具有以下目录结构

  • 数据
      甲烷
      • image1.jpg
    • 塑料
      • image2.jpg
    • dsci
      • image3.jpg

模型将了解image1为甲烷类,image2为塑料类,image3为dsci类,因此无需传递标签。
如果没有该目录结构,则可能需要基于tf.keras.utils.Sequence类定义自己的生成器类。您可以了解有关here

的更多信息