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
参数。
答案 0 :(得分:0)
您正在使用的数据生成器将返回图像及其标签,该标签是从目录名称中提取的。如果您具有以下目录结构
模型将了解image1为甲烷类,image2为塑料类,image3为dsci类,因此无需传递标签。
如果没有该目录结构,则可能需要基于tf.keras.utils.Sequence
类定义自己的生成器类。您可以了解有关here