使用keras.utils.Sequence作为输入时,不支持y参数.错误 [英] `y` argument is not supported when using `keras.utils.Sequence` as input. error
问题描述
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文件,与垃圾/垃圾/
NOTE: garbage/garbage/ contains images mpd.csv is a CSV file which corresponds to the images in garbage/garbage/
这是输出-
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.
推荐答案
您正在使用的数据生成器将返回图像及其标签,该标签是从目录名称中提取的.如果您具有以下目录结构
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
- 数据
-
甲烷
- image1.jpg
- data
- methane
- image1.jpg
- image2.jpg
- image3.jpg
模型将理解image1为甲烷类,image2为可塑类,image3为dsci类,因此无需传递标签.
如果没有该目录结构,则可能需要基于tf.keras.utils.Sequence
类定义自己的生成器类.您可以在此处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 ontf.keras.utils.Sequence
class. you can read more about that here这篇关于使用keras.utils.Sequence作为输入时,不支持y参数.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- methane